make CollisionTargets an array, and don't recompute it every frame

GitOrigin-RevId: 92a02231edae5729778ea9ac60a46c83e248c744
This commit is contained in:
Colin McMillen 2020-01-27 21:58:48 -05:00
parent 644ed88dd8
commit 61b50efa40
3 changed files with 13 additions and 10 deletions

View File

@ -33,7 +33,7 @@ namespace SemiColinGames {
return new Rectangle(position.X - spriteWidth, position.Y - 7, spriteWidth * 2, 26); return new Rectangle(position.X - spriteWidth, position.Y - 7, spriteWidth * 2, 26);
} }
public void Update(float modelTime, History<Input> input, List<Rectangle> collisionTargets) { public void Update(float modelTime, History<Input> input, Rectangle[] collisionTargets) {
Point oldPosition = position; Point oldPosition = position;
Vector2 movement = HandleInput(modelTime, input); Vector2 movement = HandleInput(modelTime, input);
position = new Point((int) (oldPosition.X + movement.X), (int) (oldPosition.Y + movement.Y)); position = new Point((int) (oldPosition.X + movement.X), (int) (oldPosition.Y + movement.Y));

View File

@ -93,7 +93,7 @@ namespace SemiColinGames {
if (!paused) { if (!paused) {
float modelTime = (float) gameTime.ElapsedGameTime.TotalSeconds; float modelTime = (float) gameTime.ElapsedGameTime.TotalSeconds;
Clock.AddModelTime(modelTime); Clock.AddModelTime(modelTime);
List<Rectangle> collisionTargets = world.CollisionTargets(); Rectangle[] collisionTargets = world.CollisionTargets;
player.Update(modelTime, input, collisionTargets); player.Update(modelTime, input, collisionTargets);
camera.Update(player.Position); camera.Update(player.Position);
} }

View File

@ -82,7 +82,8 @@ namespace SemiColinGames {
class World { class World {
public const int TileSize = 16; public const int TileSize = 16;
readonly List<Tile> tiles = new List<Tile>(); readonly Tile[] tiles;
readonly Rectangle[] collisionTargets;
public int Width { get; private set; } public int Width { get; private set; }
public int Height { get; private set; } public int Height { get; private set; }
@ -106,6 +107,7 @@ namespace SemiColinGames {
....................] [............................................] [.............] [..............................................................] [......................................................."; ....................] [............................................] [.............] [..............................................................] [.......................................................";
public World(Texture2D texture) { public World(Texture2D texture) {
var tilesList = new List<Tile>();
string[] worldDesc = worldString.Split('\n'); string[] worldDesc = worldString.Split('\n');
Width = worldDesc.AsQueryable().Max(a => a.Length); Width = worldDesc.AsQueryable().Max(a => a.Length);
Height = worldDesc.Length; Height = worldDesc.Length;
@ -146,10 +148,15 @@ namespace SemiColinGames {
} }
if (terrain != Terrain.Empty) { if (terrain != Terrain.Empty) {
var position = new Rectangle(i * TileSize, j * TileSize, TileSize, TileSize); var position = new Rectangle(i * TileSize, j * TileSize, TileSize, TileSize);
tiles.Add(new Tile(texture, terrain, position)); tilesList.Add(new Tile(texture, terrain, position));
} }
} }
} }
tiles = tilesList.ToArray();
collisionTargets = new Rectangle[tiles.Length];
for (int i = 0; i < tiles.Length; i++) {
collisionTargets[i] = tiles[i].Position;
}
} }
public void Draw(SpriteBatch spriteBatch, Camera camera) { public void Draw(SpriteBatch spriteBatch, Camera camera) {
@ -158,12 +165,8 @@ namespace SemiColinGames {
} }
} }
public List<Rectangle> CollisionTargets() { public Rectangle[] CollisionTargets {
var result = new List<Rectangle>(); get { return collisionTargets; }
foreach (Tile t in tiles) {
result.Add(t.Position);
}
return result;
} }
} }
} }