private void OnMenuControl(object source, KeyboardKeyEventArgs args) { if (activeState.Equals(GameState.MAINMENU)) { switch (activeState = m.GetStateByKey(args)) { case GameState.SINGLEPL: player = new LocalPlayer(); level = new Level(new Map(MapGenerator.GenerateMap(GameMode.Mode.CLASSIC))); level.AddTank(player, AbstractTank.Type.PlayerNormal, (int)(-windowWidth / 2 + gameRenderer.ElementWidth * 7), (int)(windowHeight / 2 - 19 * gameRenderer.ElementHeight), Texture.Rotation.Top); Keyboard.KeyUp += player.KeyUpEventHandler; Keyboard.KeyDown += player.KeyDownEventHandler; break; case GameState.EXIT: Exit(); break; } } }
/// <summary> /// Called when it is time to setup the next frame. /// </summary> /// <param name="e">Contains timing information for framerate independent logic.</param> protected override void OnUpdateFrame(FrameEventArgs e) { base.OnUpdateFrame(e); //GameLogic gameplay = new GameLogic(); //gameplay.AddPlayer(player); if (activeState.Equals(GameState.SINGLEPL)) { if (Keyboard[Key.Q]) { level = new Level(new Map(MapGenerator.GenerateMap(GameMode.Mode.CLASSIC))); level.AddTank(player, AbstractTank.Type.PlayerFast, (int)(-windowWidth / 2 + gameRenderer.ElementWidth * 7), (int)(windowHeight / 2 - 19 * gameRenderer.ElementHeight), Texture.Rotation.Top); } if (Keyboard[Key.W]) { level = new Level(new Map(MapGenerator.GenerateMap(GameMode.Mode.DM))); //level.AddTank(player, AbstractTank.Type.PlayerNormal, (int)(-windowWidth / 2 + gameRenderer.ElementWidth * 7), (int)(windowHeight / 2 - 19 * gameRenderer.ElementHeight)); } if (Keyboard[Key.E]) { level = new Level(new Map(MapGenerator.GenerateMap(GameMode.Mode.TDMB))); //level.AddTank(player, AbstractTank.Type.PlayerNormal, (int)(-windowWidth / 2 + gameRenderer.ElementWidth * 7), (int)(windowHeight / 2 - 19 * gameRenderer.ElementHeight)); } if (Keyboard[Key.R]) { level = new Level(new Map(MapGenerator.GenerateMap(GameMode.Mode.TDM))); //level.AddTank(player, AbstractTank.Type.PlayerNormal, (int)(-windowWidth / 2 + gameRenderer.ElementWidth * 7), (int)(windowHeight / 2 - 19 * gameRenderer.ElementHeight)); } if (Keyboard[Key.Space]) { } if (player.tank.IsUpState) { player.tank.Rotate(Texture.Rotation.Top); if (gameRenderer.TankCanMove(level, player.tank, Texture.Rotation.Top)) { player.tank.MoveUp(); } } else if (player.tank.IsDownState) { player.tank.Rotate(Texture.Rotation.Bottom); if (gameRenderer.TankCanMove(level, player.tank, Texture.Rotation.Bottom)) { player.tank.MoveDown(); } } else if (player.tank.IsRightState) { player.tank.Rotate(Texture.Rotation.Right); if (gameRenderer.TankCanMove(level, player.tank, Texture.Rotation.Right)) { player.tank.MoveRight(); } } else if (player.tank.IsLeftState) { player.tank.Rotate(Texture.Rotation.Left); if (gameRenderer.TankCanMove(level, player.tank, Texture.Rotation.Left)) { player.tank.MoveLeft(); } } } }
private List<MapObject.Types> NextMapObjects(Level level, AbstractTank tank, Texture.Rotation rotation) { PointF tankPositionOnMap = TankPositionOnMap(tank); List<MapObject.Types> objects = new List<MapObject.Types>(); switch (rotation) { case Texture.Rotation.Top: if (((int)(tankPositionOnMap.Y)).Equals(0)) { return new List<MapObject.Types> { MapObject.Types.TEMPORARY }; } objects.Add(level.MapInstance[(int)tankPositionOnMap.Y - 1][(int)tankPositionOnMap.X].Type);//get current tank pos MapObject if (tankPositionOnMap.X - ((int)tankPositionOnMap.X) > 0)//if tank is on two MapObjects { objects.Add(level.MapInstance[(int)tankPositionOnMap.Y - 1][(int)tankPositionOnMap.X + 1].Type); } break; case Texture.Rotation.Bottom: if (((int)(tankPositionOnMap.Y)).Equals(19)) { return new List<MapObject.Types> { MapObject.Types.TEMPORARY }; } objects.Add(level.MapInstance[(int)tankPositionOnMap.Y + 1][(int)tankPositionOnMap.X].Type); if (tankPositionOnMap.X - ((int)tankPositionOnMap.X) > 0) { objects.Add(level.MapInstance[(int)tankPositionOnMap.Y + 1][(int)tankPositionOnMap.X + 1].Type); } break; case Texture.Rotation.Right: if (((int)(tankPositionOnMap.X)).Equals(18)) { return new List<MapObject.Types> { MapObject.Types.TEMPORARY }; } objects.Add(level.MapInstance[(int)tankPositionOnMap.Y][(int)tankPositionOnMap.X + 1].Type); if (tankPositionOnMap.Y - ((int)tankPositionOnMap.Y) > 0) { objects.Add(level.MapInstance[(int)tankPositionOnMap.Y + 1][(int)tankPositionOnMap.X + 1].Type); } break; case Texture.Rotation.Left: if (((int)(tankPositionOnMap.X)).Equals(0)) { return new List<MapObject.Types> { MapObject.Types.TEMPORARY }; } objects.Add(level.MapInstance[(int)tankPositionOnMap.Y][(int)tankPositionOnMap.X - 1].Type); if (tankPositionOnMap.Y - ((int)tankPositionOnMap.Y) > 0) { objects.Add(level.MapInstance[(int)tankPositionOnMap.Y + 1][(int)tankPositionOnMap.X - 1].Type); } break; } return objects; }
private List<MapObject.Types> CurrentMapObjects(Level level, AbstractTank tank, Texture.Rotation rotation) { PointF tankPositionOnMap = TankPositionOnMap(tank); float fX = (float)Math.Round(tankPositionOnMap.X, 0), fY = (float)Math.Round(tankPositionOnMap.Y, 0); List<MapObject.Types> objects = new List<MapObject.Types>(); switch (rotation) { case Texture.Rotation.Top: objects.Add(level.MapInstance[(int)tankPositionOnMap.Y][(int)tankPositionOnMap.X].Type); if (!tank.Y.Equals((int) (windowHeight/2 - fY*elementHeight)) && tankPositionOnMap.X - ((int)tankPositionOnMap.X) > 0) { objects.Add(level.MapInstance[(int) tankPositionOnMap.Y][(int) tankPositionOnMap.X + 1].Type); } break; case Texture.Rotation.Bottom: objects.Add(level.MapInstance[(int)tankPositionOnMap.Y][(int)tankPositionOnMap.X].Type); if (!(tank.Y - ElementHeight).Equals((int) (windowHeight/2 - (fY + 1)*elementHeight)) && tankPositionOnMap.X - ((int) tankPositionOnMap.X) > 0) { objects.Add(level.MapInstance[(int) tankPositionOnMap.Y][(int) tankPositionOnMap.X + 1].Type); } break; case Texture.Rotation.Right: objects.Add(level.MapInstance[(int)tankPositionOnMap.Y][(int)tankPositionOnMap.X].Type); if (!(tank.X + elementWidth).Equals((int) (-windowWidth/2 + (fX + 1)*ElementHeight)) && tankPositionOnMap.Y - ((int) tankPositionOnMap.Y) > 0) { objects.Add(level.MapInstance[(int) tankPositionOnMap.Y + 1][(int) tankPositionOnMap.X].Type); } break; case Texture.Rotation.Left: objects.Add(level.MapInstance[(int)tankPositionOnMap.Y][(int)tankPositionOnMap.X].Type); if (!tank.X.Equals((int) (-windowWidth/2 + fX*elementWidth)) && tankPositionOnMap.Y - ((int) tankPositionOnMap.Y) > 0) { objects.Add(level.MapInstance[(int) tankPositionOnMap.Y + 1][(int) tankPositionOnMap.X].Type); } break; } return objects; }
public bool TankCanMove(Level level, AbstractTank tank, Texture.Rotation rotation) { PointF pos = TankPositionOnMap(tank); List<MapObject.Types> nextMapObjectType = NextMapObjects(level, tank, rotation); List<MapObject.Types> currentMapObjectType = CurrentMapObjects(level, tank, rotation); nextMapObjectType.RemoveAll(x => x.Equals(MapObject.Types.FOREST) || x.Equals(MapObject.Types.EMPTY)); currentMapObjectType.RemoveAll(x => x.Equals(MapObject.Types.FOREST) || x.Equals(MapObject.Types.EMPTY)); switch (rotation) { case Texture.Rotation.Top: return windowHeight/2 - ((int) pos.Y)*elementHeight - tank.Y == 0 ? nextMapObjectType.Count.Equals(0) : currentMapObjectType.Count.Equals(0); case Texture.Rotation.Bottom: return windowHeight/2 - ((int) pos.Y)*elementHeight - elementHeight - tank.Y == -elementHeight ? nextMapObjectType.Count.Equals(0) : currentMapObjectType.Count.Equals(0); case Texture.Rotation.Right: return -windowWidth/2 + ((int) pos.X)*elementWidth + elementWidth - tank.X == elementWidth ? nextMapObjectType.Count.Equals(0) : currentMapObjectType.Count.Equals(0); case Texture.Rotation.Left: return -windowWidth/2 + ((int) pos.X)*elementWidth - tank.X == 0 ? nextMapObjectType.Count.Equals(0) : currentMapObjectType.Count.Equals(0); } return false; }
public void RenderLevel(Level level) { DrawMap(level.MapInstance); DrawTanks(level.Tanks); }