Game.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /******************************************************************************/
  2. #include "stdafx.h"
  3. /******************************************************************************/
  4. State StateGame(UpdateGame, DrawGame, InitGame, ShutGame);
  5. Str GameWorld;
  6. bool DrawPhysics, DrawWorldPath;
  7. VIEW_MODE ViewMode=VIEW_TPP;
  8. Memx<Game::ObjMap<Game::Static > > Statics;
  9. Memx<Game::ObjMap<Game::ObjLightPoint> > PointLights;
  10. Memx<Game::ObjMap<Game::ObjLightCone > > ConeLights;
  11. Memx<Game::ObjMap<Game::ObjParticles > > ObjParticles;
  12. Memx<Game::ObjMap<Player > > Players;
  13. /******************************************************************************/
  14. void StartGame(Elm &elm)
  15. {
  16. GameWorld=EncodeFileName(elm.id);
  17. StateGame.set(StateFadeTime);
  18. }
  19. void ExitGame(ptr)
  20. {
  21. SetProjectState();
  22. }
  23. /******************************************************************************/
  24. void InitGameObjContainers()
  25. {
  26. // set object type enum
  27. Memt<Enum::Elm> obj_types; REPA(Proj.existing_obj_classes)if(Elm *elm=Proj.findElm(Proj.existing_obj_classes[i]))obj_types.New().set(NameToEnum(elm->name), elm->id);
  28. ObjType.create("OBJ_TYPE", obj_types);
  29. // set game object containers
  30. //Statics.del(); PointLights.del(); ConeLights.del(); ObjParticles.del(); Players.del(); don't delete those objects because Game.World obj containers may point to some of them, instead, just create new ones if needed
  31. int statics=0, point_lights=0, cone_lights=0, particles=0, players=0;
  32. FREPA(ObjType)
  33. {
  34. EditObjectPtr obj_class=Proj.editPath(ObjType[i].id);
  35. // must set object container casted directly target type and not Game.Obj because CType will not be detected properly
  36. if(obj_class->editor_type==EditObjType.elmID(EDIT_OBJ_LIGHT_POINT))Game::World.setObjType(GetObjContainer( PointLights, point_lights), i);else
  37. if(obj_class->editor_type==EditObjType.elmID(EDIT_OBJ_LIGHT_CONE ))Game::World.setObjType(GetObjContainer( ConeLights, cone_lights), i);else
  38. if(obj_class->editor_type==EditObjType.elmID(EDIT_OBJ_PARTICLES ))Game::World.setObjType(GetObjContainer(ObjParticles, particles), i);else
  39. Game::World.setObjType(GetObjContainer( Statics, statics), i);
  40. }
  41. }
  42. bool InitGame()
  43. {
  44. SetKbExclusive();
  45. Proj.pause();
  46. VidOpt.hideAll();
  47. AppStore.hide();
  48. DataPath(Proj.game_path);
  49. InitGameButtons();
  50. InitGameObjContainers();
  51. Cam=ActiveCam;
  52. Game::World.activeRange(D.viewRange(300).viewRange()).mode(Game::WORLD_STREAM);
  53. if(!Game::World.NewTry(GameWorld)){Gui.msgBox(S, "Can't load world"); ExitGame();}
  54. if(Game::World.settings().environment)Game::World.settings().environment->set();else DefaultEnvironment.set();
  55. DrawPhysics=DrawWorldPath=false;
  56. return true;
  57. }
  58. void ShutGame()
  59. {
  60. Game::World.del(); // manually delete the world to unload all the memory before switching back to the world editing
  61. Ms.show().clip(null, 0); // restore cursor visibility and disable any clipping
  62. ShutGameButtons();
  63. DataPath(S);
  64. Proj.resume();
  65. }
  66. /******************************************************************************/
  67. void UpdateGameCam()
  68. {
  69. Cam.at.y+=DPadY.dir*Time.d()*Cam.dist;
  70. if(DPad.touched)
  71. {
  72. Vec2 d; CosSin(d.x, d.y, DPad.angle+Cam.yaw);
  73. Cam.at.x+=d.x*Time.d()*Cam.dist;
  74. Cam.at.z+=d.y*Time.d()*Cam.dist;
  75. }
  76. Cam.yaw -=Rot.delta.x;
  77. Cam.pitch+=Rot.delta.y;
  78. Cam.dist *=ScaleFactor(Zoom.zoom);
  79. Cam.transformByMouse(0.1f, 1000, CAMH_ZOOM | (((Ms.b(0) || Ms.b(3)) && (!Gui.ms() || Gui.ms()==Gui.desktop())) ? CAMH_MOVE_XZ : (Ms.b(1) ? CAMH_MOVE : CAMH_ROT)));
  80. }
  81. bool UpdateGame()
  82. {
  83. if(Kb.bp(KB_ESC) || Kb.bp(KB_NAV_BACK))ExitGame();
  84. Server.update(null, true);
  85. DrawPhysics ^=(Kb.alt() && Kb.shift() && !Kb.ctrlCmd() && Kb.bp(KB_B));
  86. DrawWorldPath^=(Kb.alt() && Kb.shift() && !Kb.ctrlCmd() && Kb.bp(KB_P));
  87. if(Kb.bp(KB_LBR)){flt range=Mid(D.viewRange()*0.8f, 32.0f, Demo ? MaxGameViewRangeDemo : MaxGameViewRange); D.viewRange(range); Game::World.activeRange(range);}
  88. if(Kb.bp(KB_RBR)){flt range=Mid(D.viewRange()/0.8f, 32.0f, Demo ? MaxGameViewRangeDemo : MaxGameViewRange); D.viewRange(range); Game::World.activeRange(range);}
  89. UpdateGameCam();
  90. Game::World.update(ActiveCam.matrix.pos);
  91. Clouds.layered.update();
  92. Water.update(0.02f);
  93. Ms.visible(Players.elms() && ViewMode==VIEW_ISO);
  94. Ms.clip (null, Ms.hidden());
  95. if(Ms.hidden())Ms.freeze();
  96. Gui.update();
  97. return true;
  98. }
  99. /******************************************************************************/
  100. void RenderGame()
  101. {
  102. Game::World.draw();
  103. }
  104. void DrawGame()
  105. {
  106. Renderer.wire=Kb.b(KB_TILDE);
  107. SHADOW_MODE shd_mode=D.shadowMode(); if(Demo)D.shadowMode(SHADOW_NONE);
  108. Renderer(RenderGame);
  109. D.shadowMode(shd_mode);
  110. Renderer.wire=false;
  111. if(DrawPhysics || DrawWorldPath)
  112. {
  113. Renderer.setDepthForDebugDrawing();
  114. if(DrawPhysics )Physics.draw();
  115. if(DrawWorldPath)Game::World.path().draw(64, 0.5f);
  116. }
  117. Gui.draw();
  118. }
  119. /******************************************************************************/
  120. /******************************************************************************/