/** * 在这里简单实现怪物的AI */ bool Monster::onMove() { if (m_bIsMoving) { return false; } CCPoint curPos = getPosition(); CCPoint curMoveVec = getMoveVector(); memset(CanMove, 0, sizeof(CanMove)); // 先判断是否可达 for (int i = 0; i < 4; ++i) { // 跟现在的方向相反 if (IsReverseDir(curMoveVec, WalkVec[i])) { continue; } if (!GI.Helper->isReachable(curPos, WalkVec[i], 1)) { continue; } ++CanMove[i]; } // 是否存在敌人 CCPoint enemyPos(0, 0); bool enemyExist = (GI.Me != NULL && GI.Me->getQueueNum() > 0); if (enemyExist) { enemyPos = GI.Me->getHead()->getPosition(); } // 如果可达,判断最优方向 for (int i = 0; i < 4; ++i) { if (CanMove[i] > 0) { if (enemyExist) { if (IsAheadOfMe(curPos, WalkVec[i], enemyPos)) { ++CanMove[i]; } } else { // 同向 if (GI.Helper->ccpEqual(curMoveVec, WalkVec[i])) { ++CanMove[i]; } } } } int index = -1, ret = 0; for (int i = 0; i < 4; ++i) { if (CanMove[i] > ret) { index = i; ret = CanMove[i]; } } if (-1 == index) { return false; } else { setMoveVector(WalkVec[index]); } return Character::onMove(); }
int main(int argc, char *argv[]){ glutInit(&argc, argv); GameDebugger* debugger = GameDebugger::GetInstance(); assert(debugger->OpenDebugFile()); debugger->WriteDebugMessageToConsole("Hello, World", 31); debugger->WriteToDebugFile("Wrote to file", 32); TaskQueue *taskManager = new TaskQueue(NUM_THREADS); ConsoleCreateRoom(); GameRoom gr; if (argc > 1){ string path = pathCat(GAME_DATA_ROOMS_FOLDER, argv[1]); GameRoom::LoadRoom(path.c_str(), gr); } //For testing purposes. To make sure it reads debug.room char debugName[1000]; strcpy(debugName, GAME_DATA_ROOMS_FOLDER); strcat(debugName, "debug.room"); GameRoom debug; assert(GameRoom::LoadRoom(debugName, debug)); vector<GameObject*> obs = debug.GetGameObjects(); //map<string, GameWorldObject>::iterator wobs = debug.GetRoomWorldObjectsIterator(); for(unsigned int w = 0; w<obs.size(); w++){ //load starting meshes GameObject *gwo = obs[w]; cout<<gwo->GetName()<<endl; MyMesh *tmp = new MyMesh(); if (gwo->GetMeshFile()){ string fname = pathCat(".", gwo->GetMeshFile()); if (! MyMeshIO::LoadMesh(*tmp, fname)){ cerr<<"couldn't load (" << gwo->GetMeshFile() << ") for " <<gwo -> GetName() <<endl; gwo->SetMesh(NULL); }else{ gwo->SetMesh(tmp); NavShot::room = tmp; } } } ComputeBoundingBoxesAndPhysicsConstants(obs); //cin.ignore(1); /////////////////////////////////////////////////// //TODO: load from file GameState *gs = GameState::GetInstance(); Vector3f pos(0.0f, 0, -10.0f); Vector3f up(0, 1.0f, 0); Vector3f dir(0.0f, 0, 1.0f); float radius =0, n = 0.1, f = 600, fovy = 80, aspect = ((float)16.0/9.0); Camera cam(pos, dir, up, radius, n, f, fovy, aspect); gs->SetRoom(&debug); gs->SetCamera(&cam); Vector3f enemyPos(0.0f, 3.0f, 10.0f); gs->AddActor(new MetaballEnemy(enemyPos, 2, 2.0f)); Render::gameState = gs; Render::GlutInitialize(); SCollision::gameState = gs; RegisterControls(); Controller::Initialize(taskManager, gs); ///////////////////////////////////////////////// // TO DO: // Pass GameRoom debug to Render module. Render the // room. ///////////////////////////////////////////////// glutTimerFunc(0, Controller::GlutSync, 0); //glutMotionFunc(mouseMoveCB); //glutMouseFunc(mouseFunc); Sound::InitializeSounds(); Music *music = new Music("sounds/run2.ogg", 0.3f); music->Loop(); Sound *backgroundNoise = new Sound("sounds/metallic_roar.wav", 0.2f); backgroundNoise->Loop(); glutMainLoop(); //this should only be called once, and AT THE END of the initialization routine. Sound::UninitializeSounds(); assert(debugger->CloseDebugFile()); return 0; }