📄 eventmanager.cpp
字号:
#include "StdAfx.h"
#include "./eventmanager.h"
#include "./callback_functions.h"
#include "gamelogic.h"
using namespace std ;
CEventManager::CEventManager(CGameLogic* contex)
{
this->contex = contex ;
is_doing_event = false ;
wait_for_enter = false ;
// 背景默认状态:不作淡入
m_background_state = BackGround_Normal ;
m_frame_count = 0 ;
m_waiting_frame = 0 ;
m_is_doing_logic_wait = false ;
}
CEventManager::~CEventManager(void)
{
}
/*
* 读取脚本进入缓冲,比如第一幕
*/
void CEventManager::LoadScene(std::string scenefile)
{
event_lib.clear() ;
scenefile.insert(0, ".\\Scenes\\") ;
ifstream in(scenefile.c_str()) ;
stringstream stream ;
string ProcName ;
int first;
int second ;
string Actorname ;
string BackBmpName ;
string MaskBmpName ;
stream.rdbuf()->str( readin( in ) ) ;
if ( in.is_open() )
{
// 读事件关系表
stream >> first >> second ;
while ( first != -1 )
{
/* 触发事件 */
relation_lib.insert( make_pair( first, second ) ) ;
stream.clear() ;
stream.rdbuf()->str(readin( in ) ) ;
stream >> first >> second ;
}
stream.clear() ;
stream.rdbuf()->str(readin( in ) ) ;
// 读默认开始事件
stream >> now_event ;
vector<string> temp ;
string reserve_word ;
// 读所有事件进入事件库
stream.clear() ;
stream.rdbuf()->str(readin( in ) ) ;
stream >> reserve_word >> ProcName ;
while ( ProcName != "END" )
{
stream.clear() ;
stream.rdbuf()->str(readin( in ) ) ;
while ( stream.str() != "END" )
{
temp.push_back( stream.str() ) ;
stream.clear() ;
stream.rdbuf()->str(readin( in ) ) ;
}
event_lib.push_back( make_pair( ProcName, temp) ) ;
temp.clear() ;
stream.clear() ;
stream.rdbuf()->str(readin( in ) ) ;
stream >> reserve_word >> ProcName ;
}
PosIterator( now_event, 0, pos_in_a_event ) ;
// 初始状态:一般状态
m_background_state = BackGround_Normal ;
m_is_in_state = false ;
is_doing_event = true ;
call_record.insert(now_event) ;
this->Continue_Doing() ;
// 提起执行脚本初始化部分
while ( !wait_for_enter && this->Has_something_todo() )
{
this->Continue_Doing() ;
}
}
}
/*
* 从文件读入一行脚本:忽略注释
*/
string CEventManager::readin( std::ifstream& in )
{
char buf[512] ;
string ret ;
in.getline(buf, 512) ;
while ( buf[0] == ';' || buf[0] == '\0' )
{
in.getline(buf, 128) ;
}
ret = buf ;
return ret ;
}
/*
* 是否正在执行情节脚本
*/
bool CEventManager::Has_something_todo()
{
return is_doing_event ;
}
/*
* 脚本解释部分:根据脚本执行不同的操作
*/
void CEventManager::Executer(std::stringstream& stream )
{
string cmd ;
stream >> cmd ;
if ( cmd == "SAY" )
{
/* 这里处理人物对话 */
contex->m_talking_board.actived = true ;
contex->m_GameState = CGameLogic::TALKING ;
string talker ;
string lisenner ;
string words ;
stream >> talker >> lisenner >> words ;
contex->m_talking_board.Talker_id = contex->m_ActorManager->GetActor(talker).face_Res_id ;
contex->m_talking_board.words = words ;
}
else if ( cmd == "FIND" )
{
string item ;
string description ;
string property ;
int val ;
/* 找到道具 */
contex->m_discovery_board.actived = true ;
stream >> item >> description >> property >> val ;
contex->m_discovery_board.item = contex->m_ActorManager->GetSimpleObj( item ).ResID ;
contex->m_discovery_board.description = description ;
SPropertyItem tool ;
tool.name = property ;
tool.description = description ;
tool.icon_id = contex->m_discovery_board.item ;
tool.val = val ;
contex->m_ActorManager->GetActiveActor().AddTool( tool ) ;
}
else if ( cmd == "SHOWTEXT" )
{
string text ;
contex->m_discovery_board.actived = true ;
stream >> text ;
contex->m_discovery_board.description = text ;
contex->m_discovery_board.item = -1 ;
}
else if ( cmd == "SHOW_STA" )
{
stringstream text ;
contex->m_discovery_board.actived = true ;
text << "<size=12> 牛二牺牲了……</p>" ;
text << " <color=16777215>在牛二死之前,您玩出</p>了本游戏 " ;
text << 100 * (double)call_record.size() / this->event_lib.size() ;
text << "% 的剧情</p>,别哭泣,别气馁,再接再</p>厉!</p>" ;
text << " 回车返回主菜单。" ;
contex->m_discovery_board.item = -1 ;
contex->m_discovery_board.description = text.str() ;
}
else if ( cmd == "LEARN" )
{
string description ;
string property ;
int val ;
string actor ;
string temp ;
/* 学会技能 */
contex->m_discovery_board.actived = true ;
stream >> actor >> description >> property >> val ;
contex->m_discovery_board.item = contex->m_ActorManager->GetSimpleObj( "菜刀" ).ResID ;
temp = "速成" ;
temp += description ;
contex->m_discovery_board.description = temp ;
SPropertyItem skill ;
skill.name = property ;
skill.description = description ;
skill.icon_id = -1 ;
skill.val = val ;
contex->m_ActorManager->GetActor(actor).LearnSkill( skill ) ;
}
else if ( cmd == "LEARNEND" )
{
contex->m_discovery_board.actived = false ;
}
else if ( cmd == "SHUTUP" )
{
contex->m_talking_board.actived = false ;
}
else if ( cmd == "FINDCLOSE" )
{
contex->m_discovery_board.actived = false ;
}
else if ( cmd == "CLOSEINPUTS" )
{
contex->m_bAccept_user_inputs = false ;
}
else if ( cmd == "OPENINPUTS")
{
contex->m_bAccept_user_inputs = true ;
}
else if ( cmd == "FADEIN" )
{
this->m_background_state = BackGround_Fadein ;
this->m_is_in_state = false ;
}
else if ( cmd == "FADEOUT" )
{
this->m_background_state = BackGround_Fadeout ;
this->m_is_in_state = false ;
}
else if ( cmd == "SPEAK" )
{
string file ;
stream >> file ;
Speak( file.c_str() ) ;
}
else if ( cmd == "LOADMUSIC" )
{
string file ;
stream >> file ;
LoadMusic( file.c_str() ) ;
}
else if ( cmd == "PLAYMUSIC" )
{
PlayMusic(true) ;
}
else if ( cmd == "STOPMUSIC" )
{
StopMusic() ;
}
else if ( cmd == "WAITKEY" )
{
wait_for_enter = true ;
}
else if ( cmd == "LOADSCENE" )
{
string scene_name, mask_name ;
int logical_map ;
stream >> scene_name >> mask_name >> logical_map ;
this->ChangeScene(scene_name, mask_name, logical_map) ;
}
else if (cmd == "ACTIVEACTOR")
{
string ActorName ;
int ActorX, ActorY, ActorFacing ;
stream >> ActorName >> ActorX >> ActorY >> ActorFacing ;
CPlayer& actor = this->contex->m_ActorManager->GetActor(ActorName) ;
if ( !actor.is_dead )
{
actor.active_in_scene = true ;
actor.facing = ActorFacing ;
actor.x = ActorX ;
actor.y = ActorY ;
}
}
else if ( cmd == "KILLACTOR" )
{
string name ;
stream >> name ;
CPlayer& player = this->contex->m_ActorManager->GetActor(name) ;
player.Die() ;
}
else if ( cmd == "USERCONTROL" )
{
string name ;
stream >> name ;
this->contex->m_ActorManager->SetActiveActor(name) ;
}
else if ( cmd == "DISABLEACTOR")
{
string ActorName ;
stream >> ActorName ;
this->contex->m_ActorManager->GetActor(ActorName).active_in_scene = false ;
}
else if ( cmd == "GOTO" ) /* 跳转到某个过程执行 */
{
string proc ;
stream >> proc ;
Do_Event( proc ) ;
}
else if ( cmd == "CALL" ) /* 调用某个过程执行 */
{
string proc ;
string Argu ;
vector<string> Argu_list ;
stream >> proc >> Argu ;
/*
* 参数压栈
*/
while ( Argu != "CALLEND" )
{
Argu_list.push_back(Argu) ;
stream >> Argu ;
}
Call_Event( proc, &Argu_list) ;
}
else if ( cmd == "RET" ) /* 过程返回 */
{
Return_from_Event() ;
}
else if ( cmd == "SHIFTACTOR" )
{
string name ;
string state ;
stream >> name >> state ;
CPlayer& actor = this->contex->m_ActorManager->GetActor(name) ;
if ( state == "BIG" )
{
actor.SetActiveRes( CPlayer::BIG ) ;
}
else if ( state == "SMALL" )
{
actor.SetActiveRes( CPlayer::SMALL ) ;
}
else if ( state == "MID" )
{
actor.SetActiveRes( CPlayer::MID ) ;
}
}
else if ( cmd == "WALKSPEED" )
{
string speed ;
stream >> speed ;
if ( speed == "FAST" )
{
contex->SetWalkingSpeed( CGameLogic::FASTMODE ) ;
}
else if ( speed == "SLOW" )
{
contex->SetWalkingSpeed( CGameLogic::SLOWMODE ) ;
}
}
else if ( cmd == "STARTFIGHT" )
{
string name ;
string npc_name ;
stream >> name >> npc_name ;
contex->StartFight(name, npc_name) ;
}
else if ( cmd == "ENDFIGHT" )
{
string name ;
stream >> name ;
contex->EndFight( name ) ;
}
else if ( cmd == "NPCFIGHT" )
{
string npc_name ;
string user_name ;
stream >> npc_name >> user_name ;
contex->m_ActorManager->GetActor( npc_name ).NpcFight( user_name ) ;
}
else if ( cmd == "FIGHTMENU" )
{
contex->BringUpMenu( CGameLogic::MENU3 ) ;
}
else if ( cmd == "IF" )
{
string condition_src ;
string op ;
string condition_dest ;
string temp ;
stringstream then_stmt ;
stringstream else_stmt ;
int nest_count = 0 ;
stream >> condition_src >> op >> condition_dest >> temp;
if ( temp != "THEN" )
{
temp.insert(0, "script error: 'THEN' expected, but found: ") ;
throw exception( temp.c_str() ) ;
}
else
{
/* 读取then部分,直到读到else为止 */
stream >> temp ;
while ( temp != "ELSE" || nest_count != 0 )
{
if ( temp == "IF" )
{
nest_count++ ;
}
if ( temp == "ENDIF" )
{
nest_count-- ;
}
then_stmt << temp << ' ';
stream >> temp ;
}
/* 读else部分,直到读到ENDIF为止 */
stream >> temp ;
while ( temp != "ENDIF" || nest_count != 0 )
{
if ( temp == "IF" )
{
nest_count++ ;
}
if ( temp == "ENDIF" )
{
nest_count-- ;
}
else_stmt << temp << ' ' ;
stream >> temp ;
}
}
/* 根据条件选择执行 */
string name ;
string property ;
size_t pos = condition_src.find('.') ;
bool cond ;
/* 解析人物属性操作,如 牛二.生命力 */
name = condition_src.substr( 0, pos ) ;
property = condition_src.substr( pos+1, condition_src.length() ) ;
CPlayer& Actor = this->contex->m_ActorManager->GetActor( name ) ;
if ( op == ">" )
{
SPropertyItem* item = Actor.Property( property ) ;
cond = ( item->val > atoi( condition_dest.c_str() ) ) ;
}
else if ( op == "<" )
{
SPropertyItem* item = Actor.Property( property ) ;
cond = ( item->val < atoi( condition_dest.c_str() ) ) ;
}
else if ( op == ">=" )
{
SPropertyItem* item = Actor.Property( property ) ;
cond = ( item->val >= atoi( condition_dest.c_str() ) ) ;
}
else if ( op == "<=" )
{
SPropertyItem* item = Actor.Property( property ) ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -