generify IsInBounds and do a Copy sanity check

This commit is contained in:
Ellpeck 2020-06-22 00:12:46 +02:00
parent 3d77da076f
commit 4742d90ba5

View file

@ -43,12 +43,13 @@ namespace TouchyTickets {
var random = new Random(); var random = new Random();
for (var x = -AdditionalRadius; x < this.Width + AdditionalRadius; x++) { for (var x = -AdditionalRadius; x < this.Width + AdditionalRadius; x++) {
for (var y = -AdditionalRadius; y < this.Height + AdditionalRadius; y++) { for (var y = -AdditionalRadius; y < this.Height + AdditionalRadius; y++) {
if (x >= 0 && y >= 0 && x < this.Width && y < this.Height) var pos = new Point(x, y);
if (this.IsInBounds(pos))
continue; continue;
if (random.Next(15) != 0) if (random.Next(15) != 0)
continue; continue;
var type = random.Next(3); var type = random.Next(3);
this.treePositions[new Point(x, y)] = type; this.treePositions[pos] = type;
} }
} }
@ -91,7 +92,7 @@ namespace TouchyTickets {
var nextPos = (camera.ToWorldPos(drag.Position + drag.Delta) / Attraction.TileSize).ToPoint(); var nextPos = (camera.ToWorldPos(drag.Position + drag.Delta) / Attraction.TileSize).ToPoint();
// drag the center of the attraction // drag the center of the attraction
nextPos -= new Point(this.PlacingAttraction.Type.Width / 2, this.PlacingAttraction.Type.Height / 2); nextPos -= new Point(this.PlacingAttraction.Type.Width / 2, this.PlacingAttraction.Type.Height / 2);
if (this.PlacingAttraction.Type.GetCoveredTiles().Select(p => nextPos + p).All(p => p.X >= 0 && p.Y >= 0 && p.X < this.Width && p.Y < this.Height)) if (this.PlacingAttraction.Type.GetCoveredTiles().Select(p => nextPos + p).All(this.IsInBounds))
this.PlacingPosition = nextPos; this.PlacingPosition = nextPos;
} else { } else {
// move the camera // move the camera
@ -171,6 +172,8 @@ namespace TouchyTickets {
} }
public bool CanPlace(Point position, Attraction attraction) { public bool CanPlace(Point position, Attraction attraction) {
if (!this.IsInBounds(position))
return false;
foreach (var offset in attraction.Type.GetCoveredTiles()) { foreach (var offset in attraction.Type.GetCoveredTiles()) {
if (this.GetAttractionAt(position + offset) != null) if (this.GetAttractionAt(position + offset) != null)
return false; return false;
@ -195,9 +198,7 @@ namespace TouchyTickets {
} }
public Attraction GetAttractionAt(Point position) { public Attraction GetAttractionAt(Point position) {
if (position.X < 0 || position.Y < 0 || position.X >= this.Width || position.Y >= this.Height) return !this.IsInBounds(position) ? null : this.attractionGrid[position.X, position.Y];
return null;
return this.attractionGrid[position.X, position.Y];
} }
public int GetAttractionAmount(AttractionType type) { public int GetAttractionAmount(AttractionType type) {
@ -212,10 +213,16 @@ namespace TouchyTickets {
return this.attractions.Any(a => modifier.IsAffected(a.Item2)); return this.attractions.Any(a => modifier.IsAffected(a.Item2));
} }
public bool IsInBounds(Point pos) {
return pos.X >= 0 && pos.Y >= 0 && pos.X < this.Width && pos.Y < this.Height;
}
public ParkMap Copy(int? newWidth = null, int? newHeight = null) { public ParkMap Copy(int? newWidth = null, int? newHeight = null) {
var newMap = new ParkMap(newWidth ?? this.Width, newHeight ?? this.Height); var newMap = new ParkMap(newWidth ?? this.Width, newHeight ?? this.Height);
foreach (var (pos, attraction) in this.attractions) foreach (var (pos, attraction) in this.attractions) {
if (newMap.CanPlace(pos, attraction))
newMap.Place(pos, attraction); newMap.Place(pos, attraction);
}
return newMap; return newMap;
} }