예제 #1
0
        public void UpdatePlayer(ref GraphicsDeviceManager graphics, GameInput input, Map map, List <Bullet> bullets)
        {
            Vector2 PositionDelta = input.MovementDirection * Config.PlayerMaxSpeed;

            if (PositionDelta != Vector2.Zero)
            {
                // Collision: Walls
                var playerBound = new BoundingSphere(new Vector3(Position + PositionDelta, 0), Radius);
                foreach (var wall in map.Walls)
                {
                    if (playerBound.Contains(wall.Bound) != ContainmentType.Disjoint)
                    {
                        bool ZeroX = false, ZeroY = false;
                        if (PositionDelta.X != 0)
                        {
                            var PositionX = Position + (PositionDelta * Vector2.UnitX);
                            var BoundForX = new BoundingSphere(new Vector3(PositionX, 0), Radius);
                            if (BoundForX.Contains(wall.Bound) != ContainmentType.Disjoint)
                            {
                                ZeroX = true;
                            }
                        }

                        if (PositionDelta.Y != 0)
                        {
                            var PositionY = Position + (PositionDelta * Vector2.UnitY);
                            var BoundForY = new BoundingSphere(new Vector3(PositionY, 0), Radius);
                            if (BoundForY.Contains(wall.Bound) != ContainmentType.Disjoint)
                            {
                                ZeroY = true;
                            }
                        }

                        if (ZeroX)
                        {
                            PositionDelta.X = 0;
                        }
                        if (ZeroY)
                        {
                            PositionDelta.Y = 0;
                        }

                        playerBound.Center = new Vector3(Position + PositionDelta, 0);
                        if (playerBound.Contains(wall.Bound) != ContainmentType.Disjoint)
                        {
                            Console.WriteLine($"[CollisionFail] Player {PlayerID} at {Position} + {PositionDelta} hits wall: {wall.Bound}!");
                            //PositionDelta = Vector2.Zero;
                        }

                        wall.ClippedInto = true;
                    }
                    else
                    {
                        wall.ClippedInto = false;
                    }
                }

                // Final position delta calculated!
                Position += PositionDelta;
            }

            // Reloading
            if (input.Reload && CanReload(map.AmmoPositions) && Ammo != Config.MaxAmmoAmount)
            {
                Ammo = Config.MaxAmmoAmount;
                AudioAtlas.Reload.Play();
            }

            // Aiming and shooting:
            Rotation = (float)(2 * Math.PI) - (float)(Math.Atan2(input.AimingDirection.X, input.AimingDirection.Y) + Math.PI);

            if (input.Shoot && Ammo > 0 && input.AimingDirection != Vector2.Zero)
            {
                if (!input.ShootPreviously)
                {
                    _singleShot = true;
                }
                else
                {
                    _continuousFire = true;
                }
            }
            else
            {
                _singleShot     = false;
                _continuousFire = false;
            }

            UpdateCollision(bullets);
        }