Difference between revisions of "Tuto Gideros Game Template1 Part 7 Game"
 (wip)  | 
				|||
| Line 21: | Line 21: | ||
function LevelX:init()  | function LevelX:init()  | ||
| − | + | 	-- move the cursor out of the way  | |
| + | 	if not application:isPlayerMode() then  | ||
		local sw, sh = application:get("screenSize") -- the user's screen size!  | 		local sw, sh = application:get("screenSize") -- the user's screen size!  | ||
		application:set("cursorPosition", sw, sh) -- 0, 0  | 		application:set("cursorPosition", sw, sh) -- 0, 0  | ||
	end  | 	end  | ||
| − | 	--   | + | 	-- layer sprites  | 
| − | 	local   | + | 	local mainlayer = Sprite.new() -- one Sprite to hold them all  | 
| − | 	local   | + | 	local bglayer = Sprite.new() -- background layer  | 
| − | 	local   | + | 	local bgfxlayer = Sprite.new() -- background fx layer  | 
| − | 	--   | + | 	local actorslayer = Sprite.new() -- actors layer  | 
| − | 	local   | + | 	local fgfxlayer = Sprite.new() -- foreground fx layer  | 
| + | 	local fglayer = Sprite.new() -- foreground layer  | ||
| + | 	-- current level assets  | ||
| + | 	local bggfx, fggfx  | ||
	if g_currlevel == 1 then  | 	if g_currlevel == 1 then  | ||
| − | + | 		bggfx = Pixel.new(math.random(0xffffff), 1, myappwidth, myappheight)  | |
| − | + | 		fggfx = Pixel.new(math.random(0xffffff), 1, myappwidth, myappheight/8)  | |
	elseif g_currlevel == 2 then  | 	elseif g_currlevel == 2 then  | ||
| − | + | 		bggfx = Pixel.new(math.random(0xffffff), 1, myappwidth, myappheight)  | |
	elseif g_currlevel == 3 then  | 	elseif g_currlevel == 3 then  | ||
| − | + | 		bggfx = Pixel.new(math.random(0xffffff), 1, myappwidth, myappheight)  | |
| − | + | 		fggfx = Pixel.new(math.random(0xffffff), 1, myappwidth, myappheight/16)  | |
	end  | 	end  | ||
| − | + | 	-- the player1  | |
| − | + | 	self.player1 = Player1.new()  | |
	-- position  | 	-- position  | ||
| − | + | 	if bggfx then bggfx:setPosition(0+myappleft, 0+myapptop) end  | |
| − | 	if   | + | 	if fggfx then fggfx:setPosition(0+myappleft, myappheight+myapptop-fggfx:getHeight()) end  | 
| + | 	self.player1.x = myappwidth/2+myappleft  | ||
| + | 	self.player1.y = myappheight/2+myapptop  | ||
| + | 	self.player1:setPosition(self.player1.x, self.player1.y)  | ||
| + | 	-- add children to corresponding layer  | ||
| + | 	if bggfx then bglayer:addChild(bggfx) end -- add the background graphics to the background layer if any  | ||
| + | 	if fggfx then fglayer:addChild(fggfx) end -- add the foreground graphics to the foreground layer if any  | ||
| + | 	actorslayer:addChild(self.player1)  | ||
	-- order  | 	-- order  | ||
| − | + | 	mainlayer:addChild(bglayer) -- first the background layer  | |
| − | + | 	mainlayer:addChild(bgfxlayer) -- then the background fx layer  | |
| − | + | 	mainlayer:addChild(actorslayer) -- then the actors layer  | |
| − | + | 	mainlayer:addChild(fgfxlayer) -- then the foreground fx layer  | |
| − | 	self:addChild(  | + | 	mainlayer:addChild(fglayer) -- lastely the foreground layer  | 
| + | 	-- the main sprite  | ||
| + | 	self:addChild(mainlayer) -- the only Sprite layer that needs to be added to to scene tree hierarchy!  | ||
	-- the game loop  | 	-- the game loop  | ||
	self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)  | 	self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)  | ||
| Line 58: | Line 71: | ||
-- game loop  | -- game loop  | ||
| + | local dt  | ||
function LevelX:onEnterFrame(e)  | function LevelX:onEnterFrame(e)  | ||
	if not ispaused then  | 	if not ispaused then  | ||
| − | 		--   | + | 		dt = e.deltaTime  | 
| + | 		if self.player1.isleft and not self.player1.isright and self.player1.x > 0 then -- LEFT  | ||
| + | 			self.player1.vx = -self.player1.speed*dt  | ||
| + | 			self.player1.flip = -1  | ||
| + | 		elseif self.player1.isright and not self.player1.isleft and self.player1.x < myappwidth then -- RIGHT  | ||
| + | 			self.player1.vx = self.player1.speed*dt  | ||
| + | 			self.player1.flip = 1  | ||
| + | 		else -- IDLE  | ||
| + | 			self.player1.vx = 0  | ||
| + | 		end  | ||
| + | 		if self.player1.isup and not self.player1.isdown and self.player1.y > 0 then -- UP  | ||
| + | 			self.player1.vy = -self.player1.speed*dt  | ||
| + | 		elseif self.player1.isdown and not self.player1.isup and self.player1.y < myappheight then -- DOWN  | ||
| + | 			self.player1.vy = self.player1.speed*dt  | ||
| + | 		else  | ||
| + | 			self.player1.vy = 0  | ||
| + | 		end  | ||
| + | 		-- actions  | ||
| + | 		if self.player1.isactionpunch1 then  | ||
| + | 			self.player1.vx *= 0.5*dt -- you choose, magik XXX  | ||
| + | 			self.player1.vy *= 0.5*dt -- you choose, magik XXX  | ||
| + | 		elseif self.player1.isactionpunch2 then  | ||
| + | 			self.player1.vx *= 0.25*dt -- you choose, magik XXX  | ||
| + | 			self.player1.vy *= 0.25*dt -- you choose, magik XXX  | ||
| + | 		elseif self.player1.isactionpunch3 then  | ||
| + | 			self.player1.vx *= 0.1*dt -- you choose, magik XXX  | ||
| + | 			self.player1.vy *= 0.1*dt -- you choose, magik XXX  | ||
| + | 		end  | ||
| + | 		-- move and flip  | ||
| + | 		self.player1.x += self.player1.vx  | ||
| + | 		self.player1.y += self.player1.vy  | ||
| + | 		self.player1:setPosition(self.player1.x, self.player1.y)  | ||
| + | 		self.player1.sprite:setScale(self.player1.sx * self.player1.flip, self.player1.sy)  | ||
	end  | 	end  | ||
end  | end  | ||
| Line 68: | Line 114: | ||
	self:addEventListener(Event.KEY_DOWN, function(e) -- KEY_UP  | 	self:addEventListener(Event.KEY_DOWN, function(e) -- KEY_UP  | ||
		if e.keyCode == KeyCode.ESC or e.keyCode == KeyCode.BACK then self:gotoScene() end -- MENU  | 		if e.keyCode == KeyCode.ESC or e.keyCode == KeyCode.BACK then self:gotoScene() end -- MENU  | ||
| − | 		if e.keyCode == KeyCode.P then ispaused = not ispaused end -- PAUSE  | + | 		if e.keyCode == KeyCode.P then ispaused = not ispaused print("is paused: "..tostring(ispaused)) end -- PAUSE  | 
		-- modifier  | 		-- modifier  | ||
		local modifier = application:getKeyboardModifiers()  | 		local modifier = application:getKeyboardModifiers()  | ||
Revision as of 15:07, 27 October 2024
The Game Scene
This is going to be the last scene for our Gideros Game Template1 tutorial. A quick and simple one because you may decide to organize your game differently.
Note: you may have to provide your own game assets here
Usually, when I make games and prototypes, I put all my levels in one scene. When I change levels I reload the Game scene with a variable indicating the current level, eg.: currlevel = 1, 2, ... and load the appropriate graphics, sounds, ...
I have been using this kind of architecture for quite some time now, instead of calling my game scenes Level1, Level2, ..., I decided to call it: LevelX.
The Game scene (LevelX) will mainly consist of:
- moving the mouse cursor out of the way :-)
 - arranging our graphics into Sprite layers: background layer, fx layer, actors layer, foreground layer, ...
 - a game loop
 
It makes sense to create the file in the scenes folder. You can call the file levelX.lua, and the code:
LevelX = Core.class(Sprite)
local ispaused = false
function LevelX:init()
	-- move the cursor out of the way
	if not application:isPlayerMode() then
		local sw, sh = application:get("screenSize") -- the user's screen size!
		application:set("cursorPosition", sw, sh) -- 0, 0
	end
	-- layer sprites
	local mainlayer = Sprite.new() -- one Sprite to hold them all
	local bglayer = Sprite.new() -- background layer
	local bgfxlayer = Sprite.new() -- background fx layer
	local actorslayer = Sprite.new() -- actors layer
	local fgfxlayer = Sprite.new() -- foreground fx layer
	local fglayer = Sprite.new() -- foreground layer
	-- current level assets
	local bggfx, fggfx
	if g_currlevel == 1 then
		bggfx = Pixel.new(math.random(0xffffff), 1, myappwidth, myappheight)
		fggfx = Pixel.new(math.random(0xffffff), 1, myappwidth, myappheight/8)
	elseif g_currlevel == 2 then
		bggfx = Pixel.new(math.random(0xffffff), 1, myappwidth, myappheight)
	elseif g_currlevel == 3 then
		bggfx = Pixel.new(math.random(0xffffff), 1, myappwidth, myappheight)
		fggfx = Pixel.new(math.random(0xffffff), 1, myappwidth, myappheight/16)
	end
	-- the player1
	self.player1 = Player1.new()
	-- position
	if bggfx then bggfx:setPosition(0+myappleft, 0+myapptop) end
	if fggfx then fggfx:setPosition(0+myappleft, myappheight+myapptop-fggfx:getHeight()) end
	self.player1.x = myappwidth/2+myappleft
	self.player1.y = myappheight/2+myapptop
	self.player1:setPosition(self.player1.x, self.player1.y)
	-- add children to corresponding layer
	if bggfx then bglayer:addChild(bggfx) end -- add the background graphics to the background layer if any
	if fggfx then fglayer:addChild(fggfx) end -- add the foreground graphics to the foreground layer if any
	actorslayer:addChild(self.player1)
	-- order
	mainlayer:addChild(bglayer) -- first the background layer
	mainlayer:addChild(bgfxlayer) -- then the background fx layer
	mainlayer:addChild(actorslayer) -- then the actors layer
	mainlayer:addChild(fgfxlayer) -- then the foreground fx layer
	mainlayer:addChild(fglayer) -- lastely the foreground layer
	-- the main sprite
	self:addChild(mainlayer) -- the only Sprite layer that needs to be added to to scene tree hierarchy!
	-- the game loop
	self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
	-- let's go
	self:myKeysPressed()
end
-- game loop
local dt
function LevelX:onEnterFrame(e)
	if not ispaused then
		dt = e.deltaTime
		if self.player1.isleft and not self.player1.isright and self.player1.x > 0 then -- LEFT
			self.player1.vx = -self.player1.speed*dt
			self.player1.flip = -1
		elseif self.player1.isright and not self.player1.isleft and self.player1.x < myappwidth then -- RIGHT
			self.player1.vx = self.player1.speed*dt
			self.player1.flip = 1
		else -- IDLE
			self.player1.vx = 0
		end
		if self.player1.isup and not self.player1.isdown and self.player1.y > 0 then -- UP
			self.player1.vy = -self.player1.speed*dt
		elseif self.player1.isdown and not self.player1.isup and self.player1.y < myappheight then -- DOWN
			self.player1.vy = self.player1.speed*dt
		else
			self.player1.vy = 0
		end
		-- actions
		if self.player1.isactionpunch1 then
			self.player1.vx *= 0.5*dt -- you choose, magik XXX
			self.player1.vy *= 0.5*dt -- you choose, magik XXX
		elseif self.player1.isactionpunch2 then
			self.player1.vx *= 0.25*dt -- you choose, magik XXX
			self.player1.vy *= 0.25*dt -- you choose, magik XXX
		elseif self.player1.isactionpunch3 then
			self.player1.vx *= 0.1*dt -- you choose, magik XXX
			self.player1.vy *= 0.1*dt -- you choose, magik XXX
		end
		-- move and flip
		self.player1.x += self.player1.vx
		self.player1.y += self.player1.vy
		self.player1:setPosition(self.player1.x, self.player1.y)
		self.player1.sprite:setScale(self.player1.sx * self.player1.flip, self.player1.sy)
	end
end
-- keys handler
function LevelX:myKeysPressed()
	self:addEventListener(Event.KEY_DOWN, function(e) -- KEY_UP
		if e.keyCode == KeyCode.ESC or e.keyCode == KeyCode.BACK then self:gotoScene() end -- MENU
		if e.keyCode == KeyCode.P then ispaused = not ispaused print("is paused: "..tostring(ispaused)) end -- PAUSE
		-- modifier
		local modifier = application:getKeyboardModifiers()
		local alt = (modifier & KeyCode.MODIFIER_ALT) > 0
		if (not alt and e.keyCode == KeyCode.ENTER) then -- nothing here!
		elseif alt and e.keyCode == KeyCode.ENTER then -- SWITCH FULLSCREEN
			ismyappfullscreen = not ismyappfullscreen
			application:setFullScreen(ismyappfullscreen)
		end
	end)
end
-- scenes navigation
function LevelX:gotoScene()
	self:removeAllListeners()
	for i = stage:getNumChildren(), 1, -1 do
		stage:removeChildAt(i)
	end collectgarbage()
	stage:addChild(Transitions.new(Menu.new())) -- next scene
end
Code comments
In the init function we:
- move the mouse cursor out of the way
 - create the Sprite layers and "y" sort them from back to front
 - add the game loop Event.ENTER_FRAME
 - listen to some key to pause the game, go back to the Menu scene, ...
 
Sprite layers
I create a main Sprite layer levelgfxlayer which will hold all the other layers (Sprites and Bitmaps).
The background layer is a Bitmap but you can change it to be a Sprite and add bitmaps to it.
The splat layer is a Sprite to add all sort of fx (splats in this case). Please feel free to rename any of the layers.
The action layer is a Sprite which we add all graphics that can be considered an actor (the player, the enemies, collectibles, ...)
Finally the foreground layer is a Bitmap which you can change to be a Sprite.
the game loop
If the game is not in Pause mode it will execute what's inside it.
keys handler
We also listen to any key press to switch the state of the game or go back to the Menu scene, ...
Next?
Well that's it for this Gideros Game Template1 tutorial.
We should be good to go and dive straight into coding the core of the game, all the Template stuff being taken care of.
This is now yours to use/modify (add scenes, ...).
I hope this tutorial was useful and will help you on your journey of making great games using Gideros Studio ;-)
the Project file
Media:GIDEROS_GAME_TEMPLATE1.zip (tip: right click and save link as)
Prev.: Tuto Gideros Game Template1 Part 6 Options
END
Tutorial - Gideros Game Template1