selfsame [Lexaloffle Blog Feed]https://www.lexaloffle.com/bbs/?uid=24591 Entity Component System <p> <table><tr><td> <a href="/bbs/?pid=44926#p"> <img src="/bbs/thumbs/pico44917.png" style="height:256px"></a> </td><td width=10></td><td valign=top> <a href="/bbs/?pid=44926#p"> Entity Component System 1.0</a><br><br> by <a href="/bbs/?uid=24591"> selfsame</a> <br><br><br> <a href="/bbs/?pid=44926#p"> [Click to Play]</a> </td></tr></table> </p> <p>This is a tiny (47 token!) Entity Component System library.</p> <p><a href="https://en.wikipedia.org/wiki/Entity%E2%80%93component%E2%80%93system">Entity Component Systems</a> are design patterns where entities are generic collections of components and logic is written for specific component types. It's popular for game design as we often have many 'things' that don't always fall within clear cut types.</p> <p>There's different approaches to ECS, here we'll consider a table to be an entity, and the key/value pairs to be component names and values.</p> <p>This library is basically a single function, <em>system</em>, which creates a function that selects every entity with the specified components and calls the user provided function on each one.</p> <div> <div style="max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre> function _has(e, ks) for n in all(ks) do if not e[n] then return false end end return true end function system(ks, f) return function(es) for e in all(es) do if _has(e, ks) then f(e) end end end end </pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>To use it we'll need a collection of entities:</p> <div> <div style="max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre> world = {} add(world, {pos={x=32, y=64}, color=12}) add(world, {pos={x=64, y=64}, sprite=0}) add(world, {pos={x=96, y=64}, color=8, sprite=1}) </pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>Now we define a couple system functions, one for things with position and color components, and one for position and sprite.</p> <div> <div style="max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre> circles = system({&quot;pos&quot;, &quot;color&quot;}, function(e) circfill(e.pos.x, e.pos.y, 8, e.color) end) sprites = system({&quot;pos&quot;, &quot;sprite&quot;}, function(e) spr(e.sprite, e.pos.x-4, e.pos.y-4) end) </pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>Finally, we call our systems on our entity collection:</p> <div> <div style="max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre> function _draw() cls() circles(world) sprites(world) end </pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> https://www.lexaloffle.com/bbs/?tid=30039 https://www.lexaloffle.com/bbs/?tid=30039 Thu, 05 Oct 2017 09:17:32 UTC bucket hash - nearest neighbor library <p> <table><tr><td> <a href="/bbs/?pid=41493#p"> <img src="/bbs/thumbs/pico41492.png" style="height:256px"></a> </td><td width=10></td><td valign=top> <a href="/bbs/?pid=41493#p"> bucket hash - library for nearest neighbor lookups 1.0</a><br><br> by <a href="/bbs/?uid=24591"> selfsame</a> <br><br><br> <a href="/bbs/?pid=41493#p"> [Click to Play]</a> </td></tr></table> </p> <p>This is a small (259 token) library for finding entities close to a point.</p> <p>A bucket hash (also called a spatial or grid hash) breaks up space by a grid size, and maintains a sparse table of &quot;buckets&quot; representing a grid location. When entity membership in these buckets is updated, you can quickly<br /> retrieve entities near a given point.</p> <div> <div style="max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre> -- usage -- create a data 'store' {size=30,prop=&quot;pos&quot;} -- store.prop should match your entities' position property name, -- which should be a 'point' value like {x=0,y=0} -- store.size should be tuned to the max neighbor distance you'll be finding -- periodically call bstore(store, entity) to update their bucket membership -- bget(store, point) returns stored entities from a 3x3 square of buckets around -- the given point, filter these by a distance function if you need more precision -- remember you can maintain multiple stores based on the needs of your game! </pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> https://www.lexaloffle.com/bbs/?tid=29466 https://www.lexaloffle.com/bbs/?tid=29466 Fri, 09 Jun 2017 15:26:50 UTC tween library <p> <table><tr><td> <a href="/bbs/?pid=41337#p"> <img src="/bbs/thumbs/pico41463.png" style="height:256px"></a> </td><td width=10></td><td valign=top> <a href="/bbs/?pid=41337#p"> tween library</a><br><br> by <a href="/bbs/?uid=24591"> selfsame</a> <br><br><br> <a href="/bbs/?pid=41337#p"> [Click to Play]</a> </td></tr></table> </p> <p>This is a small (257 token) tweening library. It allows you to tween any value over time, and can be extended<br /> by supplying your own easing and lerping functions. It also allows callbacks, functions that are called when the tween has finished, letting you sequence animations and code.</p> <p>quick start:</p> <div> <div style="max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre> tween(player.position, &quot;x&quot;, 64, 100, {e=pow(2)}) -- tween(object, prop_name, target_value, duration, options_table) -- options are -- &quot;e&quot; dual in+out easing fn -- &quot;ei&quot;,&quot;eo&quot; in,out easing fn -- &quot;f&quot; tween callback (takes 1 argument - the object) -- &quot;l&quot; lerping function, default lerp works with number values </pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>Please check the cart code for further examples.</p> <p>Advanced tips:</p> <p>If you need to stop a tween, keep a reference to it (the return value of &quot;tween&quot;) and set it's &quot;o&quot; prop to nil.</p> <p>Changelog:</p> <p>v 1.0 - shaved off 7 tokens, moved bbs post to &quot;code snippets&quot;</p> https://www.lexaloffle.com/bbs/?tid=29438 https://www.lexaloffle.com/bbs/?tid=29438 Tue, 06 Jun 2017 15:02:44 UTC