x
Yes
No
Do you want to visit DriveHQ English website?
首页
产品服务
价格
免费试用
下载客户端
关于我们
云文件服务
|
云备份服务
|
FTP服务
|
企业邮箱服务
|
网站托管
|
客户端软件
云文件服务
云备份服务
FTP服务
企业级邮箱服务
网站托管
客户端软件
Character.cpp - Hosted on DriveHQ Cloud IT Platform
返回上层目录
上传
下载
共享
发布
新建文件夹
新建文件
复制
剪切
删除
粘贴
评论
升级服务
路径: \\game3dprogramming\materials\DarkPuzzle\GameEngine\Character.cpp
旋转
特效
属性
历史版本
#pragma once //#include "DXUT.h" #include "DarkPuzzle.h" #include "Character.h" #include "GameLevel.h" #include "GameEngine.h" namespace DarkBattle{ #define FUNC_FRAMEMOVE "FrameMove" //#define FUNC_MOVE_WAYPOINT "MoveWayPoint" #define CHARACTER_NAME "Character" extern FILE* fdebug; //extern GameLevel* theGameLevel; const char* Character::characterNames[] = {"Hamster","DarkWizzard"}; Character* Character::theHero = NULL; MyHashTable
Character::pool; float Character::FireRange = 60; float Character::FireHeight = 8; int Character::EvaluateRange = 6; int Character::WayPointQueueLength = 10; void Character::ThinkStep(){ ScriptEngine::GetInstance()->ExecuteOneParamFunction("ThinkStep",(void*)this,CHARACTER_NAME); } void Character::FireToHero(){ if ((Character::GetHero()->GetPos()-GetPos()).Length() < Character::FireRange){ if ( GameLevel::RayCast(GetPos()+Vec3(0,0,Character::FireHeight), Character::GetHero()->GetPos()+Vec3(0,0,Character::FireHeight),NULL,NULL) ){ }else{ GameLevel::DrawDebugLine(GetPos()+Vec3(0,0,Character::FireHeight), Character::GetHero()->GetPos()+Vec3(0,0,Character::FireHeight),Vec3(0,0,1)); } } } Character::Character(Game3DObject* pObjectResource, btRigidBody* pRigidBody, const char* Name): Game3DInstance(pObjectResource,pRigidBody), closeList(GameLevel::GetInstance()->GetMap()->SizeX(), GameLevel::GetInstance()->GetMap()->SizeY()), distanceMap(GameLevel::GetInstance()->GetMap()->SizeX(), GameLevel::GetInstance()->GetMap()->SizeY()), isCovered(GameLevel::GetInstance()->GetMap()->SizeX(), GameLevel::GetInstance()->GetMap()->SizeY()) { this->forceStop = false; this->isMoving = false; this->openList.Clean(); this->hitPoint = 10; this->light = NULL; CLONE_STR(this->name,Name); type = identifyCharacterType(name); assert(type!=CHARACTER_NOTCHARACTER); //if (type==CHARACTER_HERO){ // GameLevel::GetInstance()->controllableCharacter = this; // Character::theHero = this; //} this->light = Light::CreateLight(this,Light::LightTypesConst::LightEye); pool.Put(name,this); } Character* Character::Add(char* modelName, const char* name){ Game3DObject* pObjResorce; btRigidBody* pRidBody; Game3DInstance::GetReference(modelName,&pObjResorce,&pRidBody); GameLevel::GetInstance()->AddRigidBody(pRidBody); return new Character(pObjResorce,pRidBody,name); } Character* Character::Get(char* name){ return pool.Get(name); } int Character::identifyCharacterType(const char* name){ for (int i=0;i
applyImpulse(impulse,btVector3(0,0,0)); pRigidBody->activate(); pRigidBody->setLinearVelocity(impulse); } void Character::MoveTo(Vec3 pos){ isMoving = true; pRigidBody->activate(); btVector3 impulse = btVector3(pos.x,pos.y,0); impulse = impulse - pRigidBody->getCenterOfMassPosition(); impulse.setZ(0); impulse.normalize(); float angle; if (impulse.getX()==0){ if (impulse.getY()>0) angle = 3.14159/2; else angle = -3.14159/2; }else{ angle = atanf(impulse.getY()/impulse.getX()); if (impulse.getX()<0) angle+=3.14159; } angle+=3.14159/2;//to make this angle identical to character angle //if (angle>3.14159/2*3) angle = -(3.14159*2-angle);//make the negative part float curAngle = this->GetAngle(); if (curAngle<0) curAngle+=3.14159*2;//make no negative part //impulse*=GameEngine::RunVelocity; //pRigidBody->setLinearVelocity(impulse); btVector3 vel = impulse*GameEngine::RunVelocity; //impulse*=GameEngine::RunAcceleration; //if ((pRigidBody->getLinearVelocity()-vel).length()>GameEngine::RunVelocityEpsilon){ // pRigidBody->applyImpulse(impulse,btVector3(0,0,0)); //}else{ pRigidBody->setLinearVelocity(vel); //} //DEBUG_OUTPUT("%f -- %f\n",curAngle/3.14159*180,angle/3.14159*180); float distPlus,distMinus,dist; if (angle>curAngle){ distPlus = abs(angle-curAngle); distMinus = abs(3.14159*2-angle+curAngle); if (distPlus
3.14159/180*2) pRigidBody->setAngularVelocity(btVector3(0,0,dist)); //if (curAngle<0 && angle > 3.14159) dist = curAngle - (-3.14159*2+angle);//must >0 //else if (curAngle>3.14159 && angle <0) dist = -3.14159*2+curAngle - angle;//must <0 //else dist = curAngle - angle; //if (dist>3.14159/180*2){ // pRigidBody->setAngularVelocity(btVector3(0,0,-dist/1.3)); //}else if (dist<-3.14159/180*2) // pRigidBody->setAngularVelocity(btVector3(0,0,+dist/1.3)); } void Character::Jump(){ pRigidBody->activate(); //pRigidBody->setLinearVelocity(btVector3(0,0,20)); pRigidBody->applyImpulse(btVector3(0,0,40),btVector3(0,0,10)); } void Character::SetPos(Vec3 pos){ btTransform trans = btTransform(); trans.setIdentity(); trans.setOrigin(btVector3(pos.x,pos.y,pos.z)); pRigidBody->activate(); pRigidBody->setLinearVelocity(btVector3(0,0,0)); pRigidBody->setWorldTransform(trans); } Vec3 Character::GetPos(){ //float ret[3]; btVector3 pos = pRigidBody->getWorldTransform().getOrigin(); return Vec3( pos.getX(),pos.getY(),pos.getZ()); //return ret; } bool Character::IsAt(Vec3 pos){ return GetPos().IsAt(pos); } void Character::Stop(){ isMoving = false; btVector3 curLinearVel = pRigidBody->getLinearVelocity(); pRigidBody->setLinearVelocity(btVector3(0,0,curLinearVel.z())); pRigidBody->setAngularVelocity(btVector3(0,0,0)); } bool Character::CheckNameBelongToThisClass(const char* name){ return (identifyCharacterType(name)!=CHARACTER_NOTCHARACTER); } void Character::FrameMove(float elapsedTime){ //perform per frame action in Lua evaluatedThisFrame = false; ScriptEngine::GetInstance()->ExecuteOneParamFunction(FUNC_FRAMEMOVE,(void*)this,CHARACTER_NAME); } void Character::InitComputeDistanceMap(){ Vec3 from = GetPos(); Map::GetInstance()->InitPathFindingParameters(NULL,NULL,&distanceMap,&from,&openList,&closeList ,NULL,NULL); } void Character::InitComputeDistanceMapBreath(){ Vec3 from = GetPos(); Map::GetInstance()->InitPathFindingParameters(NULL,NULL,&distanceMap,&from,&openList,&closeList ,&visitedPos,&isCovered); } bool Character::ComputeDistanceMapStep(Vec3 to){ return Map::GetInstance()->ComputeDistanceMapStep(to,distanceMap,openList,closeList); } bool Character::ComputeDistanceMapBreathStep(){ return Map::GetInstance()->ComputeDistanceMapStep(GetPos(),Character::EvaluateRange,distanceMap, openList,closeList,visitedPos,isCovered); } void Character::InitMakePath(Vec3 to){ Stop(); Map::GetInstance()->GetCell(to,curPathPos.x,curPathPos.y); Map::GetInstance()->InitPathFindingParameters(&wayPoints,&smoothWP,NULL,NULL,NULL,NULL ,NULL,NULL); } bool Character::MakePathStep(){ return Map::GetInstance()->MakePathStep(GetPos(),curPathPos,distanceMap,wayPoints); } void Character::InitMakeSmoothPath(Vec3 to){ Map::GetInstance()->InitMakeSmoothPath(GetPos(),smoothRefPos,smoothPrePos); } bool Character::MakeSmoothPathStep(Vec3 to){ return Map::GetInstance()->MakeSmoothPathStep(smoothRefPos,smoothPrePos,to,wayPoints,smoothWP); } bool Character::IsCovered(const Vec3 &from, const Vec3 &to){ return ((from-to).Length()>Character::FireRange) || GameLevel::RayCast(from + Vec3(0,0,Character::FireHeight), to + Vec3(0,0,Character::FireHeight),NULL,NULL); } bool Character::InitEvaluateEvadeStep(){ if (evaluatedThisFrame) return false; if (isMoving) return false; if (IsCovered(Character::GetHero()->GetPos(),GetPos())) return false; Map::GetInstance()->GetCell(GetPos(),bestSoFar.x,bestSoFar.y); bestCovered = false; bestDistance = 0; found = false; return true; } #define EVADE_STEPS 10 bool Character::EvaluateEvadeStep(){ evaluatedThisFrame = true; //ComputeDistanceMap(); Vec2 pos; int processedStep = 0; while (!visitedPos.IsEmpty()){ pos = visitedPos.Pop(); if (!bestCovered){ if (isCovered.get(pos.x,pos.y)){ bestCovered = true; bestSoFar = pos; bestDistance = distanceMap.get(pos.x,pos.y); found =true; } }else{ if (isCovered.get(pos.x,pos.y)){ if (distanceMap.get(pos.x,pos.y)
EVADE_STEPS) return false; } if (found){ //smoothWP.Clean(); //smoothWP.PushBack(Map::GetInstance()->GetPosition(bestSoFar.x,bestSoFar.y)); evaluatedPos = Map::GetInstance()->GetPosition(bestSoFar.x,bestSoFar.y); } return true; //if (found) // MoveWayPointTo(Map::GetInstance()->GetPosition(bestSoFar.x,bestSoFar.y)); } void Character::MakePath(Vec3 to){ ScriptEngine::GetInstance()->ExecuteTwoParamFunction("MakePath", (void*)this,CHARACTER_NAME,(void*)&to,"Vec3"); } void Character::MakeSmoothPath(Vec3 to){ //MakePathStep(to); ScriptEngine::GetInstance()->ExecuteTwoParamFunction("MakeSmoothPath", (void*)this,CHARACTER_NAME,(void*)&to,"Vec3"); } Character* Character::GetHero(){ assert(Character::theHero!=NULL); return Character::theHero; } void Character::SetHero(Character* hero){ assert(hero!=NULL); theHero = hero; GameLevel::GetInstance()->controllableCharacter = theHero; }; btVector3 Character::GetCenter(){ D3DXMATRIX ret = this->GetTransformMatrix(); return btVector3(ret._41,ret._42,ret._43); } }
Character.cpp
网页地址
文件地址
上一页 1/65
下一页
下载
( 10 KB )
Comments
Total ratings:
0
Average rating:
无评论
of 10
Would you like to comment?
Join now
, or
Logon
if you are already a member.