/
atom.coffee
142 lines (122 loc) · 3.27 KB
/
atom.coffee
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
requestAnimationFrame = window.requestAnimationFrame or
window.webkitRequestAnimationFrame or
window.mozRequestAnimationFrame or
window.oRequestAnimationFrame or
window.msRequestAnimationFrame or
(callback) ->
window.setTimeout((-> callback 1000 / 60), 1000 / 60)
window.atom = atom = {}
atom.input = {
_bindings: {}
_down: {}
_pressed: {}
_released: []
mouse: { x:0, y:0 }
bind: (key, action) ->
@_bindings[key] = action
onkeydown: (e) ->
action = @_bindings[eventCode e]
return unless action
@_pressed[action] = true unless @_down[action]
@_down[action] = true
e.stopPropagation()
e.preventDefault()
onkeyup: (e) ->
action = @_bindings[eventCode e]
return unless action
@_released.push action
e.stopPropagation()
e.preventDefault()
clearPressed: ->
for action in @_released
@_down[action] = false
@_released = []
@_pressed = {}
pressed: (action) -> @_pressed[action]
down: (action) -> @_down[action]
released: (action) -> (action in @_released)
onmousemove: (e) ->
@mouse.x = e.pageX
@mouse.y = e.pageY
onmousedown: (e) -> @onkeydown(e)
onmouseup: (e) -> @onkeyup(e)
onmousewheel: (e) ->
@onkeydown e
@onkeyup e
oncontextmenu: (e) ->
if @_bindings[atom.button.RIGHT]
e.stopPropagation()
e.preventDefault()
}
document.onkeydown = atom.input.onkeydown.bind(atom.input)
document.onkeyup = atom.input.onkeyup.bind(atom.input)
document.onmouseup = atom.input.onmouseup.bind(atom.input)
atom.button =
LEFT: -1
MIDDLE: -2
RIGHT: -3
WHEELDOWN: -4
WHEELUP: -5
atom.key =
TAB: 9
ENTER: 13
ESC: 27
SPACE: 32
LEFT_ARROW: 37
UP_ARROW: 38
RIGHT_ARROW: 39
DOWN_ARROW: 40
for c in [65..90]
atom.key[String.fromCharCode c] = c
eventCode = (e) ->
if e.type == 'keydown' or e.type == 'keyup'
e.keyCode
else if e.type == 'mousedown' or e.type == 'mouseup'
switch e.button
when 0 then atom.button.LEFT
when 1 then atom.button.MIDDLE
when 2 then atom.button.RIGHT
else if e.type == 'mousewheel'
if e.wheel > 0
atom.button.WHEELUP
else
atom.button.WHEELDOWN
atom.canvas = document.getElementsByTagName('canvas')[0]
atom.canvas.style.position = "absolute"
atom.canvas.style.top = "0"
atom.canvas.style.left = "0"
atom.context = atom.canvas.getContext '2d'
atom.canvas.onmousemove = atom.input.onmousemove.bind(atom.input)
atom.canvas.onmousedown = atom.input.onmousedown.bind(atom.input)
atom.canvas.onmouseup = atom.input.onmouseup.bind(atom.input)
atom.canvas.onmousewheel = atom.input.onmousewheel.bind(atom.input)
atom.canvas.oncontextmenu = atom.input.oncontextmenu.bind(atom.input)
window.onresize = (e) ->
atom.canvas.width = window.innerWidth
atom.canvas.height = window.innerHeight
atom.width = atom.canvas.width
atom.height = atom.canvas.height
window.onresize()
class Game
constructor: ->
update: (dt) ->
draw: ->
run: ->
return if @running
@running = true
s = =>
return unless @running
@step()
requestAnimationFrame s
@last_step = Date.now()
requestAnimationFrame s
stop: ->
@running = false
step: ->
now = Date.now()
dt = (now - @last_step) / 1000
@last_step = now
@update dt
@draw()
atom.input.clearPressed()
atom.Game = Game