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

📄 gameobj.h

📁 大型3D游戏设计制作详析及源代码对于想了解游戏设计的程序来说
💻 H
字号:
/********************************************************************
	created:	2004/05/25
	created:	25:5:2004   13:25
	filename: 	d:\Works\Gamelib\GameObj.h
	file path:	d:\Works\Gamelib
	file base:	GameObj
	file ext:	h
	author:		lazybug
	
	purpose:	定义游戏中常用的基本结构体和类
*********************************************************************/

#pragma	   once

#include <string>
#include <vector>
#include "./callback_functions.h"

class CGameLogic ;

/*
 *	描述说话窗口的结构体
 */ 
struct STalking_Board
{
	int		Talker_id ;		 // 说话人的头像资源号
	int		Lisenner_id ;	 // 听者的头像资源号

	std::string	words ;		 // 说话内容(可以带有tag标记)
	bool	actived ;		 // 是否激活(显示在屏幕)
};

/*
 *	描述发现物品窗口的结构体
 */
struct SDiscovery_Board
{
	int		item ;				// 显示图片的资源号
	std::string description ;	// 对应的文本描述

	bool	actived ;			// 是否激活(显示在屏幕)
};

/*
 *	菜单项
 */
struct SMenuItem
{
	int			pic_id ;		// 菜单项的小图片
	const char*	description ;	// 菜单的文字描述

	int			x ;				// 相对于菜单图片的坐标x
	int			y ;				// 相对于菜单图片的坐标y
	int			width ;			// 菜单项宽度
	int			height ;		// 菜单项高度
	int			is_focus ;		// 菜单项是否聚焦
};

/*
 *	游戏图片资源结构体
 */
struct GameRes
{
	std::string filename ;		// 图片文件名
	int			h_frame ;		// 图片的水平帧数
	int			v_frame ;		// 图片的垂直帧数
	DWORD		color_key ;		// 掩码色
};


/*
 *	人物属性结构体
 */
struct SPropertyItem
{
	std::string name ;			// 属性名
	std::string description ;	// 属性描述
	int			icon_id ;		// 对应的图片资源号
	int			val ;			// 属性值
};

/*
 *	游戏对象的基类,包涵一些基本信息,用于提供给Delphi引擎
 */
class CBaseStruct
{
public:
	int ResID ;		// 图片资源号
	int x ;			// x坐标
	int y ;			// y坐标
	int facing ;	// 正面朝向(仅用于Actor)
	int state ;		// frame状态,静止或者跑步或者战斗(仅用于Actor)

	const char* attack_str ;	// 信息字符串指针,用于人物头部渐升的字符,比如“攻击升级”(仅用于Actor)
} ;

/*
 *	简单对象类,表示一些简单的物品图片对象,比如胡萝卜的图
 */
class CSimpleObj : public CBaseStruct
{
public:
	/*
	 *	构造函数
	 *  参数:
	 *  res_info	- 表示资源信息的结构体
	 */
	CSimpleObj(GameRes* res_info) 
	{
		(*LoadActor)( res_info->filename.c_str(), res_info->h_frame, res_info->v_frame, res_info->color_key, &ResID ) ;
	};
	/*
	 *	析构函数(空)
	 */
	~CSimpleObj() {};
};

/*
 *	用来表示人物对象的类,包括npc和用户控制的人物
 */
class CPlayer : public CBaseStruct
{
public :
	/*
	 *	构造函数
	 *  参数:
	 *	name			- 人物名
	 */
	CPlayer( const std::string& name ) ;

	/*
	 *	析构函数
	 */
	~CPlayer() ;

	// 用来表示人物的形象状态
	enum RanderState { 
						BIG,	    // 室内用BIG
						SMALL,	    // 室外用SMALL
						MID,		// 大厅等地的中等大小
					 };

	// npc对话的模式
	enum TalkState { RANDOM,		// 随机选一个说话脚本执行
					 CONDITIONAL,   // 有条件的执行连续的几个脚本,脚本中控制条件
					 CONTINUAL,		// 顺序执行几个预定的说话脚本
					 LOOP			// 循环执行预定的说话脚本
				   };

	/*
	 *	返回Actor的属性结构体指针
	 *  参数:
	 *  name	- 属性名,比如“生命力”
	 *  返回值:
	 *  结构体指针,保证不为NULL,如果没有此属性,则抛出异常
	 */
	SPropertyItem* Property(const std::string& name) ;

   /*
	*  返回Actor的技能结构体指针
	*  参数:
	*  name	- 技能名,比如“斗鸡刀法”
	*  返回值:
	*  结构体指针,保证不为NULL,如果没有此技能,则抛出异常,参考CPlayer::Property()
	*/
	SPropertyItem* Skill( const std::string& name ) ;

	/*
	 *	Actor学会某项技能
	 *  参数:
	 *  skill	- 技能结构体的引用
	 */
	void LearnSkill( const SPropertyItem& skill ) ;

	/*
	 *	根据物品描述来寻找装备
	 */
	SPropertyItem* FindTools(const std::string& description) ;

	/*
	 *	根据装备描述来去除装备
	 */
	void DelTools(const std::string& description) ;

	/*
	 *	给人物添加装备
	 *  参数:
	 *  tool	- 装备(属性)结构体的引用
	 */
	void AddTool( SPropertyItem& tool ) ;

	/*
	 *	设置人物使用的图片资源
	 *  参数:
	 *  s	- 枚举型,参见CPlayer::RanderState
	 */
	void SetActiveRes( RanderState s ) ;

	/*
	 *	在队列末尾加入新的路点
	 */
	void push_way_point(int x, int y) ;

	/*
	 *	在队列头加入路点,改变现有路径
	 */
	void insert_way_point(int x, int y) ;

	/*
	 *	清空原有路点
	 */
	void flush_all_way_point();

	/*
	 *	设置路线是否循环
	 *  参数:
	 *  is_loop	- true表示路线循环,false表示仅一次路线
	 */
	void set_task_loop(bool is_loop) ;

	/*
	 *	状态自更新
	 */
	bool AutoUpdate() ;

	/*
	 *	进入自等待状态,傻乎乎的啥都不做
	 *  参数:
	 *  time	- 等待的事件,ms为单位
	 */
	void EnterWaitingMode( unsigned int time ) ;

	/* 
	DetectOtherActor 用来测试目标位置是否会遇到其他Actor
	Parameters
	dx	 - 从当前位置开始的X方向目标偏移量
	dy   - 从当前位置开始的Y方向目标偏移量 
	*/
	bool DetectOtherActor(int dx, int dy) ;

   /*
	*	在npc内置对话库末尾加入新的脚本过程名
	*   参数:
	*   ProcName	- 脚本的过程名,不作存在性检查
	*/
	void push_script( std::string& ProcName ) ;

	/*
	 *	根据Actor当前状态,返回Actor的对话脚本
	 *  返回值:
	 *  npc对话脚本的过程名
	 */
	std::string& TalkScript() ;

	/*
	 *	设置npc对话的模式
	 *  参数:
	 *  state	- 枚举型变量,表示不同的对话模式
	 */
	void SetTalkState(TalkState state) ;

	/*
	 *	根据脚本更改Talking状态
	 *  参数:
	 *  ProcName	- 下一次对话的过程名
	 */
	void NextTalking( std::string& ProcName ) ;

	/*
	 *	设置人物形象上的信息字符串,并在预定的帧数之后消失
	 *  参数:
	 *  info		- 信息字符串
	 */
	void SetInfoText( const std::string& info ) ;

	/*
	 *	npc的简单智能,选择招数,对另一Actor进行攻击
	 *  参数:
	 *  fighter_name	- 对手的名字,通常是用户控制的人物
	 */
	void NpcFight( std::string& fighter_name ) ;

	/*
	 *	开始播放人物攻击动画帧,并在预定帧数之后停止
	 */
	void PlayFightMovie() ;

	/*
	 *	Actor攻击另一个人物,执行相应的操作
	 *  参数:
	 *  skill	- 使用的技能,必须包涵在技能列表里,否则抛出异常
	 *  name	- 受攻击的Actor的名字
	 */
	void Attack( std::string& skill, std::string& name ) ;

	/*
	 *	Actor尝试逃跑,可能成功,可能不成功,执行相应操作
	 *  参数:
	 *  name	- npc对手的名字
	 */
	void TryEscape( const std::string& name ) ;

	/*
	 *	改变人物一个属性的值(饱和加减法,以及处理升级)
	 *	参数:
	 *  property	- 属性名
	 *	val			- 变化的相对值
	 */
	void ChangeProperty( const std::string& property, int val ) ;

	/*
	 *	设Actor死掉
	 */
	void Die() ;

	/*
	 *	设置Actor是否处于自动攻击模式
	 *  val	- true 表示自动攻击
	 */
	void AutoAttack( bool val ) ;

protected:
	/*
	 *	类内常量
	 */
	enum CONSTANTS { FRAMENUMFORTEXT = 30, FRAMENUMFORMOV = 10 };

	/*
	 *	根据现在节点,计算到目的节点的平均偏移
	 */
	double compute_dxy() ;

	/*
	 *	测试是否能一步到达目的地
	 */
	bool test_goal() ;

	/*
	 *	动态计算地图上两点间距离
	 */
	double ActiveLength(int x1, int y1, int x2, int y2) ;

	/*
	 *	检查动画帧和信息字符串是否播放完毕,如完毕则关闭动画
	 */
	void CheckMovFrameEnd() ;

private:
	/* 当前选择的路线偏移x */
	int		way_point_dx ;

	/* 当前选择的路线偏移y */
	int		way_point_dy ;
	
	/* 是否正在执行预定路线 */
	bool	is_doing_task ;

	/* 坚持路线的时间,frame为单位 */
	int		consist_way_time ;

	/* 在等待状态下,需要等待的时间,ms为单位 */
	size_t  waiting_time ;

	/* 在等待状态下,开始等待的时间,ms为单位 */
	size_t  start_time ;

	/* 是否在等待状态 */
	bool	waiting_mode ;

	/* 预定路线是否循环 */
	bool    is_loop ;

	/* Actor是否会自动攻击 */
	bool    is_auto_attack ;

	/* 人物对话的状态迁移 */
	TalkState m_talk_state ;

	/* 当前的对话指针 */
	size_t m_current_words ;

	/* 人物内置的对话脚本调用 */
	std::vector<std::string>	m_talking_script ;

	/* 记录人物对象的信息字符 */
	std::string m_attack_str_buf ;

	/* 记录帧数,用于播放人物信息字符串 */
	size_t	m_frame_count_for_info ;

	/* 记录帧数,用于播放人物动画 */
	size_t m_frame_count_for_movie ;

public:

	/* 环境变量 */
	static CGameLogic*	context ;

	/* 是否在场景中激活 */
	bool active_in_scene ;

	/* Actor 是否死掉(永远不出现在场景里) */
	bool	is_dead ;

	/* 大图片的资源号,由Unicough引擎分配 */
	int Big_Res_id ;

	/* 中等大小图片的资源号,由Unicough引擎分配 */
	int Middle_Res_id ;

	/* 小图片的资源号,由Unicough引擎分配 */
	int Small_Res_id ;

	/* 人物头像的资源号 */
	int face_Res_id ;

	/* 出现在游戏中的人物名 */
	std::string name ;

	/* 人物的描述,用于查看生平简介 */
	std::string self_description ;

	std::vector<SPropertyItem>  m_property ;	// 属性list
	std::vector<SPropertyItem>  m_tools ;		// 装备list
	std::vector<SPropertyItem>  m_skills ;		// 技能list
	std::list< std::pair<int, int> >	m_way_points ;			// 走路点
	std::list< std::pair<int, int> >    m_backup_way_points ;	// 记录走过的路点
};

⌨️ 快捷键说明

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