-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
executable file
·86 lines (70 loc) · 3.05 KB
/
main.js
File metadata and controls
executable file
·86 lines (70 loc) · 3.05 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
import setupMap from './js/map.js';
import setupTilesets from './js/tilesets.js';
import setupLayers from './js/layers.js';
import setupSettings from './js/settings.js';
/* Define spritesheet object for use throughout */
/* TODO: relocate to tilesets.js with getter/setter methods? */
const spritesheet = {};
spritesheet.image = new Image();
/* Define object to represent the currently-selected sprite for placing on the map */
/* TODO: relocate to tilesets.js with getter/setter methods? */
const sprite = {
x: -1,
y: 0,
get gid() {
return (this.y * (spritesheet.image.width / spritesheet.tileWidth)) + this.x + 1
},
getXY: function(gid) {
let y = Math.trunc((gid-1) / (spritesheet.image.width / spritesheet.tileWidth));
let x = (gid-1) % (spritesheet.image.width / spritesheet.tileWidth);
return [x, y];
}
};
/* Define map object representing all data for the map, with helper functions for import/export */
/* TODO: refactor into other files - mainly layers.js? */
const map = {
layerData : [],
currentLayer : '',
widthInTiles : 6,
heightInTiles : 6,
createLayer : function(name) {
let data = new Array(this.widthInTiles * this.heightInTiles);
data.fill(0);
this.layerData.push({name: name, data:data, visible: true});
},
updateLayerLength : function() {
// TODO: Redo with splice() to maintain position of tiles in x-y plane
let fullArray = new Array(this.widthInTiles * this.heightInTiles);
fullArray.fill(0);
for(let i = 0; i < this.layerData.length; i++) {
this.layerData[i].data = this.layerData[i].data.concat(fullArray).splice(0, this.widthInTiles * this.heightInTiles);
}
}
}
/* Attach event listeners to components in each section of the page */
setupMap(map, spritesheet, sprite);
setupTilesets(map, spritesheet, sprite);
setupLayers(map, spritesheet, sprite);
setupSettings(map, spritesheet, sprite);
/* Define keyboard shortcuts */
/* TODO: Refactor into other files? ie `KeyE` to map.js? */
function reactToKeys(e) {
if(e.code == 'Enter') {
if(document.querySelector('#fileOverlay').style.display!='none' && document.querySelector('#submitData').style.display!='none')
document.querySelector('#submitData').click();
if(document.querySelector('#layerOverlay').style.display!='none')
document.querySelector('#submitLayer').click();
if(document.querySelector('#settingsOverlay').style.display!='none')
document.querySelector('#updateSettings').click();
}
if(e.code == 'KeyE' && e.target.tagName.toLowerCase() != 'input')
document.querySelector('#eraserTool').click();
if(e.code == 'KeyP' && e.target.tagName.toLowerCase() != 'input')
document.querySelector('#penTool').click();;
}
document.addEventListener('keydown', reactToKeys, true);
/* Require confirmation before leaving page so unsaved changes aren't lost */
addEventListener('beforeunload', function (e) {
e.preventDefault();
e.returnValue = '';
});