annotate org/games.org @ 31:f0562b9bde94

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