~alect/junk-drawer

A collection of random tools for gamedev.
87721715 — Alec Troemel 1 year, 8 months ago
Merge pull request #18 from AlecTroemel/17-ecs-enhance
2a2f1d8f — alec troemel 1 year, 8 months ago
use new def-component syntax with some simple type checking,
5ac6e3a9 — alec troemel 1 year, 8 months ago
return the entities ID from create

refs

main
browse  log 

clone

read-only
https://git.sr.ht/~alect/junk-drawer
read/write
git@git.sr.ht:~alect/junk-drawer

You can also use your local clone with git send-email.

Everyones got one (usually somewhere in the kitchen). Junk Drawer is a small collection of tools & utils for developing games.

#Contents:

  • ecs: A simple Entity Component System
  • fsm: Finite(ish) State Machine
  • gamestate: Easy gamestate management.
  • timers: Delayed & Schedule functions (requires using ECS)
  • messages: Communication between systems (requires using ECS)
  • tweens: Some common tweening functions and a way to interpolate with them (mostly requires ECS)

Here's an obligitory example that uses most the stuff here. For more detailed examples...look in the examples/ folder.

(use junk-drawer)

(fsm/define
  colors
  {:green
   {:next |(:goto $ :yellow)}
   :yellow
   {:next |(:goto $ :red)}
   :red
   {:next |(:goto $ :green)}})

(def-tag next-color)

(def-system colored-printer
  {color-fsms [:colors]}
  (each [c] color-fsms
    (printf "current color: %q" (c :current))))

(def-system colored-switcher
  {wld :world
   msgs [:message :next-color]
   color-fsms [:colors]}
  (when (> (length msgs) 0)
    (each [msg] msgs
      (each [c] color-fsms
        ((msg :content) c))
      (messages/consume msg))))

(def GS (gamestate/init))

(def example
  {:name "Example Gamestate"
   :world (create-world)
   :init (fn [self]
           (let [world (get self :world)]
             (add-entity world (colors :green))
             (add-entity world (colors :red))
             (register-system world timers/update-sys)
             (register-system world messages/update-sys)
             (register-system world colored-printer)
             (register-system world colored-switcher)
             (timers/every world 4
                           (fn [wld dt]
                             (messages/send wld :next next-color)))
             (timers/after world 7
                           (fn [wld dt]
                             (messages/send wld :next next-color)))))
   :update (fn [self dt]
             (:update (self :world) dt))})

(:push GS example)

(for i 0 20
  (:update GS 1))