-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
117 lines (105 loc) · 3.62 KB
/
main.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
-- PlaneChase : an app to play PlaneChase with your friends
-- See https://mtg.fandom.com/wiki/Planechase_(format)
-- Load assets in the beginning of the game
function love.load()
-- Load the DejaVuSans font, big size
font = love.graphics.newFont("DejaVuSans.ttf", 40)
love.graphics.setFont(font)
-- Configuration
love.window.setTitle("Plane Chase for Magic the Gathering ~ By Elliot & Lilian")
love.graphics.setBackgroundColor(1, 1, 1)
love.math.setRandomSeed(os.time())
-- Get all the images in the images/planes/ directory
local files = love.filesystem.getDirectoryItems("images/planes")
images = {}
for _, image in ipairs(files) do
print("Loading", image)
-- if isJPG(image) or isPNG(image) then
if isJPG(image) then
table.insert(images, love.graphics.newImage("images/planes/" .. image))
end
end
if #images > 0 then
imageActuelle = getRandomPlaneImage()
end
die = "rien"
dieSize = 0.30
planeChaseDiceImage = love.graphics.newImage("images/dice/planechase.png")
chaosDiceImage = love.graphics.newImage("images/dice/chaos.png")
end
-- Update the game, every frame, if needed
function love.update(delta_time)
-- TODO: maybe we will need a love.update(dt) ?
end
-- Draw the game, for each new frame
function love.draw()
-- Draw the image of the current plane
if #images > 0 then
love.graphics.draw(imageActuelle, love.graphics.getWidth() - 100, 100, math.pi/2)
end
-- Print the result of the dice
love.graphics.setColor(0, 0, 0)
love.graphics.print("Dé :", love.graphics.getWidth()/2 - 60, 30)
love.graphics.setColor(1, 1, 1)
if die == "planechase" then
love.graphics.draw(planeChaseDiceImage, love.graphics.getWidth() / 2 + 20, 10, 0, dieSize, dieSize)
elseif die == "chaos" then
love.graphics.draw(chaosDiceImage, love.graphics.getWidth() / 2 + 20, 10, 0, dieSize, dieSize)
end
end
function love.keypressed(key)
if key == "escape" then
print("Exit the app with escape.")
love.event.quit(0)
end
if key == "right" then
print("Next plane, with right arrow.")
if #images > 0 then
imagePrecedente = imageActuelle
imageActuelle = getRandomPlaneImage()
die = "rien"
end
end
if key == "left" then
-- TODO: this "last plane" feature should use a stack of previously seen planes, not just one!
print("Last plane, with right arrow.")
if #images > 0 then
local tmp = imagePrecedente
imagePrecedente = imageActuelle
imageActuelle = tmp
die = "rien"
end
end
if key == "space" then
die = rollChaosDie()
end
end
-- Tiny fonction, to check if a given filename ends with ".jpg"
function isJPG(filename)
return string.sub(filename, -4) == ".jpg"
end
-- Tiny fonction, to check if a given filename ends with ".png"
function isPNG(filename)
return string.sub(filename, -4) == ".png"
end
-- Go visit a new random plane, possibly the same
-- TODO: improve this! we should NOT be able to go back to a plane we already visisted
function getRandomPlaneImage()
local new_image = images[love.math.random(1, #images)]
while new_image == imageActuelle do
new_image = images[love.math.random(1, #images)]
end
return new_image
end
-- Roll the chaos dice
function rollChaosDie()
local n = love.math.random(6)
local result = "rien"
if n == 1 then
result = "chaos"
elseif n == 6 then
result = "planechase"
end
print("Planar dice :", result)
return result
end