public GameScreen(Game game) { InitializeComponent(); this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true); this.Size = new Size((int)DimensionsUtil.GetMapWidth(), (int)DimensionsUtil.GetMapHeight()); _game = game; _game.InvalidateNeeded += _game_InvalidateNeeded; Application.Idle += Application_Idle; }
public Player(ushort teamNumber, string id) : base(new PointF(), new SizeF()) { this.TeamNumber = teamNumber; this.TeamColor = DetermineTeamColor(); this.ShieldLocation = ShieldLocationType.Front; this.Size = new SizeF(DimensionsUtil.GetAvatarWidth(), DimensionsUtil.GetAvatarHeight()); this.ID = id; this.Health = _maxHealth; this.Ammo = _maxAmmo; this.FacingDirection = FacingDirectionType.North; }
private PointF PlacePlayer() { bool found = false; Player dummyPlayer = new Player(1, "0"); Random random = new Random(); PointF loc = new PointF(); while (!found) { loc.X = random.Next(0, 840 - (int)DimensionsUtil.GetAvatarWidth()); loc.Y = random.Next(0, 540 - (int)DimensionsUtil.GetAvatarHeight()); dummyPlayer.Location = loc; found = true; foreach (Player playerB in Players.Values) { found = !CollisionDetector.Collision(dummyPlayer, playerB); if (!found) { break; } } if (found) { foreach (Obstacle obstacle in Obstacles) { found = !CollisionDetector.Collision(dummyPlayer, obstacle); if (!found) { break; } } } } return(loc); }
public static bool OffScreen(VideoPaintballCommon.MapObjects.MapObject mapObject) { return(mapObject.Location.X < 0 || mapObject.Location.Y < 0 || mapObject.Location.X > DimensionsUtil.GetMapWidth() || mapObject.Location.Y > DimensionsUtil.GetMapHeight()); }
public void DoTurnAction(Player[] players) { while (_targetedPlayer == null || _targetedPlayer.Health == 0 || _targetedPlayer.ID == this.ID) { Random random = new Random(); _targetedPlayer = players[random.Next(0, players.Length)]; } /* this code chooses a human target instead * foreach (Player player in players) * { * * if (player is AIPlayer) * { * } * else * { * targetedPlayer = player; * break; * } * } */ if (_turnsPassedSinceLastMove == 10) { if (_targetedPlayer.Location.Y - 10 > this.Location.Y /*+ DimensionsUtil.GetAvatarHeight() * 2*/) { //target below ai player, check facing direction, then either move down to match Ys or if Xs line up, shoot at player if (this.FacingDirection != FacingDirectionType.South) { _action = MessageConstants.PlayerActionRotateRight; } else if (this.ShieldLocation != ShieldLocationType.Back) { _action = MessageConstants.PlayerActionShieldBack; } else { if (_targetedPlayer.Location.X + DimensionsUtil.GetAvatarWidth() < this.Location.X || _targetedPlayer.Location.X > this.Location.X + DimensionsUtil.GetAvatarWidth()) { //target below ai player, and not lined up with gun _action = MessageConstants.PlayerActionDown; } else { _action = MessageConstants.PlayerActionShoot; } } } else if (_targetedPlayer.Location.Y + 10 < this.Location.Y /*- DimensionsUtil.GetAvatarHeight() * 2*/) { //target above ai player, check facing direction, then either move up to match Ys or if Xs line up, shoot at player if (this.FacingDirection != FacingDirectionType.North) { _action = MessageConstants.PlayerActionRotateRight; } else if (this.ShieldLocation != ShieldLocationType.Front) { _action = MessageConstants.PlayerActionShieldFront; } else { if (_targetedPlayer.Location.X < this.Location.X || _targetedPlayer.Location.X > this.Location.X + DimensionsUtil.GetAvatarWidth()) { //target above ai player, and not lined up with gun _action = MessageConstants.PlayerActionUp; } else { _action = MessageConstants.PlayerActionShoot; } } } else { //target player on same y as ai player if (_targetedPlayer.Location.X < this.Location.X) { //target player to the left of ai player if (this.FacingDirection != FacingDirectionType.West) { _action = MessageConstants.PlayerActionRotateLeft; } else if (this.ShieldLocation != ShieldLocationType.Left) { _action = MessageConstants.PlayerActionShieldLeft; } else { _action = MessageConstants.PlayerActionShoot; } } else { //target player to the right of ai player if (this.FacingDirection != FacingDirectionType.East) { _action = MessageConstants.PlayerActionRotateRight; } else if (this.ShieldLocation != ShieldLocationType.Right) { _action = MessageConstants.PlayerActionShieldRight; } else { _action = MessageConstants.PlayerActionShoot; } } } _turnsPassedSinceLastMove = 0; } else { _turnsPassedSinceLastMove++; _action = MessageConstants.PlayerActionNone; } }
public Paintball(PointF location) : base(location) { this.Size = new SizeF(DimensionsUtil.GetPaintballWidth(), DimensionsUtil.GetPaintballHeight()); }
public Obstacle(PointF location, SizeF size) : base(location) { this.Size = new SizeF(DimensionsUtil.GetObstacleWidth(size.Width), DimensionsUtil.GetObstacleHeight(size.Height)); }
public void Update(List <PlayerAction> playerActions) { //restart game if only 1 active player int activePlayers = 0; foreach (Player player in Players.Values) { if (player.Health > 0 && player.Ammo > 0) { activePlayers++; } } if (activePlayers == 1) { StartNewGame(playerActions); return; } //AI players take their turn here Player[] players = new Player[Players.Count]; Players.Values.CopyTo(players, 0); foreach (Player player in Players.Values) { if (player is AIPlayer) { ((AIPlayer)player).DoTurnAction(players); foreach (PlayerAction action in playerActions) { if (action.PlayerID == player.ID) { action.Action = ((AIPlayer)player).Action; } } } } foreach (PlayerAction playerAction in playerActions) { Player player = Players[playerAction.PlayerID]; if (player.Health > 0) { //set velocities on players, etc. switch (playerAction.Action) { case MessageConstants.PlayerActionDown: player.Velocity = new PointF(0, 4); break; case MessageConstants.PlayerActionLeft: player.Velocity = new PointF(-4, 0); break; case MessageConstants.PlayerActionRight: player.Velocity = new PointF(4, 0); break; case MessageConstants.PlayerActionUp: player.Velocity = new PointF(0, -4); break; case MessageConstants.PlayerActionRotateLeft: player.RotateLeft(); break; case MessageConstants.PlayerActionRotateRight: player.RotateRight(); break; case MessageConstants.PlayerActionShieldBack: player.MoveShieldToBack(); break; case MessageConstants.PlayerActionShieldFront: player.MoveShieldToFront(); break; case MessageConstants.PlayerActionShieldLeft: player.MoveShieldToLeft(); break; case MessageConstants.PlayerActionShieldRight: player.MoveShieldToRight(); break; case MessageConstants.PlayerActionShoot: if (player.Ammo > 0) { PointF velocity = new PointF(0, 0); PointF location = new PointF(0, 0); switch (player.FacingDirection) { case FacingDirectionType.North: velocity = new PointF(0, -4); location = new PointF(player.Location.X + (DimensionsUtil.GetAvatarWidth() / 2) - (DimensionsUtil.GetPaintballWidth() / 2), Players[playerAction.PlayerID].Location.Y - DimensionsUtil.GetGunHeight() - DimensionsUtil.GetPaintballOffset()); break; case FacingDirectionType.South: velocity = new PointF(0, 4); location = new PointF(player.Location.X + (DimensionsUtil.GetAvatarWidth() / 2) - (DimensionsUtil.GetPaintballWidth() / 2), Players[playerAction.PlayerID].Location.Y + DimensionsUtil.GetPaintballOffset() + DimensionsUtil.GetAvatarHeight()); break; case FacingDirectionType.East: velocity = new PointF(4, 0); location = new PointF(player.Location.X + DimensionsUtil.GetPaintballOffset() + DimensionsUtil.GetAvatarWidth(), Players[playerAction.PlayerID].Location.Y + (DimensionsUtil.GetAvatarHeight() / 2) - (DimensionsUtil.GetPaintballHeight() / 2)); break; case FacingDirectionType.West: velocity = new PointF(-4, 0); location = new PointF(player.Location.X - DimensionsUtil.GetPaintballOffset(), Players[playerAction.PlayerID].Location.Y + (DimensionsUtil.GetAvatarHeight() / 2) - (DimensionsUtil.GetPaintballHeight() / 2)); break; } Paintballs.Add(new Paintball(location, velocity)); player.RecordPaintballFired(); } break; case MessageConstants.PlayerActionPowerShoot: break; case MessageConstants.PlayerActionNone: break; } } } //move all objects foreach (Player player in Players.Values) { player.Move(); player.Velocity = new PointF(0, 0); } foreach (Paintball paintball in Paintballs) { paintball.Move(); } List <PaintballHit> paintballHitsToRemove = new List <PaintballHit>(); foreach (PaintballHit ph in PaintballHits) { if (ph.ShouldRemove) { paintballHitsToRemove.Add(ph); } ph.RenderTimes++; } foreach (PaintballHit ph in paintballHitsToRemove) { PaintballHits.Remove(ph); } //detect and record collisions between paintballs and players //issue [B.2.6] of the design document Shield shield = null; PointF shieldLocation = new PointF(0, 0); SizeF shieldSize = new SizeF(0, 0); bool wasCollision = false; foreach (Paintball paintball in Paintballs) { wasCollision = false; foreach (Player player in Players.Values) { if (player.Health > 0) { switch (player.ShieldLocation) { case ShieldLocationType.Front: shieldLocation = new PointF(player.Location.X, player.Location.Y - DimensionsUtil.GetAvatarShieldOffset()); shieldSize = new SizeF(DimensionsUtil.GetAvatarWidth(), 1); break; case ShieldLocationType.Back: shieldLocation = new PointF(player.Location.X, player.Location.Y + DimensionsUtil.GetAvatarHeight() + DimensionsUtil.GetAvatarShieldOffset()); shieldSize = new SizeF(DimensionsUtil.GetAvatarWidth(), 1); break; case ShieldLocationType.Left: shieldLocation = new PointF(player.Location.X - DimensionsUtil.GetAvatarShieldOffset(), player.Location.Y); shieldSize = new SizeF(1, DimensionsUtil.GetAvatarHeight()); break; case ShieldLocationType.Right: shieldLocation = new PointF(player.Location.X + DimensionsUtil.GetAvatarWidth() + DimensionsUtil.GetAvatarShieldOffset(), player.Location.Y); shieldSize = new SizeF(1, DimensionsUtil.GetAvatarHeight()); break; } shield = new Shield(shieldLocation, shieldSize); if (CollisionDetector.Collision(paintball, shield)) { wasCollision = true; _paintballsToRemove.Add(paintball); } else if (CollisionDetector.Collision(paintball, player)) { wasCollision = true; player.RecordPaintballHit(); _paintballsToRemove.Add(paintball); PaintballHits.Add(new PaintballHit(paintball.Location, 0)); } if (wasCollision) { break; } } } if (!wasCollision) { foreach (Obstacle obstacle in Obstacles) { if (CollisionDetector.Collision(paintball, obstacle)) { wasCollision = true; _paintballsToRemove.Add(paintball); } } } if (!wasCollision) { if (OffscreenDetector.OffScreen(paintball)) { _paintballsToRemove.Add(paintball); } } } //remove paintballs that hit players, obstacles or went offscreen foreach (Paintball paintball in _paintballsToRemove) { Paintballs.Remove(paintball); } _paintballsToRemove.Clear(); //don't allow players to move over other players or over obstacles, or off-screen //issue [B.2.5] of the design document bool wasUndoed = false; foreach (Player player in Players.Values) { wasUndoed = false; if (OffscreenDetector.OffScreen(player)) { player.UndoMove(); } else { foreach (Obstacle obstacle in Obstacles) { if (CollisionDetector.Collision(player, obstacle)) { wasUndoed = true; player.UndoMove(); } if (wasUndoed) { break; } } if (!wasUndoed) { foreach (Player playerB in Players.Values) { if (playerB.Health > 0) //issue [B.2.7] of the design document { if (CollisionDetector.Collision(player, playerB) && player.ID != playerB.ID) { wasUndoed = true; player.UndoMove(); } if (wasUndoed) { break; } } } } } } }
public Map() { Size = new SizeF(DimensionsUtil.GetMapWidth(), DimensionsUtil.GetMapHeight()); PaintballHits = new List <PaintballHit>(); }
/// <summary> /// issue [A.1.5] of the design document /// </summary> /// <param name="graphics"></param> public void Render(Graphics graphics) { Pen pen = new Pen(TeamColor); int healthMeterX = (int)Location.X + (int)(DimensionsUtil.GetAvatarWidth() - 20); int healthMeterY = (int)Location.Y + 20; float healthMeterBarHeight = ((float)Health / (float)_maxHealth) * DimensionsUtil.GetHealthMeterHeight(); int ammoMeterX = (int)Location.X + 10; int ammoMeterY = (int)Location.Y + 20; float ammoMeterBarHeight = ((float)Ammo / (float)_maxAmmo) * DimensionsUtil.GetAmmoMeterHeight(); Color healthMeterColor = MeterUtil.GetColorFromValue(Health); SolidBrush healthMeterBrush = new SolidBrush(healthMeterColor); Color ammoMeterColor = MeterUtil.GetColorFromValue(Ammo); SolidBrush ammoMeterBrush = new SolidBrush(ammoMeterColor); GraphicsPath graphicsPath = new GraphicsPath(); RectangleF avatarMainBody = new RectangleF(Location, Size); RectangleF avatarGun = new RectangleF(Location.X + ((Size.Width / 2) - (DimensionsUtil.GetGunWidth() / 2)), Location.Y - DimensionsUtil.GetGunHeight(), DimensionsUtil.GetGunWidth(), DimensionsUtil.GetGunHeight()); RectangleF healthMeterOutline = new RectangleF(healthMeterX, healthMeterY, DimensionsUtil.GetHealthMeterWidth(), DimensionsUtil.GetHealthMeterHeight()); RectangleF ammoMeterOutline = new RectangleF(ammoMeterX, ammoMeterY, DimensionsUtil.GetAmmoMeterWidth(), DimensionsUtil.GetAmmoMeterHeight()); graphicsPath.AddRectangle(avatarMainBody); graphicsPath.AddRectangle(avatarGun); graphicsPath.AddRectangle(healthMeterOutline); graphicsPath.AddRectangle(ammoMeterOutline); RectangleF healthMeterBar = new RectangleF(healthMeterX, healthMeterY + (DimensionsUtil.GetHealthMeterHeight() - healthMeterBarHeight), DimensionsUtil.GetHealthMeterWidth(), healthMeterBarHeight); RectangleF ammoMeterBar = new RectangleF(ammoMeterX, ammoMeterY + DimensionsUtil.GetAmmoMeterHeight() - ammoMeterBarHeight, DimensionsUtil.GetAmmoMeterWidth(), ammoMeterBarHeight); GraphicsPath healthMeterBarPath = new GraphicsPath(); healthMeterBarPath.AddRectangle(healthMeterBar); GraphicsPath ammoMeterBarPath = new GraphicsPath(); ammoMeterBarPath.AddRectangle(ammoMeterBar); Matrix rotationMatrix = new Matrix(1, 0, 0, 1, 0, 0); PointF rotationPoint = new PointF(avatarMainBody.Location.X + (DimensionsUtil.GetAvatarWidth() / 2), avatarMainBody.Location.Y + (DimensionsUtil.GetAvatarHeight() / 2)); switch (FacingDirection) { case FacingDirectionType.North: break; case FacingDirectionType.East: rotationMatrix.RotateAt(90F, rotationPoint); break; case FacingDirectionType.South: rotationMatrix.RotateAt(180F, rotationPoint); break; case FacingDirectionType.West: rotationMatrix.RotateAt(270F, rotationPoint); break; } graphicsPath.Transform(rotationMatrix); ammoMeterBarPath.Transform(rotationMatrix); healthMeterBarPath.Transform(rotationMatrix); graphics.FillPath(healthMeterBrush, healthMeterBarPath); graphics.FillPath(ammoMeterBrush, ammoMeterBarPath); graphics.DrawPath(pen, graphicsPath); //render the shield, ignoring any rotations (rotations are ignored because it is easier for collision detection with the shield) switch (ShieldLocation) { case ShieldLocationType.Front: graphics.DrawLine(pen, Location.X, Location.Y - DimensionsUtil.GetAvatarShieldOffset(), Location.X + Size.Width, Location.Y - DimensionsUtil.GetAvatarShieldOffset()); break; case ShieldLocationType.Back: graphics.DrawLine(pen, Location.X, Location.Y + DimensionsUtil.GetAvatarShieldOffset() + Size.Height, Location.X + Size.Width, Location.Y + DimensionsUtil.GetAvatarShieldOffset() + Size.Height); break; case ShieldLocationType.Left: graphics.DrawLine(pen, Location.X - DimensionsUtil.GetAvatarShieldOffset(), Location.Y, Location.X - DimensionsUtil.GetAvatarShieldOffset(), Location.Y + Size.Height); break; case ShieldLocationType.Right: graphics.DrawLine(pen, Location.X + Size.Width + DimensionsUtil.GetAvatarShieldOffset(), Location.Y, Location.X + Size.Width + DimensionsUtil.GetAvatarShieldOffset(), Location.Y + Size.Height); break; } }