Unity 2D platformer – Green Lantern – most recent updates!

Hello all!

I worked quite hard recently on updating my Unity 2D platformer with the Green lantern theme. Recent updates include

-Melee combat system, similar to Streets of Rage style (yes, my frequent referencing of that franchise is a running joke with my colleagues) including a camera shake.

-More AI variation – some AI characters will attack GL with hand to hand techniques.

-GL also contains AI states, such as for being hit, knocked over, entering or exiting a level

-Particles. What fun would a Green Lantern game be without some nice green visual effects?

You may download the executable of the game here: https://sourceforge.net/projects/green-lantern-game/?source=typ_redirect


Below is more information including code designs.

This game started off as a means for me to get more familiar with Unity and C#, but has turned into me wanting to make the most well designed code structure I can come up with at the time, despite this being a simple game. For example my animation and AI state machines consist of separate classes for the different states (and of course, Unity’s excellent component system helps with this). This massively improves portability, flexibility and maintainability, and actually isn’t much work (when its up and running, it cuts down the work needed!).

Animation

Regarding the animation state machine, I have an abstract class (GLAnimState, GL for Green Lantern, the character) that is inherited by all of the other animation states, containing an abstract function VOnUpdate() that is overridden by all subclasses (in C++, this would be called a pure virtual function):

animStatesBig

My motivation for this is that there are many different types of animations. We have some that will loop forever until a different state is implemented, such as the idle and running animation states. Then we have some that will play once and return to a different state, such as the ‘hit’ or ‘attack/shoot’ state which is followed immediately by idle, and then some that will loop forever but on the last frame (or the last few frames), such as the jumping animation. Rather than a tangled mess of if/else statements, which is not easily updated or maintained, separate subclasses make the code much tidier, and easily allow code to be swapped in or out.

IdleCode
The code of the idle animation state

As you can see, the constructor of each animation state class takes in an animator object, containing arrays of all the sprites (as passed in by Unity editor) and an enumerated type, defined below:

enum

The enumerated type changes according to game logic. Then all animation states are stored in an array as type <Superclass> and simply looped through. The animation state that is active is then called.

stateList

forUpdate I did consider having subclasses instead named “Play once”, “Play looping”, “Play last frames” etc, which would have been more generic. But I opted to not follow that strategy, as specific game logic can be called by these subclasses (with references to player controllers, animation controllers etc in the superclass). For example the “CreateBullet()” function is called at the end of the “Attack” state class, creating a bullet at the end of the animation.** For better encapsulaion an event system could be used, but for now this seems sufficient. An alternative could be to gave generic classes wrapped in more specific class names, such as “Idle” containing a call to “Play Looping”. But this seems convoluted.

** Update – Actually ‘PlayLooping’, ‘PlayOnce’ etc would be the most portable option. For end of animation logic, a generic function in class CharacterAnimator taking as parameter an object of type AnimState. Within the function I could have a switch statement based on the AnimState’s enumerated value, allowing for the different end of animation logic to be called from there .

AIStates

The AI and player controller were derived from a generic CharacterController as follows:

playercontroller

Where GLEntity would contain physics data such as the rigid body and AABB components, with GLEntity containing transformation information. CharacterController contained code that could be common to both AI and the player, such as shooting bullets. The AI states themselves were constructed and implemented in a similar way to the animation states and processed in GLAIController:

aistates2

As before GLAIState would have an abstract function AOnUpdate() (and an AOnReset()) and enumerated types denoting the individual AI States. They were updated in the same way in the class AIController :

processaistates

However this time I made the AIState base class derive from Unity’s MonoBehaviour class, e.g., they were custom components that could be added to prefabs selectively according to the desired AI behaviour.

The nice thing about this approach is these AI state components can be copied into other platformer games easily. Also as the PlayerController class is derived from the AIController class, the AI states can be used by the player too, making behaviour where control is taken away from the player, e.g., knocking over, being hit, or level beginning/level end states, (In this case when Green Lantern flies in and out of the level) easily added and implemented.

player_aistates
AI components on the Player prefab. Note that the player can have AI Components too, which is convenient for being hit, killed or opening or closing animations in the game levels
brawler_aistates
The brawler character has a brawl AI state for punching and kicking the player
shooter_aistates
The shooting enemy has a shoot AI state for shooting the player. The same player detection code as the brawler is called, but the resulting behaviour is different.

Below is my playthrough for the version of the game that I have now:

 

Leave a Reply

Your email address will not be published.