Showing posts with label Entity Systems. Show all posts
Showing posts with label Entity Systems. Show all posts

Tuesday, October 15, 2013

EStroids - An Entity System Game

After having stumbled upon entity systems, I have been a little bit infatuated with the design concept. I decided to spike out a little game for fun and brush off my math skills; thus EStroids was born. It is just a quick and dirty little game, doesn't have all the defensive programming checks that would normally go into an app or game, but was pretty fun to write. (It also has things that will make a mathematician cry, such as multiplying a 4✗4 matrix and a vector3.)

I have the little project hosted on GitHub in case I decide to poke around it some more. Overall, it was just fun and nice to see how easily it is to put little games together. I have absolutely no real-world experience that tells me this sort of approach is scalable, but I do see merits to the approach.

And without further ado, the link: https://github.com/jjvdangelo/EStroids

Systems

Each of the systems inherit from SystemBase that just takes in a manager. (Like I said, just quick and dirty.)

public abstract class SystemBase
{
    private readonly EntityManager _manager;
    protected EntityManager Manager { get { return _manager; } }

    public virtual void Initialize() { }
    public virtual void Shutdown() { }
    public abstract void Frame(double dt);

    protected SystemBase(EntityManager manager)
    {
        _manager = manager;
    }
}

Here's the "lifetime" system that manages objects that have a timed lifespan:

public class LifetimeSystem : SystemBase
{
    public override void Frame(double dt)
    {
        var toDestroy = new List();
        foreach (var entity in Manager.GetEntitiesWithComponent())
        {
            var expiration = entity.GetComponent();
            expiration.TimeAlive += dt;

            if (expiration.TimeAlive < expiration.MaxLifetime) continue;

            toDestroy.Add(entity);
        }

        toDestroy.ForEach(x => Manager.DestroyEntity(x));
    }

    public LifetimeSystem(EntityManager manager)
        : base(manager) { }
}

So much cleaner than trying to shove this into some base class and figure out where this behavior should live within an object oriented hierarchy.

Components

And this is a simple component (the Expiration component) used by the LifetimeSystem:

public class Expiration
{
    public double TimeAlive { get; set; }
    public double MaxLifetime { get; set; }
}

Basically, we work to separate the data away from the behavior and away from the entities themselves. This the complete opposite type of work that I've done for so many years trying to efficiently pull all of these things together. It is almost always a code smell when you have "Manager" objects, quickly leading to an anemic object model.

Make Sense?

For anybody who may experience with entity systems, does this make sense? Am I interpreting the intention of entity systems? I feel like I'm picking up the idea but it's always good to have a sanity check here and there!

Monday, September 30, 2013

Exploring Entity Systems

I am always interested in exploring ways of writing code. Lately, I have been trying to break the monotony of writing "business apps" and decided to take a look at writing a few games. It has been a long time since I have written a non-web app so I started up by doing a little research. Since I am a fan of CQRS/DDD/event sourcing/etc, I wanted to learn more about writing systems that do not require giant object graphs and "god objects." My previous exposure to game systems (a long time ago) seemed to run on magic, duct tape, and a lot of luck. But, maybe that was just programming in general.

In my research, what I have come across has been pretty interesting. I ran across a way of developing using "Entity Systems." It is like an OOP-less way of writing applications with the added benefit of built-in Aspect Oriented Programming (AOP). There are plenty of blog posts covering introductory information, so I won't rehash it here. (I may leave that to another blog post later.)

One of my favorite ways of exploring new ideas is to write up a quick spike. In that fashion, I have written a simple little test app that throws around some colored dots on the screen. It's not perfect, doesn't do everything I would want in a real system, but was fun to write. Entity systems will definitely remain on my radar for a while. Well, without further ado, here is my quick little app (in C#).

Links

For a nice overview of entity systems, check out the following links:

(While these are "old," they still contain some really good information.)