?? manager.cpp
字號:
#include "StdAfx.h"
#include "Manager.h"
#include <mmsystem.h>
#pragma comment(lib,"winmm.lib")
Manager::Manager()
{
target=NULL;
index=1;
gamestate=0;
}
Manager::~Manager()
{
if(target!=NULL)
{
delete target;
}
}
bool Manager::CheckState() //是否完成任務
{
bool finish=true;
for(int i=0;i<this->num;i++)
{
if(map[this->target[i].y][this->target[i].x]==2)
{
this->target[i].status=1;
}
else
{
this->target[i].status=0;
finish=false;
}
}
return finish;
}
bool Manager::Empty(int a,int b) //是否為背景
{
return map[a][b]==0?true:false;
}
void Manager::GameStart()
{
this->GetMap();
this->Init();
playSnd();
gamestate=1;
}
void Manager:: playSnd()
{
char path[200];
::GetModuleFileName(NULL,path,200);
(::_tcsrchr(path,'\\'))[1]='\0';
strcat(path,"\\sound\\1.wav");
::PlaySound(path,NULL,SND_LOOP|SND_ASYNC|SND_NODEFAULT);
}
bool Manager::MoveBox(int x,int y,int action) //是否可以搬運箱子,并且更新數據
{
int i=x,j=y;
if(action==1)
{
j--;
}
else if(action==2)
{
i--;
}
else if(action==3)
{
j++;
}
else
{
i++;
}
if(Empty(i,j))
{
map[i][j]=2;
map[x][y]=0;
return true;
}
else
{
return false;
}
}
bool Manager::DoMsg(int action) //處理鍵盤事件
{
if(action==37)
{
if(this->Empty(man.y,man.x-1))
{
man.x-=1;
return true;
}
else if(this->IsBox(man.y,man.x-1))
{
if(this->MoveBox(man.y,man.x-1,1))
{
man.x-=1;
return true;
}
}
}
else if(action==38)
{
if(this->Empty(man.y-1,man.x))
{
man.y-=1;
return true;
}
else if(this->IsBox(man.y-1,man.x))
{
if(this->MoveBox(man.y-1,man.x,2))
{
man.y-=1;
return true;
}
}
}
else if(action==39)
{
if(this->Empty(man.y,man.x+1))
{
man.x+=1;
return true;
}
else if(this->IsBox(man.y,man.x+1))
{
if(this->MoveBox(man.y,man.x+1,3))
{
man.x+=1;
return true;
}
}
}
else if(action==40)
{
if(this->Empty(man.y+1,man.x))
{
man.y+=1;
return true;
}
else if(this->IsBox(man.y+1,man.x))
{
if(this->MoveBox(man.y+1,man.x,4))
{
man.y+=1;
return true;
}
}
}
return false;
}
bool Manager::IsBox(int a,int b) //是否為箱子
{
return map[a][b]==2?true:false;
}
void Manager::Init() //根據從文件讀取的地圖初始化數據
{
int number=0;
for(int i=0;i<CY;i++)
{
for(int j=0;j<CX;j++)
{
if(map[i][j]==3||map[i][j]==5) //3是目標,5是完成的箱子
{
number++;
}
}
}
num=number;
target=new Target[num];
number=0;
for(i=0;i<CY;i++)
{
for(int j=0;j<CX;j++)
{
if(map[i][j]==3)
{
target[number].x=j;
target[number].y=i;
target[number].status=0;
number++;
map[i][j]=0;
}
else if(map[i][j]==5)
{
target[number].x=j;
target[number].y=i;
target[number].status=1;
number++;
map[i][j]=2;
}
else if(map[i][j]==4) //人物
{
man.x=j;
man.y=i;
map[i][j]=0;
}
}
}
}
void Manager::GetMap() //從文件讀取的地圖,并初始化其它數據
{
mm.GetMap(map,index);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -