Tuto Gideros Game Template1 Part 3 Main

From GiderosMobile
Revision as of 19:27, 25 October 2024 by MoKaLux (talk | contribs) (Created page with "__TOC__ == the main.lua file == In the ''main.lua'' file we will start shaping our game, that is: * give our window a title and position * configure our save system: the actua...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

the main.lua file

In the main.lua file we will start shaping our game, that is:

  • give our window a title and position
  • configure our save system: the actual data (game difficulty, sound levels, player controls) and the load and save functions
  • a preloader to show a splash screen/logo. It can also be used to preload assets
  • finally we "launch" the game

The code:

-- windows title and position
if not application:isPlayerMode() then
	application:set("windowTitle", "GIDEROS GAME TEMPLATE")
	application:set("windowPosition", (screenwidth - myappwidth) * 0.5, (screenheight - myappheight) * 0.4)
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_currlevel = g_currlevel
		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_currlevel = mydata.g_currlevel
		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_currlevel = g_currlevel
	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|configfile.txt"
g_language = application:getLanguage()
g_totallevel = 3
g_currlevel = 1
g_difficulty = 1 -- 0=easy, 1=normal, 2=hard
g_bgmvolume = 50 -- 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.D -- eg.: punch1
g_keyaction2 = KeyCode.S -- eg.: punch2
g_keyaction3 = KeyCode.Q -- eg.: jump/jump punch1

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

-- ************
-- APP LOADING
-- ************
local logo = Bitmap.new(Texture.new("gfx/logo.png"))

local function drawLogo()
	logo:setAnchorPoint(0.5, 0.5)
	logo:setPosition(myappwidth/2+myappleft, myappheight/2)
	stage:addChild(logo)
end

local function preloader() -- loading textures and sounds when app is starting
	stage:removeEventListener(Event.ENTER_FRAME, preloader)
	-- you can preload your assets here or preload them calling a Class function eg.:
--	MenuScene.preload()
--	SoundManager.preload()
	-- timer
	local timer = Timer.new(3000, 1) -- show logo: duration
	timer:addEventListener(Event.TIMER, function()
		stage:removeAllListeners()
		for i = stage:getNumChildren(), 1, -1 do
			stage:removeChildAt(i)
		end collectgarbage()
		stage:addChild(Transitions.new(Menu.new())) -- next scene
	end)
	timer:start()
end

-- start
drawLogo()
stage:addEventListener(Event.ENTER_FRAME, preloader)

Code comments

There is no particular way to write your code. I tried to put each section where it made sense. It is important to write comments to separate each part of the code.

prefs file and initial default global prefs values

All are global variables because they will be used in many scenes of our game.

Please note that not all those variables will be saved to the prefs file, for example it makes sense to save the new key mapping for the player and the sound volume to file.

You need to add the variables that will be saved to file in the load and save functions. If it is the first time the game is launched then default values are assigned and used, eg.: g_difficulty = 1, g_bgmvolume = 50, g_keyleft = KeyCode.LEFT, ...

App loading

We set up a logo for our game, it can be your studio logo or anything you like.

Then there are those two functions to load our logo and a preloader function where we show our logo for a couple of seconds. You could also preload assets in the preloader function.

When the timer ends we load the next scene using some transitions (please note the next scene doesn't exist yet!)

Start the app

Finally we can start the app by calling the two functions above: drawLogo() and preloader()

Next?

We are ready to create the scenes for our game. Let's start with the transition scene.


Prev.: Tuto Gideros Game Template1 Part 2 Init
Next: Tuto Gideros Game Template1 Part 4 Transitions


Tutorial - Gideros Game Template1