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

📄 human.cpp

📁 一个平面RPG的游戏demo,使用directx
💻 CPP
字号:
#include "RpgMain.h"

int LongTalk=0;
int Busy1=1;

int down=0;
int left=1;
int up=2;
int right=3;

int WhereX[4]={0,-1,0,1};
int WhereY[4]={1,0,-1,0};

unsigned long NoKey=DDBLTFAST_NOCOLORKEY|DDBLTFAST_WAIT;
unsigned long SrcKey=DDBLTFAST_SRCCOLORKEY|DDBLTFAST_WAIT;

HUMAN::HUMAN()
{
	HalfMoving=0;
	x=9;
	y=10;
	map=Inn;
	face=down;
	leg=1;
	status=Free;
	fstatus=Normal;
	progress=0;
	TalkOver=0;
	health=100;
	ti=100;
	nei=100;
	ThingAmount=0;
	weapon=Hand;
	cooldown=0;
}

HUMAN::~HUMAN()
{
	
}

void HUMAN::Show()
{
	if ((status!=Away)&&(status!=Dead)&&(map==Human[Boy]->map))
	{
		int dx=WhereX[face]*HalfMoving*8;
		int dy=WhereY[face]*HalfMoving*8;
		MakeRect(leg*32+id*64,face*32); 
		lpDDSBuffer->BltFast(x*32+dx,y*32+dy,lpDDSSprite,&r,SrcKey);
		MakeRect(67,34,99,39);
		lpDDSBuffer->BltFast(x*32+dx,y*32+dy,lpDDSThing,&r,SrcKey);
		MakeRect(x*32+dx+1+health*30/100,y*32+dy+1,x*32+dx+31,y*32+dy+4);
		DDBLTFX ddBltFx;
		ddBltFx.dwSize=sizeof(DDBLTFX);
		ddBltFx.dwFillColor=0;
		lpDDSBuffer->Blt(&r,NULL,&r,DDBLT_WAIT|DDBLT_COLORFILL,&ddBltFx);
	}
}

int HUMAN::CanMove(int where)
{
	int xx=x+WhereX[where];
	int yy=y+WhereY[where];
	for (int i=0;i<MaxHuman;i++)
	{
		if ((Human[i]->x+WhereX[Human[i]->face]==xx)&&
			(Human[i]->y+WhereY[Human[i]->face]==yy)&&
			(Human[i]->HalfMoving!=0)&&(Human[i]->status!=Dead)
			&&(Human[i]->status!=Away))
			return 0;
	}
	if ((GetFaceID(where)==-1)&&
		(xx>=0)&&(yy>=0)&&(xx<Map[map]->width)&&(yy<Map[map]->height)
		&&(Map[map]->MapData[xx][yy]!=48))
		return 1;
	return 0;
}

int HUMAN::GetFaceID(int where)
{
	for (int i=0;i<MaxHuman;i++)
	{
		if ((Human[i]->x==x+WhereX[where])&&
			(Human[i]->y==y+WhereY[where])&&
			(Human[i]->status!=Away)&&(Human[i]->status!=Dead)&&
			(Human[i]->map==Human[Boy]->map))
			return i;
	}
	return -1;
}

int HUMAN::Move(int where)
{
	if ((status==Free)&&(ti>=2))
	{
		if (CanMove(where)==1)
		{
			status=Moving;
			face=where;
			HalfMoving=1;
			SetTi(ti-2);
			return 1;
		}
		else
		{
			face=where;
			return 0;
		}
	}
	else
		return -1;
}

void HUMAN::Speak()
{
	HUMAN *obj;
	if (LongTalk==1) 
	{
		obj=Human[Teacher];
		if (obj->TalkOver==1)
			obj->TalkOver=0;
	}
	else
		if (GetFaceID(face)!=-1)
		{
			obj=Human[GetFaceID(face)];
			if (obj->status==Free) 
				Busy1=0;
		}
	if (((GetFaceID(face)!=-1)&&(Busy1==0))||(LongTalk==1))
	{
		if (obj->TalkOver!=1)
		{
			status=Busy;
			obj->status=Busy;
			if (LongTalk==0)
				obj->face=(face+2)%4;
			switch (obj->id)
			{
				case 1:
				switch (obj->progress)
				{
				case 0:
					Say("你好,AAA!");
					break;
				case 1:
					obj->Say("你好,Kane!");
					obj->TalkOver=1;
					GetThing(HealDrug);
					break;
				}
				break;
				case 2:
				switch (obj->progress)
				{
				case 0:
					Say("你好,BBB!");
					break;
				case 1:
					obj->Say("你好,Kane!");
					obj->TalkOver=1;
					GetThing(TiDrug);
					break;
				}	
				break;
			case 3:
				switch (obj->progress)
				{
				case 0:
					Say("你好,CCC!");
					break;
				case 1:
					obj->Say("你好,Kane!");
					break;
				case 2:	
					Say("你觉得我们的数学老师怎么样?");
					break;
				case 3:
					obj->Say("我不知道,我从来都不听他的课!");
					break;
				case 4:
					Say("其实听不听都差不多,老师只是念课本。");
					break;
				case 5:
					obj->Say("是啊。");
					obj->TalkOver=1;
					GetThing(NeiDrug);
					break;
				}
				break;
			case 4:
				switch(obj->progress)
				{
				case 1:
					obj->SayTo(0,"今天我们讲导数,请大家打开课本第104页!");
					break;
				case 2:
					obj->SayTo(0,"导数就是...(省去400字),下面请大家做练习!");
					break;
				case 3:
					SayTo(4,"(都不知他讲什么!)");
					break;
				case 4:
					obj->SayTo(0,"请大家自己看幻灯片,下堂课讲评!");
					break;
				case 5:
					SayTo(4,"题目真难啊......");
					break;
				case 6:
					CleanDialog();	
					DrawBox(288,235,100,30,2);
					Print("n分钟后......",300,240);
					obj->progress++;
					break;
				case 7:
					MakeRect(288,235,388,265);
					CleanRect(lpDDSOver);
					obj->SayTo(0,"下课!");
					break;
				case 8:
					SayTo(4,"啊,下课了,太好了!!!");
					break;
				case 9:
					CleanOver();
					LongTalk=0;
					obj->TalkOver=1;
					SetAll(Free);
					obj->status=Away;
					break;
				case 10:
					obj->SayTo(Boy,"大家的题目做得怎样?Kane,你怎么做这道题?");
					break;
				case 11:
					SayTo(4,"(啊,怎么会是我,真倒霉!)");
					break;
				case 12:
					SayTo(4,"嗯,这道题嘛,啊,这道题吧...");
					break;
				case 13:
					obj->SayTo(Boy,"你不会做又不听课,真是...");
					break;
				case 14:
					SayTo(4,"老师,我想到了。首先...然后...就可以了。");
					break;
				case 15:
					obj->SayTo(Boy,"嗯,好。大家听明白了吧,现在讲下一题。");
					break;
				case 16:
					CleanDialog();
					DrawBox(278,235,135,30,2);
					Print("又过了n分钟......",290,240);
					obj->progress++;
					break;
				case 17:
					MakeRect(278,235,413,265);
					CleanRect(lpDDSOver);
					obj->SayTo(Boy,"下课啦,大家出去活动一下吧。");
					break;
				case 18:
					SayTo(4,"(好险...)");
					break;
				case 19:
					CleanDialog();
					DrawBox(238,235,220,30,2);
					Print("演示情节结束,按ESC退出...",250,241);
					GameOver=1;
				}
				break;
			}
		}
		else
		{
			CleanDialog();
			Busy1=1;
			status=Free;
			if (LongTalk==0)
				obj->status=Free;
		}
	}
}

void HUMAN::Say(char words[255])
{	
	if (GetFaceID(face)!=-1)
		SayTo(GetFaceID(face),words);
}

void HUMAN::SayTo(int objid,char words[255])
{
	temp=0;
	HUMAN *obj=Human[objid];
	CleanDialog();
	HDC dc;
	lpDDSOver->GetDC(&dc);
	SetBkMode(dc, TRANSPARENT);
	SetTextColor(dc, RGB(255,255,255));
	TextOut(dc, 180, 400 , words, strlen(words));
	lpDDSOver->ReleaseDC(dc);
	MakeRect(0,id*160,160,id*160+160);
	lpDDSOver->BltFast(0,320,lpDDSHuman,&r,SrcKey);
	if (objid!=0)
		obj->progress++;
	else
		progress++;
}

void HUMAN::SetXY(int newx,int newy)
{
	x=newx;
	y=newy;
}

//---------------Thing Functions----------------------------------
void HUMAN::DropThing(int ItemId)
{
	for (int i=ItemId;i<ThingAmount;i++)
	{
		Thing[i]=Thing[i+1];
	}
	ThingAmount--;
	if (id==Boy)
	{
		for (i=0;i<=ThingAmount;i++)
		{
			MakeRect(194+38*i,442,220+38*i,471);
			CleanRect(lpDDSOver);
		}
		for (i=0;i<ThingAmount;i++)
		{
			MakeRect(Thing[i]*32,0);
			lpDDSOver->BltFast(192+38*i,442,lpDDSThing,&r,SrcKey);
		}
		/*for (i=0;i<=ThingAmount;i++)
		{
			MakeRect(5*i,32,5*i+5,40);
			lpDDSOver->BltFast(190+38*i,440,lpDDSThing,&r,SrcKey);
		}*/
	}
}

void HUMAN::GetThing(int id)
{
	Thing[ThingAmount]=id;
	MakeRect(Thing[ThingAmount]*32,0);
	lpDDSOver->BltFast(192+38*ThingAmount,442,lpDDSThing,&r,SrcKey);
	/*MakeRect(5*ThingAmount,32,5*ThingAmount+5,40);
	lpDDSOver->BltFast(190+38*ThingAmount,440,lpDDSThing,&r,SrcKey);*/
	ThingAmount++;
}

void HUMAN::GiveThing(int who,int ItemId)
{
	DropThing(ItemId);
	Human[who]->GetThing(Thing[ItemId]);
}

void HUMAN::UseThing(int ItemId)
{
	if (ItemId<ThingAmount)
	{
		switch (Thing[ItemId])
		{
		case HealDrug:
			if (health<=60)
				SetHealth(health+40);
			else
				SetHealth(100);
			DropThing(ItemId);
			break;
		case TiDrug:
			if (ti<=20)
				SetTi(ti+80);
			else
				SetTi(100);
			DropThing(ItemId);
			break;
		case NeiDrug:
			if (nei<=60)
				SetNei(nei+40);
			else
				SetNei(100);
			DropThing(ItemId);
			break;
		case Book:
			DropThing(ItemId);
			break;
		}
	}
}

//---------------Status Functions----------------------------------

void HUMAN::SetNei(int newnei)
{
	if (newnei<0)
		newnei=0;
	nei=newnei;
}

void HUMAN::SetTi(int newti)
{
	if (newti<0)
		newti=0;
	ti=newti;
}

void HUMAN::SetHealth(int newhealth)
{
	if (newhealth<0)
		newhealth=0;
	health=newhealth;
	if (newhealth==0)
	{
		status=Dead;
		if (id==Boy)
		{
			DrawBox(238,235,220,30,2);
			Print("GAME OVER...  按ESC退出...",250,241);
			GameOver=1;
		}
	}
}

//---------------FIght Functions-----------------------------------

void HUMAN::Fight()
{
	if (weapon==Hand)
	{
		int k=GetFaceID(face);
		if ((k!=-1)&&(ti>=6)&&(status==Free))
		{
			Human[k]->fstatus=Angry;
			Human[k]->SetHealth(Human[k]->health-4);
			SetTi(ti-6);
			leg=(leg+1)%2;
		}
	}
	if ((weapon==Missile)&&(status==Free)&&(nei>=10))
	{
		SetNei(nei-10);
		status=Casting;
		Bullet[LastBullet]->x=x;
		Bullet[LastBullet]->y=y;
		Bullet[LastBullet]->user=id;
		Bullet[LastBullet]->face=face;
		Bullet[LastBullet]->status=0;
		Bullet[LastBullet]->HalfMove=0;
		LastBullet++;
	}
}

⌨️ 快捷键说明

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