StarWar.cpp
资源名称:g.rar [点击查看]
上传用户:laitongbao
上传日期:2021-02-20
资源大小:8176k
文件大小:6k
源码类别:
射击游戏
开发平台:
Visual C++
- // StarWar.cpp : Defines the entry point for the application.
- //
- #include "stdafx.h"
- #include "includeCGL.h"
- #include "Playership.h"
- #include "MachineGun.h"
- #include "LaserGun.h"
- #include "Enemy.h"
- #include "EnemyList.h"
- #include "stdio.h"
- #pragma comment(lib, "cgllib.lib")
- //全局变量
- Timer *g_timer;
- CGLApplication *g_app;//程序
- SceneTree *g_tree;//场景
- View *g_view;//视图
- UIController *g_ui;//用户界面管理器
- GraphicController *g_gc;//图象管理器
- PlayerShip *g_pship;
- EnemyList *g_enemyList;
- MeshObject *bigship,*background;
- Camera *g_cam;//摄象机
- Point *g_lookat,*g_star ;
- Light *g_light;
- FILE *script=NULL;
- enum GAMESTATE{running,stop,end};
- GAMESTATE game_state;
- MusicPlayer *g_music;
- SoundPlayer *g_expsound;
- SoundPlayer *g_playershotsound;
- SoundPlayer *g_enemyshotsound;
- int points = 0;
- bool key[256];//是否按键
- void CALLBACK updateText(float);
- MEM_FILE image[10];//存储图形的
- void init()
- {
- game_state = running;
- //init
- g_view = g_app->GetView();
- g_tree = g_view->GetScene();
- g_ui = g_view->GetUIController();
- g_gc = g_view->GetGraphicController();
- //init sound
- g_music = new MusicPlayer("sound\6_05.mp3");
- g_music ->PlaySound ();
- g_expsound = new SoundPlayer("sound\02zd036.wav");
- g_playershotsound = new SoundPlayer("sound\02zd164.wav");
- g_enemyshotsound = new SoundPlayer("sound\25328_1072_157.wav");
- //init timer
- g_timer = new Timer(INTERNAL_CLOCK,1.f,1.f);
- g_timer->AddFunction(&updateText);
- //setbg
- g_gc->SetBgColor (0x00000000);
- g_gc->SetAmbient (0x00101022);
- g_gc->Apply();
- //set base
- Point pt("base");
- pt.SetVelocity(VECTOR3(0,0,100));
- g_tree->AddObject(&pt,(NodeObject*)NULL);
- //setlight
- Light light("light",LIG_DIRECTIONAL,0xffffffff,0xffffffff,0x00000000,5.f,
- VECTOR3(-100,100,0),VECTOR3(-10,10,0),10000.f);
- g_light = (Light*)g_tree->AddObject(&light,(NodeObject*)NULL);
- //set label
- g_ui->AddLabel(0,0,0,0,NULL,AL_NOCLIP,0,0xffaaaaaa,0x00000000,NULL);
- //background
- // background = (MeshObject*)g_tree->AddObject(&MeshObject("background",VECTOR3()),"base");
- // background->LoadMeshData("background.x");
- //bigship
- bigship = new MeshObject("bigship",VECTOR3(-70,-120,3800));
- bigship->LoadMeshData("bigship.x");
- bigship->SetVelocity(VECTOR3(0,0,-40));
- g_tree->AddObject(bigship,"base");
- //set player
- g_pship = new PlayerShip();
- //set enemy list
- g_enemyList = new EnemyList();
- //setup camera
- g_cam = new Camera("camera",false,true,VECTOR3(0.f,100.f,-80.f));//创建一个透视目标摄象机
- TransitionController::SetPosition(g_cam->GetLookAtPoint(),VECTOR3(0.f,10.f,0.f));
- g_cam = (Camera*)g_tree->AddObject (g_cam,"base");
- g_lookat = g_cam->GetLookAtPoint();
- g_tree->SetParent(g_lookat->GetName(),"base");
- g_view->SetCamera("camera");
- //star
- g_star = new Point("star","Star.png",3.f,true);
- script = fopen("script.txt","r");
- }
- void DoScript()
- {
- static int time = 0;
- char Entity[10];
- float posx,posy,posz;
- float vx,vy,vz;
- int bonus;
- if(g_timer->GetTime() >= time && game_state == running)
- {
- if(EOF!=fscanf(script,"%i: %s %f %f %f %f %f %f %i",&time,Entity,&posx,&posy,&posz,&vx,&vy,&vz,
- &bonus))
- {
- if(!strncmp(Entity,"Enemy1",strlen(Entity)))
- g_enemyList->AddEnemy(new Enemy1(VECTOR3(posx,posy,posz),VECTOR3(vx,vy,vz),bonus));
- }
- else
- game_state = end;
- }
- }
- void CALLBACK button_input(int id)
- {
- }
- void CALLBACK messagehandler(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
- {
- if(msg==WM_KEYDOWN)
- {
- key[wparam] = true;
- }else if(msg==WM_KEYUP)
- {
- key[wparam] = false;
- if(wparam==VK_ESCAPE)
- {
- fclose(script);
- g_music->ReleaseSoundDevice();
- g_expsound->ReleaseSoundDevice();
- g_playershotsound->ReleaseSoundDevice();
- g_enemyshotsound->ReleaseSoundDevice();
- g_app->Exit();
- }
- }
- }
- void CALLBACK updateText(float time)
- {
- VECTOR3 min,max;
- char *message = game_state==end?"Stage Clear!":"Fight!";
- g_pship->mesh ->GetBoundingBox(min,max);
- g_ui->SetText(0,"FPS:%f Shell: %i POINTS: %i Game State: %s",
- 1.f/g_timer->GetFrameTime(),g_pship->shell,points,message);
- }
- void CALLBACK mainloop()
- {
- //deal star
- //set starfield
- static int i = 0;
- static Cloud* last, *current , *forward;
- if(g_cam->GetWorldPos().z >= i*200.f)
- {
- if(i==0)
- {
- current=(Cloud*)g_tree->AddObject(&Cloud("starfield",g_star,400,
- VECTOR3(0,0,i*200+50),100,400,400),(NodeObject*)NULL);
- }
- forward=(Cloud*)g_tree->AddObject(&Cloud("starfield",g_star,400,
- VECTOR3(0,0,i*200+250),100,400,400),(NodeObject*)NULL);
- if(i!=0)
- {
- g_tree->DeleteObject(last);
- }
- last = current;
- current = forward;
- i++;
- }
- //doframes
- DoScript();
- g_pship->DoFrame();
- g_enemyList->DoFrame();
- //process input
- if(key[VK_SPACE])
- {
- g_pship->Fire();
- static float t0 = 0;
- if(g_timer->GetTime () - t0 >2.0f)
- {
- t0 = g_timer->GetTime ();
- }
- }
- if(key[VK_UP])
- {
- g_pship->MoveForward();
- }else if(key[VK_DOWN])
- {
- g_pship->MoveBackward();
- }else
- {
- g_pship->ZengineStop();
- }
- if(key[VK_LEFT])
- {
- g_pship->MoveLeft();
- }else if(key[VK_RIGHT])
- {
- g_pship->MoveRight();
- }else
- {
- g_pship->XengineStop();
- g_pship->RotateBack();
- }
- if(key['W'])
- {
- g_cam->SetVelocity(VECTOR3(0,20,0));
- }else if(key['S'])
- {
- g_cam->SetVelocity(VECTOR3(0,-20,0));
- }
- else
- {
- g_cam->SetVelocity(VECTOR3(0,0,0));
- }
- }
- int APIENTRY WinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPSTR lpCmdLine,
- int nCmdShow)
- {
- // TODO: Place code here.
- g_app=new CGLApplication();
- g_app->Create(hInstance,"StarWar",NULL,false,800,600);
- init();
- g_ui->SetButtonClickProc(&button_input);
- g_app->SetUserMsgProc(&messagehandler);
- g_app->SetPreRenderFunc(&mainloop);
- g_app->Show();
- g_app->Run();
- return 0;
- }