Skip to content

Instantly share code, notes, and snippets.

@phrohdoh
Created Jun 24, 2014
Embed
What would you like to do?
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace PrototypeRPG.Traits
{
public class World
{
public const int TickTimestep = 40;
public SpriteBatch SpriteBatch { get; private set; }
public List<Actor> Actors = new List<Actor>();
public int TotalTickCount { get { return TickCount + TickRenderCount; } }
public int TickCount { get; private set; }
public int TickRenderCount { get; private set; }
int lastTickDelta = 0;
public World(SpriteBatch spriteBatch) { SpriteBatch = spriteBatch; }
public void Tick(GameTime gameTime)
{
if (gameTime.ElapsedGameTime >= TickTimestep)
{
InnerTick();
lastTickDelta = 0;
}
else
lastTickDelta++;
}
void InnerTick()
{
TickCount++;
foreach (var actor in Actors)
{
foreach (var trait in actor.TraitsImplementing<ITick>())
trait.Tick(actor);
}
}
public void TickRender()
{
TickRenderCount++;
foreach (var actor in Actors)
foreach (var trait in actor.TraitsImplementing<ITickRender>())
trait.TickRender(actor, SpriteBatch);
}
public Actor CreateActor(params ITrait[] traits)
{
var newActor = new Actor();
foreach (var trait in traits)
newActor.AddTrait(trait);
Actors.Add(newActor);
return newActor;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment