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();
for (var x = -AdditionalRadius; x < this.Width + AdditionalRadius; x++) {
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;
if (random.Next(15) != 0)
continue;
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();
// drag the center of the attraction
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;
} else {
// move the camera
@ -171,6 +172,8 @@ namespace TouchyTickets {
}
public bool CanPlace(Point position, Attraction attraction) {
if (!this.IsInBounds(position))
return false;
foreach (var offset in attraction.Type.GetCoveredTiles()) {
if (this.GetAttractionAt(position + offset) != null)
return false;
@ -195,9 +198,7 @@ namespace TouchyTickets {
}
public Attraction GetAttractionAt(Point position) {
if (position.X < 0 || position.Y < 0 || position.X >= this.Width || position.Y >= this.Height)
return null;
return this.attractionGrid[position.X, position.Y];
return !this.IsInBounds(position) ? null : this.attractionGrid[position.X, position.Y];
}
public int GetAttractionAmount(AttractionType type) {
@ -212,10 +213,16 @@ namespace TouchyTickets {
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) {
var newMap = new ParkMap(newWidth ?? this.Width, newHeight ?? this.Height);
foreach (var (pos, attraction) in this.attractions)
newMap.Place(pos, attraction);
foreach (var (pos, attraction) in this.attractions) {
if (newMap.CanPlace(pos, attraction))
newMap.Place(pos, attraction);
}
return newMap;
}