annotate org/games.org @ 297:d1206b11ae2d

creating final video
author Robert McIntyre <rlm@mit.edu>
date Thu, 16 Feb 2012 12:48:51 -0700
parents 1381a6ebd08b
children 7e7f8d6d9ec5
rev   line source
rlm@34 1 #+title: jMonkeyEngine3 from Clojure
rlm@0 2 #+author: Robert McIntyre
rlm@0 3 #+email: rlm@mit.edu
rlm@34 4 #+description: Using jMonkeyEngine3 from clojure
rlm@34 5 #+keywords: clojure, jMonkeyEngine3, tutorial, examples
rlm@4 6 #+SETUPFILE: ../../aurellem/org/setup.org
rlm@4 7 #+INCLUDE: ../../aurellem/org/level-0.org
rlm@21 8 #+babel: :mkdirp yes :noweb yes :exports both
rlm@0 9
rlm@34 10 [TABLE-OF-CONTENTS]
rlm@0 11
rlm@0 12
rlm@31 13 Here are the jMonkeyEngine "Hello" programs translated to clojure.
rlm@34 14
rlm@34 15 * Hello Simple App
rlm@23 16 Here is the hello world example for jme3 in clojure. It's a more or
rlm@23 17 less direct translation from the java source [[http://jmonkeyengine.org/wiki/doku.php/jme3:beginner:hello_simpleapplication][here]].
rlm@0 18
rlm@0 19 Of note is the fact that since we don't have access to the
rlm@0 20 =AssetManager= via extendig =SimpleApplication=, we have to build one
rlm@0 21 ourselves.
rlm@0 22
rlm@66 23 #+name: hello-simple-app
rlm@0 24 #+begin_src clojure :results silent
rlm@34 25 (ns hello.hello-simple-app
rlm@34 26 (:use cortex.world)
rlm@34 27 (:import com.jme3.math.Vector3f)
rlm@34 28 (:import com.jme3.material.Material)
rlm@34 29 (:import com.jme3.scene.Geometry)
rlm@34 30 (:import com.jme3.math.ColorRGBA)
rlm@34 31 (:import com.jme3.app.SimpleApplication)
rlm@34 32 (:import com.jme3.scene.shape.Box))
rlm@0 33
rlm@0 34 (def cube (Box. Vector3f/ZERO 1 1 1))
rlm@0 35
rlm@0 36 (def geom (Geometry. "Box" cube))
rlm@0 37
rlm@0 38 (def mat (Material. (asset-manager) "Common/MatDefs/Misc/Unshaded.j3md"))
rlm@0 39
rlm@0 40 (.setColor mat "Color" ColorRGBA/Blue)
rlm@0 41
rlm@0 42 (.setMaterial geom mat)
rlm@0 43
rlm@0 44 (defn simple-app []
rlm@0 45 (doto
rlm@0 46 (proxy [SimpleApplication] []
rlm@0 47 (simpleInitApp
rlm@0 48 []
rlm@0 49 ;; Don't take control of the mouse
rlm@0 50 (org.lwjgl.input.Mouse/setGrabbed false)
rlm@0 51 (.attachChild (.getRootNode this) geom)))
rlm@0 52 ;; don't show a menu to change options.
rlm@0 53 (.setShowSettings false)
rlm@0 54 (.setPauseOnLostFocus false)
rlm@0 55 (.setSettings *app-settings*)))
rlm@0 56 #+end_src
rlm@0 57
rlm@0 58 Running this program will begin a new jMonkeyEngine game which
rlm@0 59 displays a single blue cube.
rlm@0 60
rlm@0 61 #+begin_src clojure :exports code :results silent
rlm@0 62 (.start (hello.hello-simple-app/simple-app))
rlm@0 63 #+end_src
rlm@0 64
rlm@0 65 #+caption: the simplest JME game.
rlm@31 66 [[../images/simple-app.jpg]]
rlm@0 67
rlm@34 68 * Simpler HelloSimpleApp
rlm@0 69
rlm@66 70 #+name: hello-simpler-app
rlm@34 71 #+begin_src clojure
rlm@34 72 (ns hello.hello-simpler-app
rlm@34 73 (:use cortex.world)
rlm@34 74 (:use cortex.util)
rlm@34 75 (:import com.jme3.math.ColorRGBA)
rlm@34 76 (:import com.jme3.scene.Node))
rlm@34 77
rlm@34 78 (defn simpler-world
rlm@34 79 "The jMonkeyEngine3 Hello World program. Displays a blue 3D cube in
rlm@34 80 a basic 3D world."
rlm@34 81 []
rlm@34 82 (world (doto (Node.)
rlm@34 83 (.attachChild
rlm@34 84 (box 1 1 1
rlm@34 85 :color ColorRGBA/Blue :physical? false)))
rlm@34 86 {} no-op no-op))
rlm@34 87 #+end_src
rlm@34 88
rlm@34 89 More information about the jMonkeyEngine3 hello world program can be
rlm@34 90 found [[http://jmonkeyengine.org/wiki/doku.php/starter:hello_world][here]].
rlm@34 91
rlm@34 92 * COMMENT Hello Physics
rlm@0 93 From http://jmonkeyengine.org/wiki/doku.php/jme3:beginner:hello_physics
rlm@0 94
rlm@66 95 #+name: brick-wall-header
rlm@0 96 #+begin_src clojure :results silent
rlm@0 97 (ns hello.brick-wall)
rlm@0 98 (require 'cortex.import)
rlm@0 99 (use 'clojure.contrib.def)
rlm@0 100 (rlm.rlm-commands/help)
rlm@0 101 (cortex.import/mega-import-jme3)
rlm@0 102 (use '[pokemon [lpsolve :only [constant-map]]])
rlm@0 103 (use 'cortex.world)
rlm@25 104 (use 'cortex.util)
rlm@0 105 #+end_src
rlm@0 106
rlm@66 107 #+name: brick-wall-body
rlm@0 108 #+begin_src clojure :results silent
rlm@0 109 (in-ns 'hello.brick-wall)
rlm@0 110
rlm@0 111 (defn floor
rlm@0 112 "make a sturdy, unmovable physical floor"
rlm@0 113 []
rlm@0 114 (box 20 1 20 :mass 0 :color false :position (Vector3f. 0 -2 0)))
rlm@0 115
rlm@0 116 (def brick-length 0.48)
rlm@0 117 (def brick-width 0.24)
rlm@0 118 (def brick-height 0.12)
rlm@34 119 (def gravity (Vector3f. 0 -9.81 0))
rlm@0 120
rlm@0 121 (defn brick* [position]
rlm@0 122 (doto (box brick-length brick-height brick-width
rlm@0 123 :position position :name "brick"
rlm@0 124 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 125 :texture "Textures/Terrain/BrickWall/BrickWall.jpg"
rlm@0 126 :mass 36)
rlm@0 127 (->
rlm@0 128 (.getMesh)
rlm@0 129 (.scaleTextureCoordinates (Vector2f. 1 0.5)))
rlm@0 130 ;;(.setShadowMode RenderQueue$ShadowMode/CastAndReceive)
rlm@0 131 )
rlm@0 132 )
rlm@0 133
rlm@0 134 (defn inception-brick-wall
rlm@0 135 "construct a physical brick wall"
rlm@0 136 []
rlm@0 137 (let [node (Node. "brick-wall")]
rlm@0 138 (dorun
rlm@0 139 (map (comp #(.attachChild node %) brick*)
rlm@0 140 (for
rlm@0 141 [x (range 15)
rlm@0 142 y (range 10)
rlm@0 143 z (range 1)]
rlm@0 144 (Vector3f.
rlm@0 145 (* brick-length x 1.03)
rlm@0 146 (* brick-width y y 10)
rlm@0 147 (* brick-height z)))))
rlm@0 148 node))
rlm@0 149
rlm@0 150 (defn gravity-toggle
rlm@0 151 [new-value]
rlm@0 152 (fn [game value]
rlm@0 153 (println-repl "set gravity to " new-value)
rlm@0 154 (if value
rlm@34 155 (set-gravity game new-value)
rlm@34 156 (set-gravity game gravity))))
rlm@0 157
rlm@0 158 (defn floor* []
rlm@0 159 (doto (box 10 0.1 5 :name "floor" ;10 0.1 5 ; 240 0.1 240
rlm@0 160 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 161 :texture "Textures/Terrain/Pond/Pond.png"
rlm@0 162 :position (Vector3f. 0 -0.1 0 )
rlm@0 163 :mass 0)
rlm@0 164 (->
rlm@0 165 (.getMesh)
rlm@0 166 (.scaleTextureCoordinates (Vector2f. 3 6)));64 64
rlm@0 167 (->
rlm@0 168 (.getMaterial)
rlm@0 169 (.getTextureParam "ColorMap")
rlm@0 170 (.getTextureValue)
rlm@0 171 (.setWrap Texture$WrapMode/Repeat))
rlm@0 172 (.setShadowMode RenderQueue$ShadowMode/Receive)
rlm@0 173 ))
rlm@0 174
rlm@0 175 (defn brick-wall* []
rlm@0 176 (let [node (Node. "brick-wall")]
rlm@0 177 (dorun
rlm@0 178 (map
rlm@0 179 (comp #(.attachChild node %) brick*)
rlm@0 180 (for [y (range 15)
rlm@0 181 x (range 4)
rlm@0 182 z (range 1)]
rlm@0 183 (Vector3f.
rlm@0 184 (+ (* 2 x brick-length)
rlm@0 185 (if (even? (+ y z))
rlm@0 186 (/ brick-length 4) (/ brick-length -4)))
rlm@0 187 (+ (* brick-height (inc (* 2 y))))
rlm@0 188 (* 2 z brick-width) ))))
rlm@0 189 (.setShadowMode node RenderQueue$ShadowMode/CastAndReceive)
rlm@0 190 node))
rlm@0 191
rlm@0 192 (defn brick-wall-game-run []
rlm@0 193 (doto
rlm@0 194 (world
rlm@0 195 (doto (Node.) (.attachChild (floor*))
rlm@0 196 (.attachChild (brick-wall*))
rlm@0 197 )
rlm@0 198 {"key-i" (gravity-toggle (Vector3f. 0 0 -9.81))
rlm@0 199 "key-m" (gravity-toggle (Vector3f. 0 0 9.81))
rlm@0 200 "key-l" (gravity-toggle (Vector3f. 9.81 0 0))
rlm@0 201 "key-j" (gravity-toggle (Vector3f. -9.81 0 0))
rlm@0 202 "key-k" (gravity-toggle Vector3f/ZERO)
rlm@0 203 "key-u" (gravity-toggle (Vector3f. 0 9.81 0))
rlm@0 204 "key-o" (gravity-toggle (Vector3f. 0 -9.81 0))
rlm@0 205 "key-f" (fn[game value]
rlm@0 206 (if (not value) (add-element game (brick-wall*))))
rlm@0 207 "key-return" (fire-cannon-ball)}
rlm@0 208 position-camera
rlm@0 209 (fn [& _]))
rlm@0 210 (.start)))
rlm@0 211 #+end_src
rlm@0 212
rlm@0 213 #+begin_src clojure :results silent
rlm@0 214 (hello.brick-wall/brick-wall-game-run)
rlm@0 215 #+end_src
rlm@0 216
rlm@0 217 #+caption: the brick wall standing
rlm@31 218 [[../images/brick-wall-standing.jpg]]
rlm@0 219
rlm@0 220 #+caption: the brick wall after it has been knocked over by a "pok\eacute{}ball"
rlm@31 221 [[../images/brick-wall-knocked-down.jpg]]
rlm@0 222
rlm@34 223 * COMMENT Other Brick Games
rlm@66 224 #+name: other-games
rlm@0 225 #+begin_src clojure :results silent
rlm@34 226 (ns hello.other-games
rlm@0 227 {:author "Dylan Holmes"})
rlm@0 228 (use 'cortex.world)
rlm@0 229 (use 'hello.brick-wall)
rlm@0 230 (use 'cortex.import)
rlm@0 231 (cortex.import/mega-import-jme3)
rlm@0 232
rlm@0 233 (defn scad [position]
rlm@0 234 (doto (box 0.1 0.1 0.1
rlm@0 235 :position position :name "brick"
rlm@0 236 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 237 :texture "Textures/Terrain/BrickWall/BrickWall.jpg"
rlm@0 238 :mass 20)
rlm@0 239 (->
rlm@0 240 (.getMesh)
rlm@0 241 (.scaleTextureCoordinates (Vector2f. 1 0.5))
rlm@0 242 )
rlm@0 243 (-> (.getControl RigidBodyControl)
rlm@0 244 (.setLinearVelocity (Vector3f. 0 100 0))
rlm@0 245 )
rlm@0 246
rlm@0 247 ;;(.setShadowMode RenderQueue$ShadowMode/Cast)
rlm@0 248 ))
rlm@0 249
rlm@0 250
rlm@0 251 (defn shrapnel []
rlm@0 252 (let [node (Node. "explosion-day")]
rlm@0 253 (dorun
rlm@0 254 (map
rlm@0 255 (comp #(.attachChild node %) scad)
rlm@0 256 (for [y (range 15)
rlm@0 257 x (range 4)
rlm@0 258 z (range 1)]
rlm@0 259 (Vector3f.
rlm@0 260 (+ (* 2 x brick-height)
rlm@0 261 (if (even? (+ y z)) (/ brick-height 4) (/ brick-height -4)))
rlm@0 262 (+ (* brick-height (inc (* 2 y))))
rlm@0 263 (* 2 z brick-height) ))))
rlm@0 264 node))
rlm@0 265
rlm@0 266
rlm@0 267 (def domino-height 0.48)
rlm@0 268 (def domino-thickness 0.12)
rlm@0 269 (def domino-width 0.24)
rlm@0 270
rlm@0 271 (def domino-thickness 0.05)
rlm@0 272 (def domino-width 0.5)
rlm@0 273 (def domino-height 1)
rlm@0 274
rlm@0 275 (defn domino
rlm@0 276 ([position]
rlm@0 277 (domino position (Quaternion/IDENTITY)))
rlm@0 278 ([position rotation]
rlm@0 279 (doto (box domino-width domino-height domino-thickness
rlm@0 280 :position position :name "domino"
rlm@0 281 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 282 :texture "Textures/Terrain/BrickWall/BrickWall.jpg"
rlm@0 283 :mass 1
rlm@0 284 :rotation rotation)
rlm@0 285 (.setShadowMode RenderQueue$ShadowMode/CastAndReceive)
rlm@0 286 )))
rlm@0 287
rlm@0 288
rlm@0 289 (defn domino-row []
rlm@0 290 (let [node (Node. "domino-row")]
rlm@0 291 (dorun
rlm@0 292 (map
rlm@0 293 (comp #(.attachChild node %) domino)
rlm@0 294 (for [
rlm@0 295 z (range 10)
rlm@0 296 x (range 5)
rlm@0 297 ]
rlm@0 298 (Vector3f.
rlm@0 299 (+ (* z domino-width) (* x 5 domino-width))
rlm@0 300 (/ domino-height 1)
rlm@0 301 (* -5.5 domino-thickness z) ))))
rlm@0 302
rlm@0 303 node))
rlm@0 304
rlm@0 305 (defn domino-cycle []
rlm@0 306 (let [node (Node. "domino-cycle")]
rlm@0 307 (dorun
rlm@0 308 (map
rlm@0 309 (comp #(.attachChild node %) (partial apply domino) )
rlm@0 310 (for [n (range 720)]
rlm@0 311 (let [space (* domino-height 5.5)
rlm@0 312 r (fn[n] (* (+ n 3) domino-width 0.5))
rlm@0 313 t (fn[n] (reduce
rlm@0 314 +
rlm@0 315 (map
rlm@0 316 (fn dt[n] (/ space (* 2 (Math/PI) (r n))))
rlm@0 317 (range n))))
rlm@0 318 t (t n)
rlm@0 319 r (r n)
rlm@0 320 ct (Math/cos t)
rlm@0 321 st (Math/sin t)
rlm@0 322 ]
rlm@0 323 (list
rlm@0 324 (Vector3f.
rlm@0 325 (* -1 r st)
rlm@0 326 (/ domino-height 1)
rlm@0 327 (* r ct))
rlm@0 328 (.fromAngleAxis (Quaternion.)
rlm@0 329 (- (/ 3.1415926 2) t) (Vector3f. 0 1 0))
rlm@0 330 )))
rlm@0 331 ))
rlm@0 332 node))
rlm@0 333
rlm@0 334
rlm@0 335 (defn domino-game-run []
rlm@0 336 (doto
rlm@0 337 (world
rlm@0 338 (doto (Node.) (.attachChild (floor*))
rlm@0 339 )
rlm@0 340 {"key-i" (gravity-toggle (Vector3f. 0 0 -9.81))
rlm@0 341 "key-m" (gravity-toggle (Vector3f. 0 0 9.81))
rlm@0 342 "key-l" (gravity-toggle (Vector3f. 9.81 0 0))
rlm@0 343 "key-j" (gravity-toggle (Vector3f. -9.81 0 0))
rlm@0 344 "key-k" (gravity-toggle (Vector3f. 0 9.81 0) )
rlm@0 345 "key-u" (fn[g v] ((gravity-toggle (Vector3f. 0 -0 0)) g true))
rlm@0 346 "key-o" (gravity-toggle (Vector3f. 0 -9.81 0))
rlm@0 347
rlm@0 348 "key-space"
rlm@0 349 (fn[game value]
rlm@0 350
rlm@0 351 (if (not value)
rlm@0 352 (let [d (domino (Vector3f. 0 (/ domino-height 0.25) 0)
rlm@0 353 (.fromAngleAxis (Quaternion.)
rlm@0 354 (/ Math/PI 2) (Vector3f. 0 1 0)))]
rlm@0 355 (add-element game d))))
rlm@0 356 "key-f"
rlm@0 357 (fn[game value](if (not value) (add-element game (domino-cycle))))
rlm@0 358 "key-return" (fire-cannon-ball)}
rlm@0 359 position-camera
rlm@0 360 (fn [& _]))
rlm@0 361 (.start)))
rlm@0 362 #+end_src
rlm@0 363
rlm@0 364 #+begin_src clojure :results silent
rlm@0 365 (cortex.other-games/domino-game-run)
rlm@0 366 #+end_src
rlm@0 367
rlm@0 368 #+caption: floating dominos
rlm@31 369 [[../images/dominos.jpg]]
rlm@0 370
rlm@34 371 * Hello Loop
rlm@66 372 #+name: hello-loop
rlm@0 373 #+begin_src clojure :results silent
rlm@34 374 (ns hello.loop
rlm@34 375 (:use cortex.world)
rlm@34 376 (:use cortex.util)
rlm@34 377 (:import com.jme3.math.ColorRGBA)
rlm@34 378 (:import com.jme3.scene.Node))
rlm@34 379
rlm@0 380 (defn blue-cube []
rlm@0 381 (box 1 1 1
rlm@0 382 :color ColorRGBA/Blue
rlm@0 383 :texture false
rlm@0 384 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 385 :name "blue-cube"
rlm@0 386 :physical? false))
rlm@0 387
rlm@0 388 (defn blue-cube-game []
rlm@0 389 (let [cube (blue-cube)
rlm@0 390 root (doto (Node.) (.attachChild cube))]
rlm@0 391 (world root
rlm@0 392 {}
rlm@0 393 no-op
rlm@0 394 (fn [game tpf]
rlm@0 395 (.rotate cube 0.0 (* 2 tpf) 0.0)))))
rlm@0 396 #+end_src
rlm@31 397
rlm@34 398 * COMMENT Hello Collision
rlm@0 399
rlm@66 400 #+name: hello-collision
rlm@0 401 #+begin_src clojure :results silent
rlm@0 402 (ns hello.collision)
rlm@0 403 (use 'cortex.world)
rlm@0 404 (use 'cortex.import)
rlm@0 405 (use 'clojure.contrib.def)
rlm@0 406
rlm@0 407
rlm@0 408 (cortex.import/mega-import-jme3)
rlm@0 409 (rlm.rlm-commands/help)
rlm@0 410 (use '[hello [brick-wall :only [fire-cannon-ball brick-wall*]]])
rlm@0 411
rlm@0 412
rlm@0 413 (defn environment []
rlm@0 414 (let
rlm@0 415 [scene-model
rlm@0 416 (doto
rlm@0 417 (.loadModel
rlm@0 418 (doto (asset-manager)
rlm@0 419 (.registerLocator
rlm@0 420 "/home/r/cortex/assets/zips/town.zip" ZipLocator))
rlm@0 421 "main.scene")
rlm@0 422 (.setLocalScale (float 2.0)))
rlm@0 423 collision-shape
rlm@0 424 (CollisionShapeFactory/createMeshShape #^Node scene-model)
rlm@0 425 landscape (RigidBodyControl. collision-shape 0)]
rlm@0 426 (.setShadowMode scene-model RenderQueue$ShadowMode/CastAndReceive)
rlm@0 427 (.addControl scene-model landscape)
rlm@0 428 scene-model))
rlm@0 429
rlm@0 430 (defn player-fn []
rlm@0 431 (doto
rlm@0 432 (CharacterControl.
rlm@0 433 (CapsuleCollisionShape. (float 1.5) (float 6)(float 1))
rlm@0 434 (float 0.05))
rlm@0 435 (.setJumpSpeed 20)
rlm@0 436 (.setFallSpeed 30)
rlm@0 437 (.setGravity 30) ;30
rlm@0 438 (.setPhysicsLocation (Vector3f. 0 10 0))))
rlm@0 439
rlm@0 440 (defn lights []
rlm@0 441 [(doto (AmbientLight.) (.setColor (.mult (ColorRGBA. 1 1 1 1) (float 1))))
rlm@0 442 (doto (AmbientLight.) (.setColor (.mult (ColorRGBA. 1 0.7 0 1) (float 1))))
rlm@0 443 (doto (DirectionalLight.)
rlm@0 444 (.setColor (.mult ColorRGBA/White (float 0.9) ))
rlm@0 445 (.setDirection (.normalizeLocal (Vector3f. 2.8 -28 2.8))))])
rlm@0 446
rlm@0 447 (defn night-lights []
rlm@0 448 [(doto (AmbientLight.) (.setColor (.mult (ColorRGBA. 0.275 0.467 0.784 1) (float 0.3))))
rlm@0 449 (doto (DirectionalLight.)
rlm@0 450 (.setColor (.mult ColorRGBA/White (float 0.2) ))
rlm@0 451 (.setDirection (.normalizeLocal (Vector3f. 2.8 -28 2.8))))])
rlm@0 452
rlm@0 453 (def player (atom (player-fn)))
rlm@0 454
rlm@0 455 (defn setup-fn [game]
rlm@0 456 (dorun (map #(.addLight (.getRootNode game) %) (lights)))
rlm@0 457 ;; set the color of the sky
rlm@0 458 (.setBackgroundColor (.getViewPort game) (ColorRGBA. 0.3 0.4 0.9 1))
rlm@0 459 ;(.setBackgroundColor (.getViewPort game) (ColorRGBA. 0 0 0 1)
rlm@0 460 (doto (.getFlyByCamera game)
rlm@0 461 (.setMoveSpeed (float 100))
rlm@0 462 (.setRotationSpeed 3))
rlm@0 463 (.add
rlm@0 464 (.getPhysicsSpace
rlm@0 465 (.getState (.getStateManager game) BulletAppState))
rlm@0 466 @player)
rlm@0 467
rlm@0 468 (doto (Node.) (.attachChild (.getRootNode game))
rlm@0 469 (.attachChild (brick-wall*))
rlm@0 470 )
rlm@0 471
rlm@0 472 )
rlm@0 473
rlm@0 474
rlm@0 475 (def walking-up? (atom false))
rlm@0 476 (def walking-down? (atom false))
rlm@0 477 (def walking-left? (atom false))
rlm@0 478 (def walking-right? (atom false))
rlm@0 479
rlm@0 480 (defn set-walk [walk-atom game value]
rlm@0 481 ;;(println-repl "setting stuff to " value)
rlm@0 482 (reset! walk-atom value))
rlm@0 483
rlm@0 484 (defn responses []
rlm@0 485 {"key-w" (partial set-walk walking-up?)
rlm@0 486 "key-d" (partial set-walk walking-right?)
rlm@0 487 "key-s" (partial set-walk walking-down?)
rlm@0 488 "key-a" (partial set-walk walking-left?)
rlm@0 489 "key-return" (fire-cannon-ball)
rlm@0 490 "key-space" (fn [game value] (.jump @player))
rlm@0 491 })
rlm@0 492
rlm@0 493 (defn update-fn
rlm@0 494 [game tpf]
rlm@0 495 (let [camera (.getCamera game)
rlm@0 496 cam-dir (.multLocal
rlm@0 497 (.clone
rlm@0 498 (.getDirection camera)) (float 0.6))
rlm@0 499 cam-left (.multLocal
rlm@0 500 (.clone
rlm@0 501 (.getLeft camera)) (float 0.4))
rlm@0 502 walk-direction (Vector3f. 0 0 0)]
rlm@0 503
rlm@0 504 (cond
rlm@0 505 @walking-up? (.addLocal walk-direction cam-dir)
rlm@0 506 @walking-right? (.addLocal walk-direction (.negate cam-left))
rlm@0 507 @walking-down? (.addLocal walk-direction (.negate cam-dir))
rlm@0 508 @walking-left? (.addLocal walk-direction cam-left))
rlm@0 509 (.setWalkDirection @player walk-direction)
rlm@0 510 (.setLocation camera (.getPhysicsLocation @player))))
rlm@0 511
rlm@0 512 (defn run-game []
rlm@0 513 (.start
rlm@0 514 (world (environment)
rlm@0 515 (responses)
rlm@0 516 setup-fn
rlm@0 517 update-fn)))
rlm@0 518 #+end_src
rlm@0 519
rlm@34 520 * COMMENT Hello Terrain
rlm@66 521 #+name: hello-terrain
rlm@0 522 #+begin_src clojure :results silent
rlm@0 523 (ns hello.terrain)
rlm@0 524 (use 'cortex.world)
rlm@0 525 (use 'cortex.import)
rlm@0 526 (use 'clojure.contrib.def)
rlm@0 527 (import jme3tools.converters.ImageToAwt)
rlm@0 528
rlm@0 529
rlm@0 530 (cortex.import/mega-import-jme3)
rlm@0 531 (rlm.rlm-commands/help)
rlm@0 532 (use '[hello [brick-wall :only [fire-cannon-ball brick-wall*]]])
rlm@0 533
rlm@0 534
rlm@0 535 (defn setup-fn [type game]
rlm@0 536 (.setMoveSpeed (.getFlyByCamera game) 50)
rlm@0 537 (.setFrustumFar (.getCamera game) 10000)
rlm@0 538 (let [env (environment type)
rlm@0 539 cameras [(.getCamera game)]
rlm@0 540 control (TerrainLodControl. env cameras)]
rlm@0 541 ;;(.addControl env control)
rlm@0 542 (.attachChild (.getRootNode game) env)))
rlm@0 543
rlm@0 544 (defn environment [type]
rlm@0 545 (let
rlm@0 546 [mat_terrain
rlm@0 547 (Material. (asset-manager) "Common/MatDefs/Terrain/Terrain.j3md")
rlm@0 548 grass (.loadTexture (asset-manager) "Textures/Terrain/splat/grass.jpg")
rlm@0 549 dirt (.loadTexture (asset-manager) "Textures/Terrain/splat/dirt.jpg")
rlm@0 550 rock (.loadTexture (asset-manager) "Textures/Terrain/splat/road.jpg")
rlm@0 551 heightmap-image (.loadTexture (asset-manager)
rlm@0 552 ({:mountain "Textures/Terrain/splat/mountains512.png"
rlm@0 553 :fortress "Textures/Terrain/splat/fortress512.png"
rlm@0 554 }type))
rlm@0 555 heightmap (ImageBasedHeightMap.
rlm@0 556 (ImageToAwt/convert (.getImage heightmap-image) false true 0))
rlm@0 557 terrain (do (.load heightmap)
rlm@0 558 (TerrainQuad. "my terrain" 65 513 (.getHeightMap heightmap)))
rlm@0 559 ]
rlm@0 560
rlm@0 561 (dorun (map #(.setWrap % Texture$WrapMode/Repeat)
rlm@0 562 [grass dirt rock]))
rlm@0 563
rlm@0 564 (doto mat_terrain
rlm@0 565 (.setTexture "Tex1" grass)
rlm@0 566 (.setFloat "Tex1Scale" (float 64))
rlm@0 567
rlm@0 568 (.setTexture "Tex2" dirt)
rlm@0 569 (.setFloat "Tex2Scale" (float 32))
rlm@0 570
rlm@0 571 (.setTexture "Tex3" rock)
rlm@0 572 (.setFloat "Tex3Scale" (float 128))
rlm@0 573
rlm@0 574 (.setTexture "Alpha"
rlm@0 575 (.loadTexture
rlm@0 576 (asset-manager)
rlm@0 577 ({:mountain "Textures/Terrain/splat/alphamap.png"
rlm@0 578 :fortress "Textures/Terrain/splat/alphamap2.png"} type))))
rlm@0 579
rlm@0 580 (doto terrain
rlm@0 581 (.setMaterial mat_terrain)
rlm@0 582 (.setLocalTranslation 0 -100 0)
rlm@0 583 (.setLocalScale 2 1 2))))
rlm@0 584
rlm@0 585
rlm@0 586
rlm@0 587 (defn run-terrain-game [type]
rlm@0 588 (.start
rlm@0 589 (world
rlm@0 590 (Node.)
rlm@0 591 {}
rlm@0 592 (partial setup-fn type)
rlm@0 593 no-op)))
rlm@0 594 #+end_src
rlm@0 595
rlm@0 596
rlm@0 597
rlm@66 598 #+name: hello-animation
rlm@0 599 #+begin_src clojure :results silent
rlm@0 600 (ns hello.animation)
rlm@0 601 (use 'cortex.world)
rlm@0 602 (use 'cortex.import)
rlm@0 603 (use 'clojure.contrib.def)
rlm@0 604 (cortex.import/mega-import-jme3)
rlm@0 605 (rlm.rlm-commands/help)
rlm@0 606 (use '[hello [collision :only [lights]]])
rlm@0 607
rlm@0 608 (defn stand
rlm@0 609 [channel]
rlm@0 610 (doto channel
rlm@0 611 (.setAnim "stand" (float 0.5))
rlm@0 612 (.setLoopMode LoopMode/DontLoop)
rlm@0 613 (.setSpeed (float 1))))
rlm@0 614
rlm@0 615 (defn anim-listener []
rlm@0 616 (proxy [AnimEventListener] []
rlm@0 617 (onAnimChange
rlm@0 618 [control channel animation-name]
rlm@0 619 (println-repl "RLM --- onAnimChange"))
rlm@0 620 (onAnimCycleDone
rlm@0 621 [control channel animation-name]
rlm@0 622 (if (= animation-name "Walk")
rlm@0 623 (stand channel)
rlm@0 624 ))))
rlm@0 625
rlm@0 626 (defn setup-fn [channel game]
rlm@0 627 (dorun (map #(.addLight (.getRootNode game) %) (lights)))
rlm@0 628 ;; set the color of the sky
rlm@0 629 (.setBackgroundColor (.getViewPort game) (ColorRGBA. 0.3 0.4 0.9 1))
rlm@0 630 ;(.setBackgroundColor (.getViewPort game) (ColorRGBA. 0 0 0 1)
rlm@0 631 (.setAnim channel "stand")
rlm@0 632 (doto (.getFlyByCamera game)
rlm@0 633 (.setMoveSpeed (float 10))
rlm@0 634 (.setRotationSpeed 1)))
rlm@0 635
rlm@0 636 (defn walk [channel]
rlm@0 637 (println-repl "zzz")
rlm@0 638 (doto channel
rlm@0 639 (.setAnim "Walk" (float 0.5))
rlm@0 640 (.setLoopMode LoopMode/Loop)))
rlm@0 641
rlm@0 642
rlm@0 643 (defn key-map [channel]
rlm@0 644 {"key-space" (fn [game value]
rlm@0 645 (if (not value)
rlm@0 646 (walk channel)))})
rlm@0 647
rlm@0 648 (defn player []
rlm@0 649 (let [model (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml")
rlm@0 650 control (.getControl model AnimControl)]
rlm@0 651 (.setLocalScale model (float 0.5))
rlm@0 652 (.clearListeners control)
rlm@0 653 (.addListener control (anim-control))
rlm@0 654 model))
rlm@0 655
rlm@0 656
rlm@0 657
rlm@0 658 (defn run-anim-game []
rlm@0 659 (let [ninja (player)
rlm@0 660 control (.getControl ninja AnimControl)
rlm@0 661 channel (.createChannel control)]
rlm@0 662 (.start
rlm@0 663 (world
rlm@0 664 ninja
rlm@0 665 (key-map channel)
rlm@0 666 (partial setup-fn channel)
rlm@0 667 no-op))))
rlm@0 668 #+end_src
rlm@0 669
rlm@34 670 * COMMENT Hello Materials
rlm@66 671 #+name: material
rlm@0 672 #+begin_src clojure :results silent
rlm@0 673 (ns hello.material)
rlm@0 674 (use 'cortex.world)
rlm@0 675 (use 'cortex.import)
rlm@34 676 (use 'cortex.util)
rlm@0 677 (use 'clojure.contrib.def)
rlm@0 678 (cortex.import/mega-import-jme3)
rlm@0 679 (rlm.rlm-commands/help)
rlm@0 680
rlm@0 681 (defn simple-cube []
rlm@0 682 (box 1 1 1
rlm@0 683 :position (Vector3f. -3 1.1 0)
rlm@0 684 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 685 :texture "Interface/Logo/Monkey.jpg"
rlm@0 686 :physical? false))
rlm@0 687
rlm@0 688 (defn leaky-box []
rlm@0 689 (box 1 1 1
rlm@0 690 :position (Vector3f. 3 -1 0)
rlm@0 691 :material "Common/MatDefs/Misc/ColoredTextured.j3md"
rlm@0 692 :texture "Textures/ColoredTex/Monkey.png"
rlm@0 693 :color (ColorRGBA. 1 0 1 1)
rlm@0 694 :physical? false))
rlm@0 695
rlm@0 696 (defn transparent-box []
rlm@0 697 (doto
rlm@0 698 (box 1 1 0.1
rlm@0 699 :position Vector3f/ZERO
rlm@0 700 :name "window frame"
rlm@0 701 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 702 :texture "Textures/ColoredTex/Monkey.png"
rlm@0 703 :physical? false)
rlm@0 704 (-> (.getMaterial)
rlm@0 705 (.getAdditionalRenderState)
rlm@0 706 (.setBlendMode RenderState$BlendMode/Alpha))
rlm@0 707 (.setQueueBucket RenderQueue$Bucket/Transparent)))
rlm@0 708
rlm@0 709 (defn bumpy-sphere []
rlm@0 710 (doto
rlm@0 711 (sphere 2
rlm@0 712 :position (Vector3f. 0 2 -2)
rlm@0 713 :name "Shiny rock"
rlm@0 714 :material "Common/MatDefs/Light/Lighting.j3md"
rlm@0 715 :texture false
rlm@0 716 :physical? false)
rlm@0 717 (-> (.getMesh)
rlm@0 718 (doto
rlm@0 719 (.setTextureMode Sphere$TextureMode/Projected)
rlm@0 720 (TangentBinormalGenerator/generate)))
rlm@0 721 (-> (.getMaterial)
rlm@0 722 (doto
rlm@31 723 (.setTexture "DiffuseMap"
rlm@31 724 (.loadTexture (asset-manager)
rlm@34 725 "Textures/Terrain/Pond/Pond.jpg"))
rlm@31 726 (.setTexture "NormalMap"
rlm@31 727 (.loadTexture (asset-manager)
rlm@31 728 "Textures/Terrain/Pond/Pond_normal.png"))
rlm@0 729 (.setFloat "Shininess" (float 5))))
rlm@0 730 (.rotate (float 1.6) 0 0)))
rlm@0 731
rlm@0 732
rlm@0 733 (defn start-game []
rlm@0 734 (.start
rlm@0 735 (world
rlm@0 736 (let [root (Node.)]
rlm@0 737 (dorun (map #(.attachChild root %)
rlm@0 738 [(simple-cube) (leaky-box) (transparent-box) (bumpy-sphere)]))
rlm@0 739 root)
rlm@0 740 {}
rlm@0 741 (fn [world]
rlm@0 742 (let [sun (doto (DirectionalLight.)
rlm@0 743 (.setDirection (.normalizeLocal (Vector3f. 1 0 -2)))
rlm@0 744 (.setColor ColorRGBA/White))]
rlm@0 745 (.addLight (.getRootNode world) sun)))
rlm@0 746 no-op
rlm@0 747 )))
rlm@0 748 #+end_src
rlm@0 749
rlm@0 750
rlm@0 751
rlm@0 752 * COMMENT code generation
rlm@0 753
rlm@0 754 #+begin_src clojure :tangle ../src/hello/brick_wall.clj
rlm@0 755 <<brick-wall-header>>
rlm@0 756 <<brick-wall-body>>
rlm@0 757 #+end_src
rlm@0 758
rlm@0 759 #+begin_src clojure :tangle ../src/hello/hello_simple_app.clj
rlm@0 760 <<hello-simple-app>>
rlm@0 761 #+end_src
rlm@0 762
rlm@34 763 #+begin_src clojure :tangle ../src/hello/hello_simpler_app.clj
rlm@34 764 <<hello-simpler-app>>
rlm@34 765 #+end_src
rlm@34 766
rlm@34 767
rlm@34 768 #+begin_src clojure :tangle ../src/hello/other_games.clj
rlm@0 769 <<other-games>>
rlm@0 770 #+end_src
rlm@0 771
rlm@0 772 #+begin_src clojure :tangle ../src/hello/loop.clj
rlm@0 773 <<hello-loop>>
rlm@0 774 #+end_src
rlm@0 775
rlm@0 776 #+begin_src clojure :tangle ../src/hello/collision.clj
rlm@0 777 <<hello-collision>>
rlm@0 778 #+end_src
rlm@0 779
rlm@0 780 #+begin_src clojure :tangle ../src/hello/terrain.clj
rlm@0 781 <<hello-terrain>>
rlm@0 782 #+end_src
rlm@0 783
rlm@0 784 #+begin_src clojure :tangle ../src/hello/animation.clj
rlm@0 785 <<hello-animation>>
rlm@0 786 #+end_src
rlm@0 787
rlm@0 788 #+begin_src clojure :tangle ../src/hello/material.clj
rlm@0 789 <<material>>
rlm@0 790 #+end_src
rlm@0 791
rlm@0 792
rlm@0 793
rlm@0 794
rlm@29 795
rlm@29 796
rlm@29 797
rlm@29 798
rlm@31 799