/
mario.cljs
216 lines (191 loc) · 5.8 KB
/
mario.cljs
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
(ns elm.examples.mario
(:require-macros
[cljs.core.async.macros :refer [go]])
(:require
[elm.core :as elm]
[om.core :as om :include-macros true]
[om.dom :as dom :include-macros true]
[cljs.core.async :refer [put! chan <! >!] :as async]))
(defonce events (elm/event-stream))
(defonce current-frame (atom {:focus false
:mario {:x 200
:y 400
:vx 0
:vy 0
:dir "right"}}))
(defn render-scene [app owner]
(reify
om/IRender
(render [this]
(dom/div
#js {:style #js {:position "relative"
:backgroundColor "#aeeeee"
:width "100%"
:height "100%"
:overflow "hidden"}}
(dom/div
#js {:style #js {:position "absolute"
:backgroundColor "green"
:height "50px"
:width "100%"
:bottom "0"
:left "0"
:right "0"}})
(when-let [mario (:mario app)]
(let [verb (cond
(not (zero? (:y mario)))
"jump"
(not (zero? (:vx mario)))
"walk"
:else
"stand")
image (str "http://www.lispcast.com/img/mario/"
verb
"-"
(:dir mario)
".gif")]
(dom/img
#js {:src image
:style #js {:position "absolute"
:bottom (str (+ 45 (:y mario)) "px")
:left (str (:x mario) "px")
:width "35px"
:height "35px"}})))
(when-not (:focus app)
(dom/div
#js {:style #js {:position "absolute"
:text-align "center"
:font-family "sans-serif"
:font-size "100px"
:color "blue"
:line-height "100px"
:width "400px"
:height "100px"
:top "0"
:left "0"
:right "0"
:bottom "0"
:margin "auto"}}
"CLICK"))))))
;; Our input nodes
(defonce focusi (elm/input events :focus false))
(defonce jumpi (elm/input events :jump 0))
(defonce movementi (elm/input events :movement 0))
(defonce timei (elm/pulse events :time 0))
(defonce dimensionsi (elm/input events :dimensions {:h 1 :w 1}))
(defn jump
"Set y velocity to +5 if mario is touching ground and jump is pressed."
[mario j]
(if (and (pos? j) (zero? (:y mario)))
(assoc mario :vy 5)
mario))
(defn gravity
"Accelerate down if mario is in the air"
[mario dt]
(if (pos? (:y mario))
(update-in mario [:vy] - (/ dt 4))
mario))
(defn physics
"Move based on velocity. Also, don't let y go negative."
[mario dt]
(-> mario
(update-in [:x] + (* dt (:vx mario)))
(update-in [:y] + (* dt (:vy mario)))
(update-in [:y] max 0)))
(defn walk
"Set x velocity based on direction. Also set facing direction."
[mario dir]
(assoc mario
:vx dir
:dir (cond
(neg? dir)
"left"
(pos? dir)
"right"
:else
(:dir mario))))
(defn toroid [mario {w :w}]
(cond
(< (:x mario) -35)
(assoc mario :x w)
(> (:x mario) w)
(assoc mario :x -35)
:else
mario))
(defn step [world f dim t j dir]
(if f
(-> world
(assoc :focus true)
(update-in [:mario] jump j)
(update-in [:mario] gravity t)
(update-in [:mario] walk dir)
(update-in [:mario] physics t)
(update-in [:mario] toroid dim))
(assoc world :focus false)))
(defn step* [& args]
(apply step args))
(defonce main
(elm/foldp step @current-frame focusi dimensionsi timei jumpi movementi))
(defn now [] (.getTime (js/Date.)))
(let [t1 (atom (now))]
(js/setInterval
#(let [t2 (now)]
(put! events [:time (/ (- t2 @t1) 20)])
(reset! t1 t2))
16.667))
(let [[m main] (elm/port main)]
(go
(while true
(reset! current-frame (:value (<! main))))))
(defonce mario-element (js/document.getElementById "mario"))
(defn set-dimensions []
(put! events [:dimensions {:h (.-clientHeight mario-element)
:w (.-clientWidth mario-element)}]))
(defonce --events
(do
(set! js/window.onresize set-dimensions)
(.addEventListener
mario-element
"keydown"
(fn [e]
(let [e (or js/window.event e)
key (or (.-keyCode e) (.-charCode e))]
(.preventDefault e)
(cond
(= 38 key)
(put! events [:jump 1])
(= 37 key)
(put! events [:movement -1])
(= 39 key)
(put! events [:movement 1]))))
false)
(.addEventListener
mario-element
"keyup"
(fn [e]
(let [e (or js/window.event e)
key (or (.-keyCode e) (.-charCode e))]
(.preventDefault e)
(cond
(= 38 key)
(put! events [:jump 0])
(= 37 key)
(put! events [:movement 0])
(= 39 key)
(put! events [:movement 0]))))
false)
(.addEventListener
mario-element
"focus"
#(put! events [:focus true])
false)
(.addEventListener
mario-element
"blur"
#(put! events [:focus false])
false)))
(set-dimensions)
(om/root
render-scene
current-frame
{:target mario-element})