📄 script.cpp.svn-base
字号:
/******************************************************************
*
* @author Alpha
*
*****************************************************************/
#include "Script.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "GameData.h"
#include "../Game/camera.h"
#include "../game/player.h"
#include "../game/game.h"
#include "Structs.h"
#include "../Renderer/Renderer.h"
Script::Script(void)
{
}
Script::~Script(void)
{
}
//关卡初始化
bool Script::LoadMission(char *mission_path)
{
FILE *pMission = NULL;
char line[MAX_LINE_SIZE]; //当前行
const char *token = NULL; //用于字符串处理
char mapSizeX[10] ;
char mapSizeY[10] ; //地图大小
pMission = fopen(mission_path,"r");
if(pMission == NULL)
{
MessageBox(NULL,NULL,"Open ini file Failed",NULL);
return false;
}
while (!feof(pMission)) //文件结束,退出处理
{
fgets(line,MAX_LINE_SIZE,pMission);
if(strstr(line,"#End")) //读到"#End",退出处理
{
break;
}
//忽略注释行
else if(strstr(line,"//"))
{continue;}
//地图大小
else if(strstr(line,"#MapSize"))
{
fgets(line,MAX_LINE_SIZE,pMission);
//以","为关键字将字符串分割得到 mapSizeX,mapSizeY
token=strtok(line,",");
strcpy(mapSizeX,token);
token=strtok(NULL,",");
strcpy(mapSizeY,token);
continue;
}
//地图路径
else if(strstr(line,"#Map"))
{
fgets(line,MAX_LINE_SIZE,pMission);
int x = atoi(mapSizeX);
int y = atoi(mapSizeY);
token = rtrim(line); //将line中的有效路径截取进指针token.
map.LoadRawFile(token,x,y);//Load地图文件
continue;
}
//Load 背景音乐
else if(strstr(line,"#LoadBGM"))
{
fgets(line,MAX_LINE_SIZE,pMission);
token = rtrim(line); //将line中的有效路径截取进指针token.
Script::LoadBGM((char*)token);
continue;
}
//开始背景音乐
else if(strstr(line,"#BGM"))
{
fgets(line,MAX_LINE_SIZE,pMission);
token = rtrim(line);
if (strstr(token,"Mission1"))
M1_BGM->play();
else if(strstr(token,"Mission2"))
M2_BGM->play();
continue;
}
//关闭背景音乐
else if(strstr(line,"#StopBGM"))
{
fgets(line,MAX_LINE_SIZE,pMission);
token = rtrim(line);
if(strstr(token,"Mission1"))
M1_BGM->stop();
else if(strstr(token,"Mission2"))
M2_BGM->stop();
continue;
}
//初始化怪物的类型
else if(strstr(line,"#LoadEnemyTypes"))
{
fgets(line,MAX_LINE_SIZE,pMission);
token = rtrim(line); //将line中的有效路径截取进指针token.
Script::LoadEnemyTypes((char*)token);
continue;
}
//从子脚本中读取对每一个怪物的类型和位置
else if(strstr(line,"#LoadEnemyPos"))
{
fgets(line,MAX_LINE_SIZE,pMission);
token = rtrim(line); //将line中的有效路径截取进指针token.
Script::LoadEnemyPos((char*)token);
continue;
}
//初始化树的类型
else if(strstr(line,"#LoadTreeTypes"))
{
fgets(line,MAX_LINE_SIZE,pMission);
token = rtrim(line); //将line中的有效路径截取进指针token.
Script::LoadTreeTypes((char*)token);
continue;
}
//从子脚本中初读取每一棵树的具体种类和位置
else if(strstr(line,"#LoadTreePos"))
{
fgets(line,MAX_LINE_SIZE,pMission);
token = rtrim(line); //将line中的有效路径截取进指针token.
Script::LoadTreePos((char*)token);
continue;
}
//显示图片
else if(strstr(line,"#DisplayBMP"))
{
//显示BMP
displayBMP = true;
displayTrees = false;
displayMap = false;
Renderer::render();
continue;
}
//取消图片
else if(strstr(line,"#CancelBMP"))
{
displayBMP = false;
displayTrees = true;
displayMap = true;
continue;
}
//Sleep
else if(strstr(line,"#Sleep"))
{
fgets(line,MAX_LINE_SIZE,pMission);
token = rtrim(line);
Sleep(atoi(token));
continue;
}
else {continue;}
}
//Close file
fclose(pMission);
return true;
}
//Load a script into memory,用于动画
bool Script::LoadScript(char* script_path)
{
// Create a file pointer for the script
FILE * pScriptFile;
nScriptSize=0;
// Open the source file in binary mode
if ( ! ( pScriptFile = fopen ( script_path, "rb" ) ) )
return false;
// Count the number of source lines
while ( ! feof ( pScriptFile ) )
if ( fgetc ( pScriptFile ) == '\n' )
++ nScriptSize;
++ nScriptSize;
// Open the script and print an error if it's not found
if ( ! ( pScriptFile = fopen ( script_path, "r" ) ) )
return false;
// Allocate a script of the proper size
ppstrScript = ( char ** ) malloc ( nScriptSize * sizeof ( char * ) );
// Load each line of code
for ( int iCurrLineIndex = 0; iCurrLineIndex < nScriptSize; ++ iCurrLineIndex )
{
// Allocate space for the line and a null terminator
ppstrScript [ iCurrLineIndex ] = ( char * ) malloc ( MAX_LINE_SIZE + 1 );
// Load the line
fgets ( ppstrScript [ iCurrLineIndex ], MAX_LINE_SIZE, pScriptFile );
}
// Close the script
fclose ( pScriptFile );
return true;
}
//Unload the current script
void Script::UnloadScript()
{
ppstrScript = NULL;
nScriptSize = 0;
}
//Excute the current script.用于动画的实现
void Script::ExecuteScript(PVOID param)
{
const char *currLine = NULL; //当前行
for(int i=0;i<nScriptSize;i++)
{
currLine = rtrim(ppstrScript[i]);
if(strstr(currLine,"#End")) //读到"#End",退出处理
{
GameStart();
break;
}
else if(strstr(currLine,"//")) //忽略注释行
{
continue;
}
else if(strstr(currLine,"#DisplaySentence")) //在屏幕上显示对话
{
i++;
currLine = rtrim(ppstrScript[i]);
sprintf(putout,currLine); //设置全局变量
Sleep(4000); //控制字显示的时间
putout[0]='\0';
continue;
}
//开始背景音乐
else if(strstr(currLine,"#BGM"))
{
i++;
currLine = rtrim(ppstrScript[i]);
if (strstr(currLine,"Mission1"))
M1_BGM->play();
else if(strstr(currLine,"Mission2"))
M2_BGM->play();
continue;
}
//关闭背景音乐
else if(strstr(currLine,"#StopBGM"))
{
i++;
currLine = rtrim(ppstrScript[i]);
if (strstr(currLine,"Mission1"))
M1_BGM->stop();
else if(strstr(currLine,"Mission2"))
M2_BGM->stop();
continue;
}
else if(strstr(currLine,"#MovePlayer")) //主角移动
{
//移动
i++;
currLine = rtrim(ppstrScript[i]);
continue;
}
//显示图片
else if(strstr(currLine,"#DisplayBMP"))
{
displayBMP = true;
displayTrees = false;
displayMap = false;
continue;
}
//取消图片
else if(strstr(currLine,"#CancelBMP"))
{
displayBMP = false;
displayTrees = true;
displayMap = true;
continue;
}
//线程 Sleep
else if(strstr(currLine,"#Sleep"))
{
i++;
currLine = rtrim(ppstrScript[i]);
Sleep(atoi(currLine));
continue;
}
//移动 Camera
else if(strstr(currLine,"#MoveCameraFromTo"))
{
Vertex a = Vertex(255,300,13);
Vertex b = Vertex(533,300,533);
//MovePlayerFromTo(a, b);
//SetCameraWithPlayer();
//SetCameraFree();
//SetCameraLookAt(Vertex(a.x, a.y+1, a.z));
MoveCameraFromTo(a,b);//移动 Carema
continue;
}
else {continue;} //忽略空行
}
//全部执行完毕,UnloadScript
UnloadScript();
}
//开启一个新的线程,用来执行动画脚本
void Script::ExecuteScriptInNewThd()
{
_beginthread(&Script::ExecuteScript,0,NULL);
}
//背景音乐初始化
bool Script::LoadBGM(char *bgm_path)
{
FILE *pBGM = NULL;
char line[MAX_LINE_SIZE]; //当前行
const char *token = NULL; //用于字符串处理
pBGM = fopen(bgm_path,"r");
if(pBGM == NULL)
{
MessageBox(NULL,NULL,"Open BGM file Failed",NULL);
return false;
}
while (!feof(pBGM)) //文件结束,退出处理
{
fgets(line,MAX_LINE_SIZE,pBGM);
if(strstr(line,"#End")) //读到"#End",退出处理
{
break;
}
//忽略注释行
else if(strstr(line,"//"))
{continue;}
else if(strstr(line,"#Mission1_Main"))
{
fgets(line,MAX_LINE_SIZE,pBGM);
token = rtrim(line);
audioFile = _T(token);
M1_BGM = new MusicPlayer(audioFile,MCI_DGV_PLAY_REPEAT);
continue;
}
else if(strstr(line,"#Mission2_Main"))
{
fgets(line,MAX_LINE_SIZE,pBGM);
token = rtrim(line);
audioFile = _T(token);
M2_BGM = new MusicPlayer(audioFile,MCI_DGV_PLAY_REPEAT);
continue;
}
//Sleep
else if(strstr(line,"#Sleep"))
{
fgets(line,MAX_LINE_SIZE,pBGM);
token = rtrim(line);
Sleep(atoi(token));
continue;
}
else {continue;}
}
fclose(pBGM);
return true;
}
//读取敌人类型设置
bool Script::LoadEnemyTypes(char* enemy_path)
{
FILE *pEnemy = NULL;
char line[MAX_LINE_SIZE]; //当前行
char *token = NULL; //用于字符串处理
pEnemy = fopen(enemy_path,"r");
if(pEnemy == NULL)
{
MessageBox(NULL,NULL,"Open ini file Failed",NULL);
return false;
}
while (!feof(pEnemy)) //文件结束,退出处理
{
fgets(line,MAX_LINE_SIZE,pEnemy);
if(strstr(line,"#End")) //读到"#End",退出处理
{
break;
}
//忽略注释行
else if(strstr(line,"//"))
{continue;}
else if(strstr(line,"@EnemyType"))
{
GameObjType got;
while(!strstr(line,"@/EnemyType"))
{
fgets(line,MAX_LINE_SIZE,pEnemy);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -