cleanup: sprite{Height,Width} -> halfSize

GitOrigin-RevId: 5620d26f72ba46c74eb7f5335ad1d2b9a3b29602
This commit is contained in:
Colin McMillen 2020-01-29 16:45:13 -05:00
parent 50479bac3c
commit ab62e8929a

View File

@ -17,13 +17,18 @@ namespace SemiColinGames {
private const int jumpSpeed = -600; private const int jumpSpeed = -600;
private const int gravity = 2400; private const int gravity = 2400;
// Details of the sprite image.
private const int spriteSize = 48; private const int spriteSize = 48;
// TODO: rename to spriteHalfWidth / spriteHalfHeight. private const int spriteCenterYOffset = 5;
private const int spriteWidth = 7;
private const int spriteHeight = 13;
private readonly Texture2D texture; private readonly Texture2D texture;
private Point position = new Point(64, 16 * 10); // 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 Point position = new Point(64, 16 * 13);
private Vector2 halfSize = new Vector2(7, 14);
private int jumps = 0; private int jumps = 0;
private Facing facing = Facing.Right; private Facing facing = Facing.Right;
private Pose pose = Pose.Jumping; private Pose pose = Pose.Jumping;
@ -39,8 +44,7 @@ namespace SemiColinGames {
public void Update(float modelTime, History<Input> input, Aabb[] collisionTargets) { public void Update(float modelTime, History<Input> input, Aabb[] collisionTargets) {
Aabb BoxOffset(Point position, int yOffset) { Aabb BoxOffset(Point position, int yOffset) {
return new Aabb(new Vector2(position.X, position.Y - 7 + spriteHeight + yOffset), return new Aabb(new Vector2(position.X, position.Y + yOffset), halfSize);
new Vector2(spriteWidth, spriteHeight));
} }
Aabb Box(Point position) { Aabb Box(Point position) {
@ -54,8 +58,8 @@ namespace SemiColinGames {
// TODO: This is strictly larger than it needs to be. We could expand only in the actual // TODO: This is strictly larger than it needs to be. We could expand only in the actual
// direction of movement. // direction of movement.
Aabb largeBox = new Aabb( Aabb largeBox = new Aabb(
new Vector2(position.X, position.Y - 7 + spriteHeight), // current player position new Vector2(position.X, position.Y), // current player position
new Vector2(spriteWidth + Math.Abs(movement.X), spriteHeight + Math.Abs(movement.Y))); new Vector2(halfSize.X + Math.Abs(movement.X), halfSize.Y + Math.Abs(movement.Y)));
foreach (var box in collisionTargets) { foreach (var box in collisionTargets) {
if (box.Intersect(largeBox) != null) { if (box.Intersect(largeBox) != null) {
Debug.AddRect(box, Color.Green); Debug.AddRect(box, Color.Green);
@ -195,7 +199,7 @@ namespace SemiColinGames {
public void Draw(SpriteBatch spriteBatch, Camera camera) { public void Draw(SpriteBatch spriteBatch, Camera camera) {
int index = SpriteIndex(pose); int index = SpriteIndex(pose);
Rectangle textureSource = new Rectangle(index * spriteSize, 0, spriteSize, spriteSize); Rectangle textureSource = new Rectangle(index * spriteSize, 0, spriteSize, spriteSize);
Vector2 spriteCenter = new Vector2(spriteSize / 2, spriteSize / 2); Vector2 spriteCenter = new Vector2(spriteSize / 2, spriteSize / 2 + spriteCenterYOffset);
SpriteEffects effect = facing == Facing.Right ? SpriteEffects effect = facing == Facing.Right ?
SpriteEffects.FlipHorizontally : SpriteEffects.None; SpriteEffects.FlipHorizontally : SpriteEffects.None;
Vector2 drawPos = new Vector2(position.X - camera.Left, position.Y); Vector2 drawPos = new Vector2(position.X - camera.Left, position.Y);