Tuto tiny-ecs 2d platformer Part 2 Init and Main

From GiderosMobile

the init.lua file

The init.lua file is the first file to be loaded when launching the game. This is where I like to initialize some global variables:

  • the plugins for our game
  • getting the app size
  • some fonts
  • a table to customize the UI for the game
  • finally we also store all tweens in a table we can refer to in other scenes (transitions, ...)

All of the above variables may be used in multiple (if not all) scenes of our game, so imho it makes sense to centralize them all here.

The code:

-- global app variables
screenwidth, screenheight = application:get("screenSize") -- the actual user screen size!
myappleft, myapptop, myappright, myappbot = application:getLogicalBounds()
myappwidth, myappheight = myappright - myappleft, myappbot - myapptop
ismyappfullscreen = false

-- UI global composite font
local str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 .:;()?!-+/*"
local myttf = TTFont.new("fonts/Cabin-Regular-TTF.ttf", 2*8, str)
local myttfo = TTFont.new("fonts/Cabin-Regular-TTF.ttf", 2*8, str, 2, 3)
cf2 = CompositeFont.new {
	{ font=myttfo, color=0x2a2a2a, alpha=1 }, -- draw outline in dark
	{ font=myttf, x=0, y=0 }, -- draw normal text with an offset
}

-- global ui theme
application:setBackgroundColor(0x363124)
g_ui_theme = {}
g_ui_theme.pixelcolorup = 0x34484b
g_ui_theme.pixelcolordown = 0x3c4550
g_ui_theme.textcolorup = 0xb3b5b1
g_ui_theme.textcolordown = 0xdea77f
g_ui_theme._textcolordisabled = 0x87837f
g_ui_theme.tooltiptextcolor = 0xc7dbe7
g_ui_theme.exit = 0xf41212
g_ui_theme.tooltipoffsetx = -10*8
g_ui_theme.tooltipoffsety = 1*8

-- tweens table for transitions
tweens = {
	"inBack", -- 1
	"outBack", -- 2
	"inOutBack", -- 3
	"inBounce", -- 4
	"outBounce", -- 5
	"inOutBounce", -- 6
	"inCircular", -- 7
	"outCircular", -- 8
	"inOutCircular", -- 9
	"inCubic", -- 10
	"outCubic", -- 11
	"inOutCubic", -- 12
	"inElastic", -- 13
	"outElastic", -- 14
	"inOutElastic", -- 15
	"inExponential", -- 16
	"outExponential", -- 17
	"inOutExponential", -- 18
	"linear", -- 19
	"inQuadratic", -- 20
	"outQuadratic", -- 21
	"inOutQuadratic", -- 22
	"inQuartic", -- 23
	"outQuartic", -- 24
	"inOutQuartic", -- 25
	"inQuintic", -- 26
	"outQuintic", -- 27
	"inOutQuintic", -- 28
	"inSine", -- 29
	"outSine", -- 30
	"inOutSine", -- 31
}

the main.lua file

The main.lua file is the last file to be loaded when launching the game. This is where I like to set up the window parameters, put global functions and start the game.

The code:

-- windows title and position, winQt, win32
if not application:isPlayerMode() then
	application:set("windowTitle", "MY GAME")
	application:set("windowPosition",
		(screenwidth-myappwidth)*0.5, (screenheight-myappheight)*0.4
	)
--	application:enableDrawInfo(vector(1, 1, 0, 1)) -- for debuging
end

-- global prefs functions
function myLoadPrefs(xconfigfilepath)
	local mydata = getData(xconfigfilepath) -- try to read information from file
	if not mydata then -- if no prefs file, create it
		mydata = {}
		-- set prefs
		mydata.g_language = g_language
		mydata.g_difficulty = g_difficulty
		mydata.g_bgmvolume = g_bgmvolume
		mydata.g_sfxvolume = g_sfxvolume
		-- controls
		mydata.g_keyleft = g_keyleft
		mydata.g_keyright = g_keyright
		mydata.g_keyup = g_keyup
		mydata.g_keydown = g_keydown
		mydata.g_keyaction1 = g_keyaction1
		mydata.g_keyaction2 = g_keyaction2
		mydata.g_keyaction3 = g_keyaction3
		-- save prefs
		saveData(xconfigfilepath, mydata) -- create file and save datas
	else -- prefs file exists, use it!
		-- set prefs
		g_language = mydata.g_language
		g_difficulty = mydata.g_difficulty
		g_bgmvolume = mydata.g_bgmvolume
		g_sfxvolume = mydata.g_sfxvolume
		-- controls
		g_keyleft = mydata.g_keyleft
		g_keyright = mydata.g_keyright
		g_keyup = mydata.g_keyup
		g_keydown = mydata.g_keydown
		g_keyaction1 = mydata.g_keyaction1
		g_keyaction2 = mydata.g_keyaction2
		g_keyaction3 = mydata.g_keyaction3
	end
end

function mySavePrefs(xconfigfilepath)
	local mydata = {} -- clear the table
	-- set prefs
	mydata.g_language = g_language
	mydata.g_difficulty = g_difficulty
	mydata.g_bgmvolume = g_bgmvolume
	mydata.g_sfxvolume = g_sfxvolume
	-- controls
	mydata.g_keyleft = g_keyleft
	mydata.g_keyright = g_keyright
	mydata.g_keyup = g_keyup
	mydata.g_keydown = g_keydown
	mydata.g_keyaction1 = g_keyaction1
	mydata.g_keyaction2 = g_keyaction2
	mydata.g_keyaction3 = g_keyaction3
	-- save prefs
	saveData(xconfigfilepath, mydata) -- save new data
end

-- prefs file and initial default global prefs values
g_configfilepath = "|D|config.txt"
g_language = application:getLanguage()
g_currlevel = 1 -- 2, change here when you want to test a level
g_difficulty = 1 -- 0=easy, 1=normal, 2=hard
g_bgmvolume = 20 -- 0-100
g_sfxvolume = 20 -- 0-100
g_keyleft = KeyCode.LEFT
g_keyright = KeyCode.RIGHT
g_keyup = KeyCode.UP
g_keydown = KeyCode.DOWN
g_keyaction1 = KeyCode.C
g_keyaction2 = KeyCode.X
g_keyaction3 = KeyCode.W

-- load saved prefs from file (|D|config.txt)
myLoadPrefs(g_configfilepath)

-- ANIMATIONS, faster accessed via int than string
local i : number = 1
g_ANIM_DEFAULT = i
i += 1
g_ANIM_IDLE_R = i
i += 1
g_ANIM_WALK_R = i
i += 1
g_ANIM_RUN_R = i
i += 1
g_ANIM_JUMPUP_R = i
i += 1
g_ANIM_JUMPDOWN_R = i
i += 1
g_ANIM_LADDER_IDLE_R = i
i += 1
g_ANIM_LADDER_UP_R = i
i += 1
g_ANIM_LADDER_DOWN_R = i
i += 1
g_ANIM_WALL_IDLE_R = i
i += 1
g_ANIM_WALL_UP_R = i
i += 1
g_ANIM_WALL_DOWN_R = i
i += 1
g_ANIM_WIN_R = i
i += 1
g_ANIM_HURT_R = i
i += 1
g_ANIM_LOSE1_R = i
i += 1
g_ANIM_STANDUP_R = i

-- global functions
function switchToScene(xscene) -- scenes transition
	-- unload current assets
	for i = stage:getNumChildren(), 1, -1 do
		stage:getChildAt(i):removeAllListeners() -- doublon of stage:removeAllListeners?
		stage:removeChildAt(i)
	end
	stage:removeAllListeners()
	collectgarbage()
	-- transition to next scene (eg.: Menu.new())
	stage:addChild(Transitions.new(xscene))
end

-- start the app
local logo = Bitmap.new(Texture.new("gfx/logo.png"))
logo:setAnchorPoint(0.5, 0.5)
logo:setPosition(myappwidth/2+myappleft, myappheight/2)
stage:addChild(logo)

-- preloader
local function preloader() -- loading textures and sounds when app is starting
	stage:removeEventListener(Event.ENTER_FRAME, preloader)
	-- timer
	local timer = Timer.new(1000, 1) -- show logo duration, you choose
	timer:addEventListener(Event.TIMER, function()
		switchToScene(Menu.new()) -- Menu, LevelX, Options, Win
	end) timer:start()
end
stage:addEventListener(Event.ENTER_FRAME, preloader)

main.lua Code comments

The global prefs functions are used to save the user's preferences like key mapping, sound volume and difficulty.

Before the game is launched, the app reads the file and sets the stored values. If there is no file preferences yet, we set the preferences with initial default values.

The switchToScene function is used to switch between scenes. It unloads the previous scene and transitions to the new one.

Then there are some global variables specific to the game: all the animations for our actors (g_ANIM_DEFAULT, g_ANIM_IDLE_R, ...). The _R suffix indicates our sprites are facing Right. Here we only need right facing animations but you may easily add left animations with an _L suffix.

Finally we start the app with a logo transitioning to the Menu scene.

Next?

We quickly go through what is already in the Gideros Game Template1 tutorial. The next part will do this as well: transitions, menu and options scenes.


Prev.: Tuto tiny-ecs 2d platformer Part 1 Setup
Next: Tuto tiny-ecs 2d platformer Part 3 transitions menu options


Tutorial - tiny-ecs 2d platformer