-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects.js
More file actions
181 lines (145 loc) · 4.41 KB
/
Copy pathobjects.js
File metadata and controls
181 lines (145 loc) · 4.41 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//this file should have no notion of what a Three.js object is, just the constants I use to represent it
//Define a whole bunch of tile states so I don't get confused by magic constants
const tileStates = {
EMPTY : 0,
WALL : 1,
COIN : 2,
MAGNET : 3,
HEART : 4,
LEVELUP : 5,
PHASER : 6,
SCRABBLER : 7,
HUNTER : 8,
SPEEDBOOST : 9
}
const settings = {
INCREASEBY : 1.3,
MINLEVEL : {
WALL : 0,
COIN : 0,
HEART : 3,
MAGNET : 4,
SPEEDBOOST: 4,
SCRABBLER : 5,
PHASER : 6,
},
DENSITY : {
WALL : 0.05,
COIN : 0.01,
HEART : 0.00025,
SPEEDBOOST : 0.00025,
MAGNET : 0.0003,
SCRABBLER : 0.0002,
PHASER : 0.0002,
},
CUBESCALEDOWN : 2,
TILEWIDTH : 16,
HUNTERSPEED : 0.75
}
var currentLevel = {
MAP : null,
PLAYER : null,
HUNTER : null,
level : 0,
gameEntities : [],
collisions : [],
speedMultiplier : 1,
speedStopTime : 0
}
Object.freeze(tileStates);
Object.freeze(settings);
function twoDarray(x, y, fill) {
var dx = new Array(x).fill(fill);
for (var i = 0; i < x; i++) {
dx[i] = new Array(y).fill(fill);
}
return dx;
}
transpose = m => m[0].map((x,i) => m.map(x => x[i])); //transpose an array
var finder = new PF.AStarFinder();
class mapClass {
constructor() {
//console.log(innerWidth, document.body.clientWidth, innerHeight);
this.widthX = Math.floor(innerWidth / settings.TILEWIDTH);
this.heightY = Math.floor(innerHeight / settings.TILEWIDTH);
this.tiles = twoDarray(this.widthX, this.heightY, tileStates.EMPTY);
this.tiles[0].fill(tileStates.WALL);
this.tiles[this.tiles.length - 1].fill(tileStates.WALL);
this.tiles.forEach(function(row){row[0] = tileStates.WALL; row[row.length - 1] = tileStates.WALL});
this.numTiles = this.widthX * this.heightY;
//First, generate all game items
for (var gameItem in settings.DENSITY) {
if (settings.MINLEVEL[gameItem] <= currentLevel.level) {
for (var i = 0; i < this.getItemDensity(gameItem); i++) {
var [x, y] = this.getRandomAvailablePoint();
this.tiles[x][y] = tileStates[gameItem];
}
}
}
this.asArray = transpose(
this.tiles.map(function(secondaryArray) {
return secondaryArray.map(function(item) {
return item == tileStates.WALL ? 1 : 0;
});
})
); //I need to transpose the array otherwise pathfinding does not work in the right most third of the map (depending on screen resolution);
var playerX, playerY, hunterX, hunterY, levelUpX, levelUpY;
var stillSpawning = true;
while (stillSpawning) {
[playerX, playerY] = this.getRandomAvailablePoint();
[hunterX, hunterY] = this.getRandomAvailablePoint();
[levelUpX, levelUpY] = this.getRandomAvailablePoint();
//Double check that the player and the hunter are not too close
if (((playerX - hunterX) ** 2 + (playerY - hunterY) ** 2) < 5) {
continue; //force next loop now
}
if (((playerX - levelUpX) ** 2 + (playerY - levelUpY) ** 2) < 5) {
continue; //force next loop now
}
//Now make sure that they can reach each other
if (!this.getPath(playerX, playerY, hunterX, hunterY).length) {
continue;
}
//and that the player can level up
if (!this.getPath(playerX, playerY, levelUpX, levelUpY).length) {
continue;
}
stillSpawning = false; //break out of loop
}
this.playerX = playerX;
this.playerY = playerY;
this.hunterX = hunterX;
this.hunterY = hunterY;
this.levelUpX = levelUpX;
this.levelUpY = levelUpY;
this.tiles[levelUpX][levelUpY] = tileStates.LEVELUP;
currentLevel.MAP = this;
}
getItemDensity(key) {
return Math.floor((settings.DENSITY[key] * (settings.INCREASEBY ** currentLevel.level)) * this.numTiles)
}
getRandomAvailablePoint() {
while (true) {
var x = Math.floor(this.widthX * Math.random());
var y = Math.floor(this.heightY * Math.random());
if (this.tiles[x][y] == tileStates.EMPTY) {
return [x,y]
}
}
}
getPath(x1,y1,x2,y2) {
return finder.findPath(x1, y1, x2, y2, new PF.Grid(this.asArray));
}
tileToCoords(x,y) {
return [
x * settings.TILEWIDTH - (settings.TILEWIDTH / 2),
y * settings.TILEWIDTH - (settings.TILEWIDTH / 2)
]
}
coordsToTile(x,y) {
return [
Math.floor((x + settings.TILEWIDTH) / settings.TILEWIDTH),
Math.floor((y + settings.TILEWIDTH) / settings.TILEWIDTH)
]
}
}