-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared.js
More file actions
97 lines (79 loc) · 3.17 KB
/
shared.js
File metadata and controls
97 lines (79 loc) · 3.17 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
import { MAP } from './map.js';
let canvas = document.getElementById("myCanvas");
let ctx = canvas.getContext("2d");
let controlCanvas = document.getElementById("controlcanvas")
let isMobileDevice = /Mobi/i.test(navigator.userAgent);
canvas.width = window.innerWidth
canvas.height = window.innerHeight
let objects = []
let mouselocation = { x: innerWidth / 2, y: innerWidth / 2 }
let isScrolling = { status: false }
let Worldoffset = { offsetX: 1, offsetY: 1 }
let selected = { selected: [] }
let tileWidth = MAP.tilewidth
let tileHeight = MAP.tileheight
let mapPiXelwidth = MAP.width * tileWidth
let mapPiXelheight = MAP.height * tileHeight
let reciprocal = 1 / tileWidth // using this to replace division with multiplication
let grid = {}
function CreateGrid(map, rows, cols) {
let MapLayers = map.layers
for (let i = 0; i < MapLayers.length; i++) {
let layer = MapLayers[i].data
for (let j = 0; j < layer.length; j++) {
let x = j % rows
let y = Math.floor(j / cols)
let cellname = `${x},${y}`
if (layer[j] == 0)
continue
if (grid[cellname]) {
grid[cellname].tile.push(layer[j])
} else {
let cell = {
x,
y,
g: Infinity,
tile: [],
adj: [],
occupied: new Set()
}
// 1, 0 right
if (x + 1 >= 0 && x + 1 < rows && y >= 0 && y < cols) {
cell.adj.push(`${x + 1},${y}`);
}
// -1, 0 left
if (x - 1 >= 0 && x - 1 < rows && y >= 0 && y < cols) {
cell.adj.push(`${x - 1},${y}`);
}
// 0, -1 up
if (x >= 0 && x < rows && y - 1 >= 0 && y - 1 < cols) {
cell.adj.push(`${x},${y - 1}`);
}
// 0, 1 down
if (x >= 0 && x < rows && y + 1 >= 0 && y + 1 < cols) {
cell.adj.push(`${x},${y + 1}`);
}
// 1, -1 top right
if (x + 1 >= 0 && x + 1 < rows && y - 1 >= 0 && y - 1 < cols) {
cell.adj.push(`${x + 1},${y - 1}`);
}
// -1, -1 top left
if (x - 1 >= 0 && x - 1 < rows && y - 1 >= 0 && y - 1 < cols) {
cell.adj.push(`${x - 1},${y - 1}`);
}
// 1, 1 bottom right
if (x + 1 >= 0 && x + 1 < rows && y + 1 >= 0 && y + 1 < cols) {
cell.adj.push(`${x + 1},${y + 1}`);
}
// -1, 1 bottom left
if (x - 1 >= 0 && x - 1 < rows && y + 1 >= 0 && y + 1 < cols) {
cell.adj.push(`${x - 1},${y + 1}`);
}
cell.tile.push(layer[j])
grid[cellname] = cell
}
}
}
}
CreateGrid(MAP, MAP.width, MAP.height)
export { canvas, ctx, objects, grid, mouselocation, isMobileDevice, isScrolling, controlCanvas, Worldoffset, tileWidth, tileHeight, mapPiXelheight, mapPiXelwidth, reciprocal, selected }