To break down the update into multiple steps use the -> macro:

=>(defn step [world-state])
      (-> world-state
        update-health
        update-physics
        update-ai))

where e.g. update-health is something like

=>(defn update-health [world-state]
      (update-in world-state [:player :health] inc))

then yeah just iterate or loop/recur

=>(take 20 (iterate step init-state))

this is the purely functional way of doing it.



On Sep 24, 3:36 pm, Dennis Haupt <d.haup...@googlemail.com> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> in java, i would start coding a game with a loop like this:
> while (true) {
> logic();
> render();
>
> }
>
> i would store the current state of the world in an object containing
> the complete data of the whole game and update its values in each
> iteration.
>
> how would i do this in clojure?
>
> the outer loop could look like
> (def next [oldstate] (....)) <- input = current game, return value =
> next iteration
>
> (loop [world initalState] (recur (next world))) // <- the loop
>
> but how would be world look like? the "best" (most trivial) thing that
> i could think of is for it to be a map which is passed along several
> transform functions, for example
>
> (def playerHealthRegen [world] (...)) <- input = world (a map), output
> = a new map with a new entry at key "playerhealth"
>
> each function would then return a slightly modified version of the
> world, and at the end, i'll have my completely new state.
>
> is that about right? or is there a completely different way i overlooked?
>
> - --
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v2.0.14 (MingW32)
> Comment: Using GnuPG with Mozilla -http://enigmail.mozdev.org/
>
> iQIcBAEBAgAGBQJOfjE+AAoJENRtux+h35aGo0MQAMipkc8e0YTPxsWsLzaVoQuz
> MtXerKHHqqbuyxy+mzlc4xFfFUfs//wQGdk/ExZhby7eNVBc9AGYKarCyG/DVxfM
> HwN7RVHIKDtWoHQk71dthSAzkHgbZvFxjO2W3EkI10rTsCYNFx1WV4o/PMt/KYJj
> phmtO9LcHmb/ySsLveTmSdJTYjSDb7ENudLbM2z/4SP9AqN21sU1HRNF/Y4gLnq3
> tnnGmbpRU8Xs6xv8O8oluRrhjgpGF58okG+JnnW+aqF95OaDMp2dQ2mPKxcWLzmt
> zkMj41jC28By05oVPIIOstB50rOzU0VAQvEJRDohz2E2sxbhFfUci7G/75hvBkYz
> vUXeQi4TCYM/gQlOOiAqUuutWpYWBbgL7OOHck3VkGn7UEKBguhkMTO/xGJjFxbY
> 6/pxIy7i7+DbSXfq+tu5sw2XAS96tctD1dWVdFjfpKukckvcDff3/L0ObKwIxTQu
> BN9tqoUOs1Tp2OBJhkEJfaBMgUKqX5+IW/mKARVywNFLRWTAYs74OTO86ei/jTPo
> kqwu2NGE9p/iHpLAxin8sz6I34kOlHJ2X7Xi4PBC19mmVgErt+A8MIvELuxhKBYw
> BxoWZ11bccphKHFUdEDaj43pd1DqFhLqqpDvFWumUIO48pnDRpYcYcRLZ/6raCXv
> apIq/CL5V7UHCJ+d/ANo
> =Ckx2
> -----END PGP SIGNATURE-----

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to