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 entity’s 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 it’s gma.moveable.xState
property. Gamma provides STILL, RIGHT and
LEFT as valid values for this property.
For convenience, gma.moveable also provides
gma.moveable.lastXState, which is used to determine what direction the
entity was going in before it stopped moving.
Note
gma.moveable.lastXState can also be used to set the direction
an entity is looking in when it is first created, due to it’s use in
gma.moveable.getRotation.
Vertical State¶
The vertical state of an entity is held in it’s gma.moveable.yState
property. Gamma provides STILL, JUMPING and
FALLING as valid values for this property.
When an entity starts jumping, it will use gma.moveable.jumpVelocity to
determine what vertical velocity the entity should start with. For the rest of
the time the entity is in the air, it’s vertical velocity is held by
gma.moveable.velocity, which is changed by
gma.moveable.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
gma.character.move and gma.character.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
gma.moveable.getMovement such that the movement state of the entity
is determined every time gma.moveable.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 gma.moveable.animate and
gma.moveable.getMovement. The gma.moveable.getMovement
function returns a movement vector calculated from the entity’s current vertical
and horizontal state and is used by the gma.moveable.animate function,
which in turn, is called as part of the Game Loop.
The animate function does the following:
Determine a movement vector
Use
gma.collisions.detectCollisionsto determine how far the entity can move along this movement vector.Change the position of the entity using
gma.moveable.updatePositions.Determine if the entity is on standing on top of something by calling
gma.moveable.findGroundand change the vertical state of the entity toFALLINGorSTILLas appropriate.
Note
gma.collisions.detectCollisions will only be used if either
gma.moveable.xState or gma.moveable.yState are not
STILL, but gma.moveable.updatePositions and
gma.moveable.findGround will always be called.
Utility functions¶
There are a couple of other functions that gma.moveable provides,
gma.moveable.getRotation and gma.moveable.kill.
The gma.moveable.kill method will set gma.moveable.alive to
false and set gma.moveable.xState to STILL.
This ensures the entity won’t continue moving after being “killed”, and
gma.manager.removeDead will become aware that this entity should
be removed.
The gma.moveable.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.