public async void gameLoop() { DateTime oldTick = DateTime.UtcNow; DateTime newTick = DateTime.UtcNow; SimulationParams simulationParams = new SimulationParams(new TimeSpan(), new TimeSpan()); GalaxyBuilder galaxyBuilder = new GalaxyBuilder(); _collisionDetection = new QuadTreeCollision(); _pathing = new BFSearch(); IsPaused = false; while (true) { var file = ViewController.hasFile(); if (file?.Length > 0) { Map Galaxy = galaxyBuilder.buildGalaxy(loadGalaxy(file)); ModelController = new ModelController(Galaxy); simulationParams.TotalTime = TimeSpan.Zero; ContainsGalaxy = true; SimulationSpeed = 2; file = null; ViewController.resetFile(); ViewController.setSpeedText("X" + SimulationSpeed); } //simulationLoop while (ContainsGalaxy && ViewController.MainView.File == null) { newTick = DateTime.UtcNow; //TODO: check public and private everywhere (blijft hier tot dpa deadline //unit testing is 15% van het punt, voeg ze voor dpa toe //TODO: gebruik reflection factory voor behaviours inputControl(ViewController.getKeyPressed()); simulationParams.SetDelta(newTick - oldTick, SimulationSpeed); ModelController.runGameTick(simulationParams); //collision related code ModelController.CurMap.celestialBodies = _collisionDetection.Collide(ModelController.CurMap.celestialBodies); if (debugMode) //this should only be enabled for the algorithm assignment, but it is a cool showcase of the program { ViewController.bounds = _collisionDetection.GetBounds(); List <CelestialBody> sortedList = ModelController.CurMap.celestialBodies .OrderByDescending(x => x.Radius).ToList() .Where(n => n.Name != null).ToList(); ModelController.CurMap.celestialBodies = _pathing .GetPath( ModelController.CurMap.celestialBodies, sortedList[0], sortedList[1] ); } else { ViewController.bounds = null; ModelController.CurMap.celestialBodies.All(cb => { cb.IsMarked = false; return(true); }); } ViewController.drawFrame(ModelController.CurMap.celestialBodies); if (DateTime.UtcNow - newTick < MinTickTime) { await Task.Delay(MinTickTime); } oldTick = newTick; while (IsPaused && ViewController.MainView.File == null) //pause loop { await Task.Delay(MinTickTime); inputControl(ViewController.getKeyPressed()); oldTick = DateTime.UtcNow; } } await Task.Delay(MinTickTime); } }
private void inputControl(string key) { switch (key) { case "faster": if (SimulationSpeed < 1000) { SimulationSpeed = SimulationSpeed * 2; } ViewController.setSpeedText("X" + SimulationSpeed); break; case "slower": if (SimulationSpeed > 0.1) { SimulationSpeed = SimulationSpeed * 0.5; } ViewController.setSpeedText("X" + SimulationSpeed); break; case "pause": IsPaused = !IsPaused; break; case "back": ModelController.mapBackFiveSeconds(); break; case "toggleDebugMode": debugMode = !debugMode; break; case "add1": ModelController.newAstroids(1); break; case "add2": ModelController.newAstroids(2); break; case "add3": ModelController.newAstroids(3); break; case "switchCollision": if (_collisionDetection.GetType() == typeof(QuadTreeCollision)) { _collisionDetection = new NaiveCollision(); } else { _collisionDetection = new QuadTreeCollision(); } break; case "switchPathing": if (_pathing.GetType() == typeof(DijkstraSearch)) { _pathing = new BFSearch(); } else { _pathing = new DijkstraSearch(); } break; case "remove1": ModelController.removeAstroids(1); break; case "remove2": ModelController.removeAstroids(2); break; case "remove3": ModelController.removeAstroids(3); break; default: break; } }