forked from gitter-badger/game-5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.js
More file actions
210 lines (177 loc) · 5.29 KB
/
map.js
File metadata and controls
210 lines (177 loc) · 5.29 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import { armor, jewelry, weapons } from '@server/core/data/respawn';
import MapUtils from 'shared/map-utils';
import PF from 'pathfinding';
import config from '@server/config';
import surfaceMap from '@server/maps/layers/surface.json';
import uuid from 'uuid/v4';
import { Shop } from './functions';
import world from './world';
class Map {
constructor(level) {
// Getters & Setters
this.players = [];
this.level = level;
this.background = world.map.background;
this.foreground = world.map.foreground;
this.setUp();
}
/**
* Load map tile data
*
* @returns {array}
*/
static async load() {
const data = await Map.fetchMap('surface');
return data;
}
/**
* Resolve a promise to find the path
*
* @param {integer} x The x-axis coord on where user clicked on game-gap
* @param {integer} y The y-axis coord on where user clicked on game-gap
*/
static findQuickestPath(x, y, playerIndex) {
const player = world.players[playerIndex];
return new Promise((resolve) => {
/**
* Get location of all 4 spots, check tile if blocked
* Get direction based off player and where to check first
*/
const path = player.path.finder.findPath(7, 5, x, y, player.path.grid);
resolve(path);
});
}
/**
* Find a path and set that path in motion
*
* @param {string} uuidPath The unique user-id indentifying who is moving
* @param {integer} x The x-axis coord on where user clicked on game-gap
* @param {integer} y The y-axis coord on where user clicked on game-gap
*/
static async findPath(uuidPath, x, y, location) {
const playerIndex = world.players.findIndex(p => p.uuid === uuidPath);
if (world.players[playerIndex].moving) {
world.players[playerIndex].path.current.interrupted = true;
}
// The player's x-y on map (always 7,5)
// to where they clicked on the map
const path = await Map.findQuickestPath(x, y, playerIndex);
// Since we are performing an action on a resource or tile,
// let's end the path one step so we don't step on it.
// (For example, mining block, tree, door, etc.)
if (location === 'edge') {
path.pop();
}
// If the tile we clicked on
// can be walked on, continue ->
if (world.players[playerIndex].path.current.walkable && path.length && path.length >= 1) {
world.players[playerIndex].path.current.path.walking = path;
world.players[playerIndex].path.current.step = 0;
// We start moving the player along their path
world.players[playerIndex].walkPath(playerIndex);
}
}
/**
* Set up the map
*/
async setUp() {
// Load the board
const board = await Map.load();
// Set background and foreground tile data
this.background = board[0].data;
this.foreground = board[1].data;
// Set items on map
const itemsOnMap = [
...armor,
...jewelry,
...weapons,
];
// Spawn items on the map
world.items = Map.readyItems(itemsOnMap);
// Set the respawns accordingly
world.respawns = {
items: itemsOnMap.map((i) => {
i.pickedUp = false;
return i;
}),
monsters: [],
resources: [],
};
// Load shops
world.shops = Shop.load();
// Add a timestamp to all dropped items
world.items = world.items.map((i) => {
i.timestamp = Date.now();
return i;
});
}
/**
* Add a UUID and mark items as respawns to all respawned items
*
* @param {array} items List of respawned items
* @returns {array}
*/
static readyItems(items) {
return items.map((i) => {
i.uuid = uuid();
i.respawn = true;
return i;
});
}
/**
* Loads the map from an external JSON file
*
* @param {string} level The level of the map
* @returns {array}
*/
static fetchMap(level) {
const mapToLoad = {
surface: surfaceMap,
};
return new Promise((resolve) => {
resolve(mapToLoad[level].layers);
});
}
/**
* Get the blocked/non-blocked tile-matrix of their viewport
*
* @param {object} player The player asking
*/
static getMatrix(player) {
const { x, y } = player;
const { size, viewport } = config.map;
return new Promise((resolve) => {
const tileCrop = {
x: x - Math.floor(0.5 * viewport.x),
y: y - Math.floor(0.5 * viewport.y),
};
const matrix = [];
// Drawing the map row by column.
for (let column = 0; column <= viewport.y; column += 1) {
const grid = [];
for (let row = 0; row <= viewport.x; row += 1) {
const onTile = (((column + tileCrop.y) * size.x) + row) + tileCrop.x;
const tiles = {
background: world.map.background[onTile] - 1,
foreground: (world.map.foreground[onTile] - 1) - 252,
};
// Push the block/non-blocked tile to the
// grid so that the pathfinder can use it
// 0 - walkable; 1 - blocked
grid.push(MapUtils.gridWalkable(
tiles,
player,
onTile,
row,
column,
));
}
// Push blocked/non-blocked array for pathfinding
matrix.push(grid);
}
// The new walkable/non-walkable grid
resolve(new PF.Grid(matrix));
});
}
}
module.exports = Map;