Monday, April 13, 2020

Tutorial 2.7: EntitySystem


Can we finally go out and play now? Hold on Smurfs. As usual with programming, with every new thing you create, 2 other problems arise that require something else to be made first. In this case, we still need to do a few things:
·         Somebody has to call “Entity.update()” (for all active entities) – to animate
·         Create some animations to test with (in “Resources.java”)
·         Adjust the Game code so it will actually use an entity instead
·         Something to think about, who will set the animations? The Puppetmaster?
Problem 4 is a future problem. Problem 2 and 3 are peanuts. Problem 1 requires a bit more attention. We could cheat, skip it, and just call update on that particular entity in our Game render cycle, instead of making another “Entity Manager” class. That will work... until you have 100 entities, created on the fly. Sigh, ok then, another file: “EnEntitySystem.java”.
 
public class EnEntitySystem {

    int uniqueId;

    ArrayList<EnEntityBase>   entities;

    public EnEntitySystem()
    {
        uniqueId = 1;
        this.entities     = new ArrayList<EnEntityBase>();
    } // create


    public void update( float deltaSecs )
    {
        // Update all entities, and remove those who got killed
        int i=0;
        while ( i < this.entities.size() ) {
            EnEntityBase    ent = this.entities.get( i );
            if ( !ent.update( deltaSecs ) ) {
                this.entities.remove( i );
                ent.dispose();
            } else
                ++i;
        }

    } // update


    public EnEntityBase get( int id )
    {   // Brute force search
        for (int i=0; i<this.entities.size(); i++) {
            EnEntityBase    ent = this.entities.get( i );
            if ( ent.getId() == id )
                return ent;
        } // for i
        return null;
    } // get

    public EnEntityBase get( String name )
    {   // Brute force search - be aware names may not be unique, AND that string searching is relative slow
        for (int i=0; i<this.entities.size(); i++) {
            EnEntityBase    ent = this.entities.get( i );
            if ( ent.getName() == name )
                return ent;
        } // for i
        return null;
    } // get


    public EnEntitySprite createSprite(String idName, GxAnimation startAnimation )
    {   // Make
        EnEntitySprite result   = new EnEntitySprite(  );
        result.setName( idName );
        result.setId( this.uniqueId++ );
        if ( startAnimation != null )
            result.setAnimation( startAnimation, true );
        // Add
        this.entities.add( result );
        return result;
    } // createSprite


    public void addCustomEntity( EnEntityBase ent )
    {
        this.entities.add( ent );
    } // addCustomEntity

} // EnEntitySystem

So we have an array of entities. The EntitySystem allows to create sprites or to add your own custom entities. Both are added to the array, so they will be updated once the engine does its “update” routine. Furthermore, you can search for entities by id or name. And that’s pretty much it. For now.

Earlier on, we started a simple "Engine.java" file, where we create the main sub-systems, do the rendering, and call the updates. Of course, our EntitySystem belongs there as well:
public class Engine {
    float   deltaSecs;

    GxCamera        camera;
    GxImageLibrary  imageLibrary;
    EnEntitySystem  entitySystem;

    public Engine()
    {
        this.camera         = new GxCamera();
        this.imageLibrary   = new GxImageLibrary();
        this.entitySystem   = new EnEntitySystem();
    } // create

    ...

    public void update()
    {
        this.deltaSecs  = Gdx.graphics.getDeltaTime();

        this.camera.update( this.deltaSecs );
        this.entitySystem.update( this.deltaSecs );
        //this.controls.update( this.deltaSecs );
        //this.physics.update( this.deltaSecs );
        //this.world.update( this.deltaSecs );
        //this.soundManager.update( this.deltaSecs );
    } // update

    ...
    
    public EnEntitySystem getEntitySystem()
    {
        return this.entitySystem;
    }

} // Engine
Now FINALLY, we can start making real use of that so called “Engine”!

No comments:

Post a Comment