void FixedUpdate() { if (scene.Ball != null) { var x = Input.GetAxis("Horizontal"); var y = Input.GetAxis("Jump"); var z = Input.GetAxis("Vertical"); var command = new KickBallCommand(x, y, z); simulator.ReceiveCommand(scene.Ball, command); if (Input.GetKeyUp(KeyCode.F)) { var dataDir = Path.Combine(Application.dataPath, "../Data"); Directory.CreateDirectory(dataDir); var statsCsvPath = Path.Combine(dataDir, string.Format("Stats-{0}.csv", DateTime.Now.ToString("yyyyMMdd-HHmmss"))); simulator.SaveStatsFrame(statsCsvPath); logger.Info("StatsCsv saved to {0}", statsCsvPath); } } client.Update(); foreach (var actor in scene.Actors) { if (!actors.ContainsKey(actor.Desc.Id)) { var actorName = actor.GetType().Name; var go = ObjectPool.Instantiate(actorName, transform); if (go == null) { continue; } if (actorName == "Ball") { var camera = GameObject.Find("Main Camera").GetComponent <CameraFollower>(); camera.followTarget = go.transform; logger.Info("Set camera to {0}", go.name); } actors.Add(actor.Desc.Id, RigidBodyInterpolate.Initialize(go, (RigidBodyActor)actor)); } } }
public static void Run() { var elapsedTime = 0; var serverSimulator = new ServerSimulator(); RegisterActorFactories(serverSimulator); var serverSceneDesc = new ExampleSceneDesc(50, 50); var serverScene = serverSimulator.CreateScene <ExampleScene>(serverSceneDesc); var server = new ReliableUdpServer(serverSimulator, 9000); server.Start(); var serverNextTime = 0; var serverAddress = new IPEndPoint(IPAddress.Loopback, 9000); var clientId = new ClientId(1); var clientSimulator = new ClientSimulator(clientId); RegisterActorFactories(clientSimulator); var clientSceneDesc = new ExampleSceneDesc(20, 50); var clientScene = clientSimulator.CreateScene <ExampleScene>(clientSceneDesc); var client = new ReliableUdpClient(clientSimulator, serverAddress); var clientNextTime = 60; var clientRunning = false; while (true) { var key = Console.ReadKey(); switch (key.Key) { case ConsoleKey.Spacebar: elapsedTime += 10; logger.Info("Time elapsed {0}ms", elapsedTime); if (serverNextTime <= elapsedTime) { logger.Info("++++ Begin server update at {0}ms", serverNextTime); server.Update(); PrintActorSnapshots(serverScene, serverSimulator.Time); logger.Info("++++ End server update at {0}ms", serverSimulator.Time); serverNextTime += 50; } if (clientNextTime <= elapsedTime) { if (clientNextTime == 60) { client.Start(); logger.Info("Client started at {0}ms", clientNextTime); clientNextTime += 20; } else { logger.Info("---- Begin Client update at {0}ms", clientNextTime); client.Update(); PrintActorSnapshots(clientScene, clientSimulator.Time); clientRunning = true; logger.Info("---- End Client update at {0}ms", clientSimulator.Time); clientNextTime += 20; } } else if (clientRunning) { logger.Info("---- Begin Client interpolate update at {0}ms", clientNextTime); PrintActorSnapshots(clientScene, clientSimulator.Time); logger.Info("---- End Client interpolate update at {0}ms", clientSimulator.Time); } break; case ConsoleKey.Escape: goto End; case ConsoleKey.LeftArrow: if (clientSimulator != null && clientScene.Ball != null) { clientSimulator.ReceiveCommand(clientScene.Ball, new KickBallCommand(x: -1)); } break; case ConsoleKey.UpArrow: if (clientSimulator != null && clientScene.Ball != null) { clientSimulator.ReceiveCommand(clientScene.Ball, new KickBallCommand(z: 1)); } break; case ConsoleKey.RightArrow: if (clientSimulator != null && clientScene.Ball != null) { clientSimulator.ReceiveCommand(clientScene.Ball, new KickBallCommand(x: 1)); } break; case ConsoleKey.DownArrow: if (clientSimulator != null && clientScene.Ball != null) { clientSimulator.ReceiveCommand(clientScene.Ball, new KickBallCommand(z: -1)); } break; default: logger.Warn("Unsupported input: {0}", key.Key); break; } } End: server.Stop(); server.Dispose(); client.Stop(); client.Dispose(); }