#region File Description
//-----------------------------------------------------------------------------
// PlayerPosition.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
#region Using Statements
using System;
using Microsoft.Xna.Framework;
using RolePlayingGameData;
#endregion
namespace RolePlaying
{
///
/// The position of a player in the tile engine.
///
/// Players are the only objects that move between tiles.
public class PlayerPosition
{
#region Position
///
/// Position in map coordinates (tiles).
///
public Point TilePosition = Point.Zero;
///
/// The offset into the tile, in pixels.
///
public Vector2 TileOffset = Vector2.Zero;
///
/// The position in screen coordinates.
///
public Vector2 ScreenPosition
{
get
{
return TileEngine.GetScreenPosition(TilePosition) + TileOffset;
}
}
#endregion
#region Direction
///
/// The direction that the player is facing.
///
public Direction Direction = Direction.South;
#endregion
#region Movement
///
/// If true, the position moved on the last update.
///
/// Used to control animation.
private bool isMoving = false;
///
/// If true, the position moved on the last update.
///
/// Used to control animation.
public bool IsMoving
{
get { return isMoving; }
}
///
/// Move the player by the given amount.
///
public void Move(Vector2 movement)
{
isMoving = (movement != Vector2.Zero);
CalculateMovement(movement, ref TilePosition, ref TileOffset);
// if the position is moving, up the direction
if (IsMoving)
{
Direction = CalculateDirection(movement);
}
}
#endregion
#region Calculation
///
/// Calculates the effect of movement on the position.
///
///
/// The movement to be used to calculate the new tile position.
///
/// The map position (tiles).
/// The offset into the current tile.
public static void CalculateMovement(Vector2 movement, ref Point TilePosition,
ref Vector2 TileOffset)
{
// add the movement
TileOffset += movement;
while (TileOffset.X > TileEngine.Map.TileSize.X / 2f)
{
TilePosition.X++;
TileOffset.X -= TileEngine.Map.TileSize.X;
}
while (TileOffset.X < -TileEngine.Map.TileSize.X / 2f)
{
TilePosition.X--;
TileOffset.X += TileEngine.Map.TileSize.X;
}
while (TileOffset.Y > TileEngine.Map.TileSize.Y / 2f)
{
TilePosition.Y++;
TileOffset.Y -= TileEngine.Map.TileSize.Y;
}
while (TileOffset.Y < -TileEngine.Map.TileSize.Y / 2f)
{
TilePosition.Y--;
TileOffset.Y += TileEngine.Map.TileSize.Y;
}
}
///
/// Determine the direction based on the given movement vector.
///
/// The vector that the player is moving.
/// The calculated direction.
public static Direction CalculateDirection(Vector2 vector)
{
if (vector.X > 0)
{
if (vector.Y > 0)
{
return Direction.SouthEast;
}
else if (vector.Y < 0)
{
return Direction.NorthEast;
}
else // y == 0
{
return Direction.East;
}
}
else if (vector.X < 0)
{
if (vector.Y > 0)
{
return Direction.SouthWest;
}
else if (vector.Y < 0)
{
return Direction.NorthWest;
}
else // y == 0
{
return Direction.West;
}
}
else // x == 0
{
if (vector.Y > 0)
{
return Direction.South;
}
else if (vector.Y < 0)
{
return Direction.North;
}
}
// x == 0 && y == 0, so... south?
return Direction.South;
}
#endregion
}
}