dartterm/lib/gen/vault.dart

125 lines
3.0 KiB
Dart
Raw Normal View History

part of 'generator.dart';
class Vault {
final int vx, vy;
final DirectionSet smooth;
final List<LevelTile> tiles;
Vault(this.vx, this.vy, this.smooth, this.tiles) {
assert(tiles.length == vx * vy);
}
// TODO: We should be assessing this based on whether the pattern in the input
// PNG had right-angled borders on this side, not based on the literal
// presence or absence of walls
//
// In other words, this is wrong.
static Vault fromVaultData(int vx, int vy, List<LevelTile> tiles) {
assert(tiles.length == vx * vy);
var smooth = {
Direction.up,
Direction.left,
Direction.down,
Direction.right
};
for (var x = 0; x < vx; x++) {
if (tiles[x + 0 * vx] == LevelTile.wall) {
smooth.remove(Direction.up);
break;
}
}
for (var x = 0; x < vx; x++) {
if (tiles[x + (vy - 1) * vx] == LevelTile.wall) {
smooth.remove(Direction.down);
break;
}
}
for (var y = 0; y < vy; y++) {
if (tiles[0 + y * vx] == LevelTile.wall) {
smooth.remove(Direction.left);
break;
}
}
for (var y = 0; y < vy; y++) {
if (tiles[vx - 1 + y * vx] == LevelTile.wall) {
smooth.remove(Direction.right);
break;
}
}
return Vault(vx, vy, DirectionSet(smooth), tiles);
}
void clear(LevelTile lt) {
for (var y = 0; y < vy; y++) {
for (var x = 0; x < vx; x++) {
tiles[y * vx + x] = lt;
}
}
}
void blitFrom(Vault other, int dx, int dy) {
assert(dx >= 0);
assert(dy >= 0);
assert(dx + other.vx <= vx);
assert(dy + other.vy <= vy);
for (var x = 0; x < other.vx; x++) {
for (var y = 0; y < other.vy; y++) {
tiles[(y + dy) * vx + x + dx] = other.tiles[y * other.vx + x];
}
}
}
Vault flip() {
List<LevelTile> tiles2 = [
for (var y = 0; y < vy; y++)
for (var x = vx - 1; x >= 0; x--) tiles[y * vx + x]
];
return Vault(vx, vy, smooth.flip(), tiles2);
}
// TODO: Actually test this logic.
// This worked in Python, so it might even be right!
Vault rotateRight() {
List<LevelTile> tiles2 = [
for (var x = 0; x < vx; x++)
for (var y = 0; y < vy; y++) tiles[(vy - 1 - y) * vx + x]
];
return Vault(vy, vx, smooth.rotateRight(), tiles2);
}
Vault rotateLeft() {
List<LevelTile> tiles2 = [
for (var x = vx - 1; x >= 0; x++)
for (var y = vy - 1; y >= 0; y++) tiles[y * vx + (vx - 1 - x)]
];
return Vault(vy, vx, smooth.rotateLeft(), tiles2);
}
Vault reorient(int r) {
assert(r >= 0 && r < 8);
Vault o = this;
while (r >= 2) {
o = o.rotateRight();
r -= 2;
}
if (r == 1) {
o = o.flip();
r -= 1;
}
return o;
}
static Vault blank(int vx, int vy, DirectionSet smooth, LevelTile lt) {
var tiles = [
for (var y = 0; y < vy; y++)
for (var x = 0; x < vx; x++) lt
];
return Vault(vx, vy, smooth, tiles);
}
}