diff org/games.org @ 30:0206878c28b4

going to start on games.org
author Robert McIntyre <rlm@mit.edu>
date Mon, 24 Oct 2011 12:43:58 -0700
parents org/cortex.org@6372c108c5c6
children f0562b9bde94
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/org/games.org	Mon Oct 24 12:43:58 2011 -0700
     1.3 @@ -0,0 +1,789 @@
     1.4 +#+title: Simulated Senses
     1.5 +#+author: Robert McIntyre
     1.6 +#+email: rlm@mit.edu
     1.7 +#+description: Simulating senses for AI research using JMonkeyEngine3
     1.8 +#+SETUPFILE: ../../aurellem/org/setup.org
     1.9 +#+INCLUDE: ../../aurellem/org/level-0.org
    1.10 +#+babel: :mkdirp yes :noweb yes :exports both
    1.11 +
    1.12 +
    1.13 +* Simulation Base
    1.14 +  
    1.15 +
    1.16 +** Hello
    1.17 +Here are the jmonkeyengine "Hello" programs translated to clojure.
    1.18 +*** Hello Simple App
    1.19 +Here is the hello world example for jme3 in clojure.  It's a more or
    1.20 +less direct translation from the java source [[http://jmonkeyengine.org/wiki/doku.php/jme3:beginner:hello_simpleapplication][here]].
    1.21 +
    1.22 +Of note is the fact that since we don't have access to the
    1.23 +=AssetManager= via extendig =SimpleApplication=, we have to build one
    1.24 +ourselves. 
    1.25 +
    1.26 +#+srcname: hello-simple-app
    1.27 +#+begin_src clojure :results silent
    1.28 +(ns hello.hello-simple-app)
    1.29 +(require 'cortex.import)
    1.30 +(use 'clojure.contrib.def)
    1.31 +(rlm.rlm-commands/help)
    1.32 +(cortex.import/mega-import-jme3)
    1.33 +(use 'cortex.world)
    1.34 +
    1.35 +
    1.36 +(def cube (Box. Vector3f/ZERO 1 1 1))
    1.37 +
    1.38 +(def geom (Geometry. "Box" cube))
    1.39 +
    1.40 +(def mat (Material. (asset-manager) "Common/MatDefs/Misc/Unshaded.j3md"))
    1.41 +
    1.42 +(.setColor mat "Color" ColorRGBA/Blue)
    1.43 +
    1.44 +(.setMaterial geom mat)
    1.45 +
    1.46 +(defn simple-app []
    1.47 +  (doto
    1.48 +      (proxy [SimpleApplication] []
    1.49 +	(simpleInitApp
    1.50 +	 []
    1.51 +	 ;; Don't take control of the mouse
    1.52 +	 (org.lwjgl.input.Mouse/setGrabbed false)
    1.53 +	 (.attachChild (.getRootNode this) geom)))
    1.54 +    ;; don't show a menu to change options.
    1.55 +    (.setShowSettings false)
    1.56 +    (.setPauseOnLostFocus false)
    1.57 +    (.setSettings *app-settings*)))
    1.58 +#+end_src
    1.59 +
    1.60 +Running this program will begin a new jMonkeyEngine game which
    1.61 +displays a single blue cube.
    1.62 +
    1.63 +#+begin_src clojure :exports code :results silent
    1.64 +(.start (hello.hello-simple-app/simple-app))
    1.65 +#+end_src
    1.66 +
    1.67 +#+caption: the simplest JME game.
    1.68 +[[./images/simple-app.jpg]]
    1.69 +
    1.70 +
    1.71 +
    1.72 +*** Hello Physics
    1.73 +From http://jmonkeyengine.org/wiki/doku.php/jme3:beginner:hello_physics
    1.74 +
    1.75 +#+srcname: brick-wall-header
    1.76 +#+begin_src clojure :results silent
    1.77 +(ns hello.brick-wall)
    1.78 +(require 'cortex.import)
    1.79 +(use 'clojure.contrib.def)
    1.80 +(rlm.rlm-commands/help)
    1.81 +(cortex.import/mega-import-jme3)
    1.82 +(use '[pokemon [lpsolve :only [constant-map]]])
    1.83 +(use 'cortex.world)
    1.84 +(use 'cortex.util)
    1.85 +#+end_src
    1.86 +
    1.87 +#+srcname: brick-wall-body
    1.88 +#+begin_src clojure :results silent
    1.89 +(in-ns 'hello.brick-wall)
    1.90 +
    1.91 +(defn floor
    1.92 +  "make a sturdy, unmovable physical floor"
    1.93 +  []
    1.94 +  (box 20 1 20 :mass 0 :color false :position (Vector3f. 0 -2 0)))
    1.95 +
    1.96 +(def brick-length 0.48)
    1.97 +(def brick-width 0.24)
    1.98 +(def brick-height 0.12)
    1.99 +
   1.100 +
   1.101 +(defn brick* [position]
   1.102 +  (doto (box brick-length brick-height brick-width
   1.103 +	     :position position :name "brick"
   1.104 +	     :material "Common/MatDefs/Misc/Unshaded.j3md"
   1.105 +	     :texture "Textures/Terrain/BrickWall/BrickWall.jpg"
   1.106 +	     :mass 36)
   1.107 +    (->
   1.108 +     (.getMesh)
   1.109 +     (.scaleTextureCoordinates (Vector2f. 1 0.5)))
   1.110 +    ;;(.setShadowMode RenderQueue$ShadowMode/CastAndReceive)
   1.111 +    )
   1.112 +  )
   1.113 +
   1.114 +(defn inception-brick-wall
   1.115 +  "construct a physical brick wall"
   1.116 +  []
   1.117 +  (let [node (Node. "brick-wall")]
   1.118 +    (dorun
   1.119 +     (map (comp #(.attachChild node %) brick*)
   1.120 +	  (for
   1.121 +	      [x (range 15)
   1.122 +	       y (range 10)
   1.123 +	       z (range 1)]
   1.124 +	    (Vector3f.
   1.125 +	     (* brick-length x 1.03)
   1.126 +	     (* brick-width y y 10)
   1.127 +	     (* brick-height z)))))
   1.128 +    node))
   1.129 +
   1.130 +(defn gravity-toggle
   1.131 +  [new-value]
   1.132 +  (fn [game value]
   1.133 +    (println-repl "set gravity to " new-value)
   1.134 +    (if value
   1.135 +      (set-gravity* game new-value)
   1.136 +      (set-gravity* game gravity))))
   1.137 +
   1.138 +(defn fire-cannon-ball 
   1.139 +  ([node]
   1.140 +     (fn [game value]
   1.141 +       (if (not value)
   1.142 +         (let [camera (.getCamera game)
   1.143 +               cannon-ball
   1.144 +               (sphere  0.7 
   1.145 +                        :material "Common/MatDefs/Misc/Unshaded.j3md"
   1.146 +                        :texture "Textures/PokeCopper.jpg"
   1.147 +                        :position
   1.148 +                        (.add (.getLocation camera)
   1.149 +                              (.mult (.getDirection camera) (float 1)))
   1.150 +                        :mass 3)] ;200 0.05
   1.151 +           (.setShadowMode cannon-ball RenderQueue$ShadowMode/CastAndReceive)
   1.152 +           (.setLinearVelocity
   1.153 +            (.getControl cannon-ball RigidBodyControl)
   1.154 +            (.mult (.getDirection camera) (float 50))) ;50
   1.155 +           (add-element game cannon-ball (if node node (.getRootNode game)))))))
   1.156 +  ([]
   1.157 +    (fire-cannon-ball false)))
   1.158 +  
   1.159 +
   1.160 +(defn floor* []
   1.161 +  (doto (box 10 0.1 5 :name "floor" ;10 0.1 5 ; 240 0.1 240
   1.162 +	     :material "Common/MatDefs/Misc/Unshaded.j3md"
   1.163 +	     :texture "Textures/Terrain/Pond/Pond.png"
   1.164 +	     :position (Vector3f. 0 -0.1 0 )
   1.165 +	     :mass 0)
   1.166 +    (->
   1.167 +     (.getMesh)
   1.168 +     (.scaleTextureCoordinates (Vector2f. 3 6)));64 64
   1.169 +    (->
   1.170 +     (.getMaterial)
   1.171 +     (.getTextureParam "ColorMap")
   1.172 +     (.getTextureValue)
   1.173 +     (.setWrap Texture$WrapMode/Repeat))
   1.174 +    (.setShadowMode RenderQueue$ShadowMode/Receive)
   1.175 +  ))
   1.176 +    
   1.177 +(defn brick-wall* []
   1.178 +  (let [node (Node. "brick-wall")]
   1.179 +    (dorun
   1.180 +     (map
   1.181 +      (comp  #(.attachChild node %) brick*)
   1.182 +       (for [y (range 15)
   1.183 +	     x (range 4)
   1.184 +	     z (range 1)]
   1.185 +       	    (Vector3f.
   1.186 +       	     (+ (* 2 x brick-length)
   1.187 +		(if (even? (+ y z)) 
   1.188 +		  (/ brick-length 4) (/ brick-length -4)))
   1.189 +       	     (+ (* brick-height (inc (* 2 y))))
   1.190 +	     (* 2 z brick-width) ))))
   1.191 +    (.setShadowMode node RenderQueue$ShadowMode/CastAndReceive)
   1.192 +    node))
   1.193 +
   1.194 +(defn brick-wall-game-run []
   1.195 +  (doto
   1.196 +      (world
   1.197 +       (doto (Node.) (.attachChild (floor*))
   1.198 +	     (.attachChild (brick-wall*))
   1.199 +	     )
   1.200 +       {"key-i" (gravity-toggle (Vector3f. 0 0 -9.81))
   1.201 +	"key-m" (gravity-toggle (Vector3f. 0 0 9.81))
   1.202 +	"key-l" (gravity-toggle (Vector3f. 9.81 0 0))
   1.203 +	"key-j" (gravity-toggle (Vector3f. -9.81 0 0))
   1.204 +	"key-k" (gravity-toggle Vector3f/ZERO)
   1.205 +	"key-u" (gravity-toggle (Vector3f. 0 9.81 0))
   1.206 +	"key-o" (gravity-toggle (Vector3f. 0 -9.81 0))
   1.207 +	"key-f" (fn[game value] 
   1.208 +		  (if (not value) (add-element game (brick-wall*))))
   1.209 +	"key-return" (fire-cannon-ball)}
   1.210 +       position-camera
   1.211 +       (fn [& _]))     
   1.212 +    (.start)))
   1.213 +#+end_src
   1.214 +
   1.215 +#+begin_src clojure :results silent
   1.216 +(hello.brick-wall/brick-wall-game-run)
   1.217 +#+end_src
   1.218 +
   1.219 +#+caption: the brick wall standing
   1.220 +[[./images/brick-wall-standing.jpg]]
   1.221 +
   1.222 +#+caption: the brick wall after it has been knocked over by a "pok\eacute{}ball"
   1.223 +[[./images/brick-wall-knocked-down.jpg]]
   1.224 +
   1.225 +*** Other Brick Games
   1.226 +#+srcname: other-games
   1.227 +#+begin_src clojure :results silent
   1.228 +(ns cortex.other-games
   1.229 +   {:author "Dylan Holmes"})
   1.230 +(use 'cortex.world)
   1.231 +(use 'hello.brick-wall)
   1.232 +(use 'cortex.import)
   1.233 +(cortex.import/mega-import-jme3)
   1.234 +
   1.235 +(defn scad [position]
   1.236 +  (doto (box 0.1 0.1 0.1
   1.237 +	     :position position :name "brick"
   1.238 +	     :material "Common/MatDefs/Misc/Unshaded.j3md"
   1.239 +	     :texture "Textures/Terrain/BrickWall/BrickWall.jpg"
   1.240 +	     :mass 20)
   1.241 +    (->
   1.242 +     (.getMesh)
   1.243 +     (.scaleTextureCoordinates (Vector2f. 1 0.5))
   1.244 +     )
   1.245 +        (->	(.getControl RigidBodyControl)
   1.246 +	(.setLinearVelocity (Vector3f. 0 100 0))
   1.247 +	)
   1.248 +	
   1.249 +    ;;(.setShadowMode RenderQueue$ShadowMode/Cast)
   1.250 +    ))
   1.251 +
   1.252 +
   1.253 +(defn shrapnel []
   1.254 +  (let [node (Node. "explosion-day")]
   1.255 +    (dorun
   1.256 +     (map
   1.257 +      (comp  #(.attachChild node %) scad)
   1.258 +       (for [y (range 15)
   1.259 +	     x (range 4)
   1.260 +	     z (range 1)]
   1.261 +       	    (Vector3f.
   1.262 +       	     (+ (* 2 x brick-height)
   1.263 +		(if (even? (+ y z)) (/ brick-height 4) (/ brick-height -4)))
   1.264 +       	     (+ (* brick-height (inc (* 2 y))))
   1.265 +	     (* 2 z brick-height) ))))
   1.266 +    node))
   1.267 +
   1.268 +
   1.269 +(def domino-height 0.48)
   1.270 +(def domino-thickness 0.12)
   1.271 +(def domino-width 0.24)
   1.272 +
   1.273 +(def domino-thickness 0.05)
   1.274 +(def domino-width 0.5)
   1.275 +(def domino-height 1)
   1.276 +
   1.277 +(defn domino
   1.278 +  ([position]
   1.279 +     (domino position (Quaternion/IDENTITY)))
   1.280 +  ([position rotation]
   1.281 +     (doto (box domino-width domino-height domino-thickness
   1.282 +	     :position position :name "domino"
   1.283 +	     :material "Common/MatDefs/Misc/Unshaded.j3md"
   1.284 +	     :texture "Textures/Terrain/BrickWall/BrickWall.jpg"
   1.285 +	     :mass 1
   1.286 +	     :rotation rotation)
   1.287 +       (.setShadowMode RenderQueue$ShadowMode/CastAndReceive)
   1.288 +       )))
   1.289 +
   1.290 +
   1.291 +(defn domino-row []
   1.292 +  (let [node (Node. "domino-row")]
   1.293 +    (dorun
   1.294 +     (map
   1.295 +      (comp  #(.attachChild node %) domino)
   1.296 +       (for [
   1.297 +	     z (range 10)
   1.298 +	     x (range 5)
   1.299 +	     ]
   1.300 +       	    (Vector3f.
   1.301 +	     (+ (* z domino-width) (* x 5 domino-width))
   1.302 +	     (/ domino-height 1)
   1.303 +	     (* -5.5 domino-thickness z) ))))
   1.304 +
   1.305 +    node))
   1.306 +
   1.307 +(defn domino-cycle []
   1.308 +  (let [node (Node. "domino-cycle")]
   1.309 +    (dorun
   1.310 +     (map
   1.311 +      (comp  #(.attachChild node %) (partial apply domino) ) 
   1.312 +      (for [n (range 720)]
   1.313 +	(let [space (* domino-height 5.5)
   1.314 +	      r (fn[n] (* (+ n 3) domino-width 0.5)) 
   1.315 +	      t (fn[n] (reduce
   1.316 +			+
   1.317 +			(map
   1.318 +			 (fn dt[n] (/ space (* 2 (Math/PI) (r n))))
   1.319 +			 (range n))))
   1.320 +	      t (t n)
   1.321 +	      r (r n)
   1.322 +	      ct (Math/cos t)
   1.323 +	      st (Math/sin t)
   1.324 +	      ]
   1.325 +	(list
   1.326 +       	    (Vector3f.
   1.327 +	     (* -1 r st)
   1.328 +	     (/ domino-height 1)
   1.329 +	     (* r ct))
   1.330 +	    (.fromAngleAxis (Quaternion.)
   1.331 +			    (- (/ 3.1415926 2) t) (Vector3f. 0 1 0))
   1.332 +	    )))
   1.333 +	))
   1.334 +    node))
   1.335 +
   1.336 +
   1.337 +(defn domino-game-run []
   1.338 +  (doto
   1.339 +      (world
   1.340 +       (doto (Node.) (.attachChild (floor*))
   1.341 +	     )
   1.342 +       {"key-i" (gravity-toggle (Vector3f. 0 0 -9.81))
   1.343 +	"key-m" (gravity-toggle (Vector3f. 0 0 9.81))
   1.344 +	"key-l" (gravity-toggle (Vector3f. 9.81 0 0))
   1.345 +	"key-j" (gravity-toggle (Vector3f. -9.81 0 0))
   1.346 +	"key-k" (gravity-toggle (Vector3f. 0 9.81 0) )
   1.347 +	"key-u" (fn[g v] ((gravity-toggle (Vector3f. 0 -0 0)) g true))
   1.348 +	"key-o" (gravity-toggle (Vector3f. 0 -9.81 0))
   1.349 +
   1.350 +	"key-space"
   1.351 +	(fn[game value]
   1.352 +	  
   1.353 +	  (if (not value)
   1.354 +	    (let [d (domino (Vector3f. 0 (/ domino-height 0.25) 0)
   1.355 +			    (.fromAngleAxis (Quaternion.)
   1.356 +					    (/ Math/PI 2) (Vector3f. 0 1 0)))]
   1.357 +	      (add-element game d))))
   1.358 +	"key-f"
   1.359 +	(fn[game value](if (not value) (add-element game (domino-cycle))))
   1.360 +	"key-return" (fire-cannon-ball)}
   1.361 +       position-camera
   1.362 +       (fn [& _]))     
   1.363 +    (.start)))       
   1.364 +#+end_src
   1.365 +
   1.366 +#+begin_src clojure :results silent
   1.367 +(cortex.other-games/domino-game-run)
   1.368 +#+end_src
   1.369 +
   1.370 +#+caption: floating dominos
   1.371 +[[./images/dominos.jpg]]
   1.372 +
   1.373 +*** Hello Loop
   1.374 +#+srcname: hello-loop
   1.375 +#+begin_src clojure :results silent
   1.376 +(ns hello.loop)
   1.377 +(use 'cortex.world)
   1.378 +(use 'cortex.import)
   1.379 +(cortex.import/mega-import-jme3)
   1.380 +(rlm.rlm-commands/help)
   1.381 +
   1.382 +(defn blue-cube []
   1.383 +  (box 1 1 1
   1.384 +       :color ColorRGBA/Blue
   1.385 +       :texture false
   1.386 +       :material "Common/MatDefs/Misc/Unshaded.j3md"
   1.387 +       :name "blue-cube"
   1.388 +       :physical? false))
   1.389 +
   1.390 +(defn blue-cube-game []
   1.391 +  (let [cube (blue-cube)
   1.392 +	root (doto (Node.) (.attachChild cube))]
   1.393 +  (world root
   1.394 +	 {}
   1.395 +	 no-op
   1.396 +	 (fn [game tpf]
   1.397 +	   (.rotate cube 0.0 (* 2 tpf) 0.0)))))	 	 		  
   1.398 +#+end_src
   1.399 +
   1.400 +*** Hello Collision
   1.401 +
   1.402 +#+srcname: hello-collision
   1.403 +#+begin_src clojure :results silent
   1.404 +(ns hello.collision)
   1.405 +(use 'cortex.world)
   1.406 +(use 'cortex.import)
   1.407 +(use 'clojure.contrib.def)
   1.408 +
   1.409 +
   1.410 +(cortex.import/mega-import-jme3)
   1.411 +(rlm.rlm-commands/help)
   1.412 +(use '[hello [brick-wall :only [fire-cannon-ball brick-wall*]]])
   1.413 +
   1.414 +
   1.415 +(defn environment []
   1.416 +     (let
   1.417 +	 [scene-model
   1.418 +	  (doto
   1.419 +	      (.loadModel
   1.420 +	       (doto (asset-manager)
   1.421 +		 (.registerLocator
   1.422 +		  "/home/r/cortex/assets/zips/town.zip" ZipLocator))
   1.423 +	       "main.scene")
   1.424 +	    (.setLocalScale (float 2.0)))
   1.425 +	  collision-shape
   1.426 +	  (CollisionShapeFactory/createMeshShape #^Node scene-model)
   1.427 +	  landscape (RigidBodyControl. collision-shape 0)]
   1.428 +       (.setShadowMode scene-model RenderQueue$ShadowMode/CastAndReceive)
   1.429 +       (.addControl scene-model landscape)
   1.430 +       scene-model))
   1.431 +	  
   1.432 +(defn player-fn []
   1.433 +  (doto
   1.434 +      (CharacterControl.
   1.435 +       (CapsuleCollisionShape. (float 1.5) (float 6)(float 1))
   1.436 +       (float 0.05))
   1.437 +    (.setJumpSpeed 20)
   1.438 +    (.setFallSpeed 30)
   1.439 +    (.setGravity 30) ;30
   1.440 +    (.setPhysicsLocation (Vector3f. 0 10 0))))
   1.441 +
   1.442 +(defn lights []
   1.443 +  [(doto (AmbientLight.) (.setColor (.mult (ColorRGBA. 1 1 1 1) (float 1))))
   1.444 +   (doto (AmbientLight.) (.setColor (.mult (ColorRGBA. 1 0.7 0 1) (float 1))))
   1.445 +   (doto (DirectionalLight.)
   1.446 +     (.setColor (.mult ColorRGBA/White (float 0.9) ))
   1.447 +     (.setDirection (.normalizeLocal (Vector3f. 2.8 -28 2.8))))])
   1.448 +
   1.449 +(defn night-lights []
   1.450 +  [(doto (AmbientLight.) (.setColor (.mult (ColorRGBA. 0.275 0.467 0.784 1) (float 0.3))))
   1.451 +   (doto (DirectionalLight.)
   1.452 +     (.setColor (.mult ColorRGBA/White (float 0.2) ))
   1.453 +     (.setDirection (.normalizeLocal (Vector3f. 2.8 -28 2.8))))])
   1.454 +
   1.455 +(def player (atom (player-fn)))
   1.456 +
   1.457 +(defn setup-fn [game]
   1.458 +  (dorun (map #(.addLight (.getRootNode game) %) (lights)))
   1.459 +  ;; set the color of the sky
   1.460 +  (.setBackgroundColor (.getViewPort game) (ColorRGBA. 0.3 0.4 0.9 1))
   1.461 +  ;(.setBackgroundColor (.getViewPort game) (ColorRGBA. 0 0 0 1)
   1.462 +  (doto (.getFlyByCamera game)
   1.463 +    (.setMoveSpeed (float 100))
   1.464 +    (.setRotationSpeed 3))
   1.465 +  (.add
   1.466 +   (.getPhysicsSpace
   1.467 +    (.getState (.getStateManager game) BulletAppState))
   1.468 +   @player)
   1.469 +
   1.470 +   (doto (Node.) (.attachChild (.getRootNode game))
   1.471 +	     (.attachChild (brick-wall*))
   1.472 +   )
   1.473 +
   1.474 +)
   1.475 +
   1.476 +
   1.477 +(def walking-up? (atom false))
   1.478 +(def walking-down? (atom false))
   1.479 +(def walking-left? (atom false))
   1.480 +(def walking-right? (atom false))
   1.481 +
   1.482 +(defn set-walk [walk-atom game value]
   1.483 +  ;;(println-repl "setting  stuff to " value)
   1.484 +  (reset! walk-atom value))
   1.485 +
   1.486 +(defn responses []
   1.487 +     {"key-w" (partial set-walk walking-up?)
   1.488 +      "key-d" (partial set-walk walking-right?)
   1.489 +      "key-s" (partial set-walk walking-down?)
   1.490 +      "key-a" (partial set-walk walking-left?)
   1.491 +      "key-return" (fire-cannon-ball)
   1.492 +      "key-space" (fn [game value] (.jump @player))
   1.493 +      })
   1.494 +     
   1.495 +(defn update-fn
   1.496 +  [game tpf]
   1.497 +  (let [camera (.getCamera game)         
   1.498 +	cam-dir (.multLocal
   1.499 +		 (.clone
   1.500 +		  (.getDirection camera)) (float 0.6))
   1.501 +	cam-left (.multLocal
   1.502 +		  (.clone
   1.503 +		   (.getLeft camera)) (float 0.4))
   1.504 +	walk-direction (Vector3f. 0 0 0)]
   1.505 +    
   1.506 +    (cond
   1.507 +     @walking-up?     (.addLocal walk-direction cam-dir)
   1.508 +     @walking-right?  (.addLocal walk-direction (.negate cam-left))
   1.509 +     @walking-down?   (.addLocal walk-direction (.negate cam-dir))
   1.510 +     @walking-left?   (.addLocal walk-direction cam-left))
   1.511 +    (.setWalkDirection @player walk-direction)
   1.512 +    (.setLocation camera (.getPhysicsLocation @player))))
   1.513 +    
   1.514 +(defn run-game []
   1.515 +  (.start
   1.516 +   (world (environment)
   1.517 +	  (responses)
   1.518 +	  setup-fn
   1.519 +	  update-fn)))
   1.520 +#+end_src
   1.521 +
   1.522 +*** Hello Terrain
   1.523 +#+srcname: hello-terrain
   1.524 +#+begin_src clojure :results silent
   1.525 +(ns hello.terrain)
   1.526 +(use 'cortex.world)
   1.527 +(use 'cortex.import)
   1.528 +(use 'clojure.contrib.def)
   1.529 +(import jme3tools.converters.ImageToAwt)
   1.530 +
   1.531 +
   1.532 +(cortex.import/mega-import-jme3)
   1.533 +(rlm.rlm-commands/help)
   1.534 +(use '[hello [brick-wall :only [fire-cannon-ball brick-wall*]]])
   1.535 +
   1.536 +
   1.537 +(defn setup-fn [type game]
   1.538 +  (.setMoveSpeed (.getFlyByCamera game) 50)
   1.539 +  (.setFrustumFar (.getCamera game) 10000)
   1.540 +  (let [env (environment type)
   1.541 +	cameras [(.getCamera game)]
   1.542 +	control (TerrainLodControl. env cameras)]
   1.543 +    ;;(.addControl env control)
   1.544 +    (.attachChild (.getRootNode game) env)))                
   1.545 +
   1.546 +(defn environment [type]
   1.547 +  (let
   1.548 +      [mat_terrain
   1.549 +       (Material. (asset-manager) "Common/MatDefs/Terrain/Terrain.j3md")
   1.550 +       grass (.loadTexture (asset-manager) "Textures/Terrain/splat/grass.jpg")
   1.551 +       dirt (.loadTexture (asset-manager) "Textures/Terrain/splat/dirt.jpg")
   1.552 +       rock (.loadTexture (asset-manager) "Textures/Terrain/splat/road.jpg")
   1.553 +       heightmap-image (.loadTexture (asset-manager)
   1.554 +				     ({:mountain "Textures/Terrain/splat/mountains512.png"
   1.555 +				       :fortress "Textures/Terrain/splat/fortress512.png"
   1.556 +				       }type))
   1.557 +       heightmap (ImageBasedHeightMap.
   1.558 +		  (ImageToAwt/convert (.getImage heightmap-image) false true 0))
   1.559 +       terrain (do (.load heightmap)
   1.560 +		   (TerrainQuad. "my terrain" 65 513 (.getHeightMap heightmap)))
   1.561 +       ]
   1.562 +    
   1.563 +    (dorun (map #(.setWrap % Texture$WrapMode/Repeat)
   1.564 +		[grass dirt rock]))
   1.565 +
   1.566 +    (doto mat_terrain
   1.567 +      (.setTexture "Tex1" grass)
   1.568 +      (.setFloat "Tex1Scale" (float 64))
   1.569 +
   1.570 +      (.setTexture "Tex2" dirt)
   1.571 +      (.setFloat "Tex2Scale" (float 32))
   1.572 +
   1.573 +      (.setTexture "Tex3" rock)
   1.574 +      (.setFloat "Tex3Scale" (float 128))
   1.575 +
   1.576 +      (.setTexture "Alpha"
   1.577 +		   (.loadTexture
   1.578 +		    (asset-manager) 
   1.579 +		    ({:mountain "Textures/Terrain/splat/alphamap.png"
   1.580 +		      :fortress "Textures/Terrain/splat/alphamap2.png"} type))))
   1.581 +
   1.582 +    (doto terrain
   1.583 +      (.setMaterial mat_terrain)
   1.584 +      (.setLocalTranslation 0 -100 0)
   1.585 +      (.setLocalScale 2 1 2))))
   1.586 +      
   1.587 +
   1.588 +
   1.589 +(defn run-terrain-game [type]
   1.590 +  (.start
   1.591 +   (world
   1.592 +    (Node.)
   1.593 +    {}
   1.594 +    (partial setup-fn type)
   1.595 +    no-op)))
   1.596 +#+end_src
   1.597 +
   1.598 +
   1.599 +
   1.600 +#+srcname: hello-animation
   1.601 +#+begin_src clojure :results silent
   1.602 +(ns hello.animation)
   1.603 +(use 'cortex.world)
   1.604 +(use 'cortex.import)
   1.605 +(use 'clojure.contrib.def)
   1.606 +(cortex.import/mega-import-jme3)
   1.607 +(rlm.rlm-commands/help)
   1.608 +(use '[hello [collision :only [lights]]])
   1.609 +
   1.610 +(defn stand
   1.611 +  [channel]
   1.612 +  (doto channel
   1.613 +    (.setAnim "stand" (float 0.5))
   1.614 +    (.setLoopMode LoopMode/DontLoop)
   1.615 +    (.setSpeed (float 1))))
   1.616 +
   1.617 +(defn anim-listener []
   1.618 +     (proxy [AnimEventListener] []
   1.619 +       (onAnimChange
   1.620 +	[control channel animation-name]
   1.621 +	(println-repl "RLM --- onAnimChange"))
   1.622 +       (onAnimCycleDone
   1.623 +	[control channel animation-name]
   1.624 +	(if (= animation-name "Walk")
   1.625 +	  (stand channel)
   1.626 +	  ))))
   1.627 +       
   1.628 +(defn setup-fn [channel game]
   1.629 +  (dorun (map #(.addLight (.getRootNode game) %) (lights)))
   1.630 +  ;; set the color of the sky
   1.631 +  (.setBackgroundColor (.getViewPort game) (ColorRGBA. 0.3 0.4 0.9 1))
   1.632 +  ;(.setBackgroundColor (.getViewPort game) (ColorRGBA. 0 0 0 1)
   1.633 +  (.setAnim channel "stand")
   1.634 +  (doto (.getFlyByCamera game)
   1.635 +    (.setMoveSpeed (float 10))
   1.636 +    (.setRotationSpeed 1)))
   1.637 +
   1.638 +(defn walk [channel]
   1.639 +  (println-repl "zzz")
   1.640 +  (doto channel
   1.641 +    (.setAnim "Walk" (float 0.5))
   1.642 +    (.setLoopMode LoopMode/Loop)))
   1.643 +    
   1.644 +
   1.645 +(defn key-map [channel]
   1.646 +  {"key-space" (fn [game value]
   1.647 +		 (if (not value)
   1.648 +		   (walk channel)))})
   1.649 +
   1.650 +(defn player []
   1.651 +  (let [model (.loadModel (asset-manager) "Models/Oto/Oto.mesh.xml")
   1.652 +	control (.getControl model AnimControl)]
   1.653 +    (.setLocalScale model (float 0.5))
   1.654 +    (.clearListeners control)
   1.655 +    (.addListener control (anim-control))
   1.656 +    model))
   1.657 +    
   1.658 +
   1.659 +
   1.660 +(defn run-anim-game []
   1.661 +  (let [ninja (player)
   1.662 +	control  (.getControl ninja AnimControl)
   1.663 +	channel (.createChannel control)]
   1.664 +    (.start
   1.665 +     (world
   1.666 +      ninja
   1.667 +      (key-map channel)
   1.668 +      (partial setup-fn channel)
   1.669 +      no-op))))
   1.670 +#+end_src
   1.671 +
   1.672 +*** Hello Materials
   1.673 +#+srcname: material
   1.674 +#+begin_src clojure :results silent
   1.675 +(ns hello.material)
   1.676 +(use 'cortex.world)
   1.677 +(use 'cortex.import)
   1.678 +(use 'clojure.contrib.def)
   1.679 +(cortex.import/mega-import-jme3)
   1.680 +(rlm.rlm-commands/help)
   1.681 +
   1.682 +(defn simple-cube []
   1.683 +  (box 1 1 1
   1.684 +       :position (Vector3f. -3 1.1 0)
   1.685 +       :material "Common/MatDefs/Misc/Unshaded.j3md"
   1.686 +       :texture "Interface/Logo/Monkey.jpg"
   1.687 +       :physical? false))
   1.688 +
   1.689 +(defn leaky-box []
   1.690 +  (box 1 1 1
   1.691 +       :position (Vector3f. 3 -1 0)
   1.692 +       :material "Common/MatDefs/Misc/ColoredTextured.j3md"
   1.693 +       :texture  "Textures/ColoredTex/Monkey.png"
   1.694 +       :color    (ColorRGBA. 1 0 1 1)
   1.695 +       :physical? false))
   1.696 +
   1.697 +(defn transparent-box []
   1.698 +  (doto
   1.699 +      (box 1 1 0.1
   1.700 +	   :position Vector3f/ZERO
   1.701 +	   :name "window frame"
   1.702 +	   :material "Common/MatDefs/Misc/Unshaded.j3md"
   1.703 +	   :texture "Textures/ColoredTex/Monkey.png"
   1.704 +	   :physical? false)
   1.705 +    (-> (.getMaterial)
   1.706 +	(.getAdditionalRenderState)
   1.707 +	(.setBlendMode RenderState$BlendMode/Alpha))
   1.708 +    (.setQueueBucket RenderQueue$Bucket/Transparent)))
   1.709 +                  
   1.710 +(defn bumpy-sphere []
   1.711 +  (doto 
   1.712 +      (sphere 2
   1.713 +	      :position (Vector3f. 0 2 -2)
   1.714 +	      :name "Shiny rock"
   1.715 +	      :material "Common/MatDefs/Light/Lighting.j3md"
   1.716 +	      :texture false
   1.717 +	      :physical? false)
   1.718 +    (-> (.getMesh)
   1.719 +	(doto 
   1.720 +	  (.setTextureMode Sphere$TextureMode/Projected)
   1.721 +	  (TangentBinormalGenerator/generate)))
   1.722 +    (-> (.getMaterial)
   1.723 +	(doto
   1.724 +	  (.setTexture "DiffuseMap" (.loadTexture (asset-manager)
   1.725 +						  "Textures/Terrain/Pond/Pond.png"))
   1.726 +	  (.setTexture "NormalMap"  (.loadTexture (asset-manager)
   1.727 +						  "Textures/Terrain/Pond/Pond_normal.png"))
   1.728 +	  (.setFloat   "Shininess"  (float 5))))
   1.729 +    (.rotate (float 1.6) 0 0)))
   1.730 +
   1.731 +
   1.732 +(defn start-game []
   1.733 +  (.start
   1.734 +   (world
   1.735 +    (let [root  (Node.)]
   1.736 +      (dorun (map #(.attachChild root %)
   1.737 +		  [(simple-cube) (leaky-box) (transparent-box) (bumpy-sphere)]))
   1.738 +      root)
   1.739 +    {}
   1.740 +    (fn [world]
   1.741 +      (let [sun (doto (DirectionalLight.)
   1.742 +		  (.setDirection (.normalizeLocal (Vector3f. 1 0 -2)))
   1.743 +		  (.setColor ColorRGBA/White))]
   1.744 +	(.addLight (.getRootNode world) sun)))
   1.745 +    no-op 
   1.746 +    )))
   1.747 +#+end_src
   1.748 +
   1.749 +
   1.750 +    
   1.751 +* COMMENT code generation
   1.752 +
   1.753 +#+begin_src clojure :tangle ../src/hello/brick_wall.clj
   1.754 +<<brick-wall-header>>
   1.755 +<<brick-wall-body>>
   1.756 +#+end_src
   1.757 +
   1.758 +#+begin_src clojure :tangle ../src/hello/hello_simple_app.clj
   1.759 +<<hello-simple-app>>
   1.760 +#+end_src
   1.761 +
   1.762 +#+begin_src clojure :tangle ../src/cortex/other_games.clj
   1.763 +<<other-games>>
   1.764 +#+end_src
   1.765 +
   1.766 +#+begin_src clojure :tangle ../src/hello/loop.clj
   1.767 +<<hello-loop>>
   1.768 +#+end_src
   1.769 +
   1.770 +#+begin_src clojure :tangle ../src/hello/collision.clj
   1.771 +<<hello-collision>>
   1.772 +#+end_src
   1.773 +
   1.774 +#+begin_src clojure :tangle ../src/hello/terrain.clj
   1.775 +<<hello-terrain>>
   1.776 +#+end_src
   1.777 +
   1.778 +#+begin_src clojure :tangle ../src/hello/animation.clj
   1.779 +<<hello-animation>>
   1.780 +#+end_src
   1.781 +
   1.782 +#+begin_src clojure :tangle ../src/hello/material.clj
   1.783 +<<material>>
   1.784 +#+end_src
   1.785 +
   1.786 +
   1.787 +  
   1.788 +
   1.789 +
   1.790 +
   1.791 +
   1.792 +