using System;
using System.Collections.Generic;
using System.Text;
using System;
using System.Collections.Generic;
using FRB;
using FRB.Collections;
using FRB.Instructions;
namespace FRB.Physics
{
class PhysicsManager
{
#region Fields
private AttachableList<PositionedObject> mPositionedObjectArray;
#endregion
#region Methods
#region Constructor
public PhysicsManager()
{
mPositionedObjectArray = new AttachableList<PositionedObject>();
}
#endregion
#region Public Methods
public PositionedObject AddPositionedObject()
{
// objects should usually be created through their Manager
PositionedObject newPositionedObject = new PositionedObject();
mPositionedObjectArray.Add(newPositionedObject);
return newPositionedObject;
}
public void ExecuteInstructions()
{
ExecuteInstructions(TimeManager.currentTick);
}
public void ExecuteInstructions(long currentTime)
{
FrbInstruction instruction;
foreach (PositionedObject po in mPositionedObjectArray)
{
// as long as there are still instructions in the PositionedObject's
// instructionArray, and the instruction at 0 (they are sorted by time)
// should be executed, the while will continue to loop
while (po.instructionArray.Count > 0 &&
po.instructionArray[0].timeToExecute <= currentTime)
{
instruction = po.instructionArray[0];
// execute the instruction
instruction.Execute();
// The instruction may have cleared the InstructionArray,
// so we need to test if it did.
if (po.instructionArray.Count < 1)
continue;
// adjust the instruction's time if it cycles, then sort the instructionArray
if (instruction.cycleTime == 0)
po.instructionArray.Remove(instruction);
else
{
instruction.timeToExecute += instruction.cycleTime;
po.instructionArray.Sort();
}
}
}
}
public void RemovePositionedObject(PositionedObject positionedObjectToRemove)
{
// objects should be removed through their manager.
// the RemoveObject takes care of two way relationships.
while (positionedObjectToRemove.arraysBelongingTo.Count != 0)
positionedObjectToRemove.arraysBelongingTo[0].RemoveObject(positionedObjectToRemove);
}
public void TimedActivity()
{
TimedActivity(TimeManager.currentTick, TimeManager.secondDifference);
}
public void TimedActivity(long currentTime, float secondDifference)
{
// this method gives the user the chance to override the default time if he prefers
// to not use the TimeManager.
double secondDifferenceSquaredDividedByTwo = (secondDifference * secondDifference) / 2.0;
foreach (PositionedObject po in mPositionedObjectArray)
{
po.TimedActivity(currentTime, secondDifference, secondDifferenceSquaredDividedByTwo);
}
}
public void UpdateDependencies()
{
UpdateDependencies(TimeManager.currentTick, TimeManager.secondDifference);
}
public void UpdateDependencies(long currentTime, float secondDifference)
{
float secondDifferenceSquaredDividedByTwo = (secondDifference * secondDifference) / 2.0f;
foreach (PositionedObject po in mPositionedObjectArray)
{
// If it hasn't been updated this frame.
if (po.lastDependencyUpdate != currentTime)
{
po.UpdateDependencies(currentTime, secondDifference,
secondDifferenceSquaredDividedByTwo);
}
}
}
#endregion
#endregion
}
}