Tuto tiny-ecs beatemup Part 5 ePlayer1
the levelX.lua file
This is where all the fun begins!
The LevelX scene holds the game loop and controls the flow of each levels.
When the scene loads, it constructs the level which is organised into layers, more on that in the code comments below ;-)
The code is not that long given it takes care of all levels of the game: level 1, 2 and 3. To make it easy to follow and debug, I use FIGlet (https://en.wikipedia.org/wiki/FIGlet).
I use this one https://sourceforge.net/projects/figletgenerator/
The LevelX code:
levelX.lua Code comments
Let's break it down!
The --!strict thing is some Luau stuff I was messing around with, you can find more information here: https://devforum.roblox.com/t/luau-type-checking-beta/435382#strict-mode-5.
I declare 2 variables which are local to the Class because I didn't want to use self on them. The random variable caches the math.random function for speed. I am also experimenting here with Luau type annotations https://devforum.roblox.com/t/luau-type-checking-beta/435382#new-syntax-6.
local random = math.random
local ispaused : boolean = false
LevelX:init()
I commented out the "move cursor" code because it was more annoying than anything else! You can use it if you want.
plugins
Here we initialize our plugins. tiny-ecs is declared as a Class variable (self.tiny) because we use it outside the init function. Bump can be local as we only use it in the init function.
It is worth noting that tiny.tworld is attached to self.tiny variable. This makes it much easier to access through the rest of the code in our project.
layers
This one is easy :-)
We create several layers which will be laid out on top of each other. The background layer will have all the graphics for the background, etc...
There is a player1inputlayer which will capture the user input to control the player, it won't hold any graphics.
levels
Time to build our levels. The sprite list is a table of all the actors we can interact with (the player, the nmes, collectibles, ...). We put them in a list so we can use that list in the systems we will create (sAI, sAnimation, sCollectible, sCollision, ...).
tiny.numberofnmes and tiny.numberofdestructibleobjects are variables we can tune to add a certain amount of enemies and destructible objects in each level.
The mapdef is a table which has the map definition (dimensions), so we can pass it to functions that will require the size of the map for calculations.
We use a camera in our game and camfollowoffsety will offset the camera following the player. It is easier to change it being a variable.
The buildLevel function is where we build the current level the player is playing. We pass it some variables so it can do its thing. Some code comments on that function below.
Then we set the variables for each level: the camera offset and the number of destructible objects. The destructible objects spawn collectible when they are destroyed (extended life, jumps).
We randomly place the destructible objects throughout the level using the map definition. We place them between 25% and 90% of the map length and a little bit above the bottom of the map.
Each destructible objects are entities and we add them to both the tiny-ECS world and the Bump world. We will create entities in the coming parts of the tutorial.
player1
The player is an ECS entity we will create in the next chapters. The arguments to the init function are: the layer the player sprite will be added to, the position as a vector, and the layer for any fancy graphics effects we may add to the player.
After the entity is created, we add it to both the tiny-ECS world and the Bump world.
hud
I added a simple head up display to the game, so we can see the player current health, number of lives and number of attacking jumps available.
The same way we attached tiny.tworld to the self.tiny variable, we attach some more variables to it. This makes it easier to access those variables.
the camera
We use a slightly modified version of MultiPain's (aka rrraptor on Gideros forum) GCam Class for our camera.
Please grab it here Media:gcam_beu.lua (tip: right click and save link as) and put it in the "classes" folder.
We pass the mainlayer as the content, then using the map definition we set the camera bounds. We also set the soft and the dead size parameters and we tell it to follow the player.
if you are curious I added an extra updateXOnly function ;-)
order
This is the order of the layers, from bottom to top. We first add the background layer and the other layers on top of it.
systems
Once all entities are done, we add the ECS systems. We will see those ECS systems in the coming parts of the tutorial.
let's go
Finally we are ready to run the game loop!
We also listen to some key events to pause the game, go fullscreen, ...
LevelX:buildLevel
Let's have a look at how we construct our levels.
background and foreground
We draw the background and foreground for each level using the DrawLevelsTiled Class. Each level will have its own graphics. A typical level is 3*1024 pixels wide. Using a power of two size for the graphics, we anticipate any kind of optimisations we may need, plus adding the possibility to port the game to mobile!
The DrawLevelsTiled Class already tries to bring in some optimisations! You can create a file called "drawlevelstiled.lua" and put it in the gfx\levels\ folder. The code:
DrawLevelsTiled = Core.class(Sprite)
function DrawLevelsTiled:init(xlayer, xtexpaths, xposy)
-- tilemaps textures
local textures = {}
for i = 1, #xtexpaths do
-- textures[i] = Texture.new(xtexpaths[i])
-- textures[i] = Texture.new(xtexpaths[i], false, { format=TextureBase.YA8}) -- best win32 perfs but b&w!
textures[i] = Texture.new(xtexpaths[i], false, { format=TextureBase.RGBA4444}) -- better win32 perfs but !
-- textures[i] = Texture.new(xtexpaths[i], false, { format=TextureBase.RGBA5551}) -- better win32 perfs but !
end
-- map size
local tilesizetarget = 64
local tilesetcols, tilesetrows = textures[1]:getWidth()/tilesizetarget, textures[1]:getHeight()/tilesizetarget
-- create the tilemaps
local function createTilemap(xtex)
local tm = TileMap.new(
tilesetcols, tilesetrows, -- map size in tiles
xtex, -- tileset texture
tilesizetarget, tilesizetarget -- tile size in pixel
)
-- build the map
for i=1,tilesetcols do
for j=1,tilesetrows do
tm:setTile(i, j, i, j)
end
end
return tm
end
-- the maps
for i = 1, #textures do
local map = createTilemap(textures[i])
map:setPosition(map:getWidth()*(i-1), xposy)
-- self:addChild(map)
xlayer:addChild(map)
end
-- params
-- self.mapwidth = self:getWidth()
-- self.mapheight = self:getHeight()
self.mapwidth = xlayer:getWidth()
self.mapheight = xlayer:getHeight()
-- clean
textures = {}
end
As the name tries to suggest (DrawLevelsTiled), we take the paths to the graphics and create tiles as if we were drawing using a tilemap. The init function parameters are: the layer we add the sprites to, the paths to the textures as a table and an y offset for flexibility.
The first step is to create the textures and put them in a table called textures. When we iterate through the xtexpaths table, I experimented with some texture formats to see if I could gain some speed.
Once the textures are created and stored in a table, we can create a TileMap of 64*64px tiles.
Finally we return the size of the map and we clean the textures table just in case.
Back to LevelX Class, now that our background and foreground layers are drawn, we set the map definition. The map definition sets the boundaries of the map where the actors can freely roam.
the enemies
Each level will have different kind of enemies. The first level should be easy with few enemies you can easily kill. The other levels will be increasingly difficult with more enemies a little bit harder to defeat.
So here, depending on the level we create a bunch of enemies. Enemies are ECS entities we will create in the following chapters. The game has four types of enemies of varying strength. We add them to both the tiny-ECS world and the Bump world.
extra gfx
Depending on the level, we may have extra sprites we want to add to the level.
We finish the function with some house cleaning.
LevelX:onEnterFrame THE GAME LOOP
Before the game loop, I declare three local variables: leveltimer is the time it takes to transition to the next level, endleveltimer is the timer itself, extragfxx is to move the extra sprite we may have on the x axis.
When we defeat all enemies, the timer decreases. When it reaches 0, the current level number is increased and we load it.
When the player is jumping we update the camera only on the x axis, otherwise we update the camera on both x and y axis.
We update the tiny-ecs world, so it can run all the systems (animations, movements, AI, ...). Some systems will be called only once, others every frame per second.
LevelX:myKeysPressed
The last function of the LevelX Class is myKeysPressed.
Here we simply listen to some KEY_DOWN Events, that is keys pressed on the keyboard. The key ESC will go back to the menu, the letter P will pause the game and when you press ALT+ENTER you switch the game to fullscreen.
Next?
That was quite a lot of work but we coded the heart of our game and we are already nearly done!!
All is left to do is add the actors. Actors will be ECS entites, those entities will have components and systems will control them.
In the next part we deal with our player1 entity.
Prev.: [[Tuto tiny-ecs beatemup Part 4 LevelX]]
Next: Tuto tiny-ecs beatemup Part 6 XXX