Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines
(+1)

Hint #3 -- Powerful queries and collision detection...

So, if you've looked at my presentation or read through some of the examples, you know that the "grab/s" functions can query the Store for entities with a particular tag or key value setting. However, you can also feed grab and friends a query function, which can be arbitrarily powerful...

Here is a system that implements 2D collision detection. Yes, there's an example of this in %invade.rkt, but this one is better.

   (define (sys-hit)
     "( --) Collision detection."
     
     (let* ([hero (q1 'hero)]
            [items (grabs (λ (e) (and e.hit
                                      (< hero.x (+ e.x e.w))
                                      (> (+ hero.x hero.w) e.x)
                                      (< hero.y (+ e.y e.h))
                                      (> (+ hero.y hero.h) e.y))))]
            )
       (when (not (empty? items))
         (for ([item in-array items])
           (item.hit item hero)))
       ))
It gets the "hero" entity and runs a function query to check it's position against everything that has a collision handler ("hit"). When it finds them, it runs the handler with the item and the hero as arguments (this is a Canned Heat convention). 

Notice that I check to see if the entity has a collision handler before I attempt the geometry testing -- this make things faster because it only does the expensive comparisons on things that warrant it.

Isn't it great how to game jam gives us more idea on how to improve our tooling! I find it interesting that you can query the component values, this is typically left out of ECS.

(1 edit)

Interesting. It seemed very natural to provide an abstraction for running across the Store and selecting the appropriate things. What do "normal" ECS imp's do? Or is it expected that the user will provide that part?