Jump to content

  • Log In with Google      Sign In   
  • Create Account


MattFF

Member Since 28 Sep 2012
Offline Last Active Nov 14 2012 08:43 AM
-----

Posts I've Made

In Topic: Handling input properly in 2d brawler

02 October 2012 - 07:23 AM

Thanks a lot for those detailed answers!

After reading all this, i wanted to write it down in pseudocode/snippets to see if i understood it correctly.

In short, it would work like that:

0. Check the current state
1. Check for events that cause loss of control, or modify control ( knocback, stun, during a jump, combo etc. )
2. If there is loss of control/modification, change state
3. Check for input
4. Pass the state and the input to the hero class
5. Adjust the state accordingly - e.g. if character is being knocked back, ignore the input, otherwise take normal input and set state
6. Apply logic ( move, change hp )
7. Draw frames
8. Check for expiration of states - end of knockback, landing from a jump
9. If the state has not expired, keep the state, and adjust time remaining in that state, otherwise return to default state

Rinse and repeat.

Now i have question about code, specifically code/class separation. Just gonna paste small snippets.

main.cpp
int main()
{
	 while (window.isOpen())
	{
		  while (window.pollEvent(event))
		{
			event_manager.handle_events;
			input_manager.handle_input;
		}
	 hero.apply_logic;
	 hero.draw;
}
}

input_manager.cpp
int handle_input
{
if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Something))
	 input_state = x;

//repeat many times for all possible outcomes
return input_state;
}

hero.cpp

apply_logic(state)
{
	 switch (state)
		  case 1: //something that prohibits action - ignore input
		  move;
		  change_hp;
		  break;

		  case 2: // input allowed
			  
			   switch(input_state)

					case 1:
			   	 move;
			   	 break;

return final_state;
}

draw(final_state)
{
	 //draw proper frame, of proper animation, depending on state
}

Would that be the way to do it?


Edit: Buttons are hardcoded just for now, later buttons could be changed to virtual buttons, and base controls on .ini files, like sox suggested.

PARTNERS