-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
194 lines (159 loc) · 5.01 KB
/
main.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
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
import {
collidesWithDOM,
getKey,
hasFocus,
initBounds,
initDOMObjectsDimensions,
listenerAdd
} from "./modules/browser_utilities.js";
import {
gooseSpriteCoordinates,
stationarySpriteIndexes,
runningSpriteIndexes,
ascendSpriteIndexes,
descendSpriteIndexes
} from "./modules/goose_sprite_coordinates.js";
import {
initialGooseXPosition,
determineDirection,
leftArrowTransform,
rightArrowTransform,
downArrowTransform,
upArrowTransform,
ascendGooseY,
randomizeStationaryAnimation,
nextStationarySpriteIndex,
nextAscendSpriteIndex,
nextDescendSpriteIndex,
nextRunningSpriteIndex
} from "./modules/goose_movements.js";
import { styleGoose, draw } from "./modules/goose_sprite.js";
// TODO: Decompose this arrow function into a namespace, distinct functions rather than being a big
// bag of self execution on `window`.
(() => {
"use strict";
/* eslint-env browser */
const UPDATE_INTERVAL = 30;
const MOVEMENT_SPEED = 5;
const JUMP_HEIGHT = 11;
const LEFT_ARROW_KEY_CODE = 37;
const UP_ARROW_KEY_CODE = 38;
const RIGHT_ARROW_KEY_CODE = 39;
const DOWN_ARROW_KEY_CODE = 40;
let _goose = {
x: 0,
y: 0,
};
let _bounds = {
height: 0,
width: 0
};
let _keyHeld = [];
let _DOMObjectsDimensions = [];
// Global animation variables.
let currentSpriteIndex = 0;
let step = 0;
let stationaryStep = 0;
let ascend = {
height: 0,
active: function() {
return this.height > -1;
}
};
function gooseify() {
if(!document.createRange)
return; // :'(
initEventListeners();
_goose = styleGoose(_goose);
resize();
_goose.x = initialGooseXPosition(_bounds.width);
_goose.y = 1;
setInterval(resize, 200);
setInterval(
() => { update(_bounds, _keyHeld, _DOMObjectsDimensions, _goose); },
UPDATE_INTERVAL
);
_goose = draw(_goose, gooseSpriteCoordinates[0]);
document.body.appendChild(_goose);
}
function initEventListeners() {
_keyHeld.fill(false);
listenerAdd(document, "keydown", function(e) {
if(!hasFocus(_goose)) { return; }
let k = getKey(e);
_keyHeld[k] = true;
if(k >= LEFT_ARROW_KEY_CODE && k <= DOWN_ARROW_KEY_CODE) {
e.preventDefault();
}
});
listenerAdd(document, "keyup", function(e) {
if(!hasFocus(_goose)) { return; }
let k = getKey(e);
_keyHeld[k] = false;
if(k >= LEFT_ARROW_KEY_CODE && k <= DOWN_ARROW_KEY_CODE) {
e.preventDefault();
}
});
}
function resize() {
_DOMObjectsDimensions = initDOMObjectsDimensions();
_bounds = initBounds();
}
function update(bounds, keyHeld, DOMObjectsDimensions, goose) {
if(step > 1000000) {
step = 0;
}
let oldX = goose.x;
let oldY = goose.y;
const [ ,, spriteFrameWidth, spriteFrameHeight ] = gooseSpriteCoordinates[currentSpriteIndex];
const gooseAtBottom = (goose.y + 2 > bounds.height);
let sitting = gooseAtBottom || collidesWithDOM(
{
"top": goose.y - spriteFrameHeight,
"left": goose.x,
"width": spriteFrameWidth,
"height": spriteFrameHeight
},
DOMObjectsDimensions,
MOVEMENT_SPEED
);
if(keyHeld[LEFT_ARROW_KEY_CODE]) {
goose.x = leftArrowTransform(goose.x, MOVEMENT_SPEED, spriteFrameWidth, bounds.width);
} else if(keyHeld[RIGHT_ARROW_KEY_CODE]) {
goose.x = rightArrowTransform(goose.x, MOVEMENT_SPEED, spriteFrameWidth, bounds.width);
}
if(keyHeld[UP_ARROW_KEY_CODE] && !ascend.active() && sitting) {
ascend = upArrowTransform(ascend, JUMP_HEIGHT);
} else if(keyHeld[DOWN_ARROW_KEY_CODE] || (!ascend.active() && !sitting && !gooseAtBottom)) {
goose.y = downArrowTransform(goose.y, MOVEMENT_SPEED);
}
let direction = determineDirection(goose.x, oldX);
let stationary = (goose.x == oldX && goose.y == oldY);
let descending = !ascend.active() && !sitting;
if(ascend.active()) {
currentSpriteIndex = nextAscendSpriteIndex(ascendSpriteIndexes[direction], step);
const spriteFrameHeight = gooseSpriteCoordinates[currentSpriteIndex][3];
goose.y = ascendGooseY(goose, ascend.height, bounds.height, spriteFrameHeight);
goose = draw(goose, gooseSpriteCoordinates[currentSpriteIndex]);
step++;
ascend.height--;
return;
} else if(stationary) {
currentSpriteIndex = nextStationarySpriteIndex(stationarySpriteIndexes, step);
goose = draw(goose, gooseSpriteCoordinates[currentSpriteIndex]);
[ step, stationaryStep ] = randomizeStationaryAnimation(step, stationaryStep);
return;
} else if(descending) {
currentSpriteIndex = nextDescendSpriteIndex(descendSpriteIndexes[direction], step);
goose = draw(goose, gooseSpriteCoordinates[currentSpriteIndex]);
step++;
return;
} else {
currentSpriteIndex = nextRunningSpriteIndex(runningSpriteIndexes[direction], step);
goose = draw(goose, gooseSpriteCoordinates[currentSpriteIndex]);
step++;
return;
}
}
window.gooseify = gooseify;
})();