/*------------------ zoom the camera C.Wang 0329, 2005 -------------------*/ void ZoomCam(int x, int y) { if (x != oldXMM || y != oldYMM) { FnObject model; model.ID(cID); model.Translate(0.0f, 0.0f, (float)(x - oldXMM)*10.0f, LOCAL); oldXMM = x; oldYMM = y; } }
void TFlyObjectEdit::showObject( unsigned typeBit , bool isShow ) { int type = 1 << typeBit; for( int i =0;i < g_editDataVec.size() ; ++i ) { EditData& data = g_editDataVec[i]; if ( data.type & typeBit ) { FnObject obj; obj.Object( data.id ); obj.Show( isShow ); } } }
//------------------------------------------------------------------------------------ // timer callback function which will be invoked by TheFly3D System every 1/30 second // C.Wang 0308, 2004 //------------------------------------------------------------------------------------ void GameAI(int skip) { if (mmID != FAILED_ID) { FnMedia md; md.Object(mmID); if (md.GetState() == MEDIA_STOPPED) { // after playing, delete the media object FyDeleteMediaPlayer(mmID); mmID = FAILED_ID; } else { return; } } FnLight lgt; lgt.Object(lID); lgt.Rotate(Z_AXIS, 0.5f, GLOBAL); FnObject model; model.Object(nID); model.Rotate(Z_AXIS, 3.0f, GLOBAL); // render the scene FnViewport vp; vp.Object(vID); vp.Render3D(cID, TRUE, TRUE); // show frame rate static char string[128]; if (frame == 0) { FyTimerReset(0); } if (frame/10*10 == frame) { float curTime; curTime = FyTimerCheckTime(0); sprintf(string, "Fps: %6.2f", frame/curTime); } frame += skip; if (frame >= 1000) { frame = 0; } FySwapBuffers(); }
/*------------------ pivot the camera C.Wang 0329, 2004 -------------------*/ void PivotCam(int x, int y) { FnObject model; if (x != oldX) { model.ID(cID); model.Rotate(Z_AXIS, (float) (x - oldX), GLOBAL); oldX = x; } if (y != oldY) { model.ID(cID); model.Rotate(X_AXIS, (float) (y - oldY), GLOBAL); oldY = y; } }
MiniMap::MiniMap(WORLDid gID, SCENEid sMiniID, Lyubu *lyubu, int x_pos, int y_pos, int width, int height ) { FnWorld gw; FnScene scene; FnViewport vp; FnCamera camera; FnObject camStand; FnTerrain terrain; //adr_turn = this->turn; /* pos_begin[0]=3569.0; pos_begin[1]=-3208.0; pos_begin[2]=1000.0f; */ gw.Object(gID); //miniMap gw.SetObjectPath("Data\\NTU4\\Scenes"); FnScene miniScene; miniScene.Object(sMiniID); miniTerrainID = miniScene.CreateObject(ROOT); FnObject miniTerrain; miniTerrain.Object(miniTerrainID); miniTerrain.Load("terrain"); miniTerrain.SetOpacity(0.8f); // create a camera stand for minimap miniMapStandID = miniScene.CreateObject(ROOT); gw.SetObjectPath("Data\\NTU4\\Characters"); anchorID = miniScene.CreateObject(miniMapStandID); FnObject anchor; anchor.Object(anchorID); anchor.Load("arrowGreen"); float pos[]={0,0,10}; anchor.SetPosition(pos); // create a camera for minimap miniMapCameraID = miniScene.CreateCamera(miniMapStandID); FnCamera miniCamera; miniCamera.Object(miniMapCameraID); miniCamera.Translate(0.0f, 0.0f, 5000.0f, REPLACE); miniCamera.SetNear(5.0f); miniCamera.SetFar(100000.0f); miniCamera.Rotate(Z_AXIS, 180.0f, LOCAL); // create a light for minimap camera FnLight miniLight; miniLight.Object(miniScene.CreateLight(miniMapCameraID)); miniLight.SetIntensity(1.0f); miniLight.SetRange(9500.0f); vpMiniMapID = gw.CreateViewport(x_pos, y_pos, width, height); }
void load(EditData& data , TLevel* level ) { FnScene& scene = level->getFlyScene(); data.type = type; data.flag = flag; switch( data.type ) { case EOT_TOBJECT: if ( data.flag & EF_LOAD_MODEL ) { TObject* tObj = new TObject( modelID ); level->addEntity( tObj ); data.id = tObj->getFlyObj().Object(); data.entity = tObj; } break; case EOT_TNPC: { XForm trans; trans.setIdentity(); TActor* actor = new TActor( modelID , trans ); level->addEntity( actor ); data.id = actor->getFlyActor().GetBaseObject(); data.entity = actor; } break; case EOT_TERRAIN: case EOT_FLY_OBJ: case EOT_OTHER: data.id = scene.GetObjectByName( (char*) name.c_str() ); break; } FnObject obj; obj.Object( data.id ); obj.SetName( (char*) name.c_str() ); if ( data.flag & EF_SAVE_XFORM ) { obj.SetWorldPosition( pos ); obj.SetWorldDirection( front , up ); } }
/*------------------ move the camera C.Wang 0329, 2005 -------------------*/ void MoveCam ( int x, int y ) { float dx = float(x - oldXM); float dy = float(y - oldYM); float dd = sqrt(dx*dx + dy*dy); if (sgn(dd) == 0)return; dx /= dd; dy /= dd; FnObject model; if ( x != oldXM ) { model.ID ( cID ); model.Translate(0.1f*dx, 0.0f, 0.0f, LOCAL); oldXM = x; } if ( y != oldYM ) { model.ID ( cID ); model.Translate(0.0f, 0.1f*dy, 0.0f, LOCAL); oldYM = y; } }
EditData& TFlyObjectEdit::createActor( char const* name , unsigned modelID ) { EditData data; data.flag |= EF_SAVE_XFORM | EF_CREATE; TCamera& cam = m_game->getCamControl(); Vec3D pos = cam.getCameraPos() + 500 * cam.getViewDir(); XForm trans; trans.setIdentity(); TActor* entity = new TActor( modelID ,trans ); data.type = EOT_TNPC; m_curLevel->addEntity( entity ); data.flag |= EF_LOAD_MODEL; FnObject obj; obj.Object( entity->getFlyActor().GetBaseObject() ); obj.SetPosition( pos ); obj.SetDirection( Vec3D(1,0,0) , Vec3D(0,0,1) ); obj.SetName( (char*) name ); entity->getFlyActor().SetName((char*) name); data.id = obj.Object(); data.entity = entity; g_editDataVec.push_back( data ); return g_editDataVec.back(); }
void TFlyObjectEdit::saveTerrainData( char const* name ) { TTerrainData terrain; for( int i=0; i < g_editDataVec.size(); ++i ) { EditData& data = g_editDataVec[i]; if ( data.type == EOT_TERRAIN ) { FnObject obj; obj.Object( data.id ); terrain.add( obj ); } } TString terrFile = TString(name) + ".ter"; terrain.save( terrFile.c_str() ); TString bvhFile = TString(name) + ".bvh"; btBvhTriangleMeshShape* shape = terrain.createConcaveShape( bvhFile.c_str() ); delete shape; }
/*------------------------------------- constructor of classic shadow object C.Wang 0608, 2007 --------------------------------------*/ FuCShadowModify::FuCShadowModify(SCENEid sID, int rgID) { // initialize the shadow mHost = sID; mShadowMat = FAILED_MATERIAL_ID; mViewport = FAILED_ID; mRenderTarget = FAILED_MATERIAL_ID; // create the camera FnScene scene; FnObject seed; scene.Object(sID); m_lightCam.Object( scene.CreateCamera(ROOT) ); // backup the rendering group int oldRG = scene.GetCurrentRenderGroup(); if (rgID >= 0) { // set current rendering group scene.SetCurrentRenderGroup(rgID); } // create the seed object for displaying the shadow mSeed = scene.CreateObject(ROOT); seed.Object(mSeed); seed.BindPostRenderFunction(FuShadowDisplayCallback); seed.SetRenderOption(ALPHA, TRUE); FuCShadowModify **s = (FuCShadowModify **)seed.InitData(sizeof(FuCShadowModify *)); *s = this; mShadowHeightOffset = 0.4f; mShadowSize = 50.0f; // recover current rendering group scene.SetCurrentRenderGroup(oldRG); }
/*------------------ move the camera C.Wang 0329, 2004 -------------------*/ void MoveCam(int x, int y) { if (x != oldXM) { FnObject model; model.ID(cID); model.Translate((float)(x - oldXM)*0.1f, 0.0f, 0.0f, LOCAL); oldXM = x; } if (y != oldYM) { FnObject model; model.ID(cID); model.Translate(0.0f, (float)(oldYM - y)*0.1f, 0.0f, LOCAL); oldYM = y; } }
/*------------------------------------------------------------- 30fps timer callback in fixed frame rate for major game loop --------------------------------------------------------------*/ void GameAI(int skip) { // play character pose FnCharacter actor; FnCharacter cur_actor; FnObject terrain; terrain.ID(tID); FnScene scene; scene.ID(sID); LyubuID.actorID = actorID; //Tang: FX float actorPos[3]; vector<char*> FX_FileName; //Tang actor.ID(actorID); //Tang: FX actor.GetPosition(actorPos); //Tang if (curPoseID == runID || curPoseID == idleID) actor.Play(LOOP, (float)skip, FALSE, TRUE); else if (actor.Play(ONCE, (float)skip, FALSE, TRUE) == 0) actor.SetCurrentAction(NULL, 0, idleID, 10.0f); for (int i = 0; i < NUM_OF_BADGUYS; i++) { cur_actor.ID(badguyID[i].actorID); if (badguyID[i].blood_remain > 0) { bool checkMove = MoveToTargetLocation(badguyID[i], LyubuID, badguyID, terrain); if (cur_actor.Play(ONCE, (float)skip, FALSE, TRUE) == 0) { if (checkMove) { ACTIONid CombatIdleID = cur_actor.GetBodyAction(NULL, "Run"); cur_actor.SetCurrentAction(NULL, 0, CombatIdleID); } else { ACTIONid CombatIdleID = cur_actor.GetBodyAction(NULL, "NormalAttack2"); cur_actor.SetCurrentAction(NULL, 0, CombatIdleID); //Tang: FX FX_FileName.clear(); FX_FileName.push_back("HitForRobber"); GenFX(sID, gFXID, dummyID, actorPos, FX_FileName); //Tang } } } } for (int i = 0; i < NUM_OF_BOSS; i++) { cur_actor.ID(BossID[i].actorID); ACTIONid cur_action = cur_actor.GetCurrentAction(NULL, 0); ACTIONid DieID = cur_actor.GetBodyAction(NULL, "Die"); if (cur_action == DieID) cur_actor.Play(ONCE, (float)skip, FALSE, TRUE); else if (BossID[i].blood_remain > 0) { bool checkMove = MoveToTargetLocation(BossID[i], LyubuID, BossID, terrain); if (cur_actor.Play(ONCE, (float)skip, FALSE, TRUE) == 0) { if (checkMove) { ACTIONid CombatIdleID = cur_actor.GetBodyAction(NULL, "Run"); cur_actor.SetCurrentAction(NULL, 0, CombatIdleID); } else { ACTIONid CombatIdleID = cur_actor.GetBodyAction(NULL, "HeavyAttack"); cur_actor.SetCurrentAction(NULL, 0, CombatIdleID); //Tang: FX FX_FileName.clear(); FX_FileName.push_back("HitForRobber"); GenFX(sID, gFXID, dummyID, actorPos, FX_FileName); //Tang } } } } // Camera's position and direction as a standard for character's location setting FnCamera camera; camera.ID(cID); float cameraPos[3], camerafDir[3], camerauDir[3]; camera.GetPosition(cameraPos); // not used camera.GetDirection(camerafDir, camerauDir); float camVertical = camerafDir[2]; camerafDir[2] = 0; // Guaranteed up direction be 1 camerauDir[0] = 0; camerauDir[1] = 0; camerauDir[2] = 1; float dir[3], origin[3], actorfDir[3], actoruDir[3];// origin is the actor's position actor.GetPosition(origin); actor.GetDirection(actorfDir, actoruDir); //Generator location genSpot[0][0] = 2905.724; genSpot[0][1] = -2856.196; genSpot[1][0] = 729.288; genSpot[1][1] = -2472.88; // Bad guys generator for (int i = 0; i < NUM_OF_GENERATOR; i++) { if (!InArea(origin, genSpot[i], generator[i].range)) { generator[i].in = 0; generator[i].double_in = 0; } if (generator[i].in && InArea(origin, genSpot[i], generator[i].range)) { generator[i].double_in = 1; } if (InArea(origin, genSpot[i], generator[i].range)) { generator[i].in = 1; } if (generator[i].in == 1 && generator[i].double_in == 0) generator[i].produce(origin, actoruDir, scene, i, NUM_OF_GENERATOR); } origin[2] = origin[2] + HEIGHTOFFSET; // need an offset to probe the hit dir[0] = actorfDir[0] / ACTORPROBE; dir[1] = actorfDir[1] / ACTORPROBE; dir[2] = -1.0f; ////////////////////////////////////////// // Homework #01 part 1 ////////////////////////////////////////// if (dirCount % SMOOTHINESS != 0) { if ((dirCount + SMOOTHINESS) % SMOOTHINESS < SMOOTHINESS / 2 + 1) { dirCount--; actor.TurnRight(360 / SMOOTHINESS); } else { dirCount++; actor.TurnRight(360 - 360 / SMOOTHINESS); } } if ((FyCheckHotKeyStatus(FY_UP) || FyCheckHotKeyStatus(FY_S)) && dirCount % SMOOTHINESS == 0) { actor.SetDirection(camerafDir, camerauDir); if (terrain.HitTest(origin, dir) > 0) actor.MoveForward(RUNSPEED, FALSE, FALSE, 0, FALSE); } if (FyCheckHotKeyStatus(FY_D) && dirCount % SMOOTHINESS == 0) actor.TurnRight(TURNSPEED); if (FyCheckHotKeyStatus(FY_A) && dirCount % SMOOTHINESS == 0) actor.TurnRight(360 - TURNSPEED); else if (FyCheckHotKeyStatus(FY_RIGHT) && dirCount % SMOOTHINESS == 0) { actorfDir[0] = camerafDir[1]; actorfDir[1] = -camerafDir[0]; actor.SetDirection(actorfDir, actoruDir); if (terrain.HitTest(origin, dir) > 0) { //actor.MoveRight(CALLIBRATION, FALSE, FALSE, 0, FALSE); actor.MoveForward(RUNSPEED / 2, FALSE, FALSE, 0, FALSE); } } else if (FyCheckHotKeyStatus(FY_LEFT) && dirCount % SMOOTHINESS == 0) { actorfDir[0] = -camerafDir[1]; actorfDir[1] = camerafDir[0]; actor.SetDirection(actorfDir, actoruDir); if (terrain.HitTest(origin, dir) > 0) { //actor.MoveRight(CALLIBRATION, FALSE, FALSE, 0, FALSE); actor.MoveForward(RUNSPEED / 2, FALSE, FALSE, 0, FALSE); } } else if (FyCheckHotKeyStatus(FY_DOWN) && dirCount % SMOOTHINESS == 0) { actor.SetDirection(camerafDir, camerauDir); actor.TurnRight(180.0f); if (terrain.HitTest(origin, dir) > 0 && camVertical > MAXCAMANGLE) actor.MoveForward(RUNSPEED, FALSE, FALSE, 0, FALSE); } //Tang: FX if (gFXID != FAILED_ID) { FnGameFXSystem gxS(gFXID); BOOL4 beOK = gxS.Play((float)skip, ONCE); if (!beOK) { FnScene scene(sID); scene.DeleteGameFXSystem(gFXID); gFXID = FAILED_ID; } } //Tang }
void FyMain(int argc, char **argv) { srand(time(NULL)); AllocConsole(); freopen("CONIN$", "r", stdin); freopen("CONOUT$", "w", stdout); freopen("CONOUT$", "w", stderr); string ip, port; cout << "Enter ip [" << GAME_PROGRAMMING_DEFAULT_IP << "]: ";getline(cin, ip); cout << "Enter port [" << GAME_PROGRAMMING_DEFAULT_PORT << "]: ";getline(cin, port); if(ip.empty()) {ip = GAME_PROGRAMMING_DEFAULT_IP;} if(port.empty()) {port = GAME_PROGRAMMING_DEFAULT_PORT;} cout << "Connect to server: " << ip << "/" << port << endl; game_client.initialize(ip, port, &game_updater); game_client.connectServer(); cout << "Server connected." << endl; game_timer.initialize(&game_updater, game_client.getGmTree().getTotalGameTime(), game_client.getGmTree().getTotalGameRounds()); std::cout<<"Start Game" << std::endl; //create a new window FyStartFlyWin32("Final Project - with Fly2", 0, 0, window_w, window_h, FALSE); //set up path FySetShaderPath("C:\\Fly2Data\\Shaders"); FySetModelPath("C:\\Fly2Data\\Scenes"); FySetTexturePath("C:\\Fly2Data\\Scenes\\Textures"); FySetScenePath("C:\\Fly2Data\\Scenes"); FySetAudioPath("C:\\Fly2Data\\Audio"); //create a viewport viewportID = FyCreateViewport(0, 0, viewPortWidth, viewPortHeight); FnViewport viewport(viewportID); mouseInput.setWindowSize(wndWidth, wndHeight); //create 3D scene sceneID = FyCreateScene(10); FnScene scene(sceneID); scene.Load("gameScene01"); scene.SetAmbientLights(1.0f, 1.0f, 1.0f, 0.6f, 0.6f, 0.6f); // create a 2D scene for sprite rendering which will be rendered on the top of 3D FnScene scene2D; sID2 = FyCreateScene(1); scene2D.Object(sID2); scene2D.SetSpriteWorldSize(1024, 768); // 2D scene size in pixels FnSprite sp; spID0 = scene2D.CreateObject(SPRITE); sp.Object(spID0); sp.SetSize(sight_w, sight_h); sp.SetImage("C:\\Fly2Data\\Image\\spiner", 0, NULL, FALSE, NULL, 2, TRUE, FILTER_LINEAR); sp.SetPosition(window_w/2-sight_w/2, window_h/2-sight_h/2, 0); FnScene scene2Dmenu; sID2menu = FyCreateScene(1); scene2Dmenu.Object(sID2menu); scene2Dmenu.SetSpriteWorldSize(1024, 768); // 2D scene size in pixels FnSprite spExit; OBJECTid spIDexit = scene2Dmenu.CreateObject(SPRITE); spExit.Object(spIDexit); spExit.SetSize(400, 70); spExit.SetImage("C:\\Fly2Data\\Image\\exit_button", 0, NULL, FALSE, NULL, 2, TRUE, FILTER_LINEAR); spExit.SetPosition(window_w/2-400/2, window_h/2-70/2, 0); /* FnSprite sp1; spID1 = scene2Dmenu.CreateObject(SPRITE); sp1.Object(spID1); sp1.SetSize(C:\\Fly2Data\\Image\\mouse_w, mouse_h); sp1.SetImage("mouse", 0, NULL, FALSE, NULL, 2, TRUE, FILTER_LINEAR); sp1.SetPosition(window_w/2-mouse_w/2, window_h/2-mouse_h/2, 0); */ //load the terrain terrainID = scene.CreateObject(OBJECT); FnObject terrain; terrain.ID(terrainID); terrain.Load("terrain"); terrain.Show(FALSE); //set terrain environment terrainRoomID = scene.CreateRoom(SIMPLE_ROOM, 10); FnRoom room; room.ID(terrainRoomID); room.AddObject(terrainID); //load the character FySetModelPath("C:\\Fly2Data\\Characters"); FySetTexturePath("C:\\Fly2Data\\Characters"); FySetCharacterPath("C:\\Fly2Data\\Characters"); // put the character on terrain float pos[3], fDir[3], uDir[3]; pos[0] = 3569.0, pos[1] = -3108; pos[2] = 0; fDir[0] = 1, fDir[1] = 0; fDir[2] = 0; uDir[0] = 0, uDir[1] = 0, uDir[2] = 1; map<int, GmCharacter*> charNode = game_client.getGmTree().getCharacterNode(); cout << "Number of Character: " << charNode.size() << endl; map<int, Character *> actors; for (map<int, GmCharacter*>::iterator it = charNode.begin(); it != charNode.end(); it++) { actors[it->first] = new Character(); cout << "Local character:" << it->second->game_id << "/" << it->second->id << endl; actors[it->first]->setMeshFileName(it->second->mesh); actors[it->first]->setCharacterName(it->second->mesh); actors[it->first]->initialize(sceneID, NULL, terrainRoomID, it->second->fdir, it->second->udir, it->second->pos); actors[it->first]->setIsAI(it->second->is_ai); //actorID = actors[it->first]->getCharacterId(); chrMgtSystem.addCharacter(*(actors[it->first]), it->second->is_main_actor); game_updater.registerCharacter(it->second->game_id, actors[it->first]->getCharacterId()); } if(game_client.getClientId() == 0) { // AI Master!!! cout << "I am AI Master !!!" << endl; chrMgtSystem.becomeAIMaster(); } cameraID = scene.CreateObject(CAMERA); FnCamera camera; camera.ID(cameraID); camera.SetNearPlane(5.0f); camera.SetFarPlane(100000.0f); setCamera(); // setup a point light /* FnLight light; lightID = scene.CreateObject(LIGHT); light.ID(lightID); light.Translate(70.0f, -70.0f, 70.0f, REPLACE); light.SetColor(1.0f, 1.0f, 1.0f); light.SetIntensity(1.0f); */ //create a text object for display message on screen textID = FyCreateText("Trebuchet MS", 18, FALSE, FALSE); textCharID = FyCreateText("Trebuchet MS", 20, TRUE, FALSE); textHP_vID = FyCreateText("Trebuchet MS", 60, TRUE, FALSE); textInfo_vID = FyCreateText("Trebuchet MS", 30, TRUE, FALSE); //set up audio bckMiscID = FyCreateAudio(); FnAudio fnAudio(bckMiscID); fnAudio.Load("game_bckgnd"); fnAudio.SetVolume(0.1); fnAudio.Play(LOOP); mouseInput.hideMouse(); stateGhost = 0; //設定鬼一開始看不到的狀態 // set Hotkeys /* FyDefineHotKey(FY_ESCAPE, QuitGame, FALSE); // escape for quiting the game FyDefineHotKey(FY_UP, Movement, FALSE); // Up for moving forward FyDefineHotKey(FY_RIGHT, Movement, FALSE); // Right for turning right FyDefineHotKey(FY_LEFT, Movement, FALSE); // Left for turning left FyDefineHotKey(FY_DOWN, Movement, FALSE); // Down for moving backward */ FyDefineHotKey(FY_TAB, ChangeActor, FALSE); FyDefineHotKey(FY_ESC, showMenu, FALSE); //define some mouse function FyBindMouseFunction(LEFT_MOUSE, InitPivot, PivotCam, NULL, NULL); FyBindMouseFunction(MIDDLE_MOUSE, InitZoom, ZoomCam, NULL, NULL); FyBindMouseFunction(RIGHT_MOUSE, InitMove, MoveCam, NULL, NULL); //bind timers, frame rate = 30 fps FyBindTimer(0, 30.0f, GameAI, TRUE); FyBindTimer(1, 30.0f, RenderIt, TRUE); //invoke the system FyInvokeFly(TRUE); system("pause"); fclose(stdout); fclose(stderr); fclose(stdin); //FreeConsole(); }
/*------------------ the main program C.Wang 1010, 2014 -------------------*/ void FyMain(int argc, char **argv) { // create a new world BOOL4 beOK = FyStartFlyWin32("NTU@2014 Homework #01 - Use Fly2", 0, 0, 1024, 768, FALSE); // setup the data searching paths FySetShaderPath("Data\\NTU6\\Shaders"); FySetModelPath("Data\\NTU6\\Scenes"); FySetTexturePath("Data\\NTU6\\Scenes\\Textures"); FySetScenePath("Data\\NTU6\\Scenes"); // create a viewport vID = FyCreateViewport(0, 0, 1024, 768); FnViewport vp; vp.ID(vID); // create a 3D scene sID = FyCreateScene(10); FnScene scene; scene.ID(sID); // load the scene scene.Load("gameScene02"); scene.SetAmbientLights(1.0f, 1.0f, 1.0f, 0.6f, 0.6f, 0.6f); // load the terrain tID = scene.CreateObject(OBJECT); FnObject terrain; terrain.ID(tID); BOOL beOK1 = terrain.Load("terrain"); terrain.Show(FALSE); // set terrain environment terrainRoomID = scene.CreateRoom(SIMPLE_ROOM, 10); FnRoom room; room.ID(terrainRoomID); room.AddObject(tID); // load the character FySetModelPath("Data\\NTU6\\Characters"); FySetTexturePath("Data\\NTU6\\Characters"); FySetCharacterPath("Data\\NTU6\\Characters"); actorID = scene.LoadCharacter("Lyubu2"); // put the character on terrain float pos[3], fDir[3], uDir[3]; FnCharacter actor; actor.ID(actorID); pos[0] = 3569.0f; pos[1] = -3208.0f; pos[2] = 1000.0f; fDir[0] = 1.0f; fDir[1] = -1.0f; fDir[2] = 0.0f; uDir[0] = 0.0f; uDir[1] = 0.0f; uDir[2] = 1.0f; actor.SetDirection(fDir, uDir); actor.SetTerrainRoom(terrainRoomID, 10.0f); beOK = actor.PutOnTerrain(pos); // Get two character actions pre-defined at Lyubu2 idleID = actor.GetBodyAction(NULL, "Idle"); runID = actor.GetBodyAction(NULL, "Run"); // set the character to idle action curPoseID = idleID; actor.SetCurrentAction(NULL, 0, curPoseID); actor.Play(START, 0.0f, FALSE, TRUE); actor.TurnRight(90.0f); // translate the camera cID = scene.CreateObject(CAMERA); FnCamera camera; camera.ID(cID); camera.SetNearPlane(5.0f); camera.SetFarPlane(100000.0f); // set camera initial position and orientation pos[0] = 4069.0f; pos[1] = -3208.0f; pos[2] = 93.046f; fDir[0] = -500.0f; fDir[1] = -0.0f; fDir[2] = -0.0f; uDir[0] = -0.116f; uDir[1] = -0.031f; uDir[2] = 0.993f; camera.SetPosition(pos); camera.SetDirection(fDir, uDir); float mainLightPos[3] = { -4579.0, -714.0, 15530.0 }; float mainLightFDir[3] = { 0.276, 0.0, -0.961 }; float mainLightUDir[3] = { 0.961, 0.026, 0.276 }; FnLight lgt; lgt.ID(scene.CreateObject(LIGHT)); lgt.Translate(mainLightPos[0], mainLightPos[1], mainLightPos[2], REPLACE); lgt.SetDirection(mainLightFDir, mainLightUDir); lgt.SetLightType(PARALLEL_LIGHT); lgt.SetColor(1.0f, 1.0f, 1.0f); lgt.SetName("MainLight"); lgt.SetIntensity(0.4f); // create a text object for displaying messages on screen textID = FyCreateText("Trebuchet MS", 18, FALSE, FALSE); // set Hotkeys FyDefineHotKey(FY_ESCAPE, QuitGame, FALSE); // escape for quiting the game FyDefineHotKey(FY_UP, Movement, FALSE); // Up for moving forward FyDefineHotKey(FY_RIGHT, Movement, FALSE); // Right for turning right FyDefineHotKey(FY_LEFT, Movement, FALSE); // Left for turning left // define some mouse functions if (!IsCameraFollow) { FyBindMouseFunction(LEFT_MOUSE, InitPivot, PivotCam, NULL, NULL); FyBindMouseFunction(MIDDLE_MOUSE, InitZoom, ZoomCam, NULL, NULL); FyBindMouseFunction(RIGHT_MOUSE, InitMove, MoveCam, NULL, NULL); } else { FyBindMouseFunction(LEFT_MOUSE, InitZoom2, ZoomCam2, NULL, NULL); FyBindMouseFunction(RIGHT_MOUSE, NULL, RotateCam, NULL, NULL); } // bind timers, frame rate = 30 fps FyBindTimer(0, 30.0f, GameAI, TRUE); FyBindTimer(1, 30.0f, RenderIt, TRUE); // invoke the system FyInvokeFly(TRUE); }
/*---------------------------------- display the shadow on the terrain C.Wang 0522, 2006 -----------------------------------*/ void FuCShadowModify::Display() { TPROFILE("Shadow Display"); if ( m_fyActor.Object() == FAILED_ID ) return; FnObject model; model.Object(mSeed); if (!model.GetVisibility()) return; WORLDid wID; FnTriangle tT; FnScene scene; FnTerrain terrain; int vLen , texLen = 2, tri[3]; float M[16], *G, vLC[3]; // get all neighboring triangles //nList = terrain.GetAllVertexNeighborTriangles(iOne, list, 64); //if (nList <= 0) return; // get the matrix to character's base coordinate m_lightCam.SetWorldPosition( m_actorPos ); G = m_lightCam.GetMatrix(TRUE); FyInverseM16(G, M); tT.Object( m_terrain.Object() , 0); scene.Object(mHost); wID = scene.GetWorld(); // reset all rendering states FyResetRenderStates(wID); FySetD3DRenderState(wID, D3DRS_LIGHTING, FALSE); FySetD3DRenderState(wID, D3DRS_FOGENABLE, FALSE); FySetD3DRenderState(wID, D3DRS_ZWRITEENABLE, FALSE); // set current material FySetCurrentMaterial(wID, mRenderTarget, FALSE, 1.0f); FySetD3DRenderState(wID, D3DRS_ALPHABLENDENABLE, TRUE); FySetD3DRenderState(wID, D3DRS_ALPHATESTENABLE, FALSE); FySetD3DRenderState(wID, D3DRS_SRCBLEND, D3DBLEND_ZERO); FySetD3DRenderState(wID, D3DRS_DESTBLEND, D3DBLEND_SRCCOLOR); LPDIRECT3DDEVICE9 dev = FyGetD3DDevice(wID); float pos[16]; FuLitedVertex v[3]; // display these triangles vLen = 6; v[0].diffuse = D3DCOLOR_RGBA(255, 255, 255, 255); v[1].diffuse = D3DCOLOR_RGBA(255, 255, 255, 255); v[2].diffuse = D3DCOLOR_RGBA(255, 255, 255, 255); int nTri = m_triIDVec.size(); for ( int i = 0; i < nTri ; i++) { // get the triangle vertices tT.GetTopology( m_triIDVec[i] , tri); for ( int j = 0; j < 3; ++j ) { tT.GetVertex(tri[2-j], pos); v[j].pos[0] = pos[0]; v[j].pos[1] = pos[1]; v[j].pos[2] = pos[2] + mShadowHeightOffset; // calculate the texture coordinate FUTransformVertexWithM16_Simple(vLC, v[j].pos, M); CalculateShadowUV(vLC, v[j].uv); } FyDrawTriangles(wID, XYZ_DIFFUSE, 3 , 1 , &texLen, (float *) &v[0] ); } }
/*----------------------- show / hide the shadow C.Wang 0910, 2006 ------------------------*/ void FuCShadowModify::Show(BOOL beShow) { FnObject seed; seed.Object(mSeed); seed.Show(beShow); }
/* ------------------ the main program C.Wang 1010, 2014 -------------------*/ void FyMain(int argc, char **argv) { // create a new world BOOL4 beOK = FyStartFlyWin32("NTU@2014 Homework #02 - Use Fly2", 0, 0, 1024, 768, FALSE); // setup the data searching paths FySetShaderPath("Data\\NTU6\\Shaders"); FySetModelPath("Data\\NTU6\\Scenes"); FySetTexturePath("Data\\NTU6\\Scenes\\Textures"); FySetScenePath("Data\\NTU6\\Scenes"); // create a viewport vID = FyCreateViewport(0, 0, 1024, 768); FnViewport vp; vp.ID(vID); // create a 3D scene sID = FyCreateScene(10); FnScene scene; scene.ID(sID); // load the scene scene.Load("gameScene02"); scene.SetAmbientLights(1.0f, 1.0f, 1.0f, 0.6f, 0.6f, 0.6f); // load the terrain tID = scene.CreateObject(OBJECT); FnObject terrain; terrain.ID(tID); BOOL beOK1 = terrain.Load("terrain"); terrain.Show(FALSE); // set terrain environment terrainRoomID = scene.CreateRoom(SIMPLE_ROOM, 10); FnRoom room; room.ID(terrainRoomID); room.AddObject(tID); // load the character FySetModelPath("Data\\NTU6\\Characters"); FySetTexturePath("Data\\NTU6\\Characters"); FySetCharacterPath("Data\\NTU6\\Characters"); actorID = scene.LoadCharacter("Lyubu2"); npc1ID = scene.LoadCharacter("Donzo2"); npc2ID = scene.LoadCharacter("Robber02"); // put the character on terrain float apos[3], cpos[3], fDir[3], uDir[3]; FnCharacter actor, npc1, npc2; actor.ID(actorID); npc1.ID(npc1ID); npc2.ID(npc2ID); apos[0] = 3569.0f; apos[1] = -3208.0f; apos[2] = 1000.0f; fDir[0] = 1.0f; fDir[1] = 0.0f; fDir[2] = 0.0f; uDir[0] = 0.0f; uDir[1] = 0.0f; uDir[2] = 1.0f; actor.SetDirection(fDir, uDir); fDir[0] = -1.0f; fDir[1] = -1.0f; npc1.SetDirection(fDir, uDir); fDir[0] = 1.0f; npc2.SetDirection(fDir, uDir); actor.SetTerrainRoom(terrainRoomID, 10.0f); npc1.SetTerrainRoom(terrainRoomID, 10.0f); npc2.SetTerrainRoom(terrainRoomID, 10.0f); beOK = actor.PutOnTerrain(apos); apos[1] -= 150.0f; beOK = npc1.PutOnTerrain(apos); apos[1] -= 50.0f; beOK = npc2.PutOnTerrain(apos); // Get two character actions pre-defined at Lyubu2 idleID = actor.GetBodyAction(NULL, "Idle"); runID = actor.GetBodyAction(NULL, "Run"); attackID = actor.GetBodyAction(NULL, "NormalAttack1"); idle1ID = npc1.GetBodyAction(NULL, "Idle"); damage1ID = npc1.GetBodyAction(NULL, "DamageL"); die1ID = npc1.GetBodyAction(NULL, "Die"); idle2ID = npc2.GetBodyAction(NULL, "CombatIdle"); damage2ID = npc2.GetBodyAction(NULL, "Damage1"); die2ID = npc2.GetBodyAction(NULL, "DEAD"); // set the character to idle action curPoseID = idleID; actor.SetCurrentAction(NULL, 0, curPoseID); actor.Play(START, 0.0f, FALSE, TRUE); actor.TurnRight(90.0f); npc1.SetCurrentAction(NULL, 0, idle1ID); npc1.Play(START, 0.0f, FALSE, TRUE); npc2.SetCurrentAction(NULL, 0, idle2ID); npc2.Play(START, 0.0f, FALSE, TRUE); // translate the camera cID = scene.CreateObject(CAMERA); FnCamera camera; camera.ID(cID); camera.SetNearPlane(5.0f); camera.SetFarPlane(100000.0f); // set camera initial position and orientation actor.GetPosition(apos); //cpos[0] = 4315.783f; cpos[1] = -3199.686f; cpos[2] = 93.046f; fDir[0] = DIST; fDir[1] = 0.0f; fDir[2] = -HEIGHT; cpos[0] = apos[0] - fDir[0]; cpos[1] = apos[1] - fDir[1]; cpos[2] = apos[2] - fDir[2]; float distance = sqrt(double((fDir[0])*(fDir[0]) + (fDir[1])*(fDir[1]))); uDir[0] = -fDir[2] * fDir[0] / distance; uDir[1] = -fDir[2] * fDir[1] / distance; uDir[2] = distance; //fDir[0] = -0.983f; fDir[1] = -0.143f; fDir[2] = -0.119f; //uDir[0] = -0.116f; uDir[1] = -0.031f; uDir[2] = 0.993f; camera.SetPosition(cpos); camera.SetDirection(fDir, uDir); float mainLightPos[3] = { -4579.0, -714.0, 15530.0 }; float mainLightFDir[3] = { 0.276, 0.0, -0.961 }; float mainLightUDir[3] = { 0.961, 0.026, 0.276 }; FnLight lgt; lgt.ID(scene.CreateObject(LIGHT)); lgt.Translate(mainLightPos[0], mainLightPos[1], mainLightPos[2], REPLACE); lgt.SetDirection(mainLightFDir, mainLightUDir); lgt.SetLightType(PARALLEL_LIGHT); lgt.SetColor(1.0f, 1.0f, 1.0f); lgt.SetName("MainLight"); lgt.SetIntensity(0.4f); // create a text object for displaying messages on screen textID = FyCreateText("Trebuchet MS", 18, FALSE, FALSE); // set Hotkeys FyDefineHotKey(FY_ESCAPE, QuitGame, FALSE); // escape for quiting the game FyDefineHotKey(FY_UP, Movement, FALSE); // Up for moving forward FyDefineHotKey(FY_DOWN, Movement, FALSE); // Down for turning right FyDefineHotKey(FY_RIGHT, Movement, FALSE); // Right for turning right FyDefineHotKey(FY_LEFT, Movement, FALSE); // Left for turning left FyDefineHotKey('W', Movement, FALSE); // Up for moving forward FyDefineHotKey('S', Movement, FALSE); // Down for turning right FyDefineHotKey('D', Movement, FALSE); // Right for turning right FyDefineHotKey('A', Movement, FALSE); // Left for turning left FyDefineHotKey('J', Movement, FALSE); // define some mouse functions FyBindMouseFunction(LEFT_MOUSE, InitPivot, PivotCam, NULL, NULL); FyBindMouseFunction(MIDDLE_MOUSE, InitZoom, ZoomCam, NULL, NULL); FyBindMouseFunction(RIGHT_MOUSE, InitMove, MoveCam, NULL, NULL); // bind timers, frame rate = 30 fps FyBindTimer(0, 30.0f, GameAI, TRUE); FyBindTimer(1, 30.0f, RenderIt, TRUE); // invoke the system FyInvokeFly(TRUE); }
/* ------------------------------------------------------------ - 30fps timer callback in fixed frame rate for major game loop -------------------------------------------------------------- */ void GameAI(int skip) { FnCharacter actor, npc1, npc2; // play character pose actor.ID(actorID); actor.Play(LOOP, (float)skip, FALSE, TRUE); npc1.ID(npc1ID); if (HP1>0 || (frame - framedamage1 + 1000) % 1000 < dietime1) npc1.Play(LOOP, (float)skip, FALSE, TRUE); npc2.ID(npc2ID); if (HP2>0 || (frame - framedamage2 + 1000) % 1000 < dietime2) npc2.Play(LOOP, (float)skip, FALSE, TRUE); if ((frame - frameattack + 1000) % 1000 == attacktime) actor.SetCurrentAction(NULL, 0, curPoseID, 5.0f); if (HP1 > 0) { if ((frame - framedamage1 + 1000) % 1000 == damagetime1) npc1.SetCurrentAction(NULL, 0, idle1ID, 0.0f); else if (frame == framedamage1) npc1.SetCurrentAction(NULL, 0, damage1ID, 0.0f); } else if (frame == framedamage1) { npc1.SetCurrentAction(NULL, 0, die1ID, 0.0f); } if (HP2 > 0) { if ((frame - framedamage2 + 1000) % 1000 == damagetime2) npc2.SetCurrentAction(NULL, 0, idle2ID, 0.0f); else if (frame == framedamage2) npc2.SetCurrentAction(NULL, 0, damage2ID, 0.0f); } else if (frame == framedamage2) { npc2.SetCurrentAction(NULL, 0, die2ID, 0.0f); } FnCamera camera; camera.ID(cID); FnObject terrain; terrain.ID(tID); float fDir[3], uDir[3] = { 0, 0, 1 }, cpos[3], apos[3], distance, height, length; camera.GetPosition(cpos); actor.GetPosition(apos); fDir[0] = apos[0] - cpos[0]; fDir[1] = apos[1] - cpos[1]; fDir[2] = apos[2] - cpos[2]; distance = sqrt(double((fDir[0])*(fDir[0]) + (fDir[1])*(fDir[1]))); height = -fDir[2]; length = sqrt(double(distance*distance + height*height)); uDir[0] = -fDir[2] * fDir[0] / distance; uDir[1] = -fDir[2] * fDir[1] / distance; uDir[2] = distance; camera.SetDirection(fDir, uDir); uDir[0] = 0; uDir[1] = 0; uDir[2] = 1; bool up = FyCheckHotKeyStatus(FY_UP) || FyCheckHotKeyStatus('W'), down = FyCheckHotKeyStatus(FY_DOWN) || FyCheckHotKeyStatus('S'), left = FyCheckHotKeyStatus(FY_LEFT) || FyCheckHotKeyStatus('A'), right = FyCheckHotKeyStatus(FY_RIGHT) || FyCheckHotKeyStatus('D'), act = (frame - frameattack + 1000) % 1000 >= attacktime; if (act) { if (up&&!down) { actor.SetDirection(fDir, uDir); float norm = sqrt(double((fDir[0])*(fDir[0]) + (fDir[1])*(fDir[1]))); actor.MoveForward(speed, TRUE); actor.GetPosition(apos); cpos[0] = apos[0] - LENGTH*fDir[0] / norm; cpos[1] = apos[1] - LENGTH*fDir[1] / norm; cpos[2] = apos[2] + HEIGHT; float dir[3] = { cpos[0] - apos[0], cpos[1] - apos[1], 0 }, result[3]; if (terrain.HitTest(apos, dir, result) > 0) { float line[3] = { result[0] - apos[0], result[1] - apos[1], result[2] - apos[2] }; if (line[0] * line[0] + line[1] * line[1]<LENGTH*LENGTH) { cpos[0] = result[0]; cpos[1] = result[1]; cpos[2] = apos[2] + sqrt(double(LENGTH*LENGTH + HEIGHT*HEIGHT - (line[0] * line[0] + line[1] * line[1]))); } } camera.SetPosition(cpos); } if (down&&!up) { float bDir[3] = { -fDir[0], -fDir[1], -fDir[2] }; actor.SetDirection(bDir, uDir); float norm = sqrt(double((bDir[0])*(bDir[0]) + (bDir[1])*(bDir[1]))); actor.MoveForward(speed, TRUE); actor.GetPosition(apos); cpos[0] = apos[0] - LENGTH*fDir[0] / norm; cpos[1] = apos[1] - LENGTH*fDir[1] / norm; cpos[2] = apos[2] + HEIGHT; float dir[3] = { cpos[0] - apos[0], cpos[1] - apos[1], 0 }, result[3]; if (terrain.HitTest(apos, dir, result) > 0) { float line[3] = { result[0] - apos[0], result[1] - apos[1], result[2] - apos[2] }; if (line[0] * line[0] + line[1] * line[1]<LENGTH*LENGTH) { cpos[0] = result[0]; cpos[1] = result[1]; cpos[2] = apos[2] + sqrt(double(LENGTH*LENGTH + HEIGHT*HEIGHT - (line[0] * line[0] + line[1] * line[1]))); } } camera.SetPosition(cpos); } if (left&&!right) { float lDir[3] = { -fDir[1], fDir[0], fDir[2] }, apos2[3]; actor.SetDirection(lDir, uDir); actor.TurnRight(-asin(speed / 2 / distance) * 180 / pi); actor.MoveForward(speed, TRUE); actor.GetPosition(apos2); if ((apos[0] - apos2[0])*(apos[0] - apos2[0]) + (apos[1] - apos2[1])*(apos[1] - apos2[1]) == 0) { camera.TurnRight(-asin(2 * speed / 2 / distance) * 180 / pi); camera.MoveRight(2 * speed); } } if (right&&!left) { float rDir[3] = { fDir[1], -fDir[0], fDir[2] }, apos2[3]; actor.SetDirection(rDir, uDir); actor.TurnRight(asin(speed / 2 / distance) * 180 / pi); actor.MoveForward(speed, TRUE); actor.GetPosition(apos2); if ((apos[0] - apos2[0])*(apos[0] - apos2[0]) + (apos[1] - apos2[1])*(apos[1] - apos2[1]) == 0) { camera.TurnRight(asin(2 * speed / 2 / distance) * 180 / pi); camera.MoveRight(-2 * speed); } } } }
/*---------------------- perform the rendering C.Wang 0720, 2006 -----------------------*/ void RenderIt(int skip) { FnViewport vp; // render the whole scene vp.ID(vID); vp.Render3D(cID, TRUE, TRUE); // render the sprites vp.RenderSprites(sID2, FALSE, TRUE); // no clear the background but clear the z buffer FySwapBuffers(); // get camera's data FnCamera camera; camera.ID(cID); FnObject terrain; terrain.ID(tID); float pos[3], fDir[3], uDir[3]; //-----------------HW2 added-----------------------// float actorPos[3], actorfDir[3], actoruDir[3]; FnCharacter actor; actor.ID(actorID); actor.GetPosition(actorPos); actor.GetDirection(actorfDir, actoruDir); camera.GetPosition(pos); camera.GetDirection(fDir, uDir); // Initialze probing ray direction float dir[3], charDir[3]; dir[0] = -fDir[0] / CAMERAPROBE; dir[1] = -fDir[1] / CAMERAPROBE; dir[2] = -1; charDir[0] = actorfDir[0] / ACTORPROBE; charDir[1] = actorfDir[1] / ACTORPROBE; charDir[2] = -1; if ((FyCheckHotKeyStatus(FY_A) || FyCheckHotKeyStatus(FY_D)) && dirCount % SMOOTHINESS == 0) { int tempHeight = pos[2]; pos[2] = actorPos[2]; int theDistance = FyDistance(pos, actorPos); pos[0] = actorPos[0] - theDistance * actorfDir[0]; pos[1] = actorPos[1] - theDistance * actorfDir[1]; pos[2] = tempHeight; fDir[0] = actorfDir[0]; fDir[1] = actorfDir[1]; uDir[0] = fDir[0] / abs(fDir[0] * fDir[1]); uDir[1] = fDir[1] / abs(fDir[0] * fDir[1]); uDir[2] = -abs(uDir[0] * fDir[0] + uDir[1] * fDir[1]) / fDir[2]; } if ((FyCheckHotKeyStatus(FY_UP) || FyCheckHotKeyStatus(FY_S)) && dirCount % SMOOTHINESS == 0) { if (pos[2] > actorPos[2] + HEIGHTOFFSET) { pos[2] = actorPos[2]; pos[2] = actorPos[2] + HEIGHTBOUND + HEIGHTOFFSET - FyDistance(pos, actorPos); fDir[0] = actorPos[0] - pos[0]; fDir[1] = actorPos[1] - pos[1]; fDir[2] = -pos[2]; uDir[0] = fDir[0] / abs(fDir[0] * fDir[1]); uDir[1] = fDir[1] / abs(fDir[0] * fDir[1]); uDir[2] = -abs(uDir[0] * fDir[0] + uDir[1] * fDir[1]) / fDir[2]; } else // Reset the up direction { pos[2] = actorPos[2] + HEIGHTOFFSET; fDir[0] = actorfDir[0]; fDir[1] = actorfDir[1]; fDir[2] = 0; uDir[0] = 0; uDir[1] = 0; uDir[2] = 1; } if (FyDistance(pos, actorPos) > LONGDIST) { pos[0] = actorPos[0] - LONGDIST * actorfDir[0]; pos[1] = actorPos[1] - LONGDIST * actorfDir[1]; pos[2] = actorPos[2] + HEIGHTOFFSET; fDir[0] = actorfDir[0]; fDir[1] = actorfDir[1]; fDir[2] = -0.0f; uDir[0] = actoruDir[0]; uDir[1] = actoruDir[1]; uDir[2] = actoruDir[2]; } } if (FyCheckHotKeyStatus(FY_RIGHT) && dirCount % SMOOTHINESS == 0) { actorPos[2] += HEIGHTOFFSET; if (terrain.HitTest(actorPos, charDir) <= 0) { camera.MoveRight(-CAMERASPEED, FALSE, FALSE, 0, FALSE); camera.GetPosition(pos); } fDir[0] = actorPos[0] - pos[0]; fDir[1] = actorPos[1] - pos[1]; fDir[2] = 0.0f; } if (FyCheckHotKeyStatus(FY_LEFT) && dirCount % SMOOTHINESS == 0) { actorPos[2] += HEIGHTOFFSET; if (terrain.HitTest(actorPos, charDir) <= 0) { camera.MoveRight(CAMERASPEED, FALSE, FALSE, 0, FALSE); camera.GetPosition(pos); } fDir[0] = actorPos[0] - pos[0]; fDir[1] = actorPos[1] - pos[1]; fDir[2] = 0.0f; } if (FyCheckHotKeyStatus(FY_DOWN) && dirCount % SMOOTHINESS == 0) { if (FyDistance(pos, actorPos) < SHORTDIST) { if (terrain.HitTest(pos, dir) > 0) { pos[0] = actorPos[0] + SHORTDIST * actorfDir[0]; pos[1] = actorPos[1] + SHORTDIST * actorfDir[1]; pos[2] = actorPos[2] + HEIGHTOFFSET; fDir[0] = -actorfDir[0]; fDir[1] = -actorfDir[1]; fDir[2] = 0.0f; uDir[0] = actoruDir[0]; uDir[1] = actoruDir[1]; uDir[2] = actoruDir[2]; } else { // go back and hit Obstacle : camera look down at the actor pos[2] = actorPos[2]; pos[2] = actorPos[2] + HEIGHTBOUND + HEIGHTOFFSET - FyDistance(pos, actorPos); fDir[0] = actorPos[0] - pos[0]; fDir[1] = actorPos[1] - pos[1]; fDir[2] = -pos[2]; uDir[0] = fDir[0] / abs(fDir[0] * fDir[1]); uDir[1] = fDir[1] / abs(fDir[0] * fDir[1]); uDir[2] = -abs(uDir[0] * fDir[0] + uDir[1] * fDir[1]) / fDir[2]; } } } //-----------------HW2 end-------------------------// if (dirCount % SMOOTHINESS == 0) { camera.SetPosition(pos); camera.SetDirection(fDir, uDir); } // show frame rate static char string[128]; if (frame == 0) { FyTimerReset(0); } if (frame / 10 * 10 == frame) { float curTime; curTime = FyTimerCheckTime(0); sprintf(string, "Fps: %6.2f", frame / curTime); } frame += skip; if (frame >= 1000) { frame = 0; } FnText text; text.ID(textID); text.Begin(vID); text.Write(string, 20, 20, 255, 0, 0); char posS[256], fDirS[256], uDirS[256], debug[256]; sprintf(posS, "pos: %8.3f %8.3f %8.3f", pos[0], pos[1], pos[2]); sprintf(fDirS, "facing: %8.3f %8.3f %8.3f", fDir[0], fDir[1], fDir[2]); sprintf(uDirS, "up: %8.3f %8.3f %8.3f", uDir[0], uDir[1], uDir[2]); sprintf(debug, "debug: %d %d %d", LyubuID.level, LyubuID.exp_cur, LyubuID.blood_remain); text.Write(posS, 20, 35, 255, 255, 0); text.Write(fDirS, 20, 50, 255, 255, 0); text.Write(uDirS, 20, 65, 255, 255, 0); text.Write(debug, 20, 90, 255, 255, 0); text.End(); // swap buffer FySwapBuffers(); }
/*------------------ the main program C.Wang 1010, 2014 -------------------*/ void FyMain(int argc, char **argv) { // create a new world BOOL4 beOK = FyStartFlyWin32("NTU@2015 Homework #02 - Use Fly2", 0, 0, 1024, 768, FALSE); // setup the data searching paths FySetShaderPath("Data\\NTU6\\Shaders"); FySetModelPath("Data\\NTU6\\Scenes"); FySetTexturePath("Data\\NTU6\\Scenes\\Textures"); FySetScenePath("Data\\NTU6\\Scenes"); //Tang: FX FySetGameFXPath("Data\\NTU6\\FX"); //Tang // create a viewport vID = FyCreateViewport(0, 0, 1024, 768); FnViewport vp; vp.ID(vID); // create a 3D scene sID = FyCreateScene(10); FnScene scene; scene.ID(sID); // load the scene scene.Load("gameScene02"); scene.SetAmbientLights(1.0f, 1.0f, 1.0f, 0.6f, 0.6f, 0.6f); // load the terrain tID = scene.CreateObject(OBJECT); FnObject terrain; terrain.ID(tID); BOOL beOK1 = terrain.Load("terrain"); terrain.Show(FALSE); // set terrain environment terrainRoomID = scene.CreateRoom(SIMPLE_ROOM, 10); FnRoom room; room.ID(terrainRoomID); room.AddObject(tID); // load the character FySetModelPath("Data\\NTU6\\Characters"); FySetTexturePath("Data\\NTU6\\Characters"); FySetCharacterPath("Data\\NTU6\\Characters"); actorID = scene.LoadCharacter("Lyubu2"); // put the character on terrain float pos[3], fDir[3], uDir[3]; SetValues(pos, 3569.0f, -3208.0f, 1000.0f); SetValues(fDir, 1.0f, 1.0f, 0.0f); SetValues(uDir, 0.0f, 0.0f, 1.0f); ActorGen(scene, terrainRoomID, LyubuID, "Lyubu2", "Idle", pos, fDir, uDir); actorID = LyubuID.actorID; FnCharacter actor; actor.ID(actorID); // Get two character actions pre-defined at Lyubu2 idleID = actor.GetBodyAction(NULL, "Idle"); runID = actor.GetBodyAction(NULL, "Run"); NormalAttack1ID = actor.GetBodyAction(NULL, "NormalAttack1"); NormalAttack2ID = actor.GetBodyAction(NULL, "NormalAttack2"); HeavyAttack1ID = actor.GetBodyAction(NULL, "HeavyAttack1"); // translate the camera cID = scene.CreateObject(CAMERA); FnCamera camera; camera.ID(cID); camera.SetNearPlane(5.0f); camera.SetFarPlane(100000.0f); //hw3 : Donzo and Robber02 initialization float temp_pos[3]; float temp_fDir[3]; /* for (int i = 0; i < NUM_OF_BADGUYS; i++) { temp_pos[0] = pos[0] + 30 * (rand()%8); temp_pos[1] = pos[1] + 30 * (rand()%8); temp_pos[2] = pos[2] + 30 * (rand()%8); temp_fDir[0] = -1.0f; temp_fDir[1] = -1.0f; temp_fDir[2] = 1.0f; ActorGen(scene, terrainRoomID, badguyID[i], "Robber02", "CombatIdle", temp_pos, temp_fDir, uDir); } */ for (int i = 0; i < NUM_OF_BOSS; i++) { temp_pos[0] = pos[0] + 30 * (rand() % 8); temp_pos[1] = pos[1] + 30 * (rand() % 8); temp_pos[2] = pos[2] + 30 * (rand() % 8); SetValues(temp_fDir, -1.0f, -1.0f, 1.0f); ActorGen(scene, terrainRoomID, BossID[i], "Donzo2", "CombatIdle", temp_pos, temp_fDir, uDir); } // hw2 initial : set camera position actor.GetPosition(pos); pos[0] = pos[0] - SHORTDIST * fDir[0]; pos[1] = pos[1] - SHORTDIST * fDir[1]; pos[2] = pos[2] + HEIGHTOFFSET; SetValues(fDir, 1.0f, 1.0f, 0.0f); SetValues(uDir, 0.0f, 0.0f, 1.0f); camera.SetPosition(pos); camera.SetDirection(fDir, uDir); // the same as actor float mainLightPos[3] = { -4579.0, -714.0, 15530.0 }; float mainLightFDir[3] = { 0.276, 0.0, -0.961 }; float mainLightUDir[3] = { 0.961, 0.026, 0.276 }; FnLight lgt; lgt.ID(scene.CreateObject(LIGHT)); lgt.Translate(mainLightPos[0], mainLightPos[1], mainLightPos[2], REPLACE); lgt.SetDirection(mainLightFDir, mainLightUDir); lgt.SetLightType(PARALLEL_LIGHT); lgt.SetColor(1.0f, 1.0f, 1.0f); lgt.SetName("MainLight"); lgt.SetIntensity(0.4f); // create a text object for displaying messages on screen textID = FyCreateText("Trebuchet MS", 18, FALSE, FALSE); // create a 2D scene for sprite rendering which will be rendered on the top of 3D sID2 = FyCreateScene(1); scene.ID(sID2); scene.SetSpriteWorldSize(800, 600); // 2D scene size in pixels spID0 = scene.CreateObject(SPRITE); sp.ID(spID0); //showPicture(sp, "talk_mission.jpg", 780, 200 , 10, 10); //showPicture parameter : FnSprite ,imageName, size, position // set Hotkeys FyDefineHotKey(FY_ESCAPE, QuitGame, FALSE); // escape for quiting the game FyDefineHotKey(FY_UP, Movement, FALSE); // Up for moving forward FyDefineHotKey(FY_RIGHT, Movement, FALSE); // Right for turning right FyDefineHotKey(FY_LEFT, Movement, FALSE); // Left for turning left FyDefineHotKey(FY_DOWN, Movement, FALSE); FyDefineHotKey(FY_Q, Movement, FALSE); FyDefineHotKey(FY_W, Movement, FALSE); FyDefineHotKey(FY_E, Movement, FALSE); FyDefineHotKey(FY_A, Movement, FALSE); // Turn left with camera rotation FyDefineHotKey(FY_D, Movement, FALSE); // Turn right with camera rotation FyDefineHotKey(FY_S, Movement, FALSE); // define some mouse functions FyBindMouseFunction(LEFT_MOUSE, InitPivot, PivotCam, NULL, NULL); FyBindMouseFunction(MIDDLE_MOUSE, InitZoom, ZoomCam, NULL, NULL); FyBindMouseFunction(RIGHT_MOUSE, InitMove, MoveCam, NULL, NULL); // bind timers, frame rate = 30 fps FyBindTimer(0, 30.0f, GameAI, TRUE); FyBindTimer(1, 30.0f, RenderIt, TRUE); // invoke the system FyInvokeFly(TRUE); }
/*------------------ the main program C.Wang 0308, 2004 -------------------*/ void FyMain(int argc, char **argv) { // create a new world FyStartFlyWin32("Hello TheFly3D !", 0, 0, 800, 600, beFullScreen); FySetModelPath("Data\\Models"); FySetTexturePath("Data\\textures"); FySetShaderPath("Data\\Shaders"); FyBeginMedia("data\\media", 2); //mmID = FyCreateMediaPlayer("dawn.mpg", 0, 0, 800, 600); //mmID = FyCreateMediaPlayer("opening.avi", 0, 0, 800, 600); mmID = FyCreateMediaPlayer("MUSIC_fogforest.mp3", 0, 0, 800, 600); FnMedia mP; mP.Object(mmID); mP.Play(ONCE); //mP.SetVolume(0.1f); // create a viewport vID = FyCreateViewport(0, 0, 800, 600); FnViewport vp; vp.Object(vID); vp.SetBackgroundColor(0.3f, 0.4f, 0.5f); // create a 3D scene & the 3D entities sID = FyCreateScene(1); FnScene scene; scene.Object(sID); nID = scene.CreateObject(OBJECT); cID = scene.CreateObject(CAMERA); lID = scene.CreateObject(LIGHT); // load the teapot FnObject model; model.ID(nID); model.Load("teapot.cw3"); model.Translate(20.0f, 0.0f, 0.0f, GLOBAL); // translate the camera FnCamera camera; camera.ID(cID); camera.Rotate(X_AXIS, 90.0f, LOCAL); camera.SetAspectRatio(800.0f/600.0f); camera.Translate(0.0f, 10.0f, 200.0f, LOCAL); // translate the light FnLight light; light.ID(lID); light.Translate(-30.0f, -30.0f, 0.0f, GLOBAL); light.SetName("MainLight"); light.SetColor(0.9f, 0.9f, 0.7f); // set Hotkeys FyDefineHotKey(FY_ESCAPE, QuitGame, FALSE); // define some mouse functions FyBindMouseFunction(LEFT_MOUSE, InitPivot, PivotCam, NULL, NULL); FyBindMouseFunction(MIDDLE_MOUSE, InitZoom, ZoomCam, NULL, NULL); FyBindMouseFunction(RIGHT_MOUSE, InitMove, MoveCam, NULL, NULL); /* bind a timer, frame rate = 30 fps */ FyBindTimer(0, 30.0f, GameAI, TRUE); // invoke the system FyInvokeFly(TRUE); }
Donzo::Donzo( WORLDid gID, SCENEid sID ) { FnWorld gw; FnScene scene; HP_MAX = 9000; HP = HP_MAX; pos_begin[0]=3469.0; pos_begin[1]=-3208.0; pos_begin[2]=1000.0f; this->gID = gID; this->sID = sID; gw.Object(gID); scene.Object(sID); gw.SetObjectPath("Data\\NTU4\\Characters"); gw.SetTexturePath("Data\\NTU4\\Characters"); gw.SetCharacterPath("Data\\NTU4\\Characters"); gw.SetShaderPath( "Data\\NTU4\\Shaders" ); aID = scene.LoadActor("Donzo"); actor.Object(aID); //-------action--------- //IDLE ourIdleAction = new OurAction(); ourIdleAction->actID = actor.GetBodyAction(NULL, "CombatIdle"); ourIdleAction->frames_num = 0; ourIdleAction->play_speed = 1; ourIdleAction->priority = 0; ourIdleAction->type.value = DonzoAction::ACTION_IDLE(); current_OurAction = ourIdleAction; actor.MakeCurrentAction(0, NULL, ourIdleAction->actID); ourCombatIdleAction = ourIdleAction; //Run ourRunAction = new OurAction(); ourRunAction->actID = actor.GetBodyAction(NULL, "Run"); ourRunAction->frames_num = 0; ourRunAction->play_speed = 1; ourRunAction->priority = 0; ourRunAction->type.value = Action_type::ACTION_WALK(); //Attack ourAttack1Action = new OurAction(); ourAttack1Action->actID = actor.GetBodyAction(NULL, "AttackL1"); ourAttack1Action->isAttack = true; ourAttack1Action->frames_num = 0; ourAttack1Action->play_speed = 1; ourAttack1Action->priority = 5; ourAttack1Action->type.value = Action_type::ACTION_ATTACK(); ourAttack1Action->numOfKeyFrames = 1; ourAttack1Action->keyFrames = new OurFrame*[1]; ourAttack1Action->keyFrames[0] = new OurFrame; ourAttack1Action->keyFrames[0]->frameNO = 10; ourAttack1Action->keyFrames[0]->start_angle = 320; ourAttack1Action->keyFrames[0]->plus_angle = 80; ourAttack1Action->keyFrames[0]->valid_dis = 200; ourAttack1Action->keyFrames[0]->damage_pt = 100; ourHeavyAttack1Action = new OurAction(); ourHeavyAttack1Action->actID = actor.GetBodyAction(NULL, "AttackH"); ourHeavyAttack1Action->isAttack = true; ourHeavyAttack1Action->frames_num = 0; ourHeavyAttack1Action->play_speed = 1; ourHeavyAttack1Action->priority = ourAttack1Action->priority + 10; ourHeavyAttack1Action->type.value = Action_type::ACTION_ATTACK(); ourHeavyAttack1Action->numOfKeyFrames = 3; ourHeavyAttack1Action->keyFrames = new OurFrame*[3]; ourHeavyAttack1Action->keyFrames[0] = new OurFrame; ourHeavyAttack1Action->keyFrames[0]->frameNO = 23; ourHeavyAttack1Action->keyFrames[0]->start_angle = 270; ourHeavyAttack1Action->keyFrames[0]->plus_angle = 100; ourHeavyAttack1Action->keyFrames[0]->valid_dis = 200; ourHeavyAttack1Action->keyFrames[0]->damage_pt = 100; ourHeavyAttack1Action->keyFrames[1] = new OurFrame; ourHeavyAttack1Action->keyFrames[1]->frameNO = 42; ourHeavyAttack1Action->keyFrames[1]->start_angle = 270; ourHeavyAttack1Action->keyFrames[1]->plus_angle = 180; ourHeavyAttack1Action->keyFrames[1]->valid_dis = 200; ourHeavyAttack1Action->keyFrames[1]->damage_pt = 90; ourHeavyAttack1Action->keyFrames[2] = new OurFrame; ourHeavyAttack1Action->keyFrames[2]->frameNO = 70; ourHeavyAttack1Action->keyFrames[2]->start_angle = 0; ourHeavyAttack1Action->keyFrames[2]->plus_angle = 360; ourHeavyAttack1Action->keyFrames[2]->valid_dis = 200; ourHeavyAttack1Action->keyFrames[2]->damage_pt = 80; ourHeavyAttack2Action = new OurAction(); ourHeavyAttack2Action->actID = actor.GetBodyAction(NULL, "HeavyAttack"); ourHeavyAttack2Action->isAttack = true; ourHeavyAttack2Action->frames_num = 0; ourHeavyAttack2Action->play_speed = 1; ourHeavyAttack2Action->priority = ourAttack1Action->priority + 10; ourHeavyAttack2Action->type.value = Action_type::ACTION_ATTACK(); ourHeavyAttack2Action->numOfKeyFrames = 2; ourHeavyAttack2Action->keyFrames = new OurFrame*[2]; ourHeavyAttack2Action->keyFrames[0] = new OurFrame; ourHeavyAttack2Action->keyFrames[0]->frameNO = 31; ourHeavyAttack2Action->keyFrames[0]->start_angle = 0; ourHeavyAttack2Action->keyFrames[0]->plus_angle = 270; ourHeavyAttack2Action->keyFrames[0]->valid_dis = 190; ourHeavyAttack2Action->keyFrames[0]->damage_pt = 80; ourHeavyAttack2Action->keyFrames[1] = new OurFrame; ourHeavyAttack2Action->keyFrames[1]->frameNO = 41; ourHeavyAttack2Action->keyFrames[1]->start_angle = 270; ourHeavyAttack2Action->keyFrames[1]->plus_angle = 180; ourHeavyAttack2Action->keyFrames[1]->valid_dis = 200; ourHeavyAttack2Action->keyFrames[1]->damage_pt = 120; //DamageL ourDamageLAction = new OurAction(); ourDamageLAction->actID = actor.GetBodyAction(NULL, "DamageL"); ourDamageLAction->frames_num = 0; ourDamageLAction->play_speed = 1.5; ourDamageLAction->priority = 100; ourDamageLAction->type.value = DonzoAction::ACTION_DAMAGED(); //fx ourDamageLAction->numOfFxFrames = 1; ourDamageLAction->fxFrames = new OurFxFrame*[1]; ourDamageLAction->fxFrames[0] = new OurFxFrame; ourDamageLAction->fxFrames[0]->frameNO = 1; ourDamageLAction->fxFrames[0]->fxName = AllFx::SmallHurt01; //DamageH ourDamageHAction = new OurAction(); ourDamageHAction->actID = actor.GetBodyAction(NULL, "DamageH"); ourDamageHAction->frames_num = 0; ourDamageHAction->play_speed = 1.5; ourDamageHAction->priority = 100; ourDamageHAction->type.value = DonzoAction::ACTION_DAMAGED(); //fx ourDamageHAction->numOfFxFrames = 1; ourDamageHAction->fxFrames = new OurFxFrame*[1]; ourDamageHAction->fxFrames[0] = new OurFxFrame; ourDamageHAction->fxFrames[0]->frameNO = 1; ourDamageHAction->fxFrames[0]->fxName = AllFx::BigHurt01; //Die ourDieAction = new OurAction(); ourDieAction->actID = actor.GetBodyAction(NULL, "Die"); ourDieAction->frames_num = 0; ourDieAction->play_speed = 1; ourDieAction->priority = 1000; ourDieAction->type.value = DonzoAction::ACTION_DIE(); //fx ourDieAction->numOfFxFrames = 1; ourDieAction->fxFrames = new OurFxFrame*[1]; ourDieAction->fxFrames[0] = new OurFxFrame; ourDieAction->fxFrames[0]->frameNO = 1; ourDieAction->fxFrames[0]->fxName = AllFx::smoke; //blood { FnObject blood; float pos[3], size[2], color[3]; blood_length = 50; blood_width = 2; bloodID = scene.CreateObject(ROOT); blood.Object(bloodID); pos[0] = 0.0f; pos[1] = 0.0f; pos[2] = 100.0f; size[0] = blood_length; size[1] = blood_width; color[0] = 1.0f; color[1] = 0.8; color[2] = 0.2f; blood.Billboard(pos, size, NULL, 0, color); blood.SetParent(actor.GetBaseObject()); } this->blood.Object(bloodID,0); }