Bound camera by right edge of World.

Fixes #32.

GitOrigin-RevId: e193c89509be035d5f6e899cd8c33ff2534a777f
This commit is contained in:
Colin McMillen 2020-01-29 18:01:41 -05:00
parent fe64ec705d
commit 76dbdc6913
3 changed files with 19 additions and 10 deletions

View File

@ -11,7 +11,7 @@ namespace SemiColinGames {
public int Height { get => bbox.Height; } public int Height { get => bbox.Height; }
public int Left { get => bbox.Left; } public int Left { get => bbox.Left; }
public void Update(Point player) { public void Update(Point player, int worldWidth) {
int diff = player.X - bbox.Center.X; int diff = player.X - bbox.Center.X;
if (Math.Abs(diff) > 16) { if (Math.Abs(diff) > 16) {
bbox.Offset((int) (diff * 0.1), 0); bbox.Offset((int) (diff * 0.1), 0);
@ -19,6 +19,9 @@ namespace SemiColinGames {
if (bbox.Left < 0) { if (bbox.Left < 0) {
bbox.Offset(-bbox.Left, 0); bbox.Offset(-bbox.Left, 0);
} }
if (bbox.Right > worldWidth) {
bbox.Offset(worldWidth - bbox.Right, 0);
}
Debug.AddToast($"p: {player.X}, {player.Y} c: {bbox.Center.X}"); Debug.AddToast($"p: {player.X}, {player.Y} c: {bbox.Center.X}");
} }
} }

View File

@ -93,7 +93,7 @@ namespace SemiColinGames {
float modelTime = (float) gameTime.ElapsedGameTime.TotalSeconds; float modelTime = (float) gameTime.ElapsedGameTime.TotalSeconds;
Clock.AddModelTime(modelTime); Clock.AddModelTime(modelTime);
player.Update(modelTime, input, world.CollisionTargets); player.Update(modelTime, input, world.CollisionTargets);
camera.Update(player.Position); camera.Update(player.Position, world.Width);
} }
base.Update(gameTime); base.Update(gameTime);

View File

@ -85,8 +85,14 @@ namespace SemiColinGames {
readonly Tile[] tiles; readonly Tile[] tiles;
readonly Aabb[] collisionTargets; readonly Aabb[] collisionTargets;
public int Width { get; private set; } // Size of World in terms of tile grid.
public int Height { get; private set; } private readonly int tileWidth;
private readonly int tileHeight;
// Size of World in pixels.
public int Width {
get { return tileWidth * TileSize; }
}
readonly string worldString = @" readonly string worldString = @"
@ -109,11 +115,11 @@ namespace SemiColinGames {
public World(Texture2D texture) { public World(Texture2D texture) {
var tilesList = new List<Tile>(); var tilesList = new List<Tile>();
string[] worldDesc = worldString.Split('\n'); string[] worldDesc = worldString.Split('\n');
Width = worldDesc.AsQueryable().Max(a => a.Length); tileWidth = worldDesc.AsQueryable().Max(a => a.Length);
Height = worldDesc.Length; tileHeight = worldDesc.Length;
Debug.WriteLine("world size: {0}x{1}", Width, Height); Debug.WriteLine("world size: {0}x{1}", tileWidth, tileHeight);
for (int i = 0; i < Width; i++) { for (int i = 0; i < tileWidth; i++) {
for (int j = 0; j < Height; j++) { for (int j = 0; j < tileHeight; j++) {
Terrain terrain = Terrain.Empty; Terrain terrain = Terrain.Empty;
if (i < worldDesc[j].Length) { if (i < worldDesc[j].Length) {
switch (worldDesc[j][i]) { switch (worldDesc[j][i]) {
@ -172,7 +178,7 @@ namespace SemiColinGames {
// 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[tiles.Length + 1] = new Aabb(
new Vector2(Width * TileSize + 1, 0), new Vector2(1, float.MaxValue)); new Vector2(Width + 1, 0), new Vector2(1, float.MaxValue));
} }
public void Draw(SpriteBatch spriteBatch, Camera camera) { public void Draw(SpriteBatch spriteBatch, Camera camera) {