Tuto tiny-ecs demo Part 9 Player Shoots Enemies Die

From GiderosMobile
Revision as of 04:10, 21 December 2023 by MoKaLux (talk | contribs) (Created page with "__TOC__ In this chapter we will make the player1 able to "shoot" and hurt a enemies. This will be done in two parts: first enabling the player1 "shoot" action, then hurting...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

In this chapter we will make the player1 able to "shoot" and hurt a enemies.

This will be done in two parts: first enabling the player1 "shoot" action, then hurting the enemies with some fx and make them "die".

Player1 can "Shoot"

We implement player1 can shoot in the SDynamicBodies system.

Please go to the "sDynamicBodies.lua" file to add some code.

SDynamicBodies = Core.class()

function SDynamicBodies:init(xtiny, xnmes) -- tiny function
	self.tiny = xtiny -- ref so we can remove entities from tiny system
	self.tiny.processingSystem(self) -- called once on init and every update
	self.nmes = xnmes -- a list of nmes we can hurt
end
-- ...

In the init function we add the list of enemies to the function signature and we make that list available to the other functions.

World addEntity

We need to add the enemy1 entity to tiny-ecs world. Let's add a couple of them.

Please go to the "LevelX.lua" file and complete the code as follow:

When we add an enemy1 entity to tiny-ecs world, we also add it to an nmes list so we can iterate through them in our game.

You can run the demo and you should see ten enemies placed randomly on the x axis.

Enemy add AI Component

Our enemy1 entities don't do anything yet. Let's equip them with an Artificial Intelligence component.

In the components folder "_C", create a file called "cAI.lua" for example, and copy the following code:

Here, in the init function, we store an entity starting position and a delta x and y to set how far it can travel.

We can now attach this AI component to our enemy1 entity in "eNme1.lua":

You can run the demo and the enemies should move to the right.

Enemy AI System

To make our enemies intelligent we need to create an AI system.

In the systems folder "_S", create a file called "sAI.lua" for example, and copy the following code:

This is a simple AI system, which switches an entity direction when it reaches some limits (delta x, delta y).

Finally we need to add this AI system to tiny-ecs world.

Please go to the "LevelX.lua" file and complete the code as follow:

You can run the demo and the enemies should be less dumb ;-)

Next?

In the next part, we make the player1 "shoots" and the enemies "die" :-(


Prev.: Tuto tiny-ecs demo Part 8 Enemies
Next: xxx


Tutorial - tiny-ecs demo