Пример #1
0
        private void CheckForEvents(List <Player> activePlayers, List <TrailSegment> trails)
        {
            foreach (var player in activePlayers)
            {
                var bike    = player.GetBike();
                var crashed = bike.Collides(trails, 1, out var collidedWith) || _arena.CheckCollision(bike, 1);

                if (crashed)
                {
                    var crashedInto = activePlayers.FirstOrDefault(p => p.GetId() == collidedWith);
                    PlayerCrashed(player, crashedInto);
                }

                foreach (var powerUp in _powerUpKeeper.GetList().Where(p => !p.IsCollected()))
                {
                    var pos    = bike.GetPos();
                    var dir    = bike.GetDir();
                    var aheadX = pos.X + (2 * dir.X);
                    var aheadY = pos.Y + (2 * dir.Y);
                    var line   = new LineSegment2D(new Vector2(pos.X, pos.Y), new Vector2(aheadX, aheadY));
                    if (powerUp.Collides(line))
                    {
                        _powerUpKeeper.PlayerCollectedPowerUp(player, powerUp);
                    }
                }
            }
        }
Пример #2
0
        public ClientUpdateDto PredictCollision()
        {
            var dDist         = 20;
            var activePlayers = _players.Where(p => p.IsAlive()).ToList();

            var allTrails = new List <TrailSegment>();

            foreach (var p in activePlayers)
            {
                var isSelf = p.GetId() == _botId;
                allTrails.AddRange(p.GetBike().GetTrailSegmentList(!isSelf));
            }

            var incCollision = _botBike.Collides(allTrails, dDist, out _) || _arena.CheckCollision(_botBike, dDist);

            if (!incCollision)
            {
                return(null);
            }

            var newVal    = _rand.Next(0, 2) == 0 ? -1 : 1;
            var updateDto = new ClientUpdateDto {
                PlayerId = _botId
            };

            if (_botBike.GetDir().X != 0)
            {
                updateDto.XDir = 0;
                updateDto.YDir = newVal;
            }
            else if (_botBike.GetDir().Y != 0)
            {
                updateDto.XDir = newVal;
                updateDto.YDir = 0;
            }

            return(RandomDirectionDto());
        }
Пример #3
0
        private void PredictCollision()
        {
            var dDist         = 20;
            var activePlayers = players.Where(p => p.IsAlive()).ToList();

            var allTrails = new List <TrailSegment>();

            foreach (var p in activePlayers)
            {
                var isSelf = p.GetId() == GetId();
                allTrails.AddRange(p.GetBike().GetTrailSegmentList(!isSelf));
            }

            var incCollision = GetBike().Collides(allTrails, dDist, out _) || arena.CheckCollision(GetBike(), dDist);

            if (!incCollision)
            {
                return;
            }

            var newVal    = _rand.Next(0, 2) == 0 ? -1 : 1;
            var updateDto = new ClientUpdateDto {
                PlayerId = GetId()
            };

            if (GetBike().GetDir().X != 0)
            {
                updateDto.XDir = 0;
                updateDto.YDir = newVal;
            }
            else if (GetBike().GetDir().Y != 0)
            {
                updateDto.XDir = newVal;
                updateDto.YDir = 0;
            }

            controller.SendUpdate(GetId(), updateDto);
        }
Пример #4
0
        public override void Update(GameTime gameTime)
        {
            if (!contentLoaded)
            {
                LoadContent();
            }

            if (player1.Status == PlayerStatus.GameOver && (player2.Status == PlayerStatus.GameOver || player2.Status == PlayerStatus.Disabled))
            {
                SetUpNewGame(this.gameOptions);
                StartGame();
                return;
            }

            if (enemies.Count == 0)
            {
                level++;
                StartLevel();
                return;
            }

            this.player1pad = GamePad.GetState(PlayerIndex.One);
            this.player2pad = GamePad.GetState(PlayerIndex.Two);
            targets.Clear();

            if (Xin.CheckKeyReleased(Keys.M)) // MUTE/MUSIC
            {
                if (MediaPlayer.State == MediaState.Playing)
                {
                    MediaPlayer.Pause();
                }
                else
                {
                    MediaPlayer.Resume();
                }
            }

            if (Xin.CheckKeyReleased(Keys.R)) // RESTART
            {
                SetUpNewGame(this.gameOptions);
            }


            if (player1.Status == PlayerStatus.Resurrecting)
            {
                player1.Sprite.Position = arena.GetSafeSpawnPosition(player2, enemies, enemyListController.shots);
            }

            if (player2.Status == PlayerStatus.Resurrecting)
            {
                player2.Sprite.Position = arena.GetSafeSpawnPosition(player1, enemies, enemyListController.shots);
            }

            //--- Player 1 ---//

            if (player1.Status == PlayerStatus.Active)
            {
                player1.Sprite.CurrentAnimation = AnimationKey.Idle;

                if (player1.shotReloadTimeRemaining > 0)
                {
                    player1.shotReloadTimeRemaining -= gameTime.ElapsedGameTime.Milliseconds;
                }

                if (Xin.KeyboardState.IsKeyDown(Keys.Left) || this.player1pad.ThumbSticks.Left.X < -0.2f) // rotate counterclockwise
                {
                    player1.Sprite.Rotation -= 0.1f;
                }
                else if (Xin.KeyboardState.IsKeyDown(Keys.Right) || this.player1pad.ThumbSticks.Left.X > 0.2f) // rotate clockwise
                {
                    player1.Sprite.Rotation += 0.1f;
                }

                if (Xin.KeyboardState.IsKeyDown(Keys.Up) || this.player1pad.Triggers.Left > 0.2f) // thrust
                {
                    float xSpeed = player1.Sprite.Velocity.X + (float)Math.Sin(player1.Sprite.Rotation) / 8;
                    float ySpeed = player1.Sprite.Velocity.Y - (float)Math.Cos(player1.Sprite.Rotation) / 8;

                    if (Math.Abs(xSpeed) > player1.MaxSpeed)
                    {
                        xSpeed = player1.MaxSpeed * Math.Sign(xSpeed);
                    }

                    if (Math.Abs(ySpeed) > player1.MaxSpeed)
                    {
                        ySpeed = player1.MaxSpeed * Math.Sign(ySpeed);
                    }

                    player1.Sprite.Velocity         = new Vector2(xSpeed, ySpeed);
                    player1.Sprite.CurrentAnimation = AnimationKey.Thrusting;
                    thrustSound.Play();
                }

                if (Xin.KeyboardState.IsKeyDown(Keys.RightShift) || this.player1pad.Triggers.Right > 0.2f) // fire!
                {
                    if (player1.shotReloadTimeRemaining <= 0)
                    {
                        // determine shot location at the nose of the ship
                        Vector2 position = new Vector2(player1.Sprite.Position.X + (float)Math.Cos(player1.Sprite.Rotation - MathHelper.PiOver2) * 30,
                                                       player1.Sprite.Position.Y + (float)Math.Sin(player1.Sprite.Rotation - MathHelper.PiOver2) * 30);
                        Vector2 velocity = new Vector2((float)Math.Sin(player1.Sprite.Rotation) * 10, (float)Math.Cos(player1.Sprite.Rotation) * -10);

                        Shot shot = new Shot(Game, position, velocity, 800, shotTexture, shotAnimations);
                        player1shots.Add(shot);
                        shotFireSound.Play();
                        player1.shotReloadTimeRemaining = player1.shotReloadTime;
                    }
                }

                targets.Add(player1.Position);
            }

            //--- Player 2 ---//

            if (player2.Status == PlayerStatus.Active)
            {
                player2.Sprite.CurrentAnimation = AnimationKey.Idle;

                if (player2.shotReloadTimeRemaining > 0)
                {
                    player2.shotReloadTimeRemaining -= gameTime.ElapsedGameTime.Milliseconds;
                }

                if (Xin.KeyboardState.IsKeyDown(Keys.A) || this.player2pad.ThumbSticks.Left.X < -0.2f) // rotate counterclockwise
                {
                    player2.Sprite.Rotation -= 0.1f;
                }
                else if (Xin.KeyboardState.IsKeyDown(Keys.D) || this.player2pad.ThumbSticks.Left.X > 0.2f) // rotate clockwise
                {
                    player2.Sprite.Rotation += 0.1f;
                }

                if (Xin.KeyboardState.IsKeyDown(Keys.W) || this.player2pad.Triggers.Left > 0.2f) // thrust
                {
                    float xSpeed = player2.Sprite.Velocity.X + (float)Math.Sin(player2.Sprite.Rotation) / 8;
                    float ySpeed = player2.Sprite.Velocity.Y - (float)Math.Cos(player2.Sprite.Rotation) / 8;

                    if (Math.Abs(xSpeed) > player2.MaxSpeed)
                    {
                        xSpeed = player2.MaxSpeed * Math.Sign(xSpeed);
                    }

                    if (Math.Abs(ySpeed) > player2.MaxSpeed)
                    {
                        ySpeed = player2.MaxSpeed * Math.Sign(ySpeed);
                    }

                    player2.Sprite.Velocity         = new Vector2(xSpeed, ySpeed);
                    player2.Sprite.CurrentAnimation = AnimationKey.Thrusting;
                    thrustSound.Play();
                }

                if (Xin.KeyboardState.IsKeyDown(Keys.LeftShift) || this.player2pad.Triggers.Right > 0.2f) // fire!
                {
                    if (player2.shotReloadTimeRemaining <= 0)
                    {
                        // determine shot location at the nose of the ship
                        Vector2 position = new Vector2(player2.Sprite.Position.X + (float)Math.Cos(player2.Sprite.Rotation - MathHelper.PiOver2) * 30,
                                                       player2.Sprite.Position.Y + (float)Math.Sin(player2.Sprite.Rotation - MathHelper.PiOver2) * 30);
                        Vector2 velocity = new Vector2((float)Math.Sin(player2.Sprite.Rotation) * 10, (float)Math.Cos(player2.Sprite.Rotation) * -10);

                        Shot shot = new Shot(Game, position, velocity, 800, shotTexture, shotAnimations);
                        player2shots.Add(shot);
                        shotFireSound.Play();
                        player2.shotReloadTimeRemaining = player2.shotReloadTime;
                    }
                }

                targets.Add(player2.Position);
            }


            // check for collisions with arena walls and obstacles
            arena.CheckCollision(player1.Sprite, true);
            if (player2.Status == PlayerStatus.Active)
            {
                arena.CheckCollision(player2.Sprite, true);
            }

            enemies.ForEach(en => arena.CheckCollision(en.Sprite, true));

            // check to see if the enemy got hit
            foreach (Shot shot in player1shots)
            {
                foreach (Enemy enemy in enemies)
                {
                    if (shot.CheckCollision(enemy))
                    {
                        player1.AddToScore(enemy.PointValue);
                    }
                }
            }

            foreach (Shot shot in player2shots)
            {
                foreach (Enemy enemy in enemies)
                {
                    if (shot.CheckCollision(enemy))
                    {
                        player2.AddToScore(enemy.PointValue);
                    }
                }
            }

            // check to see if the player got hit
            foreach (Shot shot in enemyListController.shots)
            {
                if (shot.CheckCollision(player1))
                {
                    player1.Status = PlayerStatus.Hit;
                }
                if ((player2.Status == PlayerStatus.Active) && (shot.CheckCollision(player2)))
                {
                    player2.Status = PlayerStatus.Hit;
                }
            }

            if (this.gameOptions.GameType == GameType.TwoPlayerDeathmatch)
            {
                // see if the players hit each other
                foreach (Shot shot in player1shots)
                {
                    if (shot.CheckCollision(player2))
                    {
                        player2.Status = PlayerStatus.Hit;
                    }
                }
                foreach (Shot shot in player2shots)
                {
                    if (shot.CheckCollision(player1))
                    {
                        player1.Status = PlayerStatus.Hit;
                    }
                }
            }

            foreach (Enemy enemy in enemies)
            {
                if (player1.Status == PlayerStatus.Active && enemy.CheckCollision(player1.Sprite))
                {
                    player1.Status = PlayerStatus.Hit;
                    enemy.Status   = EnemyStatus.Hit;
                }
                if ((player2.Status == PlayerStatus.Active) && (enemy.CheckCollision(player2.Sprite)))
                {
                    player2.Status = PlayerStatus.Hit;
                    enemy.Status   = EnemyStatus.Hit;
                }
            }

            enemyListController.shots.RemoveAll(shot => arena.CheckCollision(shot.Sprite, false));
            enemyListController.shots.RemoveAll(shot => shot.Range <= 0);

            player1shots.RemoveAll(sh => arena.CheckCollision(sh.Sprite, false));
            player1shots.RemoveAll(sh => sh.Range <= 0);
            player2shots.RemoveAll(sh => arena.CheckCollision(sh.Sprite, false));
            player2shots.RemoveAll(sh => sh.Range <= 0);

            enemies.RemoveAll(en => en.Status == EnemyStatus.Dead);

            // update the game objects
            player1shots.ForEach(sh => sh.Update(gameTime));
            player2shots.ForEach(sh => sh.Update(gameTime));
            enemyListController.shots.ForEach(shot => shot.Update(gameTime));

            enemyListController.Update(gameTime, targets);
            enemies.ForEach(en => en.Update(gameTime));

            if ((player1.Status != PlayerStatus.Disabled) && (player1.Status != PlayerStatus.Dead))
            {
                player1.Update(gameTime);
            }

            if ((player2.Status != PlayerStatus.Disabled) && (player2.Status != PlayerStatus.Dead))
            {
                player2.Update(gameTime);
            }

            arena.Player1Score = player1.Score;
            arena.Player1Lives = player1.Lives;

            arena.Player2Lives = player2.Score;
            arena.Player2Lives = player2.Lives;

            //base.Update(gameTime);
        }