📄 unit.cpp.svn-base
字号:
#include "unit.h"
#include "camera.h"
#include "game.h"
#include "game_object.h"
#include "msg.h"
#include "msgroute.h"
#include "../gamedata/gamedata.h"
#include "math.h"
list<GameObject>::iterator i;
float xx, zz;
void InitUnits()
{
unsigned int id; //从1开始,0被player占了
for (i = units.begin(), id = 1; i != units.end(); ++i, ++id) {
i->checked = false;
i->collide = false;
i->NeedChecked = false;
i->bMarkedForDeletion = false;
i->radius = sqrt(pow(i->lenX, 2) + pow(i->lenZ, 2)) * 0.5;
GOInitialize(&(*i), id);
}
}
//获取各个GO指针的对外接口
GameObject* GODBGetGO( unsigned int unique_id )
{
if (unique_id == player.unique_id)
return &player;
else if (unique_id == gameGO.unique_id)
return &gameGO;
else if (unique_id == cameraGO.unique_id)
return &cameraGO;
list<GameObject>::iterator i;
for (i = units.begin(); i != units.end(); ++i) {
if (i->unique_id == unique_id)
return (&(*i));
}
return (0);
}
//整体更新的接口
void UpdateUnits()
{
for (i = units.begin(); i != units.end(); i++)
GOUpdate(&(*i));
i = units.begin();
while (i != units.end()) {
if (i->bMarkedForDeletion)
i = units.erase(i);
else
++i;
}
}
//计算新位置的整体接口
void CalcUnitsNewPos()
{
for (i = units.begin(); i != units.end(); i++)
SendMsg(MSG_CalcNewPos, i->unique_id, i->unique_id);
}
//unsigned int Unit::ID = 0;
//
//Unit::Unit(void)
//{
// moveSpeed = 0.1f;
// height = 1.0f;
// oneStepLen = 1.0f;
//
// cmd[STAND] = cmd[RUN] = cmd[ATTACK] = false;
//
//
// preState = state = STAND;
//
// pos.x = pos.z = 100;
// pos.y = 100;
// rotY = 0;
//
// newPos = pos;
// newRotY = rotY;
//
// curT = 0;
// preT = 0;
// begT = 0;
// endT = ULONG_MAX;
//
// ani[STAND].durT = 1;
// ani[STAND].firstF = 0;
// ani[STAND].lastF = 10;
// ani[STAND].numF = 11;
// ani[STAND].type = STAND;
// ani[RUN].durT = 1;
// ani[RUN].firstF = 0;
// ani[RUN].lastF = 10;
// ani[RUN].numF = 11;
// ani[RUN].type = RUN;
// ani[ATTACK].durT = 1;
// ani[ATTACK].firstF = 0;
// ani[ATTACK].lastF = 10;
// ani[ATTACK].numF = 11;
// ani[ATTACK].type = ATTACK;
// int curF = ani[STAND].firstF;
//}
//
//Unit::~Unit(void)
//{
//}
//
//Vertex Unit::getPos()
//{
// return pos;
//}
//
//Vertex Unit::getNextPos()
//{
// float avgRotY = (rotY+newRotY) * 0.5; //上一帧和这一帧方向的平均值
// float len = moveSpeed * (curT-preT); //上一帧到这一帧,走了多长的距离
// newPos.x = pos.x + len*sinf(avgRotY); //根据距离和方向计算新的坐标
// newPos.z = pos.z + len*cosf(avgRotY);
// //newPos.y = ??????????????
//
// return newPos;
//}
//
//float Unit::getRotY()
//{
// return rotY;
//}
//
//
//int Unit::getState(int* type)
//{
// *type = preState;
// return curT;
//}
//
//
//void Unit::move(Vertex dest)
//{
// cmd[RUN] = true;
// newPos = dest;
//}
//
//
//void Unit::stand(Vertex dest)
//{
// cmd[STAND] = true;
// newPos = dest;
//}
//
//void Unit::rotate(float r)
//{
// newRotY = r;
//}
//
//void Unit::update(unsigned long curTime)
//{
// curT = curTime;
//
// updateState();
// updatePosition();
// updateTimer();
// updateFrame();
//
// cmd[STAND] = cmd[RUN] = cmd[ATTACK] = false;
// preT = curT;
// preState = state;
//}
//
//
//
//
//void Unit::updateState()
//{
// switch (preState)
// {
// case STAND:
// if (cmd[RUN]) state = RUN;
// if (cmd[ATTACK]) state = ATTACK;
// break;
// case RUN:
// if (curT >= endT && !cmd[RUN]) {state = STAND; }
// if (cmd[STAND]) state = STAND;
// if (cmd[ATTACK]) state = ATTACK;
// break;
// case ATTACK:
// if (curT >= endT) {
// state = STAND;
// if (cmd[RUN]) state = RUN;
// if (cmd[STAND]) state = STAND;
// if (cmd[ATTACK]) state = ATTACK;
// }
// break;
// }
//}
//
//
//void Unit::updatePosition()
//{
// switch (preState)
// {
// case STAND:
// if (state == RUN) pos = newPos;
// break;
// case RUN:
// if (state == STAND || state == ATTACK) pos = newPos;
// else if (cmd[RUN]) pos = newPos;
// else pos = newPos; // 如果没有任何命令,且preState为RUN则getNewPos()一定已经被调用过。
// break;
// case ATTACK:
// break;
// }
//}
//
//
//void Unit::updateTimer()
//{
// switch (preState)
// {
// case STAND:
// if (state == RUN ) {
// begT = curT;
// //================================
// //calculate the endT
// //================================
// float powOfIncX = pow(newPos.x - pos.x, 2);
// float powOfIncZ = pow(newPos.z - pos.z, 2);
// float powOfOneStepLen = pow(oneStepLen, 2);
// if (powOfIncX + powOfIncZ <= powOfOneStepLen) // 目的地小于等于一步的距离
// endT = curT + ani[RUN].durT;
// else { // 目的地大于一步的距离
// float nStep = sqrt(powOfIncX + powOfIncZ) / oneStepLen;
// endT = curT + (unsigned long)(ani[RUN].durT * (ceil(nStep)));
// }
// } else if (state == ATTACK) {
// begT = curT;
// endT = curT + ani[ATTACK].durT;
// }
// break;
// case RUN:
// if (state == STAND) {
// begT = curT;
// endT = ULONG_MAX;
// //endT = 1000000;
// } else if (state == ATTACK) {
// begT = curT;
// endT = curT + ani[ATTACK].durT;
// } else if (cmd[RUN]) {
// if (curT >= endT) endT += ani[RUN].durT; // 如果到达一步的结尾,则要开始下一步
// }
// break;
// case ATTACK:
// if (state == STAND) {
// begT = curT;
// endT = ULONG_MAX;
// } else if (state == RUN) {
// begT = curT;
// //================================
// //calculate the endT
// //================================
// float powOfIncX = pow(newPos.x - pos.x, 2);
// float powOfIncZ = pow(newPos.z - pos.z, 2);
// float powOfOneStepLen = pow(oneStepLen, 2);
// if (powOfIncX + powOfIncZ <= powOfOneStepLen) // 目的地小于等于一步的距离
// endT = curT + ani[RUN].durT;
// else { // 目的地大于一步的距离
// float nStep = sqrt(powOfIncX + powOfIncZ) / oneStepLen;
// endT = curT + (unsigned long)(ani[RUN].durT * (ceil(nStep)));
// }
// } else if (curT >= endT) {
// begT = curT;
// endT = curT + ani[ATTACK].durT;
// }
// break;
// }
//}
//
//
//void Unit::updateFrame()
//{
// curF = ani[state].firstF + (int)(fmod((float)(curT-begT), ani[state].durT) * ani[state].numF);
//}
//
//
//
//
/*
void Unit::updateState()
{
switch (state)
{
case STAND:
if (cmd[RUN]) {
//MessageBox(NULL, "stand - run", "stand - run", 0);
collisionDetect(&pos, &newPos);
if (pos.x == newPos.x || pos.z == newPos.z) { //撞墙了
rotY = newRotY;
return;
}
state = RUN;
begT = curT;
//================================
//calculate the endT
float a = newPos.x - pos.x;
float b = newPos.z - pos.z;
float a2 = pow(a, 2);
float b2 = pow(b, 2);
float oneStepLen2 = pow(oneStepLen, 2);
if (a2 + b2 <= oneStepLen2)
endT = curT + ani[RUN].durT;
else {
float nStep = sqrt(a2+b2) / oneStepLen;
endT = curT + (int)(ani[RUN].durT * (ceil(nStep)));
}
pos = newPos;
rotY = newRotY;
}
else if (curT >= endT) //上一次设置的时间用尽了,重新设置时间
endT += ani[STAND].durT*100;
else {
rotY = newRotY;
//MessageBox(NULL, "updateState", "updateState()", 0);
}
break;
case RUN:
if (cmd[RUN]) {
collisionDetect(&pos, &newPos);
if (pos.x == newPos.x || pos.z == newPos.z) { //撞墙了
state = STAND;
begT = curT;
endT = curT + ani[STAND].durT*100;
rotY = newRotY;
return;
}
//begT unchanged
if (curT >= endT) //如果到达一步的结尾,则要开始下一步
endT += ani[RUN].durT;
pos = newPos;
rotY = newRotY;
}
else if (curT < endT) { //这一步还没走完
float avgRotY = (rotY+newRotY)/2; //上一帧和这一帧方向的平均值
float len = moveSpeed * (curT-preT); //上一帧到这一帧,走了多长的距离
newPos.x = pos.x + len*sinf(avgRotY); //根据距离和方向计算新的坐标
newPos.z = pos.z + len*cosf(avgRotY);
collisionDetect(&pos, &newPos);
if (pos.x == newPos.x || pos.z == newPos.z) { //撞墙了
state = STAND;
begT = curT;
endT = curT + ani[STAND].durT*100;
rotY = newRotY;
return;
}
pos = newPos;
rotY = newRotY;
}
else { //这一步已经走完
state = STAND;
begT = curT;
endT += ani[STAND].durT*100;
rotY = newRotY;
//MessageBox(NULL, " run stand -", " - run stand", 0);
}
break;
}
}
void Unit::setRotCommand(float rotY)
{
u_rotY = rotY;
}
void Unit::setAttackCommand(unsigned int durTime)
{
u_cmd[ATTACK] = durTime;
}
void Unit::setStandCommand(unsigned int durTime)
{
u_cmd[STAND] = durTime;
}
void Unit::updateFrame()
{
if (u_curT <= u_endT) {
u_curF = u_ani[u_state].lastF - (u_ani[u_state].numF * ((u_endT-u_curT)% u_ani[u_state].durT) / u_ani[u_state].durT);
}
if ((u_endT - u_curT) < u_minT)
u_state = NO_ACTION;
}
void Unit::updatePosition()
{
}
if (u_cmd[ATTACK]) {
u_state = ATTACK;
u_endT = u_curT + u_cmd[ATTACK];
return;
}
if (u_cmd[STAND]) {
u_endT = u_curT + u_cmd[STAND];
return;
}
if (u_cmd[ATTACK]) {
u_state = ATTACK;
u_endT = u_curT + u_cmd[ATTACK];
return;
}
if (u_cmd[STAND]) {
u_state = STAND;
u_endT = u_curT + u_cmd[STAND];
return;
}
case ATTACK:
u_cmd[ROT] = u_rotY; //不响应ROT命令
break;
case NO_ACTION:
if (u_cmd[ATTACK]) {
u_state = ATTACK;
u_endT = u_curT + u_cmd[ATTACK];
return;
}
if (u_cmd[RUN]) {
u_state = RUN;
u_endT = u_curT + u_cmd[RUN];
return;
}
if (u_cmd[STAND]) {
u_state = STAND;
u_endT = u_curT + u_cmd[STAND];
return;
}
setStandCommand(100000);
updateState();
return;
*/
/*
Vertex* Unit::getPos()
{
return &u_pos;
}
Vertex* Unit::getLookat()
{
return &u_lookat;
}
int Unit::getCurrentFrame(int *type)
{
*type = u_state;
return u_curF;
}
*/
/*
save the old state to u_oldState,
after this function, u_state is the state of the next frame
*/
/*
void Unit::updateState()
{
int elapsedT;
u_curT = get_sys_time();
unsigned int enduring_time;
switch(u_state)
{
case STAND:
if(beingAttack()){
}
else if(seePlayer()){
if(canAttack()){
set_state(ATTACK, ani[ATTACK].durT);
}
else{
// calculate the enduring time according to the playerPos
//enduring_time = getEnduringTime(getDistance(&u_pos, player->getPos()), u_move_speed);
//u_endT = u_begT + enduring time
// set the u_Lookat to the player
}
}
else{
u_state = RUN;
u_begT = u_curT;
// random a destination
//set u_endT;
//set u_lookat;
}
break;
case RUN:
elapsedT = u_curT - u_begT;
//u_curF = [RUN].begF + ([RUN].endF - [RUN].begF) * elapsedT / [RUN].durT;
if (elapsedT > ani[RUN].durT)
//u_begT =
break;
case ATTACK:
break;
default:
return;
}
return;
}
void Unit::set_state(int type, unsigned int time)
{
switch(type)
{
case ATTACK:
u_state = ATTACK;
u_begT = u_curT;
u_endT = u_begT + ani[ATTACK].durT;
break;
case RUN:
break;
case STAND:
break;
}
}
void Unit::updateState()
{
get state
switch(state){
case stand:
if(command[attack]) set attack state, call myself, return
else {
if (command[rot]) set rot
if (command[run]) set run state, call myself, return
else break;
}
case run:
if(command[attac]) set attack, call myself, return
else if(command[rot]) set rot
else if(run time over) set stand, call myself, return
else breakl;
case attack:
if(attack over){
if (command[rot]) set rot
if (command[run]) set run
else set stand, call myself, return
}
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -