A Cat’s Tale is an episodic action adventure game that we have been designing (more like brainstorming) for a couple of months but never actually got into developing, until now. I have a fair programming background, mostly in C and C++, but only recently I got into games, so this is my first documented try. In this post I’ll write down the general requirements and behavior of the playable character and describe how it will be implemented in Unity using C# as a scripting language.
The different behavior of the character will be implemented by various classes each of them handling different parts of that behavior. A class for a example will take care of the playable character’s state and stats, while other classes will handle the animation and the interaction of the character with the game world. The player can control the character either using the keyboard and mouse or with a controller. I will probably have to use a custom input manager as Unity doesn’t support changing configuration at run time, as far as I know at least. To keep track of game events and messages an event manager will be used. For example when the player lands after a jump, the CharacterController will send a notification to the EventManager as soon as the controller touches the ground. The event manager will rely the message to the CharacterAnimation class, which will perform the jump animation. I’ll probably write about event and messaging systems in more detail sometime in the future and my event manager will probably be based on the AdvancedCSharpMessenger found here, but for now I’ll just use the Unity’s build in message system (GameObject.SendMessage()).
What can the character do?
Before designing and coding the behavior, we need to know what the playable character can and cannot do. The following list describes more or less the character’s behavior in our game.
- Double jump
- Attack (Default: Claws, kick, Secondary-story driven: Weapon).
- Pick up item-collectible: Hold only one item at a time.
- Use-Activate: Item-static (if not holding anything), Item-collectible on Item-static (If holding item).
- Drop item (if holding item-collectible).
- Drag item (if not holding).
- Frenzy: activated automatically once enough smash points or story-driven. Wears off after X seconds. Increased speed, attack and defense.
Character state machine
The above diagram describes the different states of the playable character and the events that drive the transitions. I still need to incorporate the “Frenzy” mechanic in this state machine, but it should be fairly easy, since it’s similar to the “Run” state but with higher moving speed, attack and defense. I found good examples of FSMs here and here (scroll down to Finite state machine section).
I still haven’t decided exactly how to code the FSM, but in general lines it will probably go like this: Once we need to change the state, either after an event or due to input, the AdvanceState() function of the FMS will be called to request an advancement to the next state. If the conditions are met, the state will change, else the request will be discarded. Basically, in its update loop, the state machine will first check any events that can change the state, then try to advance to the requested state and after that, notify the right classes. For example, when the jump button is pressed, the SM will try to advance to the jump or double jump state, depending on the current state and any other conditions defined in the diagram above. Once the FSM has decided that it will advance to jump state, the character animation class will be notified to start playing the jump animation and the class responsible for moving the character controller will start moving the controller vertically in order to “physically” perform the jump.
The player’s character model will be placed as a child object to a CharacterContainer gameobject in Unity. The CharacterContainer will hold the scripts that will keep track of the character stats and his state. The character model will hold the scripts that implement the character’s movement, interaction with the world and animations.
The above diagram shows the relationships between the classes that implement the player’s character. I tried to keep each class relevant to specific parts of the character’s behavior. The Character class will keep track of certain character statistics, such as health, attack, defense and also control the character based on input. The character class will hold, for example, the character’s move direction and current speed and move the CharacterController through the CharacterThirdPersonController class. The CharacterStateMachine implements the finite state machine responsible for the character’s actions and animations. I’ve currently separated the FSM from the character class, but it makes more sense and simplifies things to just put those two together. The CharacterAnimation class will handle all the character’s animations and will be controlled only from the CharacterStateMachine. The CharacterThirdPersonController takes care of the CharacterController movement. The CharacterAction implements most of the character’s interaction with the world, performing character actions on enemies and objects, such as picking objects up or attacking enemies. These three classes will occasionally broadcast messages, for example when the character lands, when his vertical velocity becomes 0 or when it attacks an enemy.
In the second part, I’ll post the code of how the character behavior will be implemented using the above diagrams. I’ll use Unity’s build in message system instead of the EventManager and upload the source files for everyone to use and improve.