using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using System; using System.Collections.Generic; namespace SemiColinGames { interface IPlayerState : IState> { public Vector2 Movement { get; } public void PostUpdate(bool standingOnGround); } class StandState : IPlayerState { private Vector2 result; // private double swordSwingTime = 0; // private int swordSwingNum = 0; private float ySpeed = 0; private int jumps = 1; private const int jumpSpeed = -600; private const int moveSpeed = 180; private const int gravity = 1600; public void Enter() { } public string Update(float modelTime, World world, History input) { result = new Vector2() { X = input[0].Motion.X * moveSpeed * modelTime }; if (input[0].Jump && !input[1].Jump && jumps > 0) { jumps--; ySpeed = jumpSpeed; } // if (input[0].Attack && !input[1].Attack && swordSwingTime <= 0) { // swordSwingTime = 0.3; // swordSwingNum++; // SoundEffects.SwordSwings[swordSwingNum % SoundEffects.SwordSwings.Length].Play(); // } result.Y = ySpeed * modelTime; ySpeed += gravity * modelTime; // swordSwingTime -= modelTime; if (input[0].IsAbsoluteMotion) { if (input[1].Motion.X == 0) { result.X = input[0].Motion.X; } else { result.X = 0; } } return null; } public Vector2 Movement { get { return result; } } // TODO: Maybe this should be Update(), and CalculateMovement() should be the Player-specific // function? public void PostUpdate(bool standingOnGround) { if (standingOnGround) { jumps = 1; ySpeed = -0.0001f; // Debug.AddRect(Box(position), Color.Cyan); } else { jumps = 0; // Debug.AddRect(Box(position), Color.Orange); } } } public class Player { private readonly FSM> fsm; // Details of the sprite image. // player_1x is 48 x 48, yOffset=5, halfSize=(7, 14) // Ninja_Female is 96 x 64, yOffset=1, halfSize=(11, 24) private const int spriteWidth = 96; private const int spriteHeight = 64; private const int spriteCenterYOffset = 1; // Details of the actual Player model. // Position is tracked at the Player's center. The Player's bounding box is a rectangle // centered at that point and extending out by halfSize.X and halfSize.Y. private Vector2 position; private Vector2 halfSize = new Vector2(11, 24); // Fractional-pixel movement that was left over from a previous frame's movement. // Useful so that we can run at a slow time-step and still get non-zero motion. private Vector2 residual = Vector2.Zero; private float invincibilityTime = 0; // For passing into Line.Rasterize() during movement updates. private readonly IList movePoints = new ProfilingList(64, "Player.movePoints"); // Possible hitboxes for player <-> obstacles. private readonly IList candidates = new ProfilingList(16, "Player.candidates"); public Player(Vector2 position, int facing) { this.position = position; Facing = facing; Health = MaxHealth; StandingOnGround = false; fsm = new FSM>("run", new Dictionary>> { { "run", new StandState() }, }); } public bool StandingOnGround { get; private set; } public int MaxHealth { get; private set; } = 3; public int Health { get; private set; } public int Facing { get; private set; } public Vector2 Position { get { return position; } } public void Update(float modelTime, World world, History input) { AABB BoxOffset(Vector2 position, int yOffset) { return new AABB(new Vector2(position.X, position.Y + yOffset), halfSize); } AABB Box(Vector2 position) { return BoxOffset(position, 0); } invincibilityTime -= modelTime; Vector2 inputMovement = HandleInput(modelTime, world, input); Vector2 movement = Vector2.Add(residual, inputMovement); residual = new Vector2(movement.X - (int) movement.X, movement.Y - (int) movement.Y); // Broad test: remove all collision targets nowhere near the player. candidates.Clear(); // Expand the box in the direction of movement. The center is the midpoint of the line // between the player's current position and their desired movement. The width increases by // the magnitude of the movement in each direction. We add 1 to each dimension just to be // sure (the only downside is a small number of false-positive AABBs, which should be // discarded by later tests anyhow.) AABB largeBox = new AABB( Vector2.Add(position, Vector2.Divide(movement, 2)), Vector2.Add(halfSize, new Vector2(Math.Abs(movement.X) + 1, Math.Abs(movement.Y) + 1))); foreach (var box in world.CollisionTargets) { if (box.Intersect(largeBox) != null) { // Debug.AddRect(box, Color.Green); candidates.Add(box); } } bool harmedByCollision = false; Line.Rasterize(0, 0, (int) movement.X, (int) movement.Y, movePoints); for (int i = 1; i < movePoints.Count; i++) { int dx = movePoints[i].X - movePoints[i - 1].X; int dy = movePoints[i].Y - movePoints[i - 1].Y; if (dy != 0) { Vector2 newPosition = new Vector2(position.X, position.Y + dy); AABB player = Box(newPosition); bool reject = false; foreach (var box in candidates) { if (box.Intersect(player) != null) { Debug.AddRect(box, Color.Cyan); reject = true; if (box.Tile?.IsHazard ?? false) { Debug.AddRect(box, Color.Red); harmedByCollision = true; } } } if (!reject) { position = newPosition; } } if (dx != 0) { Vector2 newPosition = new Vector2(position.X + dx, position.Y); AABB player = Box(newPosition); bool reject = false; foreach (var box in candidates) { if (box.Intersect(player) != null) { Debug.AddRect(box, Color.Cyan); reject = true; if (box.Tile?.IsHazard ?? false) { Debug.AddRect(box, Color.Red); harmedByCollision = true; } } } if (!reject) { position = newPosition; } } } StandingOnGround = false; AABB groundIntersect = BoxOffset(position, 1); foreach (var box in candidates) { if (groundIntersect.Intersect(box) != null) { Debug.AddRect(box, Color.Cyan); StandingOnGround = true; if (box.Tile?.IsHazard ?? false) { Debug.AddRect(box, Color.Red); harmedByCollision = true; } } } ((IPlayerState) fsm.State).PostUpdate(StandingOnGround); if (harmedByCollision && invincibilityTime <= 0) { world.ScreenShake(); Health -= 1; invincibilityTime = 0.6f; } if (inputMovement.X > 0) { Facing = 1; } else if (inputMovement.X < 0) { Facing = -1; } } // Returns the desired (dx, dy) for the player to move this frame. Vector2 HandleInput(float modelTime, World world, History input) { fsm.Update(modelTime, world, input); // TODO: remove ugly cast. return ((IPlayerState) fsm.State).Movement; } private Rectangle GetTextureSource() { double time = Clock.ModelTime.TotalSeconds; IPlayerState state = (IPlayerState) fsm.State; if (StandingOnGround && state.Movement.X == 0) { return Sprites.Ninja.GetTextureSource("idle", time); } else { return Sprites.Ninja.GetTextureSource("run", time); } } public void Draw(SpriteBatch spriteBatch) { Rectangle textureSource = GetTextureSource(); Vector2 spriteCenter = new Vector2(spriteWidth / 2, spriteHeight / 2 + spriteCenterYOffset); SpriteEffects effect = Facing == 1 ? SpriteEffects.None : SpriteEffects.FlipHorizontally; Color color = Color.White; if (invincibilityTime > 0 && invincibilityTime % 0.2f > 0.1f) { color = new Color(0.5f, 0.5f, 0.5f, 0.5f); } spriteBatch.Draw(Textures.Ninja.Get, Vector2.Floor(position), textureSource, color, 0f, spriteCenter, Vector2.One, effect, 0f); } } }