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

📄 fsm_script.cpp.svn-base

📁 自己做的小游戏
💻 SVN-BASE
字号:
#include "fsm_script.h"
#include "fsmmacros.h"
#include "msg.h"
#include "game_object.h"
#include "custom_time.h"
#include "util.h"

#include "../gamedata/gamedata.h"
#include "../gamedata/structs.h"
#include "../gamedata/mdlmodel.h"

#include "math.h"
#include "assert.h"


//-------------------------------------------------------------------
//这个状态机跟摄像机的状态机基本上差不多,所以没什么可说的了,很简单。
//-------------------------------------------------------------------




float slope;		//斜率
Vertex tempPos;
float v_x1, v_z1;

void UpdatePos(GameObject* go);

bool ScriptProcessStateMachine( GameObject* go, unsigned int state, MsgObject* msg )
{

BeginStateMachine

	OnEnter
		SetState(STATE_Stand);

	OnMsg(MSG_ScriptSetDest)
		go->dest = msg->ex.pos;
		go->dest.y = map.getHeight(go->dest.x, go->dest.z);

	OnMsg(MSG_ScriptSetPosition)
		go->pos = msg->ex.pos;
		go->pos.y = map.getHeight(go->pos.x, go->pos.z);

	OnMsg(MSG_ScriptSetDirection)
		go->rotY = msg->ex.rotY;

	//-------------------------------------

	State(STATE_Stand)

		OnEnter
			go->model->SetSeq(IDLE);
			go->dest = go->pos;

		OnUpdate
			if (abs(go->dest.x-go->pos.x) >= 1.f || abs(go->dest.z-go->pos.z) >= 1.f) {
				SetState(STATE_Run);
			}
		
	//-------------------------------------

	State(STATE_Run)

		OnEnter
			go->model->SetSeq(WALK);
			float aaa = GetDistance(go->pos, go->dest);
			v_x1 = (go->dest.x-go->pos.x) * player.moveSpeed / aaa;
			v_z1 = (go->dest.z-go->pos.z) * player.moveSpeed / aaa;

		OnUpdate
			if (abs(go->dest.x-go->pos.x) < 1.f && abs(go->dest.z-go->pos.z < 1.f)) {
				SetState(STATE_Stand);
				return true;
			}
			UpdatePos(go);

EndStateMachine

}


void UpdatePos(GameObject* go)
{
	//slope = atan2(go->dest.z-go->pos.z, go->dest.x-go->pos.x);

	//go->pos.x += go->moveSpeed * cosf(slope) * incT;
	//go->pos.z += go->moveSpeed * sinf(slope) * incT;
	//go->pos.y = map.getHeight(go->pos.x, go->pos.z);

	go->pos.x += v_x1 * incT;
	go->pos.z += v_z1 * incT;
	go->pos.y = map.getHeight(go->pos.x, go->pos.z);
	
}

⌨️ 快捷键说明

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