Skip to content

Commit f02b4ec

Browse files
committed
Committing the output from the examples.
1 parent 966d048 commit f02b4ec

File tree

5 files changed

+206
-0
lines changed

5 files changed

+206
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@
44
!/test.lua2p
55

66
/examples/*.lua
7+
!/examples/*.output.lua
8+
79
/local/

examples/namedConstants.output.lua

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
--[[============================================================
2+
--=
3+
--= LuaPreprocess example: Named constants.
4+
--=
5+
--= Here we use named constants in the code but the final
6+
--= program will only have literal values.
7+
--=
8+
--============================================================]]
9+
10+
function newAnimation(totalDuration, name)
11+
name = name or "my_animation"
12+
13+
local animation = {}
14+
15+
animation.name = name
16+
animation.totalDuration = totalDuration
17+
animation.currentPosition = 0
18+
19+
return animation
20+
end
21+
22+
function updateAnimation(animation, deltaTime)
23+
local deltaPosition = deltaTime * 2
24+
25+
animation.currentPosition = (animation.currentPosition + deltaPosition) % animation.totalDuration
26+
end
27+
28+
function testAnimationStuff()
29+
local animation = newAnimation(5)
30+
31+
for i = 1, 5 do
32+
updateAnimation(animation, 0.1)
33+
34+
print(animation.name.." position: "..animation.currentPosition)
35+
end
36+
end
37+
38+
testAnimationStuff()
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
--[[============================================================
2+
--=
3+
--= LuaPreprocess example: Optimize data access.
4+
--=
5+
--= Here we have all data defined in one single place in the
6+
--= metaprogram, then we export parts of the data into multiple
7+
--= smaller tables that are easy and fast to access in the
8+
--= final program.
9+
--=
10+
--============================================================]]
11+
12+
-- Array of character IDs, excluding special characters if we're not in developer mode.
13+
CHARACTER_IDS = {
14+
"war1",
15+
"war2",
16+
"mage1",
17+
"mage2",
18+
"arch1",
19+
"arch2",
20+
}
21+
22+
-- Maps between character IDs and other parameters.
23+
CHARACTER_NAMES = {
24+
war1 = "Steve",
25+
war2 = "Bog",
26+
mage1 = "Elise",
27+
mage2 = "Cyan",
28+
arch1 = "Di",
29+
arch2 = "#&%€",
30+
dev = "Dev",
31+
}
32+
CHARACTER_TYPES = {
33+
war1 = "warrior",
34+
war2 = "warrior",
35+
mage1 = "mage",
36+
mage2 = "mage",
37+
arch1 = "archer",
38+
arch2 = "archer",
39+
dev = "dev",
40+
}
41+
CHARACTERS_UNLOCKED_BY_DEFAULT = {
42+
war1 = true,
43+
mage1 = true,
44+
arch1 = true,
45+
dev = true,
46+
}
47+
48+
-- Instead of iterating over the CHARACTERS array until we find
49+
-- the character with the specified ID, we use the maps above to
50+
-- get the information we want through a single table lookup.
51+
function getCharacterName(charId)
52+
return CHARACTER_NAMES[charId]
53+
end
54+
function getCharacterType(charId)
55+
return CHARACTER_TYPES[charId]
56+
end
57+
function isCharacterUnlockedByDefault(charId)
58+
return CHARACTERS_UNLOCKED_BY_DEFAULT[charId] == true
59+
end
60+
61+
function printCharacterInfo()
62+
for _, charId in ipairs(CHARACTER_IDS) do
63+
print(getCharacterName(charId))
64+
print(" Type ...... "..getCharacterType(charId))
65+
print(" Unlocked .. "..tostring(isCharacterUnlockedByDefault(charId)))
66+
end
67+
end
68+
69+
printCharacterInfo()
70+
print("Type of 'war1': "..getCharacterType("war1"))
71+
print("Is 'mage2' unlocked by default: "..tostring(isCharacterUnlockedByDefault("mage2")))

examples/parseFile.output.lua

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
--[[============================================================
2+
--=
3+
--= LuaPreprocess example: Pre-parse a data file.
4+
--=
5+
--= Here we convert a JSON data string to a more appropriate
6+
--= data format in the metaprogram. The final program will not
7+
--= contain anything related to JSON - just a nice Lua table
8+
--= literal with all data.
9+
--=
10+
--============================================================]]
11+
12+
-- Metaprogram.
13+
--==============================================================
14+
15+
16+
17+
-- The program.
18+
--==============================================================
19+
20+
local characters = {{actions={{slot="left",title="Slash"},{slot="right",title="Block"}},id=1,name="Warrior",type="melee"},{actions={{slot="left",title="Fireball"},{slot="right",title="Illuminate"},{slot="familiar",title="Swoop"}},id=2,name="Spell Caster",type="magic"}}
21+
22+
function printAvailableCharacters()
23+
print("Available characters:")
24+
25+
for i, character in ipairs(characters) do
26+
print(string.format(
27+
"%d. %s (type: %s, actions: x%d)",
28+
i,
29+
character.name,
30+
character.type,
31+
#character.actions
32+
))
33+
end
34+
end
35+
36+
printAvailableCharacters()
37+
38+
--==============================================================
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
--[[============================================================
2+
--=
3+
--= LuaPreprocess example: Selective functionality.
4+
--=
5+
--= Here we decide what code should be included and run in the
6+
--= final program with some flags set in the metaprogram.
7+
--=
8+
--============================================================]]
9+
10+
quitGame = false
11+
12+
function addNormalLevelsToArray(levels) print("Adding normal levels") end
13+
function addTestLevelsToArray(levels) print("(*) Adding test levels") end
14+
15+
function getPlayableLevels()
16+
local levels = {}
17+
addNormalLevelsToArray(levels)
18+
19+
return levels
20+
end
21+
22+
function loadAssets() print("Loading assets") end
23+
function showLevelsToPlayer(levels) print("Showing levels to player") end
24+
function readInput() print("Reading input") end
25+
function updateGameState() print("Updating game state") end
26+
function render() print("Rendering") end
27+
28+
function initConsole() print("(*) Initting console") end
29+
function updateConsole() print("(*) Updating console") end
30+
31+
function runGame()
32+
print("Starting game")
33+
34+
loadAssets()
35+
36+
initConsole()
37+
38+
local levels = getPlayableLevels()
39+
showLevelsToPlayer(levels)
40+
41+
-- Main game loop.
42+
repeat
43+
readInput()
44+
45+
updateConsole()
46+
47+
updateGameState()
48+
render()
49+
50+
-- In this example, don't run the loop forever!
51+
quitGame = true
52+
until quitGame
53+
54+
print("Quitting game")
55+
end
56+
57+
runGame()

0 commit comments

Comments
 (0)