BlankGame.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // The general flow of a gane - server's creation, loading and hosting clients, and then destruction is as follows:
  2. // First, a client will always create a server in the event that they want to host a single player
  3. // game. Torque3D treats even single player connections as a soft multiplayer game, with some stuff
  4. // in the networking short-circuited to sidestep around lag and packet transmission times.
  5. // initServer() is called, loading the default server scripts.
  6. // After that, if this is a dedicated server session, initDedicated() is called, otherwise initClient is called
  7. // to prep a playable client session.
  8. // When a local game is started - a listen server - via calling StartGame() a server is created and then the client is
  9. // connected to it via createAndConnectToLocalServer().
  10. function BlankGame::create( %this )
  11. {
  12. exec("./scripts/guis/playGui.gui");
  13. datablock CameraData(Observer) {};
  14. // Create a local game server and connect to it.
  15. new SimGroup(ServerGroup);
  16. new GameConnection(ServerConnection);
  17. // This calls GameConnection::onConnect.
  18. ServerConnection.connectLocal();
  19. //-----------------------------------------------------------------------------
  20. // Add a material to give the ground some colour (even if it's just white).
  21. singleton Material(BlankWhite) {
  22. diffuseColor[0] = "White";
  23. };
  24. // Create objects!
  25. new SimGroup(MissionGroup)
  26. {
  27. new LevelInfo(TheLevelInfo)
  28. {
  29. canvasClearColor = "0 0 0";
  30. };
  31. new GroundPlane(TheGround)
  32. {
  33. position = "0 0 0";
  34. material = BlankWhite;
  35. };
  36. new Sun(TheSun)
  37. {
  38. azimuth = 230;
  39. elevation = 45;
  40. color = "1 1 1";
  41. ambient = "0.1 0.1 0.1";
  42. castShadows = true;
  43. };
  44. };
  45. new SimGroup(MissionCleanup);
  46. // Allow us to exit the game...
  47. GlobalActionMap.bind("keyboard", "escape", "quit");
  48. }
  49. function BlankGame::destroy( %this )
  50. {
  51. // Clean up ghosts.
  52. ServerConnection.delete();
  53. // Delete the objects we created.
  54. MissionGroup.delete();
  55. MissionCleanup.delete();
  56. // Delete server-side objects and datablocks.
  57. ServerGroup.delete();
  58. deleteDataBlocks();
  59. }
  60. //-----------------------------------------------------------------------------
  61. // Called when all datablocks have been transmitted.
  62. function GameConnection::onEnterGame(%client) {
  63. // Create a camera for the client.
  64. new Camera(TheCamera) {
  65. datablock = Observer;
  66. };
  67. TheCamera.setTransform("0 0 2 1 0 0 0");
  68. // Cameras are not ghosted (sent across the network) by default; we need to
  69. // do it manually for the client that owns the camera or things will go south
  70. // quickly.
  71. TheCamera.scopeToClient(%client);
  72. // And let the client control the camera.
  73. %client.setControlObject(TheCamera);
  74. // Add the camera to the group of game objects so that it's cleaned up when
  75. // we close the game.
  76. MissionGroup.add(TheCamera);
  77. LocalClientConnection.camera = TheCamera;
  78. // Activate HUD which allows us to see the game. This should technically be
  79. // a commandToClient, but since the client and server are on the same
  80. // machine...
  81. Canvas.setContent(PlayGui);
  82. activateDirectInput();
  83. }
  84. // Called when we connect to the local game.
  85. function GameConnection::onConnect(%this) {
  86. %this.transmitDataBlocks(0);
  87. }
  88. // Called when all datablocks from above have been transmitted.
  89. function GameConnection::onDataBlocksDone(%this) {
  90. closeSplashWindow();
  91. Canvas.showWindow();
  92. // Start sending ghosts to the client.
  93. %this.activateGhosting();
  94. %this.onEnterGame();
  95. }
  96. //stubs to keep the console clean. These aren't needed for such a lightweight implementation.
  97. function serverCmdsetClientAspectRatio(){}
  98. function onDataBlockObjectReceived(){}
  99. function onGhostAlwaysObjectReceived(){}
  100. function onGhostAlwaysStarted(){}
  101. function onGhostAlwaysObjectReceived(){}