Finite State Machine: Game Engine
First off, I''d like to say this post has nothing to do with graphics, platforms or anything hardware related. I''m trying to design a game engine for a game similar to Legend of Zelda: Ocarina of Time. Except the player can jump. I suppose there''s probably a game out there more closer to what I''m doing, but that''s not the point. Here''s what I got so far. I have a class CCharater. It''s properties are as follows: xpos, ypos, zpos, and state. Well, state is more of a general term as I actually have several variables discribing the state, but those might change as I further develop the engine. But let me discribe how the state changes with certain events. If the player moves the control stick or pushes a direction button the state changes to indecate the character is moving in a particular direction. If the player then pushed the jump button, the state changes to indecate the character is moving in a particular direction but also indicate the character in the air (jumping) as well. And then if the player so chooses and pressed the attack button, the character is now moving in a particular direction, in the air and also swinging his sword. Now, you might suspect I have a variable for each of those states, moving, attacking, jumping. Which is correct. But of course, the player can''t change direction in mid air and he can''t swing his sword if he''s already swinging his sword, I solve that by just checking if the player is in there air before applying a new direction and whether or not he''s swinging his sword already before applying the attack state, etc. I think you get the idea. Now the problem is when I start to implement enemy collisions and stuff. The problem isn''t detecting the collision, I can do that. The problem is how to describe that state. Currently I have a damage (for lack of a better word) state variable. And if that is set, then the character is taking damage and thus cannot change direction, swing his sword or start a jump until that state is finished, after a predetimened amount of time that is. Also, if he took enough damage he gets knocked off his feet and flung across the room. I suppose I could just have a variable for that state, flung maybe, to indecate the character is in the state of taking damage and being flung across the room. Ok, so what''s the problem? Should have a million variables to describe all the states the character could be in and a million if or switch statements to figure out which state the character should enter baseded on a particular event. I started with that, but then my code got messy and hard to read. Is there a simpiler way to do that. I don''t want to have to cut down on my characters ability, like taking away the jump ability, just because I want simpiler code. Metroid Prime for the GCN is doing just about what I want to do, only in first person and with a gun instead of a sword. You can make a third person game similar to that without too many changes to the player input and character engine. How do the big boys do it? Anyone want to discuss this with me?
P.S. Please excuss my grammer and spelling, my fingers weren''t working to well at the time.
Ugh - paragraphs please, that was tricky to read.
Couldnt you just have a ''disabled'' state which handles both the player taking damage and the player being flung?
Simply set this state for whatever duration the attack takes - and if the damage is high enough, you simply keep the state turned on until the player is no longer being flung.
I''m not sure on the general design issue however, ''switch statement'' is a C++ thing I think? If I was designing this in VB, I have have a state variable stored as an integer, with each numerical value representing a specific state (ie. 1 = Jumping, 2 = Attacking, 3 = Disables, etc), and would use a case select to check which state the player is in and act appropriately (note that this only works if states are mutually exclusive, and probably isnt the best approach, its just the first thing that came to mind).
Couldnt you just have a ''disabled'' state which handles both the player taking damage and the player being flung?
Simply set this state for whatever duration the attack takes - and if the damage is high enough, you simply keep the state turned on until the player is no longer being flung.
I''m not sure on the general design issue however, ''switch statement'' is a C++ thing I think? If I was designing this in VB, I have have a state variable stored as an integer, with each numerical value representing a specific state (ie. 1 = Jumping, 2 = Attacking, 3 = Disables, etc), and would use a case select to check which state the player is in and act appropriately (note that this only works if states are mutually exclusive, and probably isnt the best approach, its just the first thing that came to mind).
my 2 cents,...
you can have states like moving_forward/moving_backward/jumping/falling/taking_damage/crouched/dying/dead/alive/pickup/swap_weapon/reloading/firing/respawning/pause_menu...
and actions like
request_move_forward/request_move_backwards/request_jump/request_fire/request_pickup/request_respawn....
the state can be a bitfield, then the actions would have two masking fields, an "ignore" mask, and a "accept" mask. depending on the state bitfield, the command request_move_forward will be aknowledged or not.
you can also split the states into categories, with mutually exclusive states, and with the possibly of using state machines for some categories.
For example, the weapon system state can be either next_weapon/prev_weapon/pickup/reload/fire/drop/next-bullet (controlled by rate of fire of the gun), horizontal state can be move back, move forward, still, vertical states would be jumping, falling, on floor, on slanted wall, posture state would be prone, crouch, stand, ect...
you can have states like moving_forward/moving_backward/jumping/falling/taking_damage/crouched/dying/dead/alive/pickup/swap_weapon/reloading/firing/respawning/pause_menu...
and actions like
request_move_forward/request_move_backwards/request_jump/request_fire/request_pickup/request_respawn....
the state can be a bitfield, then the actions would have two masking fields, an "ignore" mask, and a "accept" mask. depending on the state bitfield, the command request_move_forward will be aknowledged or not.
you can also split the states into categories, with mutually exclusive states, and with the possibly of using state machines for some categories.
For example, the weapon system state can be either next_weapon/prev_weapon/pickup/reload/fire/drop/next-bullet (controlled by rate of fire of the gun), horizontal state can be move back, move forward, still, vertical states would be jumping, falling, on floor, on slanted wall, posture state would be prone, crouch, stand, ect...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement