<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://wiki.gideros.rocks/index.php?action=history&amp;feed=atom&amp;title=Tuto_tiny-ecs_2d_platformer_Part_9_Systems</id>
	<title>Tuto tiny-ecs 2d platformer Part 9 Systems - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.gideros.rocks/index.php?action=history&amp;feed=atom&amp;title=Tuto_tiny-ecs_2d_platformer_Part_9_Systems"/>
	<link rel="alternate" type="text/html" href="https://wiki.gideros.rocks/index.php?title=Tuto_tiny-ecs_2d_platformer_Part_9_Systems&amp;action=history"/>
	<updated>2026-11-17T13:20:03Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.11</generator>
	<entry>
		<id>https://wiki.gideros.rocks/index.php?title=Tuto_tiny-ecs_2d_platformer_Part_9_Systems&amp;diff=24724&amp;oldid=prev</id>
		<title>MoKaLux: /* Next? */</title>
		<link rel="alternate" type="text/html" href="https://wiki.gideros.rocks/index.php?title=Tuto_tiny-ecs_2d_platformer_Part_9_Systems&amp;diff=24724&amp;oldid=prev"/>
		<updated>2025-11-06T02:56:39Z</updated>

		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;Next?&lt;/span&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en-GB&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 02:56, 6 November 2025&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l877&quot;&gt;Line 877:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 877:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Systems are fairly straight forward and fairly short for what they achieve.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Systems are fairly straight forward and fairly short for what they achieve.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;We have covered the first set of systems, &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;we have a couple more to add&lt;/del&gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;We have covered the first set of systems, &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;the next System is the heart of the game: the collision System&lt;/ins&gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Prev.: [[Tuto tiny-ecs 2d platformer Part 8 more entities]]&amp;lt;/br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Prev.: [[Tuto tiny-ecs 2d platformer Part 8 more entities]]&amp;lt;/br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&#039;&#039;&#039;Next: [[Tuto tiny-ecs 2d platformer Part 10 &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;Systems 2&lt;/del&gt;]]&#039;&#039;&#039;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&#039;&#039;&#039;Next: [[Tuto tiny-ecs 2d platformer Part 10 &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;Collision System&lt;/ins&gt;]]&#039;&#039;&#039;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#039;&amp;#039;&amp;#039;[[Tutorial - tiny-ecs 2d platformer]]&amp;#039;&amp;#039;&amp;#039;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#039;&amp;#039;&amp;#039;[[Tutorial - tiny-ecs 2d platformer]]&amp;#039;&amp;#039;&amp;#039;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;{{GIDEROS IMPORTANT LINKS}}&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;{{GIDEROS IMPORTANT LINKS}}&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>MoKaLux</name></author>
	</entry>
	<entry>
		<id>https://wiki.gideros.rocks/index.php?title=Tuto_tiny-ecs_2d_platformer_Part_9_Systems&amp;diff=24722&amp;oldid=prev</id>
		<title>MoKaLux at 05:56, 5 November 2025</title>
		<link rel="alternate" type="text/html" href="https://wiki.gideros.rocks/index.php?title=Tuto_tiny-ecs_2d_platformer_Part_9_Systems&amp;diff=24722&amp;oldid=prev"/>
		<updated>2025-11-05T05:56:48Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;a href=&quot;https://wiki.gideros.rocks/index.php?title=Tuto_tiny-ecs_2d_platformer_Part_9_Systems&amp;amp;diff=24722&amp;amp;oldid=24721&quot;&gt;Show changes&lt;/a&gt;</summary>
		<author><name>MoKaLux</name></author>
	</entry>
	<entry>
		<id>https://wiki.gideros.rocks/index.php?title=Tuto_tiny-ecs_2d_platformer_Part_9_Systems&amp;diff=24721&amp;oldid=prev</id>
		<title>MoKaLux: wip</title>
		<link rel="alternate" type="text/html" href="https://wiki.gideros.rocks/index.php?title=Tuto_tiny-ecs_2d_platformer_Part_9_Systems&amp;diff=24721&amp;oldid=prev"/>
		<updated>2025-11-05T03:46:47Z</updated>

		<summary type="html">&lt;p&gt;wip&lt;/p&gt;
&lt;a href=&quot;https://wiki.gideros.rocks/index.php?title=Tuto_tiny-ecs_2d_platformer_Part_9_Systems&amp;amp;diff=24721&amp;amp;oldid=24719&quot;&gt;Show changes&lt;/a&gt;</summary>
		<author><name>MoKaLux</name></author>
	</entry>
	<entry>
		<id>https://wiki.gideros.rocks/index.php?title=Tuto_tiny-ecs_2d_platformer_Part_9_Systems&amp;diff=24719&amp;oldid=prev</id>
		<title>MoKaLux: wip</title>
		<link rel="alternate" type="text/html" href="https://wiki.gideros.rocks/index.php?title=Tuto_tiny-ecs_2d_platformer_Part_9_Systems&amp;diff=24719&amp;oldid=prev"/>
		<updated>2025-11-05T02:55:37Z</updated>

		<summary type="html">&lt;p&gt;wip&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;__TOC__&lt;br /&gt;
&lt;br /&gt;
== The Systems ==&lt;br /&gt;
We have our entities, we have our components, now the systems. What is an ECS &amp;#039;&amp;#039;&amp;#039;System&amp;#039;&amp;#039;&amp;#039;?&lt;br /&gt;
&lt;br /&gt;
 A System is a wrapper around function callbacks for manipulating Entities. Systems are implemented as tables that contain at least one method; an update function that takes parameters like so:&lt;br /&gt;
 &lt;br /&gt;
 &amp;#039;&amp;#039;&amp;#039;function system:update(dt)&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
 &lt;br /&gt;
 There are also a few other optional callbacks:&lt;br /&gt;
 *&amp;#039;&amp;#039;&amp;#039;function system:filter(entity)&amp;#039;&amp;#039;&amp;#039; Returns true if this System should include this Entity, otherwise should return false. If this isn&amp;#039;t specified, no Entities are included in the System.&lt;br /&gt;
 *&amp;#039;&amp;#039;&amp;#039;function system:onAdd(entity)&amp;#039;&amp;#039;&amp;#039; Called when an Entity is added to the System.&lt;br /&gt;
 *&amp;#039;&amp;#039;&amp;#039;function system:onRemove(entity)&amp;#039;&amp;#039;&amp;#039; Called when an Entity is removed from the System.&lt;br /&gt;
 *&amp;#039;&amp;#039;&amp;#039;function system:onModify(dt)&amp;#039;&amp;#039;&amp;#039; Called when the System is modified by adding or removing Entities from the System.&lt;br /&gt;
 *&amp;#039;&amp;#039;&amp;#039;function system:onAddToWorld(world)&amp;#039;&amp;#039;&amp;#039; Called when the System is added to the World, before any entities are added to the system.&lt;br /&gt;
 *&amp;#039;&amp;#039;&amp;#039;function system:onRemoveFromWorld(world)&amp;#039;&amp;#039;&amp;#039; Called when the System is removed from the world, after all Entities are removed from the System.&lt;br /&gt;
 *&amp;#039;&amp;#039;&amp;#039;function system:preWrap(dt)&amp;#039;&amp;#039;&amp;#039; Called on each system before update is called on any system.&lt;br /&gt;
 *&amp;#039;&amp;#039;&amp;#039;function system:postWrap(dt)&amp;#039;&amp;#039;&amp;#039; Called on each system in reverse order after update is called on each system.&lt;br /&gt;
 &lt;br /&gt;
 &amp;#039;&amp;#039;&amp;#039;Please see [[Tiny-ecs#System_functions]] for more information&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
That looks scary but worry not we won&amp;#039;t use all the callback functions :-)&lt;br /&gt;
&lt;br /&gt;
To put it simple a &amp;#039;&amp;#039;&amp;#039;System&amp;#039;&amp;#039;&amp;#039; manipulates entities. Let&amp;#039;s see our first &amp;#039;&amp;#039;&amp;#039;System&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== sDrawable.lua ==&lt;br /&gt;
Please create a file &amp;quot;&amp;#039;&amp;#039;&amp;#039;sDrawable.lua&amp;#039;&amp;#039;&amp;#039;&amp;quot; in the &amp;#039;&amp;#039;&amp;#039;&amp;quot;_S&amp;quot;&amp;#039;&amp;#039;&amp;#039; folder and the code:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
SDrawable = Core.class()&lt;br /&gt;
&lt;br /&gt;
function SDrawable:init(xtiny) -- tiny function&lt;br /&gt;
	xtiny.system(self) -- called only once on init (no update)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function SDrawable:filter(ent) -- tiny function&lt;br /&gt;
	return ent.spritelayer and ent.sprite&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function SDrawable:onAdd(ent) -- tiny function&lt;br /&gt;
--	print(&amp;quot;SDrawable:onAdd(ent)&amp;quot;)&lt;br /&gt;
	ent.spritelayer:addChild(ent.sprite)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function SDrawable:onRemove(ent) -- tiny function&lt;br /&gt;
--	print(&amp;quot;SDrawable:onRemove(ent)&amp;quot;)&lt;br /&gt;
	ent.spritelayer:removeChild(ent.sprite)&lt;br /&gt;
	-- cleaning?&lt;br /&gt;
	ent.sprite = nil&lt;br /&gt;
	ent = nil&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What it does:&lt;br /&gt;
* runs only once when it is called&lt;br /&gt;
* affects only entities which have a spritelayer variable (id) &amp;#039;&amp;#039;&amp;#039;and&amp;#039;&amp;#039;&amp;#039; a sprite variable (id)&lt;br /&gt;
* when an Entity is added to tiny-ecs &amp;#039;&amp;#039;&amp;#039;World&amp;#039;&amp;#039;&amp;#039;, the &amp;#039;&amp;#039;&amp;#039;System&amp;#039;&amp;#039;&amp;#039; adds the Entity to its Sprite layer&lt;br /&gt;
* when an Entity is removed from tiny-ecs &amp;#039;&amp;#039;&amp;#039;World&amp;#039;&amp;#039;&amp;#039;, the &amp;#039;&amp;#039;&amp;#039;System&amp;#039;&amp;#039;&amp;#039; removes the Entity from its Sprite layer&lt;br /&gt;
&lt;br /&gt;
In other words, the &amp;#039;&amp;#039;&amp;#039;System&amp;#039;&amp;#039;&amp;#039; adds an Entity to a Sprite layer when the Entity is added to tiny-ecs World, and removes it from that Sprite layer when the Entity is destroyed.&lt;br /&gt;
&lt;br /&gt;
== sPlayer1Control.lua ==&lt;br /&gt;
I am adding systems in an order that seems logical and helps in understanding the making of the game. The next &amp;#039;&amp;#039;&amp;#039;System&amp;#039;&amp;#039;&amp;#039; we add is the player1 controller.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;&amp;#039;&amp;#039;&amp;#039;sPlayer1Control.lua&amp;#039;&amp;#039;&amp;#039;&amp;quot; in the &amp;#039;&amp;#039;&amp;#039;&amp;quot;_S&amp;quot;&amp;#039;&amp;#039;&amp;#039; folder. The code:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
SPlayer1Control = Core.class()&lt;br /&gt;
&lt;br /&gt;
function SPlayer1Control:init(xtiny, xplayer1inputlayer) -- tiny function&lt;br /&gt;
	xtiny.system(self) -- called only once on init (no update)&lt;br /&gt;
	self.player1inputlayer = xplayer1inputlayer&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function SPlayer1Control:filter(ent) -- tiny function&lt;br /&gt;
	return ent.isplayer1&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function SPlayer1Control:onAdd(ent) -- tiny function&lt;br /&gt;
	self.player1inputlayer:addEventListener(Event.KEY_DOWN, function(e)&lt;br /&gt;
		if ent.currlives &amp;gt; 0 then&lt;br /&gt;
			if e.keyCode == KeyCode.LEFT or e.keyCode == g_keyleft then ent.isleft = true end&lt;br /&gt;
			if e.keyCode == KeyCode.RIGHT or e.keyCode == g_keyright then ent.isright = true end&lt;br /&gt;
			if e.keyCode == KeyCode.UP or e.keyCode == g_keyup then ent.isup = true end&lt;br /&gt;
			if e.keyCode == KeyCode.DOWN or e.keyCode == g_keydown then ent.isdown = true end&lt;br /&gt;
			-- ACTIONS:&lt;br /&gt;
			-- isactionpunch1, isactionpunch2, isactionjumppunch1,&lt;br /&gt;
			-- isactionkick1, isactionkick2, isactionjumpkick1,&lt;br /&gt;
			-- isactionjump1&lt;br /&gt;
			if e.keyCode == g_keyaction1 then&lt;br /&gt;
				ent.animation.frame = 0&lt;br /&gt;
				ent.isactionpunch1 = true&lt;br /&gt;
			elseif e.keyCode == g_keyaction2 then&lt;br /&gt;
				ent.animation.frame = 0&lt;br /&gt;
				ent.isactionkick1 = true&lt;br /&gt;
			end&lt;br /&gt;
			if e.keyCode == g_keyaction3 then&lt;br /&gt;
				if ent.body.isonfloor then&lt;br /&gt;
					ent.animation.frame = 0&lt;br /&gt;
					ent.positionystart = ent.pos.y&lt;br /&gt;
					ent.body.isonfloor = false&lt;br /&gt;
					ent.body.isgoingup = true&lt;br /&gt;
					ent.isactionjump1 = true&lt;br /&gt;
				end&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
	end)&lt;br /&gt;
	self.player1inputlayer:addEventListener(Event.KEY_UP, function(e)&lt;br /&gt;
		if ent.currlives &amp;gt; 0 then&lt;br /&gt;
			if e.keyCode == KeyCode.LEFT or e.keyCode == g_keyleft then ent.isleft = false end&lt;br /&gt;
			if e.keyCode == KeyCode.RIGHT or e.keyCode == g_keyright then ent.isright = false end&lt;br /&gt;
			if e.keyCode == KeyCode.UP or e.keyCode == g_keyup then ent.isup = false end&lt;br /&gt;
			if e.keyCode == KeyCode.DOWN or e.keyCode == g_keydown then ent.isdown = false end&lt;br /&gt;
--			if e.keyCode == g_keyaction1 then ent.isactionpunch1 = false end&lt;br /&gt;
--			if e.keyCode == g_keyaction2 then ent.isactionkick1 = false end&lt;br /&gt;
--			if e.keyCode == g_keyaction3 then end&lt;br /&gt;
		end&lt;br /&gt;
	end)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What it does:&lt;br /&gt;
* runs only once when it is called&lt;br /&gt;
* affects only entities with the &amp;#039;&amp;#039;isplayer1&amp;#039;&amp;#039; id&lt;br /&gt;
* when the player1 Entity is added to tiny-ecs &amp;#039;&amp;#039;&amp;#039;World&amp;#039;&amp;#039;&amp;#039;, the System registers KEY_DOWN and KEY_UP events&lt;br /&gt;
&lt;br /&gt;
The System processes the user keys input and sets various flags to be processed in a future &amp;#039;&amp;#039;sDynamicBodies&amp;#039;&amp;#039; System we will add.&lt;br /&gt;
&lt;br /&gt;
== sPlayer1.lua ==&lt;br /&gt;
&amp;quot;&amp;#039;&amp;#039;&amp;#039;sPlayer1.lua&amp;#039;&amp;#039;&amp;#039;&amp;quot; in the &amp;#039;&amp;#039;&amp;#039;&amp;quot;_S&amp;quot;&amp;#039;&amp;#039;&amp;#039; folder. The code:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
SPlayer1 = Core.class()&lt;br /&gt;
&lt;br /&gt;
function SPlayer1:init(xtiny, xcamera) -- tiny function&lt;br /&gt;
	self.tiny = xtiny -- ref so we can remove entities from tiny system&lt;br /&gt;
	self.tiny.processingSystem(self) -- called once on init and every update&lt;br /&gt;
	-- fx&lt;br /&gt;
	self.camera = xcamera -- camera shake&lt;br /&gt;
	-- sfx&lt;br /&gt;
	self.snd = Sound.new(&amp;quot;audio/sfx/sfx_deathscream_human14.wav&amp;quot;)&lt;br /&gt;
	self.channel = self.snd:play(0, false, true)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function SPlayer1:filter(ent) -- tiny function&lt;br /&gt;
	return ent.isplayer1&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function SPlayer1:onAdd(ent) -- tiny function&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function SPlayer1:onRemove(ent) -- tiny function&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local resetanim = true&lt;br /&gt;
function SPlayer1:process(ent, dt) -- tiny function&lt;br /&gt;
	-- hurt fx&lt;br /&gt;
	if ent.washurt and ent.washurt &amp;gt; 0 and not (ent.wasbadlyhurt and ent.wasbadlyhurt &amp;gt; 0) then&lt;br /&gt;
		ent.washurt -= 1&lt;br /&gt;
		ent.animation.curranim = g_ANIM_HURT_R&lt;br /&gt;
		if ent.washurt &amp;lt; ent.recovertimer*0.5 then ent.hitfx:setVisible(false) end&lt;br /&gt;
		if ent.washurt &amp;lt;= 0 then&lt;br /&gt;
			ent.sprite:setColorTransform(1, 1, 1, 1)&lt;br /&gt;
			self.camera:setZoom(1) -- zoom&lt;br /&gt;
		end&lt;br /&gt;
	elseif ent.wasbadlyhurt and ent.wasbadlyhurt &amp;gt; 0 then&lt;br /&gt;
		ent.hitfx:setVisible(false)&lt;br /&gt;
		ent.wasbadlyhurt -= 1&lt;br /&gt;
		ent.animation.curranim = g_ANIM_LOSE1_R&lt;br /&gt;
		if ent.wasbadlyhurt &amp;lt; ent.recoverbadtimer/2 then&lt;br /&gt;
			if resetanim then&lt;br /&gt;
				resetanim = false&lt;br /&gt;
				ent.animation.frame = 0&lt;br /&gt;
			end&lt;br /&gt;
			ent.animation.curranim = g_ANIM_STANDUP_R&lt;br /&gt;
		end&lt;br /&gt;
		if ent.wasbadlyhurt &amp;lt;= 0 then&lt;br /&gt;
			ent.sprite:setColorTransform(1, 1, 1, 1)&lt;br /&gt;
			self.camera:setZoom(1) -- zoom&lt;br /&gt;
			resetanim = true&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	if ent.isdirty then -- hit&lt;br /&gt;
		local function map(v, minSrc, maxSrc, minDst, maxDst, clampValue)&lt;br /&gt;
			local newV = (v - minSrc) / (maxSrc - minSrc) * (maxDst - minDst) + minDst&lt;br /&gt;
			return not clampValue and newV or clamp(newV, minDst&amp;gt;&amp;lt;maxDst, minDst&amp;lt;&amp;gt;maxDst)&lt;br /&gt;
		end&lt;br /&gt;
		self.channel = self.snd:play()&lt;br /&gt;
		if self.channel then self.channel:setVolume(g_sfxvolume*0.01) end&lt;br /&gt;
		ent.hitfx:setVisible(true)&lt;br /&gt;
		ent.hitfx:setPosition(ent.pos.x+ent.collbox.w/2+(ent.headhurtbox.x*ent.flip), ent.pos.y+ent.headhurtbox.y-ent.headhurtbox.h/2)&lt;br /&gt;
		ent.spritelayer:addChild(ent.hitfx)&lt;br /&gt;
		ent.currhealth -= ent.damage&lt;br /&gt;
		local hudhealthwidth = map(ent.currhealth, 0, ent.totalhealth, 0, 100)&lt;br /&gt;
		self.tiny.hudhealth:setWidth(hudhealthwidth)&lt;br /&gt;
		if ent.currhealth &amp;lt; ent.totalhealth/3 then self.tiny.hudhealth:setColor(0xff0000)&lt;br /&gt;
		elseif ent.currhealth &amp;lt; ent.totalhealth/2 then self.tiny.hudhealth:setColor(0xff5500)&lt;br /&gt;
		else self.tiny.hudhealth:setColor(0x00ff00)&lt;br /&gt;
		end&lt;br /&gt;
		ent.washurt = ent.recovertimer -- timer for a flash effect&lt;br /&gt;
		ent.sprite:setColorTransform(2, 0, 0, 2) -- the flash effect (a bright red color)&lt;br /&gt;
		ent.isdirty = false&lt;br /&gt;
		self.camera:shake(0.6, 16) -- (duration, distance), you choose&lt;br /&gt;
		self.camera:setZoom(1.2) -- zoom&lt;br /&gt;
		if ent.currhealth &amp;lt;= 0 then&lt;br /&gt;
			ent.wasbadlyhurt = ent.recoverbadtimer -- timer for player1 to stand back up&lt;br /&gt;
			self.camera:shake(0.8, 64) -- (duration, distance), you choose&lt;br /&gt;
			ent.currlives -= 1&lt;br /&gt;
			for i = 1, ent.totallives do self.tiny.hudlives[i]:setVisible(false) end -- dirty but easy XXX&lt;br /&gt;
			for i = 1, ent.currlives do self.tiny.hudlives[i]:setVisible(true) end -- dirty but easy XXX&lt;br /&gt;
			if ent.currlives &amp;gt; 0 then&lt;br /&gt;
				ent.currhealth = ent.totalhealth&lt;br /&gt;
				hudhealthwidth = map(ent.currhealth, 0, ent.totalhealth, 0, 100)&lt;br /&gt;
				self.tiny.hudhealth:setWidth(hudhealthwidth)&lt;br /&gt;
				self.tiny.hudhealth:setColor(0x00ff00)&lt;br /&gt;
				if ent.currlives == 1 then self.tiny.hudlives[1]:setColor(0xff0000) end&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	if ent.currlives &amp;lt;= 0 then -- deaded&lt;br /&gt;
		-- stop all movements&lt;br /&gt;
		ent.isleft = false&lt;br /&gt;
		ent.isright = false&lt;br /&gt;
		ent.isup = false&lt;br /&gt;
		ent.isdown = false&lt;br /&gt;
		-- play dead sequence&lt;br /&gt;
		ent.isdirty = false&lt;br /&gt;
		resetanim = false&lt;br /&gt;
		ent.washurt = ent.recovertimer&lt;br /&gt;
		ent.wasbadlyhurt = ent.recoverbadtimer&lt;br /&gt;
		ent.animation.curranim = g_ANIM_LOSE1_R&lt;br /&gt;
		ent.sprite:setColorTransform(255*0.5/255, 255*0.5/255, 255*0.5/255, 1)&lt;br /&gt;
		self.camera:setZoom(1) -- zoom&lt;br /&gt;
		ent.animation.bmp:setY(ent.animation.bmp:getY()-1)&lt;br /&gt;
		if ent.animation.bmp:getY() &amp;lt; -200 then -- you choose&lt;br /&gt;
			switchToScene(LevelX.new())&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This System deals with the player1 being hit or killed:&lt;br /&gt;
* runs once on init and &amp;#039;&amp;#039;&amp;#039;every game loop&amp;#039;&amp;#039;&amp;#039; (&amp;#039;&amp;#039;process&amp;#039;&amp;#039;)&lt;br /&gt;
* in &amp;#039;&amp;#039;init&amp;#039;&amp;#039; we add the camera and a sound to add some juice to the game&lt;br /&gt;
* there are 2 kind of hurt animations depending on the player1 health (&amp;#039;&amp;#039;washurt&amp;#039;&amp;#039; and &amp;#039;&amp;#039;wasbadlyhurt&amp;#039;&amp;#039;)&lt;br /&gt;
* when the player1 is hit, we add a camera shake and play some sound&lt;br /&gt;
* we update the &amp;#039;&amp;#039;&amp;#039;HUD&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* when the player1 is dead we play a death sequence and restart the current level&lt;br /&gt;
&lt;br /&gt;
== sNmes.lua ==&lt;br /&gt;
&amp;quot;&amp;#039;&amp;#039;&amp;#039;sNmes.lua&amp;#039;&amp;#039;&amp;#039;&amp;quot; in the &amp;#039;&amp;#039;&amp;#039;&amp;quot;_S&amp;quot;&amp;#039;&amp;#039;&amp;#039; folder. The code:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
SNmes = Core.class()&lt;br /&gt;
&lt;br /&gt;
local random = math.random&lt;br /&gt;
&lt;br /&gt;
function SNmes:init(xtiny, xbump) -- tiny function&lt;br /&gt;
	self.tiny = xtiny -- class ref so we can remove entities from tiny world&lt;br /&gt;
	self.tiny.processingSystem(self) -- called once on init and every update&lt;br /&gt;
	self.bworld = xbump&lt;br /&gt;
	-- sfx&lt;br /&gt;
	self.snd = Sound.new(&amp;quot;audio/sfx/sfx_deathscream_human14.wav&amp;quot;)&lt;br /&gt;
	self.channel = self.snd:play(0, false, true)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function SNmes:filter(ent) -- tiny function&lt;br /&gt;
	return ent.isnme&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function SNmes:onAdd(ent) -- tiny function&lt;br /&gt;
	ent.flip = math.random(100)&lt;br /&gt;
	if ent.flip &amp;gt; 50 then ent.flip = 1 else ent.flip = -1 end&lt;br /&gt;
	ent.currlives = ent.totallives&lt;br /&gt;
	ent.currhealth = ent.totalhealth&lt;br /&gt;
	ent.washurt = 0&lt;br /&gt;
	ent.wasbadlyhurt = 0&lt;br /&gt;
	ent.isdead = false&lt;br /&gt;
	ent.curractiontimer = ent.actiontimer&lt;br /&gt;
	ent.positionystart = 0&lt;br /&gt;
	-- abilities&lt;br /&gt;
	ent.abilities = {}&lt;br /&gt;
	if ent.headhitboxattack1 then ent.abilities[#ent.abilities+1] = 1 end -- punch1&lt;br /&gt;
	if ent.headhitboxattack2 then ent.abilities[#ent.abilities+1] = 2 end -- punch2&lt;br /&gt;
	if ent.spinehitboxattack1 then ent.abilities[#ent.abilities+1] = 3 end -- kick1&lt;br /&gt;
	if ent.spinehitboxattack2 then ent.abilities[#ent.abilities+1] = 4 end -- kick2&lt;br /&gt;
	if ent.headhitboxjattack1 then ent.abilities[#ent.abilities+1] = 5 end -- jumppunch1&lt;br /&gt;
	if ent.spinehitboxjattack1 then ent.abilities[#ent.abilities+1] = 6 end -- jumpkick1&lt;br /&gt;
--	for k, v in pairs(ent.abilities) do print(k, v) end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function SNmes:onRemove(ent) -- tiny function&lt;br /&gt;
	self.bworld:remove(ent) -- remove collision box from cbump world here!&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local resetanim = true&lt;br /&gt;
function SNmes:process(ent, dt) -- tiny function&lt;br /&gt;
	-- hurt fx&lt;br /&gt;
	if ent.washurt and ent.washurt &amp;gt; 0 and not (ent.wasbadlyhurt and ent.wasbadlyhurt &amp;gt; 0) and not ent.isdead then&lt;br /&gt;
		ent.washurt -= 1&lt;br /&gt;
		ent.animation.curranim = g_ANIM_HURT_R&lt;br /&gt;
		if ent.washurt &amp;lt; ent.recovertimer*0.5 then ent.hitfx:setVisible(false) end&lt;br /&gt;
		if ent.washurt &amp;lt;= 0 then ent.sprite:setColorTransform(1, 1, 1, 1) end&lt;br /&gt;
	elseif ent.wasbadlyhurt and ent.wasbadlyhurt &amp;gt; 0 and not ent.isdead then&lt;br /&gt;
		ent.wasbadlyhurt -= 1&lt;br /&gt;
		ent.animation.curranim = g_ANIM_LOSE1_R&lt;br /&gt;
		if ent.wasbadlyhurt &amp;lt; ent.recoverbadtimer*0.5 then&lt;br /&gt;
			ent.hitfx:setVisible(false)&lt;br /&gt;
			if resetanim then&lt;br /&gt;
				resetanim = false&lt;br /&gt;
				ent.animation.frame = 0&lt;br /&gt;
			end&lt;br /&gt;
			ent.animation.curranim = g_ANIM_STANDUP_R&lt;br /&gt;
		end&lt;br /&gt;
		if ent.wasbadlyhurt &amp;lt;= 0 then&lt;br /&gt;
			ent.sprite:setColorTransform(1, 1, 1, 1)&lt;br /&gt;
			resetanim = true&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	if ent.isdirty then -- hit&lt;br /&gt;
		self.channel = self.snd:play()&lt;br /&gt;
		if self.channel then self.channel:setVolume(g_sfxvolume*0.01) end&lt;br /&gt;
		ent.hitfx:setVisible(true)&lt;br /&gt;
		ent.hitfx:setPosition(ent.pos.x+ent.collbox.w/2+(ent.headhurtbox.x*ent.flip), ent.pos.y+ent.headhurtbox.y-ent.headhurtbox.h/2)&lt;br /&gt;
		ent.spritelayer:addChild(ent.hitfx)&lt;br /&gt;
		ent.currhealth -= ent.damage&lt;br /&gt;
		ent.washurt = ent.recovertimer -- timer for a flash effect&lt;br /&gt;
--		ent.sprite:setColorTransform(0, 0, 2, 3) -- the flash effect (a bright red color)&lt;br /&gt;
		ent.isdirty = false&lt;br /&gt;
		if ent.currhealth &amp;lt;= 0 then&lt;br /&gt;
			ent.wasbadlyhurt = ent.recoverbadtimer -- timer for actor to stand back up&lt;br /&gt;
			ent.currlives -= 1&lt;br /&gt;
			if ent.currlives &amp;gt; 0 then ent.currhealth = ent.totalhealth end&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	if ent.currlives &amp;lt;= 0 then -- deaded&lt;br /&gt;
		-- stop all movements&lt;br /&gt;
		ent.isleft = false&lt;br /&gt;
		ent.isright = false&lt;br /&gt;
		ent.isup = false&lt;br /&gt;
		ent.isdown = false&lt;br /&gt;
		-- play dead sequence&lt;br /&gt;
		ent.isdirty = false&lt;br /&gt;
		ent.washurt = ent.recovertimer&lt;br /&gt;
		ent.wasbadlyhurt = ent.recoverbadtimer&lt;br /&gt;
		-- blood&lt;br /&gt;
		if not ent.isdead then&lt;br /&gt;
			ent.hitfx:setVisible(true)&lt;br /&gt;
			ent.hitfx:setColorTransform(3, 0, 0, random(1, 3)/10) -- blood stain&lt;br /&gt;
			ent.hitfx:setPosition(ent.pos.x+ent.collbox.w/2, ent.pos.y)&lt;br /&gt;
			ent.hitfx:setRotation(random(360))&lt;br /&gt;
			ent.hitfx:setScale(random(5, 8)/10)&lt;br /&gt;
			ent.bgfxlayer:addChild(ent.hitfx)&lt;br /&gt;
			ent.isdead = true&lt;br /&gt;
		end&lt;br /&gt;
		ent.animation.curranim = g_ANIM_LOSE1_R&lt;br /&gt;
		resetanim = false -- ??? XXX&lt;br /&gt;
		ent.sprite:setColorTransform((-ent.pos.y&amp;lt;&amp;gt;ent.pos.y)/255, (-ent.pos.y&amp;lt;&amp;gt;ent.pos.y)/255, 0, 1)&lt;br /&gt;
		ent.shadow.sprite:setVisible(false)&lt;br /&gt;
		ent.pos -= vector(8*ent.flip, 8)&lt;br /&gt;
		ent.sprite:setPosition(ent.pos)&lt;br /&gt;
		ent.sprite:setScale(ent.sprite:getScale()+0.07)&lt;br /&gt;
		if ent.pos.y &amp;lt; -256 then&lt;br /&gt;
			self.tiny.tworld:removeEntity(ent) -- sprite is removed in SDrawable&lt;br /&gt;
			self.tiny.numberofnmes -= 1&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This System deals with the enemies being hit or killed:&lt;br /&gt;
* runs once on init and &amp;#039;&amp;#039;&amp;#039;every game loop&amp;#039;&amp;#039;&amp;#039; (&amp;#039;&amp;#039;process&amp;#039;&amp;#039;)&lt;br /&gt;
* in &amp;#039;&amp;#039;init&amp;#039;&amp;#039; we add a sound to add some juice to the game&lt;br /&gt;
* &amp;#039;&amp;#039;onAdd&amp;#039;&amp;#039; some explanation below&lt;br /&gt;
* when an enemy is hit, we play some sound&lt;br /&gt;
&lt;br /&gt;
In the &amp;#039;&amp;#039;onAdd&amp;#039;&amp;#039; function it is worth noting that instead of creating all the variables for the enemies in their Entity code, I found it &amp;#039;clever&amp;#039; to put them in the enemy System as they all share the same variables.&lt;br /&gt;
&lt;br /&gt;
The other thing worth noting is an enemy ability. Depending on the attacks an Entity has, they are stored in an &amp;#039;&amp;#039;abilities&amp;#039;&amp;#039; table. In a artificial intelligence System we will add shortly, the code will iterate the &amp;#039;&amp;#039;abilities&amp;#039;&amp;#039; table and pick a random attack an Entity can perform.&lt;br /&gt;
&lt;br /&gt;
== sAI.lua ==&lt;br /&gt;
&amp;quot;&amp;#039;&amp;#039;&amp;#039;sAI.lua&amp;#039;&amp;#039;&amp;#039;&amp;quot; in the &amp;#039;&amp;#039;&amp;#039;&amp;quot;_S&amp;quot;&amp;#039;&amp;#039;&amp;#039; folder. The code:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
SAI = Core.class()&lt;br /&gt;
&lt;br /&gt;
local random = math.random&lt;br /&gt;
&lt;br /&gt;
function SAI:init(xtiny, xplayer1) -- tiny function&lt;br /&gt;
	xtiny.processingSystem(self) -- called once on init and every update&lt;br /&gt;
	self.player1 = xplayer1&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function SAI:filter(ent) -- tiny function&lt;br /&gt;
	return ent.ai&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function SAI:onAdd(ent) -- tiny function&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function SAI:onRemove(ent) -- tiny function&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local p1rangex = 192 -- 192 -- magik XXX&lt;br /&gt;
local p1hitrangex = 64 -- magik XXX&lt;br /&gt;
local p1rangey = 96 -- 96 -- magik XXX&lt;br /&gt;
local p1hitrangey = 16 -- magik XXX&lt;br /&gt;
local rndaction = 0 -- random punch/kick action&lt;br /&gt;
local p1rangetoofar = myappwidth -- disable system to save some CPU, magik XXX&lt;br /&gt;
function SAI:process(ent, dt) -- tiny function&lt;br /&gt;
	if ent.isdead then return end&lt;br /&gt;
	local function fun()&lt;br /&gt;
		-- some flags&lt;br /&gt;
		ent.doanimate = true -- to save some cpu&lt;br /&gt;
		ent.readytohit = false&lt;br /&gt;
		if (ent.pos.x &amp;gt; self.player1.pos.x + p1rangetoofar or ent.pos.x &amp;lt; self.player1.pos.x - p1rangetoofar) then&lt;br /&gt;
			ent.doanimate = false&lt;br /&gt;
			return&lt;br /&gt;
		end&lt;br /&gt;
		if (ent.pos.x &amp;gt; self.player1.pos.x + p1rangex or ent.pos.x &amp;lt; self.player1.pos.x - p1rangex) or&lt;br /&gt;
			(ent.pos.y &amp;gt; self.player1.pos.y + p1rangey or ent.pos.y &amp;lt; self.player1.pos.y - p1rangey) then -- OUTSIDE ATTACK RANGE&lt;br /&gt;
			-- idle&lt;br /&gt;
			ent.isleft, ent.isright = false, false&lt;br /&gt;
			ent.isup, ent.isdown = false, false&lt;br /&gt;
			ent.body.currspeed = ent.body.speed&lt;br /&gt;
			ent.body.currjumpspeed = ent.body.jumpspeed&lt;br /&gt;
		else -- INSIDE ATTACK RANGE&lt;br /&gt;
			-- x&lt;br /&gt;
			if ent.pos.x &amp;gt; random(self.player1.pos.x+p1hitrangex, self.player1.pos.x+p1rangex) then&lt;br /&gt;
				ent.isleft, ent.isright = true, false&lt;br /&gt;
				ent.body.currspeed = ent.body.speed * random(10, 15)*0.1 -- magik XXX&lt;br /&gt;
			elseif ent.pos.x &amp;lt; random(self.player1.pos.x-p1rangex, self.player1.pos.x-p1hitrangex) then&lt;br /&gt;
				ent.isleft, ent.isright = false, true&lt;br /&gt;
				ent.body.currspeed = ent.body.speed * random(10, 15)*0.1 -- magik XXX&lt;br /&gt;
			end&lt;br /&gt;
			-- y&lt;br /&gt;
			if ent.pos.y &amp;gt; random(self.player1.pos.y, self.player1.pos.y+p1hitrangey) then&lt;br /&gt;
				ent.isup, ent.isdown = true, false&lt;br /&gt;
				ent.body.currjumpspeed = ent.body.jumpspeed * random(10, 64) -- magik XXX&lt;br /&gt;
				ent.readytohit = true&lt;br /&gt;
			elseif ent.pos.y &amp;lt; random(self.player1.pos.y-p1hitrangey, self.player1.pos.y) then&lt;br /&gt;
				ent.isup, ent.isdown = false, true&lt;br /&gt;
				ent.body.currjumpspeed = ent.body.jumpspeed * random(10, 64) -- magik XXX&lt;br /&gt;
				ent.readytohit = true&lt;br /&gt;
			end&lt;br /&gt;
			-- nmes always face player1&lt;br /&gt;
			if not (ent.isactionjumppunch1 or ent.isactionjumpkick1) then&lt;br /&gt;
				if ent.pos.x &amp;gt; self.player1.pos.x then ent.flip = -1&lt;br /&gt;
				else ent.flip = 1&lt;br /&gt;
				end&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
		-- ATTACK&lt;br /&gt;
		if ent.readytohit then&lt;br /&gt;
			ent.curractiontimer -= 1&lt;br /&gt;
			if ent.curractiontimer &amp;lt; 0 then&lt;br /&gt;
				ent.animation.frame = 0&lt;br /&gt;
				rndaction = ent.abilities[random(#ent.abilities)] -- pick a random attack&lt;br /&gt;
				if rndaction == 1 and not (ent.isactionjumppunch1 or ent.isactionjumpkick1) then &lt;br /&gt;
					ent.isactionpunch1 = true&lt;br /&gt;
				elseif rndaction == 2 and not (ent.isactionjumppunch1 or ent.isactionjumpkick1) then&lt;br /&gt;
					ent.isactionpunch2 = true&lt;br /&gt;
				elseif rndaction == 3 and not (ent.isactionjumppunch1 or ent.isactionjumpkick1) then&lt;br /&gt;
					ent.isactionkick1 = true&lt;br /&gt;
				elseif rndaction == 4 and not (ent.isactionjumppunch1 or ent.isactionjumpkick1) then&lt;br /&gt;
					ent.isactionkick2 = true&lt;br /&gt;
				elseif rndaction == 5 and not (ent.isactionjumppunch1 or ent.isactionjumpkick1) then&lt;br /&gt;
					ent.positionystart = ent.pos.y&lt;br /&gt;
					ent.body.isonfloor = false&lt;br /&gt;
					ent.body.isgoingup = true&lt;br /&gt;
					ent.isactionjumppunch1 = true&lt;br /&gt;
					ent.body.currspeed *= 1+random(4)*0.1 -- randomize speed, you choose to add it and the params&lt;br /&gt;
					-- jump in the direction of the flip&lt;br /&gt;
					if ent.flip == 1 then ent.isleft = false ent.isright = true&lt;br /&gt;
					else ent.isleft = true ent.isright = false&lt;br /&gt;
					end&lt;br /&gt;
				elseif rndaction == 6 and not (ent.isactionjumppunch1 or ent.isactionjumpkick1) then&lt;br /&gt;
					ent.positionystart = ent.pos.y&lt;br /&gt;
					ent.body.isonfloor = false&lt;br /&gt;
					ent.body.isgoingup = true&lt;br /&gt;
					ent.isactionjumpkick1 = true&lt;br /&gt;
					ent.body.currspeed *= 1+random(3)*0.1 -- randomize speed, you choose to add it and the params&lt;br /&gt;
					-- jump in the direction of the flip&lt;br /&gt;
					if ent.flip == 1 then ent.isleft = false ent.isright = true&lt;br /&gt;
					else ent.isleft = true ent.isright = false&lt;br /&gt;
					end&lt;br /&gt;
				end&lt;br /&gt;
				ent.curractiontimer = ent.actiontimer&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
		Core.yield(1)&lt;br /&gt;
	end&lt;br /&gt;
	Core.asyncCall(fun) -- profiler seems to be faster without asyncCall (because of pairs traversing?)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This System controls all the entities with an artificial intelligence (&amp;#039;&amp;#039;ai&amp;#039;&amp;#039;) id. In this System an entity can be in an &amp;#039;&amp;#039;&amp;#039;idle&amp;#039;&amp;#039;&amp;#039; state, a &amp;#039;&amp;#039;&amp;#039;move&amp;#039;&amp;#039;&amp;#039; state or an &amp;#039;&amp;#039;&amp;#039;attack&amp;#039;&amp;#039;&amp;#039; state. Each states are applied relative to the distance between the Entity and the &amp;#039;&amp;#039;player1&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== sShadow.lua ==&lt;br /&gt;
Let&amp;#039;s quickly add the Shadow System. &amp;quot;&amp;#039;&amp;#039;&amp;#039;sShadow.lua&amp;#039;&amp;#039;&amp;#039;&amp;quot; in the &amp;#039;&amp;#039;&amp;#039;&amp;quot;_S&amp;quot;&amp;#039;&amp;#039;&amp;#039; folder. The code:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
SShadow = Core.class()&lt;br /&gt;
&lt;br /&gt;
function SShadow:init(xtiny) -- tiny function&lt;br /&gt;
	xtiny.processingSystem(self) -- called once on init and every update&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function SShadow:filter(ent) -- tiny function&lt;br /&gt;
	return ent.shadow&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function SShadow:onAdd(ent) -- tiny function&lt;br /&gt;
	ent.spritelayer:addChildAt(ent.shadow.sprite, 1) -- add shadow behind ent&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function SShadow:onRemove(ent) -- tiny function&lt;br /&gt;
	ent.spritelayer:removeChild(ent.shadow.sprite)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function SShadow:process(ent, dt) -- tiny function&lt;br /&gt;
	local function fun()&lt;br /&gt;
		ent.shadow.sprite:setPosition(ent.pos+vector(ent.collbox.w/2, ent.collbox.h/2))&lt;br /&gt;
		if ent.body and not ent.body.isonfloor then&lt;br /&gt;
			ent.shadow.sprite:setPosition(ent.pos.x+ent.collbox.w/2, ent.positionystart+ent.collbox.h/2)&lt;br /&gt;
		end&lt;br /&gt;
		Core.yield(1)&lt;br /&gt;
	end&lt;br /&gt;
	Core.asyncCall(fun)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This System adds a shadow below an Entity. If the Entity is in a jump state, we update the shadow only on the x axis.&lt;br /&gt;
&lt;br /&gt;
== Next? ==&lt;br /&gt;
Systems are fairly straight forward and fairly short for what they can achieve.&lt;br /&gt;
&lt;br /&gt;
We have covered the first set of systems, we have a couple more to add.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Prev.: [[Tuto tiny-ecs 2d platformer Part 8 more entities]]&amp;lt;/br&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Next: [[Tuto tiny-ecs 2d platformer Part 10 Systems 2]]&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;[[Tutorial - tiny-ecs 2d platformer]]&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
{{GIDEROS IMPORTANT LINKS}}&lt;/div&gt;</summary>
		<author><name>MoKaLux</name></author>
	</entry>
</feed>