Wizard Fu's Cocos2dx Platformer Game Engine v1.0.1


See the file LICENSE for the license governing this code.

LevelObject > Character > Player

The Player is unique in that it’s the only object to accept input from the local player. Being a Character, it has hp and maxHP as well as all the basics of LevelObject.

This implementation of Player can walk, turn, jump, fall, land, crouch, stand up, hang from edges, aim in 8 directions, fire projectiles, swim, climb ladders, step up stairs, be given items, perform special animations like walking out a door and getting up from bed, and of course, die.

This implementation of Player can attack enemies by jumping on top of them. Otherwise, if a contact occurs with an enemy, the player is damaged. The player’s other mode of attack is through firing projectiles.

As the player transitions between levels, the current velocity, distance from the ground and current sprite frame are preserved to give the illusion of continuity. Actually, a whole new level is being created with a brand new player and the saved player state is re-applied.

#pragma once
#include "Headers.h"


When playing single-player mode, the player begins with no abilities. The player can simply walk around. In this way, the platformer engine can be used to create an adventure genre game.

The player can gain abilities through items granted from chests. Here’s the list of abilities available in the current implementation:

The names, descriptions and help text for the player’s abilities can be specified in the Assets/Strings.plist localization file.

You can “hack” your save game to change the player’s currently enabled abilities. See Game.h.



class Player : public Character

Each LevelObject must implement profileName so the appropriate profile can be loaded. We use Wizard here to load Assets/Profiles/Wizard.plist.

		virtual const char* const profileName() {return "Wizard";}

This player’s index. In single player, this will always be zero. In multiplayer it will be a positive value below kMaxPlayers.

		GETTER(int, playerIndex, PlayerIndex);

Amount of experience points. Determines the players level. XP can be increased by obtaining tokens.

		GETTER_SETTER(uint64_t, xp, XP);

Constructor / destructor.

		virtual ~Player();

Sets the Player’s properties with the given dictionary.

		virtual void setProperties(Level& level, ValueMap& dict);

Create fixtures attached to the physical body.

		virtual void createFixtures();

Load the player’s previous physical state onEnter.

		virtual void onEnter();

The tick function is called to update the player’s movement, action, start animations, etc.

		virtual void tick(float delta);

Animate is called to update the player’s animations.

		virtual void animate(float delta);

Called when the player has input.

		virtual void handleInput(float delta);

Looks through the current physical contacts for objects which the Player might interact with. Example: if currently touching a Fire object, take damage.

		virtual void contactObjects();

Handle being attacked by another object.

		virtual void attackedBy(LevelObject* attacker);

Save / load the game.

		void saveGame();
		void loadGame();

Give the player an item.

		virtual void giveItem(const string& type);

Save a certain id to the player’s save game. These uniquely identify items, enemies and other objects. Attaching the id maxhealth03 to a chest and then calling the player’s setId will cause the chest to stay open the next time the level is loaded. A similar method can be used to keep enemies from respawing.

		virtual void saveId(const string& id, bool enabled);

Stop all animations and stand still.

		virtual void standStill();

Set sprite frame for current aim position.

		virtual void setStandingFrame();

Run the flipping animation.

		virtual void animateFlip(bool left, bool rotate);


		virtual void die();

Get offset for staff light.

		virtual Point getLightOffset() const;

Get player’s strength percentages.

		float getJumpPercent() const;
		float getMissilePercent() const;

Save physical state.

		void savePhysicalState();

Get current XP level.

		static int getXPLevel(long xp);
		static int getNextXPMinimum(long xp);
		static int getPreviousXPMinimum(long xp);

Return true if the player will animate the door.

		static bool willAnimateDoor();

Private / Protected

		typedef Character super;
		typedef Player self;

		bool abilities[kNumberOfAbilities];
		map<string, bool> ids;
		float hangTime;
		bool hanging, onLadder, inWater;
		bool crouching;
		bool currentFoot;
		double lastFootTime, lastClimbTime;
		double lastJumpTime;
		bool didWallJump;
		bool didFall;
		bool didTurn;
		double lastTurnTime;
		double walkStartTime;
		bool didShoot;
		double shootCooldown;
		Vec2 aimPos;
		double magicChargeDuration;
		double actionKeyStartTime;
		int waterSoundId;
		double nextBubbleTime;
		Vec2 lastPos;
		int lastHP;
		long gold;
		static Vec2 savedVelocity;
		static float savedDistanceFromGround;
		static SpriteFrame* savedSpriteFrame;
		static bool savedFlipX;

		static bool didAnimateDoor;
		void loadPhysicalState();
		void animateDoor(Level& level);
		const char* const getAimPrefix() const;
		void endBattle();

Class hierarchy