Compare commits

...

2 Commits

Author SHA1 Message Date
36e70989af rename "tiles" to "obstacles" 2020-03-08 18:36:42 -04:00
fa4784fc49 IsHarmful -> IsHazard 2020-03-08 18:30:46 -04:00
2 changed files with 17 additions and 15 deletions

View File

@ -94,7 +94,7 @@ namespace SemiColinGames {
if (box.Intersect(player) != null) { if (box.Intersect(player) != null) {
Debug.AddRect(box, Color.Cyan); Debug.AddRect(box, Color.Cyan);
reject = true; reject = true;
if (box.Tile?.IsHarmful ?? false) { if (box.Tile?.IsHazard ?? false) {
Debug.AddRect(box, Color.Red); Debug.AddRect(box, Color.Red);
harmedByCollision = true; harmedByCollision = true;
} }
@ -112,7 +112,7 @@ namespace SemiColinGames {
if (box.Intersect(player) != null) { if (box.Intersect(player) != null) {
Debug.AddRect(box, Color.Cyan); Debug.AddRect(box, Color.Cyan);
reject = true; reject = true;
if (box.Tile?.IsHarmful ?? false) { if (box.Tile?.IsHazard ?? false) {
Debug.AddRect(box, Color.Red); Debug.AddRect(box, Color.Red);
harmedByCollision = true; harmedByCollision = true;
} }
@ -130,7 +130,7 @@ namespace SemiColinGames {
if (groundIntersect.Intersect(box) != null) { if (groundIntersect.Intersect(box) != null) {
Debug.AddRect(box, Color.Cyan); Debug.AddRect(box, Color.Cyan);
standingOnGround = true; standingOnGround = true;
if (box.Tile?.IsHarmful ?? false) { if (box.Tile?.IsHazard ?? false) {
Debug.AddRect(box, Color.Red); Debug.AddRect(box, Color.Red);
harmedByCollision = true; harmedByCollision = true;
} }

View File

@ -15,11 +15,11 @@ namespace SemiColinGames {
Position = position; Position = position;
this.texture = texture; this.texture = texture;
this.textureSource = textureSource; this.textureSource = textureSource;
IsHarmful = isHazard; IsHazard = isHazard;
} }
public Rectangle Position { get; private set; } public Rectangle Position { get; private set; }
public bool IsHarmful = false; public bool IsHazard = false;
public void Draw(SpriteBatch spriteBatch) { public void Draw(SpriteBatch spriteBatch) {
spriteBatch.Draw( spriteBatch.Draw(
@ -35,7 +35,7 @@ namespace SemiColinGames {
// TODO: remove this. // TODO: remove this.
public const int TileSize = 16; public const int TileSize = 16;
readonly Tile[] tiles; readonly Tile[] obstacles;
readonly Tile[] decorations; readonly Tile[] decorations;
// Kept around for resetting the world's entities after player death or level restart. // Kept around for resetting the world's entities after player death or level restart.
readonly JToken entitiesLayer; readonly JToken entitiesLayer;
@ -134,8 +134,8 @@ namespace SemiColinGames {
} }
// Get all the obstacles into a single array, sorted by X. // Get all the obstacles into a single array, sorted by X.
obstacleTiles.AddRange(hazardTiles); obstacleTiles.AddRange(hazardTiles);
tiles = obstacleTiles.ToArray(); obstacles = obstacleTiles.ToArray();
Array.Sort(tiles, CompareByX); Array.Sort(obstacles, CompareByX);
// The background tiles are added before the rest of the decorations, so that they're drawn // The background tiles are added before the rest of the decorations, so that they're drawn
// in the back. // in the back.
backgroundTiles.AddRange(decorationTiles); backgroundTiles.AddRange(decorationTiles);
@ -144,21 +144,21 @@ namespace SemiColinGames {
// The obstacles are sorted by x-position. We maintain this invariant so that it's possible // The obstacles are sorted by x-position. We maintain this invariant so that it's possible
// to efficiently find CollisionTargets that are nearby a given x-position. // to efficiently find CollisionTargets that are nearby a given x-position.
CollisionTargets = new AABB[tiles.Length + 2]; CollisionTargets = new AABB[obstacles.Length + 2];
// Add a synthetic collisionTarget on the left side of the world. // Add a synthetic collisionTarget on the left side of the world.
CollisionTargets[0] = new AABB(new Vector2(-1, 0), new Vector2(1, float.MaxValue)); CollisionTargets[0] = new AABB(new Vector2(-1, 0), new Vector2(1, float.MaxValue));
// Now add all the normal collisionTargets for every static terrain tile. // Now add all the normal collisionTargets for every obstacle.
Vector2 halfSize = new Vector2(TileSize / 2, TileSize / 2); Vector2 halfSize = new Vector2(TileSize / 2, TileSize / 2);
for (int i = 0; i < tiles.Length; i++) { for (int i = 0; i < obstacles.Length; i++) {
Vector2 center = new Vector2( Vector2 center = new Vector2(
tiles[i].Position.Left + halfSize.X, tiles[i].Position.Top + halfSize.Y); obstacles[i].Position.Left + halfSize.X, obstacles[i].Position.Top + halfSize.Y);
CollisionTargets[i + 1] = new AABB(center, halfSize, tiles[i]); CollisionTargets[i + 1] = new AABB(center, halfSize, obstacles[i]);
} }
// Add a final synthetic collisionTarget on the right side of the world. // Add a final synthetic collisionTarget on the right side of the world.
CollisionTargets[tiles.Length + 1] = new AABB( CollisionTargets[obstacles.Length + 1] = new AABB(
new Vector2(Width + 1, 0), new Vector2(1, float.MaxValue)); new Vector2(Width + 1, 0), new Vector2(1, float.MaxValue));
} }
@ -176,6 +176,7 @@ namespace SemiColinGames {
(Player, npcs) = ParseEntities(entitiesLayer); (Player, npcs) = ParseEntities(entitiesLayer);
} }
// Draws everything that's behind the player, from back to front.
public void DrawBackground(SpriteBatch spriteBatch) { public void DrawBackground(SpriteBatch spriteBatch) {
foreach (Tile t in decorations) { foreach (Tile t in decorations) {
t.Draw(spriteBatch); t.Draw(spriteBatch);
@ -185,8 +186,9 @@ namespace SemiColinGames {
} }
} }
// Draws everything that's in front of the player, from back to front.
public void DrawForeground(SpriteBatch spriteBatch) { public void DrawForeground(SpriteBatch spriteBatch) {
foreach (Tile t in tiles) { foreach (Tile t in obstacles) {
t.Draw(spriteBatch); t.Draw(spriteBatch);
} }
} }