Moveable Entities

Gamma has abstracted the functionality required to a move an entity into the gma.moveable class. This class is currently used by gma.character and gma.enemy.

This class provides properties that represent the movement state of an entity as well as functions for changing these states and functions for updating the entitys position.

Representing an entities state

Although Gamma is rendered in 3D all entities move in 2D. As a result of this, the movement state only needs to keep track of the entities horizontal and vertical movement.

Horizontal State

The horizontal state of an entity is held in its xState property. Gamma provides STILL, RIGHT and LEFT as valid values for this property.

For convenience, gma.moveable also provides lastXState, which is used to determine what direction the entity was going in before it stopped moving.

Note

lastXState can also be used to set the direction an entity is looking in when it is first created, due to its use in getRotation.

Vertical State

The vertical state of an entity is held in its yState property. Gamma provides STILL, JUMPING and FALLING as valid values for this property.

When an entity starts jumping, it will use jumpVelocity to determine what vertical velocity the entity should start with. For the rest of the time the entity is in the air, its vertical velocity is held by velocity, which is changed by getMovement.

Changing an entities direction

Changing an entities movement state is not controlled by gma.moveable. Rather, it is the responsibility of each class that subclasses gma.moveable.

For example, gma.character provides the functions move and jump which when called through combination with keybindings will change the entities movement states and allow the player to control the entity.

On the other hand, gma.enemy entities override getMovement such that the movement state of the entity is determined every time animate is called.

Updating an entities` position

To change the position of an entity according to it’s vertical and horizontal state, gma.moveable provides animate and getMovement. The getMovement function returns a movement vector calculated from the entity’s current vertical and horizontal state and is used by the animate function, which in turn, is called as part of the Game Loop.

The animate function does the following:

  • Determine a movement vector
  • Use detectCollisions to determine how far the entity can move along this movement vector.
  • Change the position of the entity using updatePositions.
  • Determine if the entity is on standing on top of something by calling findGround and change the vertical state of the entity to FALLING or STILL as appropriate.

Note

detectCollisions will only be used if either xState or yState are not STILL, but updatePositions and findGround will always be called.

Utility functions

There are a couple of other functions that gma.moveable provides, getRotation and kill.

The kill method will set alive to false and set xState to STILL. This ensures the entity wont continue moving after being killed, and removeDead will become aware that this entity should be removed.

The getRotation is purely for changing the visual representation of an entity. When called, it will look at the state of the entity and determine which direction it should be facing and return a number representing how far around the y-axis the entity should be rotated.