-
Notifications
You must be signed in to change notification settings - Fork 23
/
bindings.js
162 lines (141 loc) · 3.53 KB
/
bindings.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
// loaded last
var keymap = {
38: 'up',
39: 'right',
40: 'down',
37: 'left',
87: 'up',
68: 'right',
83: 'down',
65: 'left'
}
var userInput = []
var mouseDown = false
var initializeOnUp = false
window.onresize = resizeWindow
function resizeWindow() {
$canv.width = window.innerWidth * devPixelRatio * quality / 10
$canv.height = window.innerHeight * devPixelRatio * quality / 10
ctx = $canv.getContext('2d')
ctx.lineJoin = 'round'
if(GAME.state === 'playing') {
GAME.spawner.resize($canv.width, $canv.height)
GAME.levelBar.resize($canv.width, $canv.height)
GAME.levelBalls.resize($canv.width, $canv.height)
} else {
if(ASSETS.loaded) drawMenu()
}
}
function eventPos(e) {
if (e.type.indexOf('touch') === -1){ // if its a mouse coord
return {x: e.pageX*devPixelRatio, y: e.pageY*devPixelRatio, width: 1, height: 1}
}
// touch event coord
if(e.type === 'touchend') return {x: 0, y: 0, width: 0, height: 0}
return {x: e.touches[0].pageX*devPixelRatio - 35, y: e.touches[0].pageY*devPixelRatio - 35, width: 70, height: 70}
}
$canv.addEventListener('mousedown', touchDown)
$canv.addEventListener('touchstart', touchDown)
function touchDown(e){
e.preventDefault()
var pos = eventPos(e)
if(GAME.state === 'playing') {
GAME.player.updateInput([pos.x - $canv.width/2, pos.y - $canv.height/2], true)
mouseDown = true
} else if (GAME.state === 'menu' && GAME.MENU.button) {
if(collideBox(pos, GAME.MENU.button)) {
drawMenuButton(true)
initializeOnUp = true
}
}
// audio
if (collideBox(pos, {
x: $canv.width - 25,
y: 10,
width: 20,
height: 26
})){
toggleMute()
}
if(!about) {
// about
if (collideBox(pos, {
x: 10,
y: 10,
width: 40,
height: 20
})){
showAbout()
}
} else {
// zolmeister
if (collideBox(pos, {
x: 32,
y: 10,
width: 80,
height: 12
})){
window.open('http://zolmeister.com')
}
// music attribution
if (collideBox(pos, {
x: 48,
y: 32,
width: 48,
height: 12
})){
window.open('https://soundcloud.com/chrissij')
}
}
}
$canv.addEventListener('mouseup', touchUp)
$canv.addEventListener('touchend', touchUp)
function touchUp(e) {
e.preventDefault()
var pos = eventPos(e)
if(GAME.state === 'playing') {
GAME.player.updateInput([], true)
mouseDown = false
} else if (GAME.state === 'menu' && GAME.MENU.button) {
drawMenuButton(initializeOnUp)
if(initializeOnUp){
init()
initializeOnUp = false
}
}
}
$canv.addEventListener('mousemove', touchMove)
$canv.addEventListener('touchmove', touchMove)
function touchMove(e) {
e.preventDefault()
var pos = eventPos(e)
if(GAME.state === 'playing') {
if(mouseDown) {
GAME.player.updateInput([pos.x - $canv.width/2, pos.y - $canv.height/2], true)
}
}
}
window.onkeydown = function(e){
if(GAME.state === 'playing') {
var k = keymap[e.which]
if (!k) return
// remove from input list if it was there already
if(userInput.indexOf(k)!=-1) {
userInput.splice(userInput.indexOf(k), 1)
}
// add to front of input list
userInput.unshift(k)
GAME.player.updateInput(userInput, false)
}
}
window.onkeyup = function(e) {
if(GAME.state === 'playing') {
var k = keymap[e.which]
if (!k) return
// remove from input list if it was there already
if(userInput.indexOf(k)!=-1) {
userInput.splice(userInput.indexOf(k), 1)
}
GAME.player.updateInput(userInput, false)
}
}