Click here to Skip to main content
15,898,134 members
Articles / Mobile Apps / Android

Diggers: cross-platform 2D game

Rate me:
Please Sign up or sign in to vote.
4.80/5 (17 votes)
5 Nov 2012CPOL4 min read 33.5K   43  
2D cross-platfrom game using SDL and Open GLES 2.0
#include <string>
#include <list>

#include "Gameplay/Chunk.h"
#include "AI/Skills.h"
#include "AI/Thinks.h"

//#include "Sprites/Sprite.h"
#include "AI/Nature.h"



enum eDwarfAnimations
{
	eDwarfAnimationIdle				= 1,
	eDwarfAnimationWave				= 2,
	eDwarfAnimationFall				= 3,
	eDwarfAnimationRotateRight		= 4,
	eDwarfAnimationRotateLeft		= 5,
	eDwarfAnimationRotateFromRight	= 6,
	eDwarfAnimationRotateFromLeft	= 7,
	eDwarfAnimationMoveRight		= 8,
	eDwarfAnimationMoveLeft			= 9,
	eDwarfAnimationWorkRight		= 10,
	eDwarfAnimationWorkLeft			= 11
};



enum eInstruments
{
	eInstrumentNone = 1,
	eInstrumentAxe = 2,
	eInstrumentPickAxe = 3,
	eInstrumentHammer = 4
};



class Dwarf : public Sprite
{
private:
	Dwarf(void);

	float m_Strength;
	float m_Mastery;
	float m_Intellect;
	float m_MineralLore;

	float m_CurrentSpeed;
	bool m_PosChanged;

	Think* m_CurrentThink;
	float m_TotalThinkTime;
	float m_TotalIdleTime;
	float m_Happyness;
	unsigned char m_HappynessGrade;
	static const unsigned char m_MaxHappyness = 100;

public:
	Dwarf(int _x, int _y, std::wstring _name, 
		float _strength, float _mastery, float _intellect, float _mineralLore, float _happyness);
	~Dwarf(void);

	void CalcThinkSpriteCoordinates(void);
	bool StartMoveToFarCell(int _x, int _y);
	bool StartMoveToNearCell(int _x, int _y);
	bool TaskMovingToNearCell(float _dt);
	bool TaskSearchForCellID(unsigned int _cellID);
	bool TaskMining(float _dt);
	bool TaskCutting(float _dt);

	void ChooseJob(float _dt);
	void ChooseTask(float _dt);

	void MakeTask(float _dt);
	void MakeJob(float _dt);
	void SwitchAnimation(void);


	bool ApplyGravity(float _dt);
	virtual bool Update(float _dt);
	void ProcessChunkChange(void);
	void ProcessPosChange(void);
	void SetPosition(int _x, int _y);
	void AddSpriteBlocksNear(bool _fullRecalculate);
	void AddNeededChunks(void);

	void ChangeHappyness(float _dHappyness, bool _init = false);

	bool FindWay(int _x, int _y);

	void SetAnimation(eDwarfAnimations _animation);
	void SetAnimationImmediately(eDwarfAnimations _animation);

	static Dwarf* DwarfFromScreenCoords(float _screenX, float _screenY);
	bool CanDragDwarf(int _x, int _y);

	bool m_IsDragging;
	int m_XDest, m_YDest;

	int m_XBeginNearBlocks, m_YBeginNearBlocks;

	bool m_WasJumping;
	bool m_IsJumping;
	bool m_IsFalling;
	bool m_IsRotating;
	bool m_IsMoving;

	static std::vector<Dwarf*> m_Dwarfs;
	static float m_DMoveInCell;

	static const unsigned int mSizeNearBlocks = 3 * Chunk::m_ChunkSize;
	static const unsigned int mRadiusToFindWay = 64;
	SpriteBlock** m_UnpassableSpriteBlocksNear[mSizeNearBlocks][mSizeNearBlocks];
	SpriteBlock** m_PropertySpriteBlocksNear[mSizeNearBlocks][mSizeNearBlocks];

	int m_CellTimes[mSizeNearBlocks][mSizeNearBlocks];
	typedef std::pair<int, int> TCoord;
	std::list< TCoord > m_Way;

	char m_WayBlocks[2 * mRadiusToFindWay + 1][2 * mRadiusToFindWay + 1];

	eJob m_PreviousJob;
	eJob m_CurrentJob;
	eTask m_CurrentTask;
	bool m_NeedWork;

	Skill* m_Skills[Skill::m_SkillsCount];

	static unsigned short m_DwarfsHappyGrades[m_HappynessGradesCount];

	short m_DXPrev;
	short m_DYPrev;

	eInstruments m_Instrument;

	eDwarfAnimations m_CurrentAnimation;
};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions