StarWar.cpp
资源名称:g.rar [点击查看]
上传用户:laitongbao
上传日期:2021-02-20
资源大小:8176k
文件大小:6k
源码类别:

射击游戏

开发平台:

Visual C++

  1. // StarWar.cpp : Defines the entry point for the application.
  2. //
  3. #include "stdafx.h"
  4. #include "includeCGL.h"
  5. #include "Playership.h"
  6. #include "MachineGun.h"
  7. #include "LaserGun.h"
  8. #include "Enemy.h"
  9. #include "EnemyList.h"
  10. #include "stdio.h"
  11. #pragma comment(lib, "cgllib.lib")
  12. //全局变量
  13. Timer             *g_timer;
  14. CGLApplication    *g_app;//程序
  15. SceneTree         *g_tree;//场景
  16. View              *g_view;//视图
  17. UIController      *g_ui;//用户界面管理器
  18. GraphicController *g_gc;//图象管理器
  19. PlayerShip        *g_pship;
  20. EnemyList         *g_enemyList;
  21. MeshObject        *bigship,*background;
  22. Camera            *g_cam;//摄象机
  23. Point             *g_lookat,*g_star ;
  24. Light             *g_light;
  25. FILE              *script=NULL;
  26. enum GAMESTATE{running,stop,end};
  27. GAMESTATE         game_state;
  28. MusicPlayer       *g_music;
  29. SoundPlayer       *g_expsound;
  30. SoundPlayer       *g_playershotsound;
  31. SoundPlayer       *g_enemyshotsound;
  32. int               points = 0;
  33. bool key[256];//是否按键
  34. void CALLBACK updateText(float);
  35. MEM_FILE image[10];//存储图形的
  36. void init()
  37. {
  38. game_state = running;
  39. //init
  40. g_view = g_app->GetView();
  41. g_tree = g_view->GetScene();
  42. g_ui = g_view->GetUIController();
  43. g_gc = g_view->GetGraphicController();
  44. //init sound
  45. g_music = new MusicPlayer("sound\6_05.mp3");
  46. g_music ->PlaySound ();
  47. g_expsound = new SoundPlayer("sound\02zd036.wav");
  48. g_playershotsound = new SoundPlayer("sound\02zd164.wav");
  49. g_enemyshotsound = new SoundPlayer("sound\25328_1072_157.wav");
  50. //init timer
  51. g_timer = new Timer(INTERNAL_CLOCK,1.f,1.f);
  52. g_timer->AddFunction(&updateText);
  53. //setbg
  54. g_gc->SetBgColor (0x00000000);
  55. g_gc->SetAmbient (0x00101022);
  56. g_gc->Apply();
  57. //set base
  58. Point pt("base");
  59. pt.SetVelocity(VECTOR3(0,0,100));
  60. g_tree->AddObject(&pt,(NodeObject*)NULL);
  61. //setlight
  62. Light light("light",LIG_DIRECTIONAL,0xffffffff,0xffffffff,0x00000000,5.f,
  63.   VECTOR3(-100,100,0),VECTOR3(-10,10,0),10000.f);
  64. g_light = (Light*)g_tree->AddObject(&light,(NodeObject*)NULL);
  65. //set label
  66. g_ui->AddLabel(0,0,0,0,NULL,AL_NOCLIP,0,0xffaaaaaa,0x00000000,NULL);
  67. //background
  68. // background = (MeshObject*)g_tree->AddObject(&MeshObject("background",VECTOR3()),"base");
  69. // background->LoadMeshData("background.x");
  70. //bigship
  71. bigship = new MeshObject("bigship",VECTOR3(-70,-120,3800));
  72. bigship->LoadMeshData("bigship.x");
  73. bigship->SetVelocity(VECTOR3(0,0,-40));
  74. g_tree->AddObject(bigship,"base");
  75. //set player
  76. g_pship = new PlayerShip();
  77. //set enemy list
  78. g_enemyList = new EnemyList();
  79. //setup camera
  80. g_cam = new Camera("camera",false,true,VECTOR3(0.f,100.f,-80.f));//创建一个透视目标摄象机
  81. TransitionController::SetPosition(g_cam->GetLookAtPoint(),VECTOR3(0.f,10.f,0.f));
  82. g_cam = (Camera*)g_tree->AddObject (g_cam,"base");
  83. g_lookat = g_cam->GetLookAtPoint();
  84. g_tree->SetParent(g_lookat->GetName(),"base");
  85. g_view->SetCamera("camera");
  86. //star
  87. g_star = new Point("star","Star.png",3.f,true);
  88. script = fopen("script.txt","r");
  89. }
  90. void DoScript()
  91. {
  92. static int time = 0;
  93. char Entity[10];
  94. float posx,posy,posz;
  95. float vx,vy,vz;
  96. int bonus;
  97. if(g_timer->GetTime() >= time && game_state == running)
  98. {
  99. if(EOF!=fscanf(script,"%i: %s %f %f %f %f %f %f %i",&time,Entity,&posx,&posy,&posz,&vx,&vy,&vz,
  100. &bonus))
  101. {
  102. if(!strncmp(Entity,"Enemy1",strlen(Entity)))
  103. g_enemyList->AddEnemy(new Enemy1(VECTOR3(posx,posy,posz),VECTOR3(vx,vy,vz),bonus));
  104. }
  105. else 
  106. game_state = end;
  107. }
  108. }
  109. void CALLBACK  button_input(int id)
  110. {
  111. }
  112. void CALLBACK messagehandler(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
  113. {
  114. if(msg==WM_KEYDOWN)
  115. {
  116. key[wparam] = true;
  117. }else if(msg==WM_KEYUP)
  118. {
  119. key[wparam] = false;
  120. if(wparam==VK_ESCAPE)
  121. {
  122. fclose(script);
  123. g_music->ReleaseSoundDevice();
  124. g_expsound->ReleaseSoundDevice();
  125. g_playershotsound->ReleaseSoundDevice();
  126. g_enemyshotsound->ReleaseSoundDevice();
  127. g_app->Exit();
  128. }
  129. }
  130. }
  131. void CALLBACK updateText(float time)
  132. {
  133. VECTOR3 min,max;
  134. char *message = game_state==end?"Stage Clear!":"Fight!";
  135. g_pship->mesh ->GetBoundingBox(min,max);
  136. g_ui->SetText(0,"FPS:%f Shell: %i             POINTS: %i            Game State: %s",
  137. 1.f/g_timer->GetFrameTime(),g_pship->shell,points,message);
  138. }
  139. void CALLBACK mainloop()
  140. {
  141. //deal star
  142. //set starfield
  143. static int i = 0;
  144. static Cloud* last, *current , *forward; 
  145. if(g_cam->GetWorldPos().z >= i*200.f)
  146. {
  147. if(i==0)
  148. {
  149. current=(Cloud*)g_tree->AddObject(&Cloud("starfield",g_star,400,
  150. VECTOR3(0,0,i*200+50),100,400,400),(NodeObject*)NULL);
  151. }
  152. forward=(Cloud*)g_tree->AddObject(&Cloud("starfield",g_star,400,
  153. VECTOR3(0,0,i*200+250),100,400,400),(NodeObject*)NULL);
  154. if(i!=0)
  155. {
  156. g_tree->DeleteObject(last);
  157. }
  158. last = current;
  159. current = forward;
  160. i++;
  161. }
  162. //doframes
  163. DoScript();
  164. g_pship->DoFrame();
  165. g_enemyList->DoFrame();
  166. //process input
  167. if(key[VK_SPACE])
  168. {
  169. g_pship->Fire();
  170. static float t0 = 0;
  171. if(g_timer->GetTime () - t0 >2.0f)
  172. {
  173. t0 = g_timer->GetTime ();
  174. }
  175. }
  176. if(key[VK_UP])
  177. {
  178. g_pship->MoveForward();
  179. }else if(key[VK_DOWN])
  180. {
  181. g_pship->MoveBackward();
  182. }else
  183. {
  184. g_pship->ZengineStop();
  185. }
  186. if(key[VK_LEFT])
  187. {
  188. g_pship->MoveLeft();
  189. }else if(key[VK_RIGHT])
  190. {
  191. g_pship->MoveRight();
  192. }else 
  193. {
  194. g_pship->XengineStop();
  195. g_pship->RotateBack();
  196. }
  197. if(key['W'])
  198. {
  199. g_cam->SetVelocity(VECTOR3(0,20,0));
  200. }else if(key['S'])
  201. {
  202. g_cam->SetVelocity(VECTOR3(0,-20,0));
  203. }
  204. else
  205. {
  206. g_cam->SetVelocity(VECTOR3(0,0,0));
  207. }
  208. }
  209. int APIENTRY WinMain(HINSTANCE hInstance,
  210.                      HINSTANCE hPrevInstance,
  211.                      LPSTR     lpCmdLine,
  212.                      int       nCmdShow)
  213. {
  214.   // TODO: Place code here.
  215. g_app=new CGLApplication();
  216. g_app->Create(hInstance,"StarWar",NULL,false,800,600);
  217. init();
  218. g_ui->SetButtonClickProc(&button_input);
  219. g_app->SetUserMsgProc(&messagehandler);
  220. g_app->SetPreRenderFunc(&mainloop);
  221. g_app->Show();
  222. g_app->Run();
  223. return 0;
  224. }