pass in AABBs instead of Rectangles to Player.Update
GitOrigin-RevId: 08fe1aaf34210415acaa41e7e407eb1275602889
This commit is contained in:
parent
946497160b
commit
793b292a29
@ -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);
|
||||
}
|
||||
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) {
|
||||
return Box(position, 0);
|
||||
}
|
||||
Aabb Box(Point position) {
|
||||
return BoxOffset(position, 0);
|
||||
}
|
||||
|
||||
private Aabb Box(Point position, int yOffset) {
|
||||
return new Aabb(new Vector2(position.X, position.Y - 7 + spriteHeight + yOffset),
|
||||
new Vector2(spriteWidth, spriteHeight));
|
||||
}
|
||||
|
||||
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) {
|
||||
|
@ -93,8 +93,7 @@ namespace SemiColinGames {
|
||||
if (!paused) {
|
||||
float modelTime = (float) gameTime.ElapsedGameTime.TotalSeconds;
|
||||
Clock.AddModelTime(modelTime);
|
||||
Rectangle[] collisionTargets = world.CollisionTargets;
|
||||
player.Update(modelTime, input, collisionTargets);
|
||||
player.Update(modelTime, input, world.CollisionTargets);
|
||||
camera.Update(player.Position);
|
||||
}
|
||||
|
||||
|
@ -83,7 +83,7 @@ namespace SemiColinGames {
|
||||
|
||||
public const int TileSize = 16;
|
||||
readonly Tile[] tiles;
|
||||
readonly Rectangle[] collisionTargets;
|
||||
readonly Aabb[] collisionTargets;
|
||||
|
||||
public int Width { get; private set; }
|
||||
public int Height { get; private set; }
|
||||
@ -153,9 +153,13 @@ namespace SemiColinGames {
|
||||
}
|
||||
}
|
||||
tiles = tilesList.ToArray();
|
||||
collisionTargets = new Rectangle[tiles.Length];
|
||||
collisionTargets = new Aabb[tiles.Length];
|
||||
|
||||
Vector2 halfSize = new Vector2(TileSize / 2, TileSize / 2);
|
||||
for (int i = 0; i < tiles.Length; i++) {
|
||||
collisionTargets[i] = tiles[i].Position;
|
||||
Vector2 center = new Vector2(
|
||||
tiles[i].Position.Left + halfSize.X, tiles[i].Position.Top + halfSize.Y);
|
||||
collisionTargets[i] = new Aabb(center, halfSize);
|
||||
}
|
||||
}
|
||||
|
||||
@ -165,7 +169,7 @@ namespace SemiColinGames {
|
||||
}
|
||||
}
|
||||
|
||||
public Rectangle[] CollisionTargets {
|
||||
public Aabb[] CollisionTargets {
|
||||
get { return collisionTargets; }
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user