|
|
@ -37,38 +37,29 @@ namespace SemiColinGames { |
|
|
|
|
|
|
|
public Point Position { get { return position; } } |
|
|
|
|
|
|
|
private Rectangle Bbox(Point position) { |
|
|
|
return new Rectangle(position.X - spriteWidth, position.Y - 7, spriteWidth * 2, 26); |
|
|
|
} |
|
|
|
|
|
|
|
private Aabb Box(Point position) { |
|
|
|
return Box(position, 0); |
|
|
|
} |
|
|
|
public void Update(float modelTime, History<Input> input, Aabb[] collisionTargets) { |
|
|
|
Aabb BoxOffset(Point position, int yOffset) { |
|
|
|
return new Aabb(new Vector2(position.X, position.Y - 7 + spriteHeight + yOffset), |
|
|
|
new Vector2(spriteWidth, spriteHeight)); |
|
|
|
} |
|
|
|
|
|
|
|
private Aabb Box(Point position, int yOffset) { |
|
|
|
return new Aabb(new Vector2(position.X, position.Y - 7 + spriteHeight + yOffset), |
|
|
|
new Vector2(spriteWidth, spriteHeight)); |
|
|
|
} |
|
|
|
Aabb Box(Point position) { |
|
|
|
return BoxOffset(position, 0); |
|
|
|
} |
|
|
|
|
|
|
|
public void Update(float modelTime, History<Input> input, Rectangle[] collisionTargets) { |
|
|
|
Vector2 movement = HandleInput(modelTime, input); |
|
|
|
|
|
|
|
// TODO: we shouldn't hardcode the tile sizes here.
|
|
|
|
Vector2 halfBoxSize = new Vector2(World.TileSize / 2, World.TileSize / 2); |
|
|
|
// Broad test: remove all collision targets nowhere near the player.
|
|
|
|
List<Rectangle> candidates = new List<Rectangle>(); |
|
|
|
var candidates = new List<Aabb>(); |
|
|
|
// TODO: This is strictly larger than it needs to be. We could expand only in the actual
|
|
|
|
// direction of movement.
|
|
|
|
Aabb largeBox = new Aabb( |
|
|
|
new Vector2(position.X, position.Y - 7 + spriteHeight), // current player position
|
|
|
|
new Vector2(spriteWidth + Math.Abs(movement.X), spriteHeight + Math.Abs(movement.Y))); |
|
|
|
for (int i = 0; i < collisionTargets.Length; i++) { |
|
|
|
Rectangle rect = collisionTargets[i]; |
|
|
|
Aabb box = new Aabb( |
|
|
|
new Vector2(rect.X + World.TileSize / 2, rect.Y + World.TileSize / 2), halfBoxSize); |
|
|
|
foreach (var box in collisionTargets) { |
|
|
|
if (box.Intersect(largeBox) != null) { |
|
|
|
Debug.AddRect(box, Color.Green); |
|
|
|
candidates.Add(rect); |
|
|
|
candidates.Add(box); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -80,9 +71,7 @@ namespace SemiColinGames { |
|
|
|
Point newPosition = new Point(position.X, position.Y + dy); |
|
|
|
Aabb player = Box(newPosition); |
|
|
|
bool reject = false; |
|
|
|
foreach (var rect in candidates) { |
|
|
|
Aabb box = new Aabb( |
|
|
|
new Vector2(rect.X + World.TileSize / 2, rect.Y + World.TileSize / 2), halfBoxSize); |
|
|
|
foreach (var box in candidates) { |
|
|
|
if (box.Intersect(player) != null) { |
|
|
|
reject = true; |
|
|
|
break; |
|
|
@ -96,9 +85,7 @@ namespace SemiColinGames { |
|
|
|
Point newPosition = new Point(position.X + dx, position.Y); |
|
|
|
Aabb player = Box(newPosition); |
|
|
|
bool reject = false; |
|
|
|
foreach (var rect in candidates) { |
|
|
|
Aabb box = new Aabb( |
|
|
|
new Vector2(rect.X + World.TileSize / 2, rect.Y + World.TileSize / 2), halfBoxSize); |
|
|
|
foreach (var box in candidates) { |
|
|
|
if (box.Intersect(player) != null) { |
|
|
|
reject = true; |
|
|
|
break; |
|
|
@ -111,24 +98,21 @@ namespace SemiColinGames { |
|
|
|
} |
|
|
|
|
|
|
|
bool standingOnGround = false; |
|
|
|
Aabb groundIntersect = Box(position, 1); |
|
|
|
foreach (var rect in candidates) { |
|
|
|
Aabb box = new Aabb( |
|
|
|
new Vector2(rect.X + World.TileSize / 2, rect.Y + World.TileSize / 2), halfBoxSize); |
|
|
|
Aabb groundIntersect = BoxOffset(position, 1); |
|
|
|
foreach (var box in candidates) { |
|
|
|
if (groundIntersect.Intersect(box) != null) { |
|
|
|
standingOnGround = true; |
|
|
|
Debug.AddRect(rect, Color.Cyan); |
|
|
|
// break;
|
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (standingOnGround) { |
|
|
|
jumps = 1; |
|
|
|
ySpeed = -0.0001f; |
|
|
|
// Debug.AddRect(playerBbox, Color.Red);
|
|
|
|
Debug.AddRect(Box(position), Color.Cyan); |
|
|
|
} else { |
|
|
|
jumps = 0; |
|
|
|
// Debug.AddRect(playerBbox, Color.Orange);
|
|
|
|
Debug.AddRect(Box(position), Color.Orange); |
|
|
|
} |
|
|
|
|
|
|
|
if (movement.X > 0) { |
|
|
|