Skip to content

Instantly share code, notes, and snippets.

@phrohdoh
Created June 24, 2014 00:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phrohdoh/96a5fb3e734330e868a2 to your computer and use it in GitHub Desktop.
Save phrohdoh/96a5fb3e734330e868a2 to your computer and use it in GitHub Desktop.
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