⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 world.h

📁 吃豆子游戏源码
💻 H
字号:
/**
 *	File	:	World.h
 *  Author	:	Kevin Lynx
 *	Date	:	2007/7/29
 */
#ifndef	_WORLD_H_
#define	_WORLD_H_

#include "Singleton.h"
#include <map>

using std::map;

/**
 * World class
 *
 * Implements the world in PacShooter, mainly building the map
 */
class World : public Singleton<World>
{
public:
	/**
	 * Stone type
	 *
	 */
	enum E_StoneType
	{
		STONE_NONE = -1,
		/// these stones below cannot be destroyed
		STONE_DESTROY_MIN,
		STONE1_1 = 1,
		STONE2_1,
		STONE_DESTROY_MAX,
		/// these stones below can be destroyed 
		STONE1_2,
		STONE1_3,
		STONE2_2,
		STONE2_3,
		STONE2_4,
		STONE_COUNT
	};

	/**
	 * Monster type
	 *
	 */
	enum E_MonsterType
	{
		MONSTER_NONE = -1,
		MONSTER_2,
		MONSTER_3,
		MONSTER_4
	};

	/**
	 * Bonus type
	 *
	 */
	enum E_BonusType
	{
		BONUS_NONE = -1,
		B_BEAN,					/// 豆子
		B_4_SIDES,				/// 朝四个方向发射子弹的奖励
		B_ARMOUR,				/// 保护主角不受伤害的盾牌奖励
		B_MONEY,				/// 增加分数
		B_TIME,					/// 延长游戏时间
		B_GLUE,					/// 使所有怪物移动速度下降
		B_SUPER_FIRE,			/// 可以连续发射子弹,不用等到子弹消失
		B_LIFE,					/// 增加生命值
		B_COUNT
	};

	/**
	 * resources set type
	 *
	 * key : the model's type 
	 * value: the resources, i mean the model
	 */
	typedef map<int, IAnimatedMesh*>	tModelSet;

	/**
	 * monster texture set
	 *
	 * key : the monster's type
	 * value : the texture
	 */
	typedef map<int, ITexture*>		tMonsterTexture;

public:
	/**
	 * Constructor
	 *
	 */
	World( IrrlichtDevice *device );

	/**
	 * Destructor
	 *
	 */
	~World();

	/**
	 * load a level 
	 *
	 */
	bool	load( bool nextLevel = false);

	/**
	 * createGround
	 *
	 */
	void	createGround();

	/**
	 * release
	 *
	 */
	void	release();

	/**
	 * collision
	 *
	 * You should know, in this game the collision between sprite(model) and
	 * world only depends on xz plane ( donot care y value in 3d world ).
	 * So just provide the rect around the model on xz plane, and the direction
	 * of the model that means the model is moving toward dir now.
	 *
	 * and this function will return the collision check result .
	 * 0 means it can move , otherwise, it got something like stone( cannot move),
	 * bonuses , especially for our hero.
	 */
	int		collision( const rect<float> &box2d, const vector3df &dir );

	/**
	 * adjustPos
	 *
	 * mainly adjust the player's position
	 */
	void	adjustPos( const rect<float> &box2d, const vector3df &dir, vector3df &outPos );

	/**
	 * an assist function.
	 *
	 */
	int		collision( const vector3df &pos );

	void	worldToMap( float x, float z, int &ax, int &az );
	void	mapToWorld( int ax, int az, float &x,float &z );
	void	mapToWorldCenter( int ax, int az, float &cx, float &cz );

	/**
	 * check whether got a bean
	 * It's impossible that our hero can eat two beans at the same time!
	 *
	 */
	bool	isGotBean( const aabbox3df &box  );

	/**
	 * getMonsterRes
	 *
	 * return a monster's mesh texture 
	 */
	bool	getMonsterRes( int type, IAnimatedMesh *&mesh, ITexture *&texture );
	
	/**
	 * getBonusRes
	 *
	 */
	bool	getBonusRes( int type, IAnimatedMesh *&mesh );
	
	/**
	 * getMapValue
	 *
	 * 
	 */
	int		getMapValue( int ax,int az );

	/**
	 * check whether the grid you can get
	 *
	 */
	bool	canReach( int ax, int az );

	/**
	 * check whether a sprite is at the center position
	 *
	 * you know, a sprite is always at some array position (a grid) , 
	 * so this function check whether the sprite is at the center of the grid
	 */
	bool	isCenterPos( const vector3df &worldPos );

	/**
	 * getValidPos
	 *
	 * return a valid position in world space, there can put a monster
	 */
	void	getValidPos( float &x, float &z );

	void	setLevelIndex( int index );

	/**
	 * isLevelClear
	 *
	 * when all the beans were eaten, the level is clear,so return true
	 */
	bool	isLevelClear();
	
	/**
	 * isOutOfWorld
	 *
	 * return whether the given rect on xz plane is out of world
	 */
	bool	isOutOfWorld( const rect<f32> &rect );

	int		getBeanCount() { return mBeanCount; }
private:
	/**
	 * load resources which used to build the world
	 *
	 * @param file the level resources config file
	 */
	bool	loadModels( const std::string &file );

	/**
	 * load map
	 *
	 * this function will read map array data and put stones on the ground
	 * @param mapFile the map array config file, the file name is 
	 *        specified in the level resources config file
	 */
	bool	loadMap( const std::string &mapFile );

	/**
	 * create map item
	 *
	 */
	void	createMapItem( int value, int x, int z );

	/**
	 * caculate a scene node id
	 *
	 */
	int		getNodeId( int ax, int az );

	/**
	 * getNextLevelIndex
	 *
	 */
	int		getNextLevelIndex( int curIndex );


private:
	IrrlichtDevice *mDevice;
	
	tModelSet		mStoneSet;
	tModelSet		mMonsterSet;
	tModelSet		mBonusSet;
	tMonsterTexture mMonsterTextSet;

	ITexture       *mBkTexture;
	ISceneNode	   *mGroundNode;
	
	int		*mMapData;
	int		mBeanCount;	
	int		mCurLevelIndex;
public:
	dimension2d<int>	mMapSize;
	dimension2d<float>	mStartPos;
	dimension2d<int>	mStoneSize;

	position2d<float>   mPlayerWorldPos;

};

/*
	MapData : 1---STONE_COUNT : cannot move
			  STONE_COUNT - STONE_COUNT + B_COUNT : get bonus	
*/
#endif // end _WORLD_H_

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -