Wizard Fu's Cocos2dx Platformer Game Engine v1.0.1

nat@wizardfu.com

See the file LICENSE for the license governing this code.

LevelObject is an abstract base class implementing many common functions and member variables for objects that reside inside the current Level. LevelObject has a Profile, text label, sprite, id, name and more. All game objects, including Player, Enemy, Item, etc. are derived from this class.

LevelObject derives from cocos2d::Node and PhysicsObject.

LevelObject accepts default properties from the its Profile and overrides these defaults with dynamic properties loaded from the tmx object.

#pragma once
#include "Headers.h"

class Level;
class Player;

Action tags

Action tags help distinguish what action an object is currently performing. An object’s current action can be queried with Node::getChildByTag or LevelObject::isAnimating.

Note that action tags are not guaranteed to be deterministic, so they should not be relied upon for multiplayer game state logic.

enum
{
	kActionRotate = 1,
	kActionJump,
	kActionWalk,
	kActionStep,
	kActionSleep,
	kActionRest,
	kActionSeek,
	kActionAttack,
	kActionDie,
	kActionOpen,
	kActionClose,
	kActionFall,
	kActionLand,
	kActionHang,
	kActionFade,
	kActionTurn,
	kActionSwim,
	kActionHurt
};

LevelObject

class LevelObject : public Node, public PhysicsObject
{
	public:

Each concrete LevelObject-derived class implements the profileName() function to specify the profile that shall be loaded. Using a profile name of Wizard will load Assets/Profiles/Wizard.plist. If a Profile cannot be loaded, a blank one is created, guaranteeing the pointer. The profileName method is marked pure virtual in base classes to prevent instantiation.

		virtual const char* const profileName()=0;

		LevelObject();
		virtual ~LevelObject();

The object’s Profile. Guaranteed to be a valid pointer, as it loads a blank profile if none is found.

		GETTER(Profile*, profile, Profile);

The object’s sprite. Uses SpriteMirrored so the sprite can automatically be reflected in lakes, puddles and other bodies of water.

		GETTER(SpriteMirrored*, sprite, Sprite);

id is used to uniquely identify this object against all other objects in the game. An enemy can stay dead across levels by setting an id. An item can only be obtained once if it has an id.

		GETTER(string, id, Id);

Width / height of the object in pixels.

		GETTER(float, width, Width);
		GETTER(float, height, Height);

The width / height of the object’s physical fixtures in pixels.

		GETTER(float, fixtureWidth, FixtureWidth);
		GETTER(float, fixtureHeight, FixtureHeight);

The radius of this object in pixels. Used primarily by objects with circular fixtures.

		GETTER_SETTER(float, radius, Radius);

Name given to this object specified using tmx object property name.

		GETTER(string, name, Name);

Damage this object will deal.

		GETTER_SETTER(int, damage, Damage);

Set this to true to have the object removed by the Level on the next update.

		GETTER(bool, removeOnUpdate, RemoveOnUpdate);

Sets properties for this object from the tmx’s object dictionary. This is a virtual method and is called from the base class upwards. For example Player::setProperties is called first, which in turn calls Character::setProperties which calls LevelObject::setProperties.

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

Create fixtures and add them to the physics body.

		virtual void createFixtures();

Runs one of the profile’s animations on the sprite.

		virtual void runAnimation(const char* animKey, int actionTag, bool repeat, bool setFirstFrame, int index = -1);

Return true if the object is animating the given animation tag.

		virtual bool isAnimating(int tag);

Flips the object’s sprite on the x plane to point toward the given position.

		virtual void pointToward(const Point& p);

Set a label string. Will appear slightly above the LevelObject.

		virtual void setLabel(const string& str);

Tick the object’s state with a fixed time delta.

		virtual void tick(float delta);

Animate the object with a variable time delta.

		virtual void animate(float delta);

Run actions when the object is attacked by another object.

		virtual void attackedBy(LevelObject* attacker);

A warning called when something dangerous happens nearby, like a projectile exploding.

		virtual void alert(LevelObject* alerter);

Disables the object. Used by chests to already be open or enemies to already be knocked out.

		virtual void disable();

Set opacity.

		virtual void setOpacity(GLubyte opacity);

Return the distance squared from this object to the given position.

		virtual float getDistanceSQ(const Vec2& p);

Return the current Level object.

		Level& getLevel();

Return the current Player object from the Level.

		Player& getPlayer();

Return the object’s position from its properties.

		static Vec2 getObjectPosition(ValueMap& properties);

Create a new level object given the class name.

		static LevelObject* newObject(const string& className);

Private / Protected

	private:
		typedef Node super;
		typedef LevelObject self;

	protected:
		Label* label;
		double timer;

Animate a dust-like effect with mutliple circular particles.

		void animateDust(int nParticles, float scale, float width, float y0);

Animate a splash effect with circular particles that arc.

		void animateSplash(int nParticles, float scale);

Animate an effect that looks like bubbles.

		void animateBubbles(int nParticles, float scale, float maxHeight);

Animate a glowing radial gradient behind the object.

		SpriteMirrored* animateGlow(const Color3B& color, float inTime, float outTime, float minScale, float maxScale, int minOpacity, int maxOpacity);

Parse a polygon from tmx object properties.

		void parsePolygon(ValueVector& points, vector<Vec2>& polygon);

		bool loadProfile(const string& name);
		virtual void createCircularFixture(float radius);
		virtual void createRectangularFixture(float width, float height);
		virtual void alertNearByObjects();
};

Class hierarchy

 
h