Enemy and Jumping Updates


Jumping now uses acceleration and looks less linear.  Enemies now move across room boundaries.

Library Update

When developing a library, one important thing to remember is to make your classes that should be accessible in the API public.  By default, classes are created with no scoping.  You will not be able to access any classes in your library, until those are declared public.  Tonight, I added some of the methods I used for drawing raised text in Resistor to the ResistorKit library as static methods in a Graphics class.


After reading about how others have implemented jumping, I decided to scrap my current linear method which is based on subtracting a constant value for a set number of updates.  That resulted in a constant jump speed and fall, which looks very angular when moving at a constant speed.  To fix this, two float acceleration variables have been added for jump and fall acceleration.  I could have probably used just one acceleration variable, but I thought breaking it out into two makes the code more readable.  When the player jumps, this acceleration value gets set to a constant which is the jump power.  This acceleration value gets subtracted from the player’s Y position on each update, which increases the jump height on each update.  Also on each update, the acceleration value is decreased by a constant value.  Therefore, the player’s jump acceleration decreases until it reaches zero.  At that point, the player’s falling variable gets set to true and a similar process occurs with the falling acceleration.  The only difference is that the falling acceleration continually increases so it relies on collision with a block to stop the fall.  I also added a terminal velocity value, which the acceleration can’t exceed.  Right now that won’t make much of a difference in my game since it doesn’t scroll vertically, but if there was a long fall then not having a terminal velocity would make the player impossible to control while falling.

Enemy Updates

I updated the Enemies collection so that a map can now correctly handle multiple enemies.  I also added a method in the Map class to move an enemy from one map collection to another.  This is used for moving a enemy from the current map to the adjacent map whenever an enemy moves past the room boundary.  Now enemies aren’t restricted to moving in only one map room.  Similar to the player, I had to add checks to ensure that an enemy doesn’t collide with blocks in the first column or last column of blocks in the right and left map rooms.

One interesting problem is that an enemy would get stuck in a block in an adjacent room whenever it was in the angry state from being shot with the wrong gun.  The problem was that the check for changing the room came before the check to change X velocity (direction) on a collision.  Moving the room boundary checking below the block collision call fixed the problem.