apa64 [Lexaloffle Blog Feed]https://www.lexaloffle.com/bbs/?uid=45600 Road to ECS <h1>Road to ECS</h1> <h2>Intro</h2> <p>This is about teaching myself Entity-Component-System (ECS) pattern. My current goal is to implement a few basic features one by one to build a code snippet library, starting from just drawing things on screen. Code will be in <a href="https://github.com/apa64/road-to-ecs">my GitHub/road-to-ecs</a> and also as cartridges in this blog thread.</p> <p>Some personal background: <div><div><input type="button" value=" Show " onClick="if (this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display != '') { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = ''; this.innerText = ''; this.value = ' Hide '; } else { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = 'none'; this.innerText = ''; this.value = ' Show '; }"></div><div><div style="display: none;">I'm a software engineer, professional coding background is a bit of data comms in C and lot of Java starting from 1.4, some Android and mostly Java EE for enterprise systems with web fronts. For the last 5 or so years I haven't really coded except shell or other script/configuration languages for automating configuration management, CI/CD and so on (nowadays they call it devops).</p> <p>I've been interested in PICO-8 for some time, but finally got it as a part of the great itch.io Black Lives Matter bundle. To me gamedev is previously unknown territory and it's fun to learn how games differ from other applications.</p> <p>Also playing around with PICO-8 on my summer vacation revealed how much brain capacity my dayjob takes. On work days there is no chance that I could do <em>anything</em> productive with PICO in the evening. Scary.</p> <p>I'm used to (and like) working within frameworks. I got a bit of blank paper syndrome with PICO-8 initially :) First I tried to apply traditional OOP but it didn't <em>feel</em> like a good fit for games. Browsing this BBS I found about ECS and it feels like a better fit for game applications. Now I just have to bend my mindset to it!</div></div></div></p> <h2>Entity-Component-System</h2> <p>ECS is a pattern for dividing data and functionality in independent components. It fullfills the <a href="https://en.wikipedia.org/wiki/Single-responsibility_principle">Single Responsibility Principle</a> nicely: a functionality (= a <em>system</em>) can do one thing and not care about others.</p> <p>The pieces of ECS are:</p> <ul> <li>Entities = &quot;things in the world&quot;</li> <li>Components = data</li> <li>Systems = functionality</li> </ul> <p><em>Systems</em> do their thing for <em>Entities</em> which have <em>Components</em> required by the System. For example we could have a creature on screen that moves according to its current speed. The <em>creature</em> is an entity, <em>speed</em> is a component and <em>movement</em> is the system.</p> <p>I was about to write more but really, this post explains everything better than I could: <a href="https://gamedev.stackexchange.com/a/31491/141341">gamedev.stackexchange: What is the role of &ldquo;systems&rdquo; in a component-based entity architecture?</a></p> <p>One thing I've picked up so far is that I should implement a system first. Required components will reveal themselves quite naturally when coding the system.</p> <h2>References</h2> <ul> <li><a href="https://www.lexaloffle.com/bbs/?tid=39021">BBS/KatrinaKitten: Tiny ECS Framework v1.1</a></li> <li><a href="https://www.lexaloffle.com/bbs/?tid=30039">BBS/selfsame: Entity Component System</a></li> <li><a href="https://www.lexaloffle.com/bbs/?pid=68554#p">BBS/alexr: ECS POC 1 v. 0.5</a></li> <li><a href="https://www.lexaloffle.com/bbs/?tid=38154">BBS: Entity Component Systems</a></li> <li><a href="https://gamedev.stackexchange.com/a/31491/141341">gamedev.stackexchange: What is the role of &ldquo;systems&rdquo; in a component-based entity architecture?</a></li> <li><a href="https://medium.com/ingeniouslysimple/entities-components-and-systems-89c31464240d">Mark Jordan: Entities, components and systems</a></li> <li><a href="https://www.gamedev.net/articles/programming/general-and-gameplay-programming/understanding-component-entity-systems-r3013/">gamedev.net/Klutzershy: Understanding Component-Entity-Systems</a></li> </ul> <p>Hmm. The links above don't look like links but they work anyway.</p> https://www.lexaloffle.com/bbs/?tid=39315 https://www.lexaloffle.com/bbs/?tid=39315 Sat, 22 Aug 2020 20:28:32 UTC How to OOP a Zelda? <p>Hi, I'm practicing how to do object oriented programming in PICO-8 but struggling a bit to map things in my head between OO design and actual game code. My practice project is doing MBoffin's top-down game tutorial with an OO approach but my long term goal is to do either a top-down adventure/rpg (=Zelda) or turn-based strategy (=UFO X-COM).</p> <p>The reason I want to do OOP is my Java dev background and also personally I don't like procedural code, it feels so messy and cluttered. I'd like to have the behaviour and presentation of an entity in its class instead of spread around the application but that's the difficult part for me at the moment.</p> <p>Bunch of questions:</p> <ul> <li>Is there any point of doing a class for a singleton entity, eg. player actor? As my player1 object will be the only instance I could as well have that and not bother with the prototype. Now I have something like this:</li> </ul> <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>-- prototype player = {} function player:new(o) local o = o or {} setmetatable(o, self) self.__index = self return o end function player:draw() spr(self.sprite, self.x*8, self.y*8) end function player:move() if (btnp(⬅️)) newx-=1 if (btnp(➡️)) newx+=1 if (btnp(⬆️)) newy-=1 if (btnp(⬇️)) newy+=1 ... end function _init() -- actual object player1 = player:new( { x = 10, y = 10, sprite = 2 } ) ... end</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <ul> <li> <p>A wider question based on above: What entities should I have as classes (or &quot;prototypes&quot; in Lua OOP)? Player actor, NPC characters/enemies, world map, world objects (text signs, anything interactive), ...?</p> </li> <li> <p>What patterns/frameworks/templates you use for game projects? State machines, OOP, functional programming, ...?</p> </li> <li>Any pointers to any non-minified game source code to study? Something with longer that 1-char variable names and comments? Anything good in GitHub?</li> </ul> https://www.lexaloffle.com/bbs/?tid=38897 https://www.lexaloffle.com/bbs/?tid=38897 Sat, 18 Jul 2020 19:38:42 UTC My first Tic Tac Toe <p> <table><tr><td> <a href="/bbs/?pid=79067#p"> <img src="/bbs/thumbs/pico8_apa64_tictactoe1-0.png" style="height:256px"></a> </td><td width=10></td><td valign=top> <a href="/bbs/?pid=79067#p"> apa64_tictactoe1</a><br><br> by <a href="/bbs/?uid=45600"> apa64</a> <br><br><br> <a href="/bbs/?pid=79067#p"> [Click to Play]</a> </td></tr></table> </p> <p>Woohoo I made my first game! Quite ugly (both graphics and code) but correctly working 3x3 tic-tac-toe for 2 players, no computer opponent. Also available at <a href="https://github.com/apa64/try-pico8/blob/master/tictactoe1.p8">GitHub</a>.</p> https://www.lexaloffle.com/bbs/?tid=38730 https://www.lexaloffle.com/bbs/?tid=38730 Wed, 08 Jul 2020 20:34:01 UTC How to modularize my Text scroller? <p> <table><tr><td> <a href="/bbs/?pid=78797#p"> <img src="/bbs/thumbs/pico8_apa64_scroller2-0.png" style="height:256px"></a> </td><td width=10></td><td valign=top> <a href="/bbs/?pid=78797#p"> apa64_scroller2</a><br><br> by <a href="/bbs/?uid=45600"> apa64</a> <br><br><br> <a href="/bbs/?pid=78797#p"> [Click to Play]</a> </td></tr></table> </p> <p>Hello world!</p> <p>How can I make my code more like a library or a self-contained independent module/component? Here's a text scroller by me (my first published cart yay!). Currently you need to call three functions to use it:</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() scroller = init_scroller(&quot;lorem ipsum dolor sit amet, consectetur... &quot;, 30, 5/30) end function _update() update_scroller() end function _draw() cls(0) draw_scroller(4, 60, 7) 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>I tried to write it more like an component but couldn't make it work. My idea was that init_scroller() would return &quot;an object&quot; which would contain everything. Then you'd call object.update() and object.draw() to use it and could create as many instances as needed. However my return object had just pointer to global functions and later instances overwrote earlier ones.</p> <p>Can you point me to a simple example how to do components in PICO-8?</p> <p>You can probably tell I'm coming from Java background ;) Are there any specific style guides or general conventions for PICO-8 Lua? I'm coding with VS Code, can't do it with the 128x128 window...</p> https://www.lexaloffle.com/bbs/?tid=38643 https://www.lexaloffle.com/bbs/?tid=38643 Thu, 02 Jul 2020 17:10:06 UTC