-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapCellsPortion.js
159 lines (123 loc) · 5.26 KB
/
mapCellsPortion.js
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const LOADEDMAP_SIZE = 32;
const SAFEZONE_SIZE = 16;
const defautBackgroundColor = '0xAAFF88';
import { Cell } from './cell.js';
/**
* the area of the map that is loaded (the sprites)
*/
export class MapCellsPortion {
cells = []; //cells that are loaded (instance of the class Cell)
backgroundColorRects = []; //"background color sprites (squares)"
lines = []; //vertical and horizontal lines between sprites (nice picture ;)
loadedCenter = undefined; //center of the current loaded map
game = undefined;
constructor(game) {
this.game = game;
this.obstacles = this.phaser.physics.add.staticGroup();
this.objects = this.phaser.physics.add.staticGroup();
}
/**
*
* @returns the boundaries of the loaded map
*/
ymin() { return Math.max(0, this.loadedCenter.y - LOADEDMAP_SIZE); }
ymax() { return Math.min(this.cellTextContents.length, this.loadedCenter.y + LOADEDMAP_SIZE); }
xmin() { return Math.max(0, this.loadedCenter.x - LOADEDMAP_SIZE); }
xmax(y) { return Math.min(this.cellTextContents[y].length, this.loadedCenter.x + LOADEDMAP_SIZE) }
get cellTextContents() { return this.game.cellTextContents; }
get player() { return this.game.player; }
get phaser() { return this.game.phaser }
get backgroundColors() { return this.game.backgroundColors }
/**
* update the map if the player is to far from the "center" (need to reload around the player is)
*/
update() {
if (Math.abs(this.player.obj.x / 32 - this.loadedCenter.x) >= SAFEZONE_SIZE ||
Math.abs(this.player.obj.y / 32 - this.loadedCenter.y) >= SAFEZONE_SIZE) {
this.destroy();
this.load();
}
}
/**
* load the sprites (coloredsquares) corresponding to the background colors
*/
loadBackgroundColor() {
const array = this.backgroundColors;
for (let y = this.ymin(); y < this.ymax(); y++) {
for (let x = this.xmin(); x < this.xmax(y); x++) {
const color = array[y][x] ? array[y][x].replace("#", "0x") : defautBackgroundColor;
const r = this.phaser.add.rectangle(x * 32, y * 32, 32, 32, color);
this.backgroundColorRects.push(r);
}
}
}
load() {
console.log("load");
const array = this.cellTextContents;
const evalArray = [];
this.loadedCenter = { x: Math.floor(this.player.obj.x / 32), y: Math.floor(this.player.obj.y / 32) };
this.loadBackgroundColor();
for (let y = this.ymin(); y < this.ymax(); y++) {
this.cells[y] = [];
evalArray[y] = [];
for (let x = this.xmin(); x < this.xmax(y); x++) {
if (array[y][x]) {
evalArray[y][x] = this.game.evalCellExpression(array[y][x]);
this.cells[y][x] = new Cell(this.game, x, y, evalArray[y][x]);
}
else {
this.cells[y][x] = new Cell(this.game, x, y, "");
//console.log(`not defined at ${x} ${y}`);
}
}
}
const firstWord = (a) => {
if (a == undefined) return ""
else return a != "" ? a.split(" ")[0] : "";
}
const needLine = (a, b) => {
a = firstWord(a);
b = firstWord(b);
const GAGA = ["W", "M", "WALL", "ROOF"];
return (GAGA.indexOf(a) >= 0) || (GAGA.indexOf(b) >= 0);
}
for (let iy = this.ymin(); iy < this.ymax() - 1; iy++)
for (let ix = this.xmin(); ix < this.xmax(iy); ix++) {
if (evalArray[iy][ix] != evalArray[iy + 1][ix] && needLine(evalArray[iy][ix], evalArray[iy + 1][ix])) {
this.lines.push(this.phaser.add.line(ix * 32, iy * 32, 0, 16, 32, 16, "black"));
}
}
for (let iy = this.ymin(); iy < this.ymax(); iy++)
for (let ix = this.xmin(); ix < this.xmax(iy) - 1; ix++) {
if (evalArray[iy][ix] != evalArray[iy][ix + 1] && needLine(evalArray[iy][ix], evalArray[iy][ix + 1])) {
this.lines.push(this.phaser.add.line(ix * 32, iy * 32, 16, 0, 16, 32, "black"));
}
}
this.phaser.physics.add.collider(this.player.obj, this.obstacles, (p, o) => {
/* this code is for the player to slide along the obstacles
it enables the player not to be litteraly blocked if only few pixels block her */
const obstacleRight = p.x < o.x;
const obstacleDown = p.y < o.y;
const ratio = 0.5;
p.y += ratio*(obstacleDown ? -1 : 1);
p.x += ratio*(obstacleRight ? -1 : 1);
});
}
/**
* @description destroy all the cells, the lines
*/
destroy() {
console.log("destroy")
for (let y = this.ymin(); y < this.ymax(); y++) {
for (let x = this.xmin(); x < this.xmax(y); x++)
if (this.cells[y][x]) {
this.cells[y][x].destroy();
this.cells[y][x] = undefined;
}
}
this.lines.map((line) => line.destroy());
this.lines = [];
this.backgroundColorRects.map((rect) => rect.destroy());
this.backgroundColorRects = [];
}
}