Wizard Fu's Cocos2dx Platformer Game Engine v1.0.1

nat@wizardfu.com

See the file LICENSE for the license governing this code.

The Input class is a statically implemented singleton that holds the state of each player’s buttons. Cocos2d-x provides the methods to obtain keyboard, mouse and touch screen input. For gamepads / joysticks, we use the open-source OIS library.

In a multiplayer game, there can be more than one player, so the Input class reserves enough to hold the buttons states for kMaxPlayers players.

In a single player game there is only one player and input is taken directly from the touch screen, keyboard, mouse, joysticks and gamepads. When playing multiplayer, there can be additional players sending input over the network and this gets stored here for the corresponding player index.

When processing input, the engine doesn’t care whether the input arrived via local input or via the network. It simply references this Input class.

#pragma once
#include "Headers.h"

Buttons

A “button” is an abstract concept. On Windows, Mac and Linux, a button can translate to a key on the keyboard or a button on a joystick or gamepad. On iOS and Android, certain rectangles on the touch screen correspond to the varying buttons.

The Input can be queried to see if a button is down or has been released. Buttons can also be manually set to up or down.

enum
{
	kButtonUp,
	kButtonLeft,
	kButtonDown,
	kButtonRight,
	kButtonJump,
	kButtonAction,
	kNumberOfButtons
};

Input class

All public methods are static. Constructor / destructor is private so it cannot accidentally be constructed.

class Input
{	
	public:

Clear all input.

		static void clear();

Create event listener for the current scene.

		static void createEventListener(Node* parent);

Update the joysticks and gamepads. Call this once per tick.

		static void tickControllers();

Set / unset locked. When locked, all buttons are essentially “up”, meaning they return false for isDown.

		static void setLocked(bool isLocked);

Get if the input is locked.

		static bool isLocked();

Buttons down

Return true if the button is down.

		static bool isButtonDown(int playerIndex, int buttonIndex);

Manually set a button down.

		static void setButtonDown(int playerIndex, int buttonIndex, bool isDown);

Set a button down for the given key code.

		static void setKeyDown(int playerIndex, int keyCode, bool isDown);

Set all buttons down for a given player index.

		static void setAllButtonsDown(int playerIndex, bool isDown);

Return true if the player index has any buttons down.

		static bool hasButtonsDown(int playerIndex);

Released buttons

Clears all released buttons. Call this once per tick.

		static void clearReleasedButtons();

Return true if the player released a given button.

		static bool didReleaseButton(int playerIndex, int buttonIndex);

Return true if the player released any button.

		static bool didReleaseButtons(int playerIndex);

Return true if any button for any player was released. Use ignore lock if needed.

		static bool didReleaseAnyButton(bool ignoreLock);

	private:
		typedef Input self;
		typedef map<int, bool> buttonStateType;
		
		static buttonStateType buttonStates[kMaxPlayers];
		static buttonStateType releasedButtons[kMaxPlayers];
		static bool locked;

		Input();
		~Input();
		Input(const Input&);
		Input& operator=(const Input&);
};
 
h