Wizard Fu's Cocos2dx Platformer Game Engine v1.0.1

nat@wizardfu.com

See the file LICENSE for the license governing this code.

Profile is a utility class that loads and caches the spritesheets, sprites, animations, sound effects and attributes for a given class name. Profiles are stored as property lists in the Assets/Profiles directory.

Animations

Animations are stored in a dictionary value called animations. The key corresponds to the name of the animation. Each animation value is a sub-dictionary which specifies the key-value pairs used by the animation.

The format key-value specifies a printf-style format string used to load all spriteframes. Blob%d.png for example will load Blob0.png, Blob1.png, Blob2.png, etc. The frameCount key-value specifies the number of frames. The delay key-value specifies the amount of delay in seconds between each frame.

Sounds

Sounds are stored in a dictionary value called sounds. Each key specifies the name of the sound and each value can either be a string or array. If the value is a string, it specifies a single sound effect. If the value is an array, it specifies multiple sound effects, one of which is chosen at random when playing the sound.

Attributes

Attributes are stored in a dictionay called attributes. An attribute specifies a default for the given object class. Setting hp to 1 in Blob.plist will indicate that all Blob objects have a default of one hitpoint. This can be overridden in the tmx map file. If a Blob is created in the tmx file and given an hp property of 2, then that particular Blob will have two hitpoints.

Here’s a list of the attributes that can be specified for any generic class. Keep in mind that individual classes may or may not extend this list of attributes in the setProperties method.

Spritesheets

An array listing all the spritesheets used by this class. These are automatically cached (along with animations and sounds) when the Profile loads.

Profile

#pragma once
#include "Headers.h"

class Profile : public Ref
{
	public:
		Profile();
		~Profile();

Get a profile by name and cache it (or return an already-cached profile). Using Zombie as the name will load Assets/Profiles/Zombie.plist.

		static Profile* profileWithName(const string& name);

Fills a vector with all the default attribute keys. Example: hp, damage, scale, etc.

		void getAllDefaultAttributeKeys(vector<string>& keys);

Return the default attribute value for the given key.

		Value defaultAttributeForKey(const string& key);

Return the animation for a given key.

		Animation* animationForKey(const string& key);

Return the animation for a given key and index. Used when the given animation dictionary is actually an array of animations.

		Animation* animationForKey(const string& key, int index);

Return true if the profile has an animation for the given key and/or index.

		bool hasAnimation(const string& key);
		bool hasAnimation(const string& key, int index);

Play a sound given a key. If the key’s value is a string, it is treated as a filename and the sound is played. If the key’s value is an array of strings, one of them is selected at random and played.

		int playSound(const string& key);

Return the sprite frame for the given key and index.

		SpriteFrame* spriteFrameForKey(const string& key, int index);

Private

	private:
		string title;
		ValueMap defaultAttributes;
		map<string,Vector<SpriteFrame*>> sprites;
		map<string,Vector<Animation*>> animations;
		ValueMap sounds;
	
		// Using this subclass of map helps the purge of cached profiles to happen as and when planned.
		class profilesMap : public map<string, Profile*>
		{
			public:
				virtual ~profilesMap()
				{
					for (auto it = this->begin(); it != this->end(); it++)
						if (it->second)
							delete (it->second);
				}
		};

		typedef profilesMap mapType;
		static mapType profiles;

		bool loadProfile(const string& name);
		void loadSpritesheets(ValueVector& spritesheets);
		void loadAnimations(ValueMap& animations);
		void loadAnimation(const string& key, ValueMap& dictionary);
		void initAnimationWithFormat(Animation* animation, const string& format, const string& subString, int frameCount, float delay);
		void initArrayWithSpriteFrames(Vector<SpriteFrame*>& array, const string& format, const string& subString, int frameCount);
		void loadSpriteFrames(ValueMap& dictionary);
		void loadSpriteFramesForKey(const string& key, const string& format, int frameCount);
		void loadSounds(ValueMap& dictionary);
		void loadSound(const string& string);
		string getSoundForKey(const string& key);
};
 
h