public void GameLoop() { while (win.IsRunning()) { gameTimer.MeasureTime(); while (gameTimer.ShouldUpdate()) { win.PollEvents(); SpaceTaxiBus.GetBus().ProcessEvents(); stateMachine.ActiveState.UpdateGameLogic(); } if (gameTimer.ShouldRender()) { win.Clear(); stateMachine.ActiveState.RenderState(); win.SwapBuffers(); } if (gameTimer.ShouldReset()) { // 1 second has passed - display last captured ups and fps from the timer win.Title = "Space Taxi | UPS: " + gameTimer.CapturedUpdates + ", FPS: " + gameTimer.CapturedFrames; } } }
public Game() { // window win = new Window("Space Taxi Game v0.1", 500, AspectRatio.R1X1); // event bus eventBus = new GameEventBus <object>(); SpaceTaxiBus.GetBus().InitializeEventBus(new List <GameEventType>() { GameEventType.InputEvent, GameEventType.WindowEvent, GameEventType.GameStateEvent, GameEventType.PlayerEvent }); win.RegisterEventBus(SpaceTaxiBus.GetBus()); // game timer gameTimer = new GameTimer(60); // 60 UPS, no FPS limit // game assets backGroundImage = new Entity( new StationaryShape(new Vec2F(0.0f, 0.0f), new Vec2F(1.0f, 1.0f)), new Image(Path.Combine("Assets", "Images", "SpaceBackground.png")) ); backGroundImage.RenderEntity(); // event delegation SpaceTaxiBus.GetBus().Subscribe(GameEventType.InputEvent, this); SpaceTaxiBus.GetBus().Subscribe(GameEventType.WindowEvent, this); SpaceTaxiBus.GetBus().Subscribe(GameEventType.GameStateEvent, this); //make level stateMachine = new StateMachine(this); }
public Game() { // window win = new Window("Space Taxi Game", 500, AspectRatio.R1X1); win.RegisterEventBus(SpaceTaxiBus.GetBus()); //statemachine stateMachine = new StateMachine(); SpaceTaxiBus.GetBus().InitializeEventBus(new List <GameEventType>() { GameEventType.WindowEvent, GameEventType.InputEvent, GameEventType.GameStateEvent, GameEventType.PlayerEvent }); SpaceTaxiBus.GetBus().Subscribe(GameEventType.WindowEvent, this); SpaceTaxiBus.GetBus().Subscribe(GameEventType.PlayerEvent, this); SpaceTaxiBus.GetBus().Subscribe(GameEventType.GameStateEvent, this); SpaceTaxiBus.GetBus().Subscribe(GameEventType.InputEvent, this); // game timer gameTimer = new GameTimer(60, 60); // 60 UPS, 60 FPS limit }
public StateMachine(Game game) { SpaceTaxiBus.GetBus().Subscribe(GameEventType.GameStateEvent, this); SpaceTaxiBus.GetBus().Subscribe(GameEventType.InputEvent, this); this.game = game; ActiveState = MainMenu.GetInstance(); }
public void RenderState() { player.RenderPlayer(); explosions.RenderAnimations(); if (!isOnPlatform) { currentVelocity = (gravity + player.thrust) * game.gameTimer.CapturedUpdates + currentVelocity; player.Entity.Shape.AsDynamicShape().ChangeDirection(new Vec2F(currentVelocity.X, currentVelocity.Y)); } if (!isOnPlatform) { player.Entity.Shape.Move(currentVelocity); } foreach (var obstacle in currentLevel.obstacles) { obstacle.RenderEntity(); } singletonScore.RenderScore(); if (first) { stopwatch = Stopwatch.StartNew(); first = false; } if (customer != null) { if (singletonTimer.stopwatch.Elapsed.Seconds > customer.droptime && customer != null) { singletonTimer.stopwatch.Reset(); ChoseLevel.GetInstance().Customer = null; //END GAME singletonScore.PointChanger("Reset"); SpaceTaxiBus.GetBus().RegisterEvent( GameEventFactory <object> .CreateGameEventForAllProcessors( GameEventType.GameStateEvent, this, "CHANGE_STATE", "GAME_OVER", "")); } } //renders customers in current level foreach (var cus in currentLevel.cusList) { if (stopwatch.Elapsed.Seconds + (stopwatch.Elapsed.Minutes * 60) >= cus.spawntime) { if (!cus.entity.IsDeleted()) { cus.RenderCustomer(); } } } }
public Game() { // window _win = new Window("Space Taxi Game v0.1", 1000, AspectRatio.R4X3); // event bus _eventBus = SpaceTaxiBus.GetBus(); _eventBus.InitializeEventBus(new List <GameEventType>() { GameEventType.GameStateEvent, // control state event GameEventType.InputEvent, // key press / key release GameEventType.WindowEvent, // messages to the window, e.g. CloseWindow() GameEventType.PlayerEvent // commands issued to the player object, e.g. move, destroy, receive health, etc. }); _win.RegisterEventBus(_eventBus); // game timer Game.ScreenTimer = SpaceTimer.GetTimer; // game assets _backGroundImage = new Entity( new StationaryShape(new Vec2F(0.0f, 0.0f), new Vec2F(1.0f, 1.0f)), new Image(Path.Combine("Assets", "Images", "SpaceBackground.png")) ); _backGroundImage.RenderEntity(); // game entities _taxi = Player.GetInstance(); // event delegation _eventBus.Subscribe(GameEventType.WindowEvent, this); _eventBus.Subscribe(GameEventType.PlayerEvent, _taxi); StateMachine = new StateMachine(); }
public void InitializeGameState() { CreateLevel(ChoseLevel.GetInstance().filename); explosionStrides = ImageStride.CreateStrides(8, Path.Combine("Assets", "Images", "Explosion.png")); explosions = new AnimationContainer(5); currentVelocity = new Vec2F(-0.000001f, 0f); foreach (var customer in currentLevel.cusList) { foreach (var obstacle in currentLevel.obstacles) { if (obstacle.symbol.ToString().Equals(customer.spawnplatform)) { spawnPlatform = obstacle; customer.entity.Shape.Position = new Vec2F(obstacle.shape.Position.X, obstacle.shape.Position.Y + 0.05f); break; } } } player = new Player(); player.SetPosition(currentLevel.spawnPos.X, currentLevel.spawnPos.Y); player.SetExtent(ChoseLevel.GetInstance().extX, ChoseLevel.GetInstance().extY); SpaceTaxiBus.GetBus().Subscribe(GameEventType.PlayerEvent, player); SpaceTaxiBus.GetBus().Subscribe(GameEventType.PlayerEvent, player); singletonTimer = SingletonTimer.Instance; singletonScore = SingletonScore.Instance; collisiondatas = new CollisionData[currentLevel.cusList.Count]; for (int i = 0; i < currentLevel.cusList.Count; i++) { collisiondatas[i] = CollisionDetection.Aabb(player.Entity.Shape.AsDynamicShape(), currentLevel.cusList[i].entity.Shape); } }
public void HandleKeyEvent(string keyValue, string keyAction) { switch (keyAction) { case "KEY_PRESS": switch (keyValue) { case "KEY_UP": if (activeMenuButton == 2) { menuButtons[1] = new Text("Main Menu", new Vec2F(0.35f, 0.1f), new Vec2F(0.5f, 0.4f)); menuButtons[2] = new Text("Quit", new Vec2F(0.35f, 0.0f), new Vec2F(0.4f, 0.4f)); menuButtons[1].SetColor(Color.Red); menuButtons[2].SetColor(Color.DarkRed); activeMenuButton = 1; menuButtons[0].RenderText(); menuButtons[1].RenderText(); menuButtons[2].RenderText(); } break; case "KEY_DOWN": if (activeMenuButton == 1) { menuButtons[1] = new Text("Main Menu", new Vec2F(0.35f, 0.1f), new Vec2F(0.4f, 0.4f)); menuButtons[2] = new Text("Quit", new Vec2F(0.35f, 0.0f), new Vec2F(0.5f, 0.4f)); menuButtons[1].SetColor(Color.DarkRed); menuButtons[2].SetColor(Color.Red); activeMenuButton = 2; menuButtons[0].RenderText(); menuButtons[1].RenderText(); menuButtons[2].RenderText(); } break; case "KEY_ENTER": switch (activeMenuButton) { case 1: SpaceTaxiBus.GetBus().RegisterEvent( GameEventFactory <object> .CreateGameEventForAllProcessors( GameEventType.GameStateEvent, this, "CHANGE_STATE", "MAIN_MENU", "")); break; case 2: // quit SpaceTaxiBus.GetBus().RegisterEvent( GameEventFactory <object> .CreateGameEventForAllProcessors( GameEventType.WindowEvent, this, "CLOSE_WINDOW", "", "")); break; } break; case "KEY_ESCAPE": SpaceTaxiBus.GetBus().RegisterEvent( GameEventFactory <object> .CreateGameEventForAllProcessors( GameEventType.WindowEvent, this, "CLOSE_WINDOW", "", "")); break; } break; case "KEY_RELEASE": break; } }
public void HandleKeyEvent(string keyValue, string keyAction) { switch (keyAction) { case "KEY_PRESS": switch (keyValue) { case "KEY_ESCAPE": SpaceTaxiBus.GetBus().RegisterEvent( GameEventFactory <object> .CreateGameEventForAllProcessors( GameEventType.GameStateEvent, this, "CHANGE_STATE", "GAME_PAUSED", "")); break; case "KEY_UP": startup = true; isOnPlatform = false; SpaceTaxiBus.GetBus().RegisterEvent( GameEventFactory <object> .CreateGameEventForSpecificProcessor( GameEventType.PlayerEvent, this, player, "BOOSTER_UPWARDS", "", "")); break; case "KEY_LEFT": SpaceTaxiBus.GetBus().RegisterEvent( GameEventFactory <object> .CreateGameEventForSpecificProcessor( GameEventType.PlayerEvent, this, player, "BOOSTER_TO_LEFT", "", "")); break; case "KEY_RIGHT": SpaceTaxiBus.GetBus().RegisterEvent( GameEventFactory <object> .CreateGameEventForSpecificProcessor( GameEventType.PlayerEvent, this, player, "BOOSTER_TO_RIGHT", "", "")); break; default: break; } break; case "KEY_RELEASE": switch (keyValue) { case "KEY_LEFT": SpaceTaxiBus.GetBus().RegisterEvent( GameEventFactory <object> .CreateGameEventForSpecificProcessor( GameEventType.PlayerEvent, this, player, "STOP_ACCELERATE_LEFT", "", "")); break; case "KEY_RIGHT": SpaceTaxiBus.GetBus().RegisterEvent( GameEventFactory <object> .CreateGameEventForSpecificProcessor( GameEventType.PlayerEvent, this, player, "STOP_ACCELERATE_RIGHT", "", "")); break; case "KEY_UP": SpaceTaxiBus.GetBus().RegisterEvent( GameEventFactory <object> .CreateGameEventForSpecificProcessor( GameEventType.PlayerEvent, this, player, "STOP_ACCELERATE_UP", "", "")); break; } break; } }
public void UpdateGameLogic() { if (singletonScore.score == 300) { SpaceTaxiBus.GetBus().RegisterEvent( GameEventFactory <object> .CreateGameEventForAllProcessors( GameEventType.GameStateEvent, this, "CHANGE_STATE", "GAME_WON", "")); } for (int i = 0; i < currentLevel.cusList.Count; i++) { collisiondatas[i] = CollisionDetection.Aabb(player.Entity.Shape.AsDynamicShape(), currentLevel.cusList[i].entity.Shape); } for (int i = 0; i < currentLevel.cusList.Count; i++) { if (collisiondatas[i].Collision) { //if no customer on board then delete (pickup) customer if (customer == null) { currentLevel.cusList[i].entity.DeleteEntity(); customer = currentLevel.cusList[i]; singletonTimer.stopwatch.Start(); } } } foreach (var obstacle in currentLevel.obstacles) { var collisionData = CollisionDetection.Aabb(player.Entity.Shape.AsDynamicShape(), obstacle.Shape); if (collisionData.Collision) { if (obstacle.fileName.Equals(GetPlatformName()[0]) || obstacle.fileName.Equals(GetPlatformName()[1]) || obstacle.fileName.Equals(GetPlatformName()[2]) || obstacle.fileName.Equals(GetPlatformName()[3])) { // lands if (customer != null) { if (obstacle.symbol.ToString().Equals(customer.landplatform) || (customer.landplatform == "^" && currentLevel.levelName == "short-n-sweet.txt")) { singletonScore.PointChanger("Add"); singletonTimer.stopwatch.Reset(); customer = null; /* if (singletonScore.score == 300) { * SpaceTaxiBus.GetBus().RegisterEvent( * GameEventFactory<object>.CreateGameEventForAllProcessors( * GameEventType.GameStateEvent, * this, * "CHANGE_STATE", * "GAME_WON", "")); * } */ } } if (currentVelocity.Y < -0.0001f && currentVelocity.Y > -0.0075f) { isOnPlatform = true; currentVelocity.Y = 0; currentVelocity.X = 0; } } else { singletonTimer.stopwatch.Reset(); ChoseLevel.GetInstance().Customer = null; singletonScore.PointChanger("Reset"); AddExplosion(player.shape.Position.X, player.shape.Position.Y, obstacle.shape.Extent.X + 0.1f, obstacle.shape.Extent.Y + 0.1f); SpaceTaxiBus.GetBus().RegisterEvent( GameEventFactory <object> .CreateGameEventForAllProcessors( GameEventType.GameStateEvent, this, "CHANGE_STATE", "GAME_OVER", "")); } } else { if (player.shape.Position.Y > 1) { // if customer has been picked up and has to be dropped off at next level if (customer == null) { singletonTimer.stopwatch.Reset(); ChoseLevel.GetInstance().Customer = null; singletonScore.PointChanger("Reset"); //END GAME SpaceTaxiBus.GetBus().RegisterEvent( GameEventFactory <object> .CreateGameEventForAllProcessors( GameEventType.GameStateEvent, this, "CHANGE_STATE", "GAME_OVER", "")); } else { if (customer.entity.IsDeleted()) { //change customer.platformname to remove ^ if (customer.landplatform.Contains("^")) { if (customer.landplatform.Length > 1) { customer.landplatform = customer.landplatform.Substring(1, 1); } } ChoseLevel.GetInstance().Customer = customer; } currentVelocity.Y = 0; currentVelocity.X = 0; isOnPlatform = true; GameRunning.instance = null; if (currentLevel.levelName.Equals("short-n-sweet.txt")) { ChoseLevel.GetInstance().filename = "the-beach.txt"; ChoseLevel.GetInstance().posX = 0.25f; ChoseLevel.GetInstance().posY = 0.20f; } else { ChoseLevel.GetInstance().filename = "short-n-sweet.txt"; ChoseLevel.GetInstance().posX = 0.45f; ChoseLevel.GetInstance().posY = 0.15f; } SpaceTaxiBus.GetBus().RegisterEvent( GameEventFactory <object> .CreateGameEventForAllProcessors( GameEventType.GameStateEvent, this, "CHANGE_STATE", "GAME_RUNNING", "")); } } } } }
public void HandleKeyEvent(string keyValue, string keyAction) { switch (keyAction) { case "KEY_PRESS": switch (keyValue) { case "KEY_UP": if (activeMenuButton == 1) { menuButtons[1] = new Text("Main Menu", new Vec2F(0.35f, 0.1f), new Vec2F(0.4f, 0.4f)); menuButtons[0] = new Text("Continue", new Vec2F(0.35f, 0.2f), new Vec2F(0.5f, 0.4f)); menuButtons[0].SetColor(Color.Red); menuButtons[1].SetColor(Color.DarkRed); activeMenuButton = 0; menuButtons[0].RenderText(); menuButtons[1].RenderText(); } break; case "KEY_DOWN": if (activeMenuButton == 0) { menuButtons[0] = new Text("Continue", new Vec2F(0.35f, 0.2f), new Vec2F(0.4f, 0.4f)); menuButtons[1] = new Text("Main Menu", new Vec2F(0.35f, 0.1f), new Vec2F(0.5f, 0.4f)); menuButtons[0].SetColor(Color.DarkRed); menuButtons[1].SetColor(Color.Red); activeMenuButton = 1; menuButtons[0].RenderText(); menuButtons[1].RenderText(); } break; case "KEY_ENTER": switch (activeMenuButton) { case 0: // continue selected SpaceTaxiBus.GetBus().RegisterEvent( GameEventFactory <object> .CreateGameEventForAllProcessors( GameEventType.GameStateEvent, this, "CHANGE_STATE", "GAME_RUNNING", "continue")); break; case 1: // back to main menu selected ChoseLevel.GetInstance().filename = "short-n-sweet.txt"; ChoseLevel.GetInstance().posX = 0.45f; ChoseLevel.GetInstance().posY = 0.075f; ChoseLevel.GetInstance().extX = 0.1f; ChoseLevel.GetInstance().extY = 0.1f; SpaceTaxiBus.GetBus().RegisterEvent( GameEventFactory <object> .CreateGameEventForAllProcessors( GameEventType.GameStateEvent, this, "CHANGE_STATE", "MAIN_MENU", "")); break; } break; } break; case "KEY_RELEASE": break; } }
public void HandleKeyEvent(string keyValue, string keyAction) { switch (keyAction) { case "KEY_PRESS": switch (keyValue) { case "KEY_UP": if (activeMenuButton == 1) { menuButtons[0] = new Text("Short n Sweet", new Vec2F(0.35f, 0.2f), new Vec2F(0.5f, 0.4f)); menuButtons[1] = new Text("The Beach", new Vec2F(0.35f, 0.1f), new Vec2F(0.4f, 0.4f)); menuButtons[0].SetColor(Color.Red); menuButtons[1].SetColor(Color.DarkRed); menuButtons[2].SetColor(Color.DarkRed); activeMenuButton = 0; menuButtons[0].RenderText(); menuButtons[1].RenderText(); menuButtons[1].RenderText(); } if (activeMenuButton == 2) { menuButtons[1] = new Text("The Beach", new Vec2F(0.35f, 0.1f), new Vec2F(0.5f, 0.4f)); menuButtons[2] = new Text("Back", new Vec2F(0.35f, 0.0f), new Vec2F(0.4f, 0.4f)); menuButtons[0].SetColor(Color.DarkRed); menuButtons[1].SetColor(Color.Red); menuButtons[2].SetColor(Color.DarkRed); activeMenuButton = 1; menuButtons[0].RenderText(); menuButtons[1].RenderText(); menuButtons[2].RenderText(); } break; case "KEY_DOWN": if (activeMenuButton == 1) { menuButtons[1] = new Text("The Beach", new Vec2F(0.35f, 0.1f), new Vec2F(0.4f, 0.4f)); menuButtons[2] = new Text("Back", new Vec2F(0.35f, 0.0f), new Vec2F(0.5f, 0.4f)); menuButtons[0].SetColor(Color.DarkRed); menuButtons[1].SetColor(Color.DarkRed); menuButtons[2].SetColor(Color.Red); activeMenuButton = 2; menuButtons[0].RenderText(); menuButtons[1].RenderText(); menuButtons[2].RenderText(); } if (activeMenuButton == 0) { menuButtons[0] = new Text("Short n Sweet", new Vec2F(0.35f, 0.2f), new Vec2F(0.4f, 0.4f)); menuButtons[1] = new Text("The Beach", new Vec2F(0.35f, 0.1f), new Vec2F(0.5f, 0.4f)); menuButtons[0].SetColor(Color.DarkRed); menuButtons[1].SetColor(Color.Red); menuButtons[2].SetColor(Color.DarkRed); activeMenuButton = 1; menuButtons[0].RenderText(); menuButtons[1].RenderText(); menuButtons[2].RenderText(); } break; case "KEY_ENTER": switch (activeMenuButton) { case 0: // short n sweet chose GameRunning.instance = null; filename = "short-n-sweet.txt"; posX = 0.45f; posY = 0.15f; extX = 0.1f; extY = 0.1f; SpaceTaxiBus.GetBus().RegisterEvent( GameEventFactory <object> .CreateGameEventForAllProcessors( GameEventType.GameStateEvent, this, "CHANGE_STATE", "GAME_RUNNING", filename)); break; case 1: // the beach chose GameRunning.instance = null; filename = "the-beach.txt"; posX = 0.25f; posY = 0.20f; extX = 0.1f; extY = 0.1f; GameRunning.instance = null; SpaceTaxiBus.GetBus().RegisterEvent( GameEventFactory <object> .CreateGameEventForAllProcessors( GameEventType.GameStateEvent, this, "CHANGE_STATE", "GAME_RUNNING", filename)); break; case 2: // back chose GameRunning.instance = null; SpaceTaxiBus.GetBus().RegisterEvent( GameEventFactory <object> .CreateGameEventForAllProcessors( GameEventType.GameStateEvent, this, "CHANGE_STATE", "MAIN_MENU", "")); break; } break; } break; case "KEY_RELEASE": break; } }
public void HandleKeyEvent(string keyValue, string keyAction) { switch (keyAction) { case "KEY_PRESS": switch (keyValue) { case "KEY_UP": if (activeMenuButton == 1) { menuButtons[0] = new Text("New Game", new Vec2F(0.35f, 0.1f), new Vec2F(0.5f, 0.4f)); menuButtons[1] = new Text("Choose Level", new Vec2F(0.35f, 0.0f), new Vec2F(0.4f, 0.4f)); menuButtons[0].SetColor(Color.Red); menuButtons[1].SetColor(Color.DarkRed); menuButtons[2].SetColor(Color.DarkRed); activeMenuButton = 0; menuButtons[0].RenderText(); menuButtons[1].RenderText(); menuButtons[2].RenderText(); menuButtons[3].RenderText(); } if (activeMenuButton == 2) { menuButtons[1] = new Text("Choose Level", new Vec2F(0.35f, 0.0f), new Vec2F(0.5f, 0.4f)); menuButtons[2] = new Text("Quit", new Vec2F(0.35f, -0.1f), new Vec2F(0.4f, 0.4f)); menuButtons[0].SetColor(Color.DarkRed); menuButtons[1].SetColor(Color.Red); menuButtons[2].SetColor(Color.DarkRed); activeMenuButton = 1; menuButtons[0].RenderText(); menuButtons[1].RenderText(); menuButtons[2].RenderText(); menuButtons[3].RenderText(); } break; case "KEY_DOWN": if (activeMenuButton == 1) { menuButtons[1] = new Text("Choose Level", new Vec2F(0.35f, 0.0f), new Vec2F(0.4f, 0.4f)); menuButtons[2] = new Text("Quit", new Vec2F(0.35f, -0.1f), new Vec2F(0.5f, 0.4f)); menuButtons[0].SetColor(Color.DarkRed); menuButtons[1].SetColor(Color.DarkRed); menuButtons[2].SetColor(Color.Red); activeMenuButton = 2; menuButtons[0].RenderText(); menuButtons[1].RenderText(); menuButtons[2].RenderText(); menuButtons[3].RenderText(); } if (activeMenuButton == 0) { menuButtons[0] = new Text("New Game", new Vec2F(0.35f, 0.1f), new Vec2F(0.4f, 0.4f)); menuButtons[1] = new Text("Choose Level", new Vec2F(0.35f, 0.0f), new Vec2F(0.5f, 0.4f)); menuButtons[0].SetColor(Color.DarkRed); menuButtons[1].SetColor(Color.Red); menuButtons[2].SetColor(Color.DarkRed); activeMenuButton = 1; menuButtons[0].RenderText(); menuButtons[1].RenderText(); menuButtons[2].RenderText(); menuButtons[3].RenderText(); } break; case "KEY_ENTER": switch (activeMenuButton) { case 0: // new game button selected GameRunning.instance = null; ChoseLevel.GetInstance().filename = "short-n-sweet.txt"; SpaceTaxiBus.GetBus().RegisterEvent( GameEventFactory <object> .CreateGameEventForAllProcessors( GameEventType.GameStateEvent, this, "CHANGE_STATE", "GAME_RUNNING", "")); break; case 1: SpaceTaxiBus.GetBus().RegisterEvent( GameEventFactory <object> .CreateGameEventForAllProcessors( GameEventType.GameStateEvent, this, "CHANGE_STATE", "CHOSE_LEVEL", "")); break; case 2: // quit SpaceTaxiBus.GetBus().RegisterEvent( GameEventFactory <object> .CreateGameEventForAllProcessors( GameEventType.WindowEvent, this, "CLOSE_WINDOW", "", "")); break; } break; case "KEY_ESCAPE": SpaceTaxiBus.GetBus().RegisterEvent( GameEventFactory <object> .CreateGameEventForAllProcessors( GameEventType.WindowEvent, this, "CLOSE_WINDOW", "", "")); break; } break; case "KEY_RELEASE": break; } }