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

📄 script.cpp

📁 Visual C++角色扮演游戏程序设计(附带CD源代码)作者: (日)坂本千寻 译者:博硕文化
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	bool	b1 = GetValue(lexer, &value1);
	const char *op = lexer.GetString();
	bool	b2 = GetValue(lexer, &value2);
	const char *p = lexer.GetString();
	const char *label = lexer.GetString();

	if (!b1 || !b2 || op == 0 || label == 0
	 || lexer.GetString() != 0 && stricmp(p, "goto") != 0) {
		Error("文法错误");
		return BreakGame;
	}

	if (strcmp(op, "==") == 0) {
		if (value1 == value2)
			return GotoCommand(label);
	}
	else if (strcmp(op, "!=") == 0) {
		if (value1 != value2)
			return GotoCommand(label);
	}
	else if (strcmp(op, "<=") == 0) {
		if (value1 <= value2)
			return GotoCommand(label);
	}
	else if (strcmp(op, ">=") == 0) {
		if (value1 >= value2)
			return GotoCommand(label);
	}
	else if (strcmp(op, "<") == 0) {
		if (value1 < value2)
			return GotoCommand(label);
	}
	else if (strcmp(op, ">") == 0) {
		if (value1 > value2)
			return GotoCommand(label);
	}
	else {
		Error("文法错误");
		return BreakGame;
	}
	return Continue;
}

//
// menu 指令
//
int CScriptAction::MenuCmd(Lexer &lexer)
{
	const char *p = lexer.GetString();

	if (p == 0 || lexer.GetString() != 0) {
		Error("文法错误(in menu command)");
		return BreakGame;
	}

	Parent->ClearMenuItemCount();
	MenuAnser = p;

	const char *str;
	for (int no=0; (str = reader->GetString()) != NULL; no++) {
		if (stricmp(str, "end") == 0)
			break;
		Parent->SetMenuItem(str, no + 1);
	}
	MenuSelect = -1;
	Parent->OpenMenu();
	return WaitMenuDone;
}

//
// load 指令
//
int CScriptAction::LoadCmd(Lexer &lexer)
{
	const char *p1 = lexer.GetString();
	const char *p2 = lexer.GetString();

	if (p1 == 0 || p2 == 0 || lexer.GetString() != 0) {
		Error("文法错误(in load command)");
		return BreakGame;
	}
	return LoadGraphic(p2, GetPosition(p1));
}

//
// update 指令
//
int CScriptAction::UpdateCmd(Lexer &lexer)
{
	const char *p = lexer.GetString();

	if (p == 0 || lexer.GetString() != 0) {
		Error("文法错误(in update command)");
		return BreakGame;
	}
	return UpdateImage(GetUpdateType(p));
}

//
// clear 指令
//
int CScriptAction::ClearCmd(Lexer &lexer)
{
	const char *p = lexer.GetString();

	if (p == 0 || lexer.GetString() != 0) {
		Error("文法错误(in clear command)");
		return BreakGame;
	}

	if (stricmp(p, "text") == 0) {		// clear text 个别处理
		Parent->ClearMessage();
		return WaitNextIdle;
	}
	return Clear(GetPosition(p));
}

//
// music 指令
//
int CScriptAction::MusicCmd(Lexer &lexer)
{
	int value;
	bool isval = lexer.GetValue(&value);

	if (!isval || value <= 0 || lexer.GetString() != 0) {
		Error("文法错误(in music command)");
		return BreakGame;
	}
	Parent->GetParam().last_bgm = value;
	Parent->StartMusic(value);
	return Continue;
}

//
// sound 指令
//
int CScriptAction::SoundCmd(Lexer &lexer)
{
	const char *path = lexer.GetString();

	if (path == 0 || lexer.GetString() != 0) {
		Error("文法错误(in sound command)");
		return BreakGame;
	}
	if (Parent->StartWave(path))
		return WaitWaveDone;
	return Continue;
}

//
// exec 指令
//
int CScriptAction::ExecCmd(Lexer &lexer)
{
	const char *path = lexer.GetString();

	if (path == 0 || lexer.GetString() != 0) {
		Error("文法错误(in exec command)");
		return BreakGame;
	}
	if (!Load(path))
		return BreakGame;
	PlayMode = MODE_SCENARIO;
	return Continue;
}

//
// wait 指令
//
int CScriptAction::WaitCmd(Lexer &lexer)
{
	int value;
	bool isval = lexer.GetValue(&value);

	if (!isval || value <= 0 || lexer.GetString() != 0) {
		Error("文法错误(in wait command)");
		return BreakGame;
	}
	Parent->SetTimer(CMainWin::TimerSleep, value * 1000);
	return WaitTimeOut;
}

//
// Text的尾端确认
//
bool CScriptAction::ChkTermination(const char *str)
{
	if (*str++ != '.')
		return false;
	while (*str) {
		if (!isspace(*str))
			return false;
	}
	return true;
}

//
// text 指令
//
int CScriptAction::TextCmd(Lexer &lexer)
{
	if (lexer.GetString() != 0) {
		Error("文法错误(in text command)");
		return BreakGame;
	}

	string	work;

	for (int i=0; ; i++) {
		const char *str;
		if ((str = reader->GetString()) == NULL) {
			Error("文法错误(text syntax)");
			return BreakGame;
		}
		if (ChkTermination(str))
			break;
		work += str;
		work += '\n';

		if (i >= MessageLine) {
			Error("内容行数过多");
			return BreakGame;
		}
	}
	Parent->WriteMessage(work.c_str());
	return WaitKeyPressed;
}

//
// mode 指令
//
int CScriptAction::ModeCmd(Lexer &lexer)
{
	const char *p = lexer.GetString();
	if (p == 0 || lexer.GetString() != 0) {
		Error("文法错误");
		return BreakGame;
	}
	if (stricmp(p, "system") == 0) {
		PlayMode = MODE_SYSTEM;
	}
	else if (stricmp(p, "scenario") == 0) {
		PlayMode = MODE_SCENARIO;
	}
	else {
		Error("文法错误");
		return BreakGame;
	}
	return Continue;
}

//
// system 指令
//
int CScriptAction::SystemCmd(Lexer &lexer)
{
	const char *p = lexer.GetString();
	if (p == 0 || lexer.GetString() != 0) {
		Error("文法错误");
		return BreakGame;
	}
	if (stricmp(p, "load") == 0) {
		Parent->SetGameLoadAction();
		return WaitNextIdle;
	}
	if (stricmp(p, "exit") == 0) {
		Parent->SendMessage(WM_CLOSE);
		return WaitNextIdle;
	}
	if (stricmp(p, "clear") == 0) {
		Parent->ClearValue();
		return Continue;
	}
	Error("文法错误");
	return BreakGame;
}

//
// battle 指令
//
int CScriptAction::BattleCmd(Lexer &lexer)
{
	const char *p = lexer.GetString();
	if (p == 0 || lexer.GetString() != 0) {
		Error("文法错误");
		return BreakGame;
	}
	Parent->Battle(p);

	return WaitNextIdle;
}

//
// stopm 指令
//
int CScriptAction::StopmCmd(Lexer &lexer)
{
	Parent->GetParam().last_bgm = 0;
	Parent->StopMusic();
	return Continue;
}

//
// fadein 指令
//
int CScriptAction::FadeInCmd(Lexer &lexer)
{
	Parent->GetParam().SetShowFlag();
	Parent->FadeIn();
	return WaitWipeDone;
}

//
// fadeout 指令
//
int CScriptAction::FadeOutCmd(Lexer &lexer)
{
	Parent->GetParam().ResetShowFlag();
	Parent->FadeOut();
	return WaitWipeDone;
}

//
// wipein 指令
//
int CScriptAction::WipeInCmd(Lexer &lexer)
{
	Parent->GetParam().SetShowFlag();
	Parent->WipeIn();
	return WaitWipeDone;
}

//
// wipeout 指令
//
int CScriptAction::WipeOutCmd(Lexer &lexer)
{
	Parent->GetParam().ResetShowFlag();
	Parent->WipeOut();
	return WaitWipeDone;
}

//
// cutin 指令
//
int CScriptAction::CutInCmd(Lexer &lexer)
{
	Parent->GetParam().SetShowFlag();
	Parent->CutIn();
	return WaitNextIdle;
}

//
// cutout 指令
//
int CScriptAction::CutOutCmd(Lexer &lexer)
{
	Parent->GetParam().ResetShowFlag();
	Parent->CutOut();
	return WaitNextIdle;
}

//
// whitein 指令
//
int CScriptAction::WhiteInCmd(Lexer &lexer)
{
	Parent->GetParam().SetShowFlag();
	Parent->WhiteIn();
	return WaitWipeDone;
}

//
// whiteout 指令
//
int CScriptAction::WhiteOutCmd(Lexer &lexer)
{
	Parent->GetParam().ResetShowFlag(TRUE);
	Parent->WhiteOut();
	return WaitWipeDone;
}

//
// flash 指令
//
int CScriptAction::FlashCmd(Lexer &lexer)
{
	Parent->Flash();
	return WaitWipeDone;
}

//
// shake 指令
//
int CScriptAction::ShakeCmd(Lexer &lexer)
{
	Parent->Shake();
	return WaitWipeDone;
}

//
// end 指令
//
int CScriptAction::EndCmd(Lexer &lexer)
{
	return BreakGame;
}

//
// 指令判定
//
CScriptAction::cmd_t CScriptAction::ParseCommand(Lexer &lexer)
{
	const char *cmd = lexer.GetString(0);

	cmdmap::iterator	p = cmd_table.find(cmd);
	if (p != cmd_table.end())
		return p->second;

	// set, calc的省略型吗?
	if (lexer.NumToken() >= 3) {
		const char *p = lexer.GetString(1);
		lexer.GetType(0);			// 回到最前面
		if (strcmp(p, "+") == 0 || strcmp(p, "-") == 0 || strcmp(p, "=") == 0) {
			return &CScriptAction::SetCmd;
		}
	}
	Error("文法错误(command syntax)");
	return NULL;
}

//
// 解析一行
//
int CScriptAction::ParserString(const char *str)
{
	Lexer	lexer(str);

	if (lexer.NumToken() == 0)
		return Continue;

	int	type = lexer.GetType();

	if (type == Lexer::IsLabel) {
		return SetLabel(lexer);
	}
	cmd_t cmd = ParseCommand(lexer);
	if (cmd == 0)
		return BreakGame;
	return (this->*cmd)(lexer);
}

//
// 执行Script的一个步骤
//
int CScriptAction::Step()
{
	ASSERT(reader);

	// 将现在的位置设定成SaveDate
	Parent->GetParam().script_pos = reader->GetPosition();

	const char *str = reader->GetString();
	if (str == 0)
		return BreakGame;
	return ParserString(str);
}

//
// CG的载入
//
int CScriptAction::LoadGraphic(const char *file, int pos)
{
	BOOL	result = FALSE;
	switch (pos) {
	  case POSITION_BACK:			// 背景
		Parent->GetParam().ClearOverlapCG();
		Parent->ClearOverlap();
		// no break

	  case POSITION_BACKONLY:		// 背景的部分
		Parent->GetParam().SetBackCG(file);
		result = Parent->LoadImageBack(file);
		break;

	  case POSITION_OVERLAP:		// 重叠
		Parent->GetParam().SetOverlapCG(file);
		result = Parent->LoadImageOverlap(file);
		break;
	}

	if (!result) {
		Parent->MessageBox(Format("档案无法读出 [%s] 档案", file));
		if (PlayMode == MODE_SYSTEM)
			Parent->SendMessage(WM_CLOSE);
		return BreakGame;
	}
	return Continue;
}

//
// 删除CG
//
int CScriptAction::Clear(int pos)
{
	switch (pos) {
	  case POSITION_BACK:			// 背景
		Parent->GetParam().ClearOverlapCG();
		Parent->ClearOverlap();
		// no break

	  case POSITION_BACKONLY:		// 背景的部分
		Parent->GetParam().ClearBackCG();
		Parent->ClearBack();
		break;

	  case POSITION_OVERLAP:		// 重叠
		Parent->GetParam().ClearOverlapCG();
		Parent->ClearOverlap();
		break;
	}
	return Continue;
}

//
// 画面更新
//
int CScriptAction::UpdateImage(int flag)
{
	Parent->GetParam().SetShowFlag();

	CRect	rect = Parent->GetInvalidRect();
	if (rect.IsRectEmpty())
		return Continue;

	switch (flag) {
	  case UPDATE_NOW:
		Parent->CutIn(rect);
		return WaitNextIdle;

	  case UPDATE_OVERLAP:
		Parent->MixFade(rect);
		break;

	  case UPDATE_WIPE:
		Parent->WipeIn(rect);
		break;
	}
	return WaitWipeDone;
}

⌨️ 快捷键说明

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