mob.lua - Entity Manager inspired by jQuery
Posted: Mon May 07, 2018 10:10 pm
During the last Ludum Dare I experimented with the idea of managing entities and filtering them using a system similar to jQuery. I found it pretty useful so I expanded on the idea and finished a first version of a library.
Usually I do my set of tools for myself but I thought it was interesting enough to be shared, so I put it on github
https://github.com/NicMagnier/mob.lua
Before, I was always creating tons of objects all over the place and all in different tables to easily loop through them (looping through enemies, through particles, and so on). It was working but it always make some horrible looking codes. So here the idea is to query the entities you want with a simply syntax so it very easy to read the source code and also to quickly query the entities you want.
So for example with mob.lua I can randomly pick a npc in a special state that is close by
Or I could draw my entities all properly sorted
I still didn't use this version on a real project but if anyone want to have a look and give some feedback already that would be super interesting.
Usually I do my set of tools for myself but I thought it was interesting enough to be shared, so I put it on github
https://github.com/NicMagnier/mob.lua
Before, I was always creating tons of objects all over the place and all in different tables to easily loop through them (looping through enemies, through particles, and so on). It was working but it always make some horrible looking codes. So here the idea is to query the entities you want with a simply syntax so it very easy to read the source code and also to quickly query the entities you want.
So for example with mob.lua I can randomly pick a npc in a special state that is close by
Code: Select all
mob.get("type_npc @about_to_explode")
:filter(function(e)
local dx = e.x - love.mouse.getX()
local dy = e.y - love.mouse.getY()
return (dx*dx+dy*dy)<150*150
end)
:random()
Code: Select all
mob.get("on_ground")
:sort(function(a,b) return a.y<b.y end)
:draw()
-- draw all the cloud and output how many cloud are drawn
love.graphics.setColor(255,255,255,100)
mob.get("cloud"):draw()