KatrinaKitten [Lexaloffle Blog Feed]https://www.lexaloffle.com/bbs/?uid=45947 Tiny ECS Framework v1.1 <p>Here's a tiny <a href="https://en.wikipedia.org/wiki/Entity_component_system">entity-component-system</a> framework for anyone who might need such a thing! It weighs in at only 108 tokens as of v1.0. This framework assumes (but does not technically require) the strictest definition of the concept, that being that entities and components define no behavior internally but rather are acted upon by externally defined systems that function on the data contained in the entity/components themselves.</p> <p>The implementation of systems is a slightly refined version of <a href="https://www.lexaloffle.com/bbs/?uid=24591"> <a href="https://www.lexaloffle.com/bbs/?uid=24591"> @selfsame</a></a>'s <a href="https://www.lexaloffle.com/bbs/?tid=30039">earlier implementation</a>, which treated the &quot;components&quot; simply as keys of the entity, rather than modular objects in their own right. It's also been slightly token-optimized, at 38 instead of 47, but assumes at least the table structure implemented by the entity factory function. I highly recommend <a href="https://www.lexaloffle.com/bbs/?uid=24591"> <a href="https://www.lexaloffle.com/bbs/?uid=24591"> @selfsame</a></a>'s version if you need something even more lightweight than this, at the cost of a little flexibility.</p> <p>If you really need to be stingy about tokens, you can even leave out the component factory function to save 16 tokens, at a minimum of 0 extra tokens per component creation. It's mostly included for simplicity and clarity, but is not strictly required to function.</p> <p>Copy and paste the code below into a new tab of your project to get started - I'd very much appreciate if you leave the credit comment at the top, but I won't be mad if you don't =)</p> <div> <div class=scrollable_with_touch style="width:100%; 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> --tiny ecs v1.1 --by katrinakitten function ent(t) local cmpt={} t=t or {} setmetatable(t,{ __index=cmpt, __add=function(self,cmp) assert(cmp._cn) self[cmp._cn]=cmp return self end, __sub=function(self,cn) self[cn]=nil return self end }) return t end function cmp(cn,t) t=t or {} t._cn=cn return t end function sys(cns,f) return function(ents,...) for e in all(ents) do for cn in all(cns) do if(not e[cn]) goto _ end f(e,...) ::_:: 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>From there, you can use the system like so. Keep in mind that the + and - operators on entities are destructive and modify the existing entity, regardless of whether you use += or -=; since they result in the same token count, I highly recommend you still use the assignment variants unless you're really in a performance crunch, for the sake of clarity.</p> <div> <div class=scrollable_with_touch style="width:100%; 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> local entity = ent({ ... }) local component = cmp(&quot;name&quot;, { ... }) local component = { _cn=&quot;name&quot;, ... } --if cmp is not included entity += component --add a component entity -= &quot;name&quot; --remove a component if entity.name then --executes only if the &quot;name&quot; component exists --access component properties via entity.name.property end local system = sys({ &quot;name&quot;, ... }, function(e, ...) --will execute for each entity with all listed components --varargs will be passed through from system call end) system({ entity, ... }) system({ entity, ... }, &quot;some args&quot;, ...) </pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>Here's a simple demo of using this system with the following test code (outdated - v1.0):</p> <div> <div class=scrollable_with_touch style="width:100%; 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 _init() local e1=ent({ x=0,y=0 }) local e2=ent({ x=0,y=0 }) e1+=cmp(&quot;test&quot;,{state=&quot;cmp1&quot;}) e2+=cmp(&quot;test&quot;,{state=&quot;cmp2&quot;}) local s=sys({&quot;test&quot;},function(e,s) ?s..e.cmp.test.state end) s({e1,e2},&quot;before: &quot;) e2-=&quot;test&quot; s({e1,e2},&quot;after: &quot;) 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> <table><tr><td> <a href="/bbs/?pid=80110#p"> <img src="/bbs/thumbs/pico8_gazihisifi-0.png" style="height:256px"></a> </td><td width=10></td><td valign=top> <a href="/bbs/?pid=80110#p"> Tiny ECS v1.0 Test</a><br><br> by <a href="/bbs/?uid=45947"> KatrinaKitten</a> <br><br><br> <a href="/bbs/?pid=80110#p"> [Click to Play]</a> </td></tr></table> </p> <p>Changelog:</p> <ul> <li>v1.1 (BREAKING) <ul> <li>Changed access pattern from e.cmp.name to e.name</li> <li>This reduces access cost but may clobber entity properties, so be careful!</li> </ul></li> <li>v1.0 <ul> <li>Initial release</li> </ul></li> </ul> https://www.lexaloffle.com/bbs/?tid=39021 https://www.lexaloffle.com/bbs/?tid=39021 Thu, 30 Jul 2020 03:24:54 UTC Snek v1.4 <p> <table><tr><td> <a href="/bbs/?pid=79811#p"> <img src="/bbs/thumbs/pico8_refebeziw-5.png" style="height:256px"></a> </td><td width=10></td><td valign=top> <a href="/bbs/?pid=79811#p"> Snek v1.4</a><br><br> by <a href="/bbs/?uid=45947"> KatrinaKitten</a> <br><br><br> <a href="/bbs/?pid=79811#p"> [Click to Play]</a> </td></tr></table> </p> <p>Hey all! This is my first little Pico-8 experiment, and like I almost always do with any game engine, a Snake clone was first on the list. I'm not a great artist, so excuse the lack of polish in the sprites ^^</p> <img style="margin-bottom:16px" border=0 src="/media/45947/5_fruit.png" alt="" /> <p>Points by fruit type:</p> <ul> <li>Apple - 1 point</li> <li>Plum - 1 point</li> <li>Blueberry - 1 point</li> <li>Peach - 1 point</li> <li>Blackberry - 1 point</li> <li>Bronze apple - 2 points</li> <li>Silver apple - 5 points</li> <li>Golden apple - 10 points</li> </ul> <p>Each fruit you eat will increase the length of your tail by 1 tile.<br /> Going off the edge of the screen will wrap you around to the other side.<br /> Don't run into your tail, and try to survive as long as you can!</p> <p>My high score as of version 1.4 is 88 - see if you can beat me!</p> <p>Changelog:</p> <ul> <li>v1.4 <ul> <li>Slowed snek down slightly (12 moves/s -&gt; 10/s)</li> <li>Added SFX on game start</li> <li>Switched from table to sprite flags for fruit scores</li> <li>Major token optimization</li> <li>Removed unnecessary blank map area</li> <li>Commented game code</li> <li>Added changelog to cart file</li> </ul></li> <li>v1.3 <ul> <li>Improved the main menu significantly</li> <li>Added a simple &quot;Game Over&quot; message</li> <li>Major internal improvements</li> </ul></li> <li>v1.2 <ul> <li>Added a main menu!</li> <li>Added persistent high scores</li> <li>Added another fruit variation (blackberry)</li> <li>Condensed and optimized a bunch of stuff</li> </ul></li> <li>v1.1 <ul> <li>Added new fruits (plum, blueberry, peach, bronze/silver apples)</li> <li>Changed background color to allow for more fruit variations</li> <li>Optimized code slightly, particularly drawing cod</li> </ul></li> <li>v1.0 <ul> <li>Initial release</li> </ul></li> </ul> https://www.lexaloffle.com/bbs/?tid=38948 https://www.lexaloffle.com/bbs/?tid=38948 Thu, 23 Jul 2020 19:06:27 UTC