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

📄 eventmanager.h

📁 大型3D游戏设计制作详析及源代码对于想了解游戏设计的程序来说
💻 H
字号:
#pragma once

#include "stdafx.h"

/*
 *	表示场景效果的枚举类型
 */
enum RanderState
{
	BackGround_Fadein,	// 淡入
	BackGround_Fadeout,	// 淡出
	BackGround_Blank,	// 黑屏
	BackGround_Normal	// 一般状态
};

/*
 *	保存脚本调用环境信息的结构体
 */
struct CallInfo
{
	std::string proc_name ;		// 过程名,相当于cs
	size_t		ip ;			// 过程内偏移,相当于ip
	int			logical_map ;	// 全局量,当前逻辑地图
	std::string Scene ;			// 全局量,当前场景
	std::string	Mask ;			// 全局量,当前场景的掩码
};

class CGameLogic ;

/*
 *	事件管理器,处理所有脚本
 */
class CEventManager
{
public:
	CEventManager(CGameLogic* contex);
	~CEventManager(void);

	/*
	 *	从文件读取某一幕的脚本
	 *  参数:
	 *  scenefile - 文件名
	 */
	void LoadScene(std::string scenefile) ;

	/*
	 *	执行预定的入口函数
	 */
	void Do_Default_Event() ;

	/*
	 *	跳转到某事件,比如进出房屋,相当与jump
	 */
	void Do_Event( const std::string& Proc, std::vector<std::string>* ArguList = NULL ) ;

	/*
	 *	重载函数
	 */
	void Do_Event( int ID, std::vector<std::string>* ArguList = NULL ) ;

	/*
	 *	调用过程,相当与call
	 */
	void Call_Event( const std::string& Proc, std::vector<std::string>* ArguList = NULL) ;
	void Call_Event( int ID, std::vector<std::string>* ArguList = NULL) ;

	/*
	 *	强行从当前过程中返回
	 */
	void Return_from_Event() ;

	/*
	 *	查询事件的后续事件,如没有,则返回-1
	 */
	int Following_Event( int ID ) ;

	/*
	 *	是否在执行脚本过程
	 */
	bool Has_something_todo() ;

	/*
	 *	执行下一句脚本
	 */
	bool Continue_Doing() ;

	/*
	 *	得到当前背景图片ID
	 */
	int GetBackImg() { return BackBmpID; }

	/*
	 *	得到当前背景掩码ID
	 */
	int GetMaskImg() { return MaskBmpID; }

	/*
	 *	绘图引擎得到状态,整体淡入BackGround_Fadein,淡出,全黑
	 */
	int GetState() ;

	/*
	 *	设置场景显示效果,见RanderState
	 */
	void SetState( RanderState state ) ;

	/*
	 *	是否在等待用户的回车
	 */
	bool is_wait_for_enter() ;

	/*
	*	开始逻辑等待,不执行脚本
	*   参数:
	*   count	- 需要等待的帧数
	*/
	void Wait( size_t count ) ;

	/*
	 *	处理用户按下的回车
	 */
	void UserPressedEnter() ;

	bool m_is_in_state ;

protected:
	/*
	 *	从文件读入一行脚本代码
	 */
	std::string	 readin(std::ifstream& in ) ;

	/*
	 *	切换场景
	 */
	void		 ChangeScene( const std::string& scene_name, const std::string& mask_name, int logical_map ) ;

	/*
	 *	执行某条语句
	 */
	void		 Executer( std::stringstream& stream ) ;

	/*
	 *	根据段代码和ip放置读指令指针,int版本用来兼容老的设计,根据过程号寻址
	 */
	void		 PosIterator( int cs, size_t ip, std::vector< std::string >::iterator& pos ) ;

	/*
	 *	根据段代码和ip放置读指令指针,新的版本,根据过程名寻址
	 */
	void		 PosIterator(const std::string& proc, size_t ip, std::vector< std::string>::iterator& pos) ;

	/*
	 *	根据过程名返回代码段的引用
	 */
	std::vector<std::string>& FindProc( const std::string& ProcName ) ;
	std::vector<std::string>& FindProc( int ProcID ) ;

	/*
	 *	判断是否在逻辑等待,不执行脚本
	 *  返回值:
	 *  true表示继续等待,不执行脚本,false表示不在等待状态
	 */
	bool is_logic_waiting() ;

private:
	/* 环境变量 */
	CGameLogic*					contex ;

	/* 指向过程的指针,相当于cs */
	std::string					now_event ;

	/* 所有的过程 */
	std::list< std::pair< std::string, std::vector< std::string > > > event_lib ;

	/* 执行过的脚本名集合,用于统计数据 */
	std::set<std::string>								call_record ;

	/* 指向具体语句的指针,相当于ip */
	std::vector< std::string >::iterator				pos_in_a_event ; 

	/* 过程调用栈 */
	std::vector<CallInfo>								Call_Stack ;

	/* 调用参数栈 */
	std::list< std::vector<std::string> >				Arguments_Stack ;

	/* 当前过程参数列表 */
	std::vector< std::string >							Arguments_list ;

	/* 事件关系库,记录一个事件的后续事件 */
	std::map<int, int>									relation_lib ;

	/* 需要等待的帧数 */
	size_t					m_waiting_frame ;

	/* 开始等待时候的帧数 */
	size_t					m_frame_count ;

	/* 是否在执行逻辑等待 */
	bool					m_is_doing_logic_wait ;

	/* 背景图片资源号 */
	int						BackBmpID ;

	/* 掩码图片资源号 */
	int						MaskBmpID ;

	/* 背景图片资源名字 */
	std::string				BackBmpResName ;

	/* 掩码图片资源名字 */
	std::string				MaskBmpResName ;

	/* 是否执行脚本过程中 */
	bool					is_doing_event ;

	/* 是否等待用户回车 */
	bool					wait_for_enter ;

	/* 场景显示效果 */
	RanderState				m_background_state ;
};

inline int CEventManager::GetState()
{
	return m_background_state ;
}

inline void CEventManager::SetState(RanderState state )
{
	m_background_state = state ;
}

inline bool CEventManager::is_wait_for_enter()
{
	return wait_for_enter ;
}

inline void CEventManager::UserPressedEnter()
{
	wait_for_enter = false ;
}

⌨️ 快捷键说明

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