Wiki

Clone wiki

agtools / Tutorial 4c - Independent Entities

Outline

ts4c.png

In the last tutorial, all entities were basically doing the same thing, with a shared tick function. The same could have been achieved by adding a shared (x,y) offset when drawing normal sprites.

This time we'll change that situation by having each entity do something different, using minimal changes, while continuing to share basic behaviour.

Setup

Previously, we just created our letter entities in a fire-and-forget manner. We didn't need to do anything with them at the point they get spawned.

e.g.

    EntitySpawn(EntityType_LETTER_X, g_viewport_worldx+xenonx+0, g_viewport_worldy+xenony+0);

Now we need to change that, because we want to specialise each spawned instance in some way, to make them act independently.

First we create a temporary pointer to an entity_t type, so we can manipulate our entities through that.

    // this time we're interested in changing each entity as soon as its created so we want
    // to have a temporary pointer to each one returned, for editing
    entity_t *penew;

The EntitySpawn() functions all return a pointer to the spawned entity. So we grab that pointer and assign a unique velocity (vx,vy) to each entity instance in turn.

    penew = EntitySpawn(EntityType_LETTER_X, g_viewport_worldx+xenonx+0, g_viewport_worldy+xenony+0);
    penew->vx = -1; // x velocity = move left
    penew->vy = 0; // y velocity = none

    penew = EntitySpawn(EntityType_LETTER_e, g_viewport_worldx+xenonx+56, g_viewport_worldy+xenony+7);
    penew->vx = 1; // x velocity = move right
    penew->vy = 0; // y velocity = none

Mainloop

We don't need to do anything to the mainloop now. One of the small benefits of using entities.

We will need to come back here later as we add more entity services - but those changes only need applied once, and we're applying them incrementally in the tutorials so its clear which stages each one relates to.

The Tick Function

Last, we change the tick function to apply the entity's own velocity to its position each tick, instead of tracking the playfield scroll via global variables.

void letter_fntick(entity_t *_pself)
{
    // move each entity according to its own individually configured velocity/direction
    // i.e. one common behaviour (fntick) is enough for each entity to do its own thing
    _pself->rx += _pself->vx;
    _pself->ry += _pself->vy;
}

The Entity Dictionary

Nothing to do here - the tick function is already set up.

Summary

We should now see the logo 'explode' with the letters moving off in different directions.

We have lost our screenspace tracking (because it wasn't a very 'entity' way to do things in the first place - more on this later), but all other features we collected so far are still working nicely. The changes made to achieve this effect were also very small.

The demo has been set up to start scrolling after a short period of no scroll - to highlight the loss of the screenspace tracking effect.

Updated