Entity component system

335 views
Skip to first unread message

msappler

unread,
Jan 9, 2011, 10:49:14 AM1/9/11
to Clojure
Hey for the lasts 6 months I am developing an action RPG game in
clojure (and learning the language).

For this game I developed an entity component system which is a way of
using composition to create entities in the game.

I also use slick2d for the 2d engine.

Here I posted some examples for defmonster/player-entity:
http://slick.javaunlimited.net/viewtopic.php?p=17375#17375
(last post under name hardcore)

Basically I just thougt maybe somebody is interested in my solution
and I would maybe write some more about this engine or make it open
source.

------

Here my post from the slick2d forums:

Hello guys

For the last 6 months I have been learning clojure (lisp for the jvm)
and writing my own entity engine for my action rpg in this language.

It is soo much less verbose than my java attempt at such an engine.

Here is an example of a few monsters defined:
Code:

(defmonster armored-skull {:hp 2 :armor 65 :lvl 4 :pxsize 31}
(default-death-trigger level)
(create-melee-avoidance-movement-comp 0.0008)
(rotation-component)
(hp-regen-component 2)
(body-image-render-component (monsterimage "armoredskull.png"))
(create-monster-melee-comp 500 250 level (sound "slash.wav") (get-id
player-body)))

(defmonster mage-skull {:hp 4.8 :armor 0 :lvl 4 :pxsize 30}
(default-death-trigger level)
(create-ranged-movement-comp 0.004 (rand-int-between 3 4))
(hp-regen-component 10)
(rotation-component)
(body-image-render-component (monsterimage "mageskull.png"))
(create-monster-ranged-comp level 3000 50))



and spawning an instance of a monster-type at position 10 5:
Code:

(try-spawn [10 5] :mage-skull)



Or the player character:
Code:

(defn create-player-body [position]
(create-body-with-id player-entity-id true :player position 30 30
(death-trigger player-death)
(create-player-skillmanager)
(movement-component
{:control-update
(fn [body control delta]
(cond
@saved-mouseover-body
(get-vector-from-to body @saved-mouseover-body)
(and (not (:leftm-consumed @state)) (is-leftbutton-down?))
(get-vector-to-mouse-coords)))}
player-move-speed)
(destructible-component player-start-hp 0)
(rotation-component)
(dmg-modifier-component)
(hp-regen-component 0.25)
(light-component 1 1 1 0.7 7)
(mana-regen-component 0.25)
(player-animation)
(show-on-minimap Color/red)))


My solution evolved over many months and I find it clean and simple.

Basically I have a map of ids to entities.

An entity just consists of multiple components (and nothing else).

If somebody is interested I would write more about my entity-component
system in clojure.

Eduardo Julian

unread,
Jan 9, 2011, 2:38:08 PM1/9/11
to Clojure
Interesting. I'm also into game development with Clojure. I'd like to
see the project open--sourced and, if possible, participate.

justinhj

unread,
Jan 9, 2011, 7:43:52 PM1/9/11
to Clojure
Thanks for sharing. I've also spent some time building a Common Lisp
game engine that uses a component architecture for the game objects.

For example in pong the player's paddle is made up of a visual,
physical and logical components.

(defun make-pong-player(side human sprite-def control-type name)
(let ((phys (make-instance '2d-physics
:collide-type 'paddle :y *paddle-start-y* :width *paddle-
width* :height *paddle-height*))
; (anim (make-instance 'animated-sprite :sprite-def sprite-def
; :current-frame 'frame-1 :speed 5.0))
(visual (make-instance 'rectangle
:w *paddle-width* :h *paddle-height*))
(pong (make-instance 'player-paddle-logic
:control-type control-type :side side))
(obj (make-instance 'composite-object :name name)))
(add-component obj phys)
(add-component obj visual)
; (add-component obj anim)
(add-component obj pong)
obj))

The objects implement message handlers in order to operate. For
example the game engine sends update and draw messages. Users can
write their own message types with custom argument lists.

I've put the project on google code http://code.google.com/p/lisp-game-engine/

Although the pong game works I wouldn't consider this a finished
project by any means; it's more an experiment in game programming
using CL and the REPL.

It would require significant refactoring to make it work with Clojure
since I use mutable state a lot, but would certainly be possible.

Justin

Daniel Kersten

unread,
Jan 11, 2011, 7:32:29 PM1/11/11
to clo...@googlegroups.com
Thanks for sharing!

Entity component systems are something I'm very interested in and
something I have tinkered with in the past. I hope to (eventually)
find some time to play around with them in Clojure too. I would be
very interested in hearing more about your solution and would be
delighted if you were to choose to open source your project.

As an aside, do you mind if I copy your sample code and possibly parts
of your email to the clojure-games.org wiki?

Thanks,
Dan.

> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@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+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

msappler

unread,
Jan 12, 2011, 6:27:25 AM1/12/11
to Clojure
No i do not mind.
A blog is being planned for promotion of my game and sharing.
Only have to find a domain name which i like.


On 12 Jan., 01:32, Daniel Kersten <dkers...@gmail.com> wrote:
> Thanks for sharing!
>
> Entity component systems are something I'm very interested in and
> something I have tinkered with in the past. I hope to (eventually)
> find some time to play around with them in Clojure too. I would be
> very interested in hearing more about your solution and would be
> delighted if you were to choose to open source your project.
>
> As an aside, do you mind if I copy your sample code and possibly parts
> of your email to the clojure-games.org wiki?
>
> Thanks,
> Dan.
>
> On 10 January 2011 00:43, justinhj <justi...@gmail.com> wrote:
>
>
>
> > Thanks for sharing.  I've also spent some time building a Common Lisp
> > game engine that uses a component architecture for the game objects.
>
> > For example in pong the player's paddle is made up of a visual,
> > physical and logical components.
>
> > (defun make-pong-player(side human sprite-def control-type name)
> >  (let ((phys (make-instance '2d-physics
> >                             :collide-type 'paddle :y *paddle-start-y* :width *paddle-
> > width* :height *paddle-height*))
> > ;       (anim (make-instance 'animated-sprite :sprite-def sprite-def
> > ;                            :current-frame 'frame-1 :speed 5.0))
> >        (visual (make-instance 'rectangle
> >                                  :w *paddle-width* :h *paddle-height*))
> >        (pong (make-instance 'player-paddle-logic
> >                             :control-type control-type :side side))
> >        (obj (make-instance 'composite-object :name name)))
> >    (add-component obj phys)
> >    (add-component obj visual)
> > ;    (add-component obj anim)
> >    (add-component obj pong)
> >    obj))
>
> > The objects implement message handlers in order to operate. For
> > example the game engine sends update and draw messages. Users can
> > write their own message types with custom argument lists.
>
> > I've put the project on google codehttp://code.google.com/p/lisp-game-engine/

Daniel Werner

unread,
Mar 6, 2011, 10:24:45 AM3/6/11
to Clojure
Hi msappler,

On Jan 12, 12:27 pm, msappler <damnedmar...@web.de> wrote:
> No i do not mind.
> A blog is being planned for promotion of my game and sharing.
> Only have to find a domain name which i like.

Have you put anything in the meantime? I'd be very interested to read
about your game's progress and learn more about the component system
you built for it.

Daniel

Daniel Kersten

unread,
Mar 16, 2011, 12:54:02 PM3/16/11
to clo...@googlegroups.com, msappler
Hi Michael,

Looks great! I love the video too. Very nice.
I have put a link to your blog post on the clojure games wiki. I'll update the wiki properly when I get a chance. If you want to put details on your game on the wiki (or want to edit my post about your component entity system), please feel free to. I'll do it myself eventually if not, but it may take some time before I do.

In any case, great work!!

Dan.

On 16 March 2011 15:09, msappler <damned...@web.de> wrote:
Finally I launched my little website: http://resatori.com

You can see a short video of my game there ;)

Fraps only allows 30 seconds so nothing more to see :(

Entity Component Post will follow this week.

On 6 Mrz., 16:24, Daniel Werner <daniel.d.wer...@googlemail.com>
wrote:
> Himsappler,
>
> On Jan 12, 12:27 pm,msappler<damnedmar...@web.de> wrote:
>
> > No i do not mind.
> > A blog is being planned for promotion of my game and sharing.
> > Only have to find a domain name which i like.
>
> Have you put anything in the meantime? I'd be very interested to read
> about your game's progress and learn more about the component system
> you built for it.
>
> Daniel

msappler

unread,
Mar 16, 2011, 2:06:27 PM3/16/11
to Clojure
Weird where did my post go?

Anyway here is the link to the article:
http://resatori.com/clojure-entity-component-system

On 16 Mrz., 17:54, Daniel Kersten <dkers...@gmail.com> wrote:
> Hi Michael,
>
> Looks great! I love the video too. Very nice.
> I have put a link to your blog post on the clojure games wiki. I'll update
> the wiki properly when I get a chance. If you want to put details on your
> game on the wiki (or want to edit my post about your component entity
> system), please feel free to. I'll do it myself eventually if not, but it
> may take some time before I do.
>
> In any case, great work!!
>
> Dan.
>

msappler

unread,
Mar 16, 2011, 11:09:18 AM3/16/11
to Clojure
Finally I launched my little website: http://resatori.com

You can see a short video of my game there ;)

Fraps only allows 30 seconds so nothing more to see :(

Entity Component Post will follow this week.

On 6 Mrz., 16:24, Daniel Werner <daniel.d.wer...@googlemail.com>
wrote:
> Himsappler,
>
Reply all
Reply to author
Forward
0 new messages