-
Notifications
You must be signed in to change notification settings - Fork 1
/
Map.ts
84 lines (74 loc) · 2.57 KB
/
Map.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
interface IMap {
canMoveTo(x: number, y: number): boolean;
isWall(x: number, y: number): boolean;
getWidth(): number;
getHeight(): number;
draw(ctx: CanvasRenderingContext2D, cellwidth: number, cellheight: number) : void;
}
interface GoalMap extends IMap {
isGoal(x: number, y: number): boolean;
}
abstract class AMap implements IMap {
walls: boolean[][];
width: number;
height: number;
surroundingWalls(): void {
for (var x = 0; x < this.width; x++) {
for (var y = 0; y < this.height; y++) {
if (x == 0 || x == this.width - 1 || y == 0 || y == this.height - 1) {
this.walls[x][y] = true;
}
}
}
}
constructor(width: number, height: number) {
this.width = width;
this.height = height;
this.walls = emptybools(width, height);
this.surroundingWalls();
}
getWidth(): number { return this.width; }
getHeight(): number { return this.height; }
canMoveTo(x: number, y: number): boolean {
return x >= 0 && y >= 0 && x < this.width && y < this.height&&!(this.walls[x][y]);
}
isWall(x: number, y: number): boolean {
return x < 0 || y < 0 || x >= this.width || y >= this.height||(this.walls[x][y]);
}
draw(ctx: CanvasRenderingContext2D, cellwidth : number, cellheight : number): void {
ctx.fillStyle = WALLCOLOR;
for (var x = 0; x < this.width; x++) {
for (var y = 0; y < this.height; y++) {
var sy = y;
while (this.walls[x][y]) {
y++;
}
if (sy != y) {
ctx.fillRect(x * cellwidth, sy * cellheight, cellwidth, (y - sy) * cellheight);
}
}
}
}
}
abstract class AGoalMap extends AMap implements GoalMap {
abstract getGoalZones(): { sx: number, sy: number, ex: number, ey: number }[];
isGoal(x: number, y: number): boolean {
var gzs = this.getGoalZones();
for (let gz of gzs) {
if (x >= gz.sx && x <= gz.ex && y >= gz.sy && y <= gz.ey) {
return true;
}
}
return false;
}
draw(ctx: CanvasRenderingContext2D, cellwidth: number, cellheight: number): void {
super.draw(ctx, cellwidth, cellheight);
ctx.fillStyle = "#000000";
for (let gz of this.getGoalZones()) {
drawFinish(ctx, gz.sx * cellwidth, gz.sy * cellheight, (gz.ex+1) * cellwidth, (gz.ey+1) * cellheight);
}
}
}
abstract class AVacuumMap extends AMap {
}