-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsummoning.lua
More file actions
194 lines (175 loc) · 6.34 KB
/
summoning.lua
File metadata and controls
194 lines (175 loc) · 6.34 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
require("game.state")
require("game.conf")
require("game.utils")
game.tilemap = game.tilemap or require("game.tilemap")
game.summoning = {}
--- callback when the game loads
function game.summoning.load()
audioGarry = {
love.audio.newSource("assets/sfx/soundSummon.mp3", "stream"),
love.audio.newSource("assets/sfx/Garry_spawn_1.mp3", "stream"),
love.audio.newSource("assets/sfx/Garry_spawn_2.mp3", "stream"),
love.audio.newSource("assets/sfx/Garry_spawn_3.mp3", "stream"),
love.audio.newSource("assets/sfx/Garry_spawn_4.mp3", "stream"),
}
for _, audio in ipairs(audioGarry) do
audio:setVolume(game.conf.volume.voices)
end
audioZombie = {
love.audio.newSource("assets/sfx/Zombie_Spawn_1.mp3", "stream"),
love.audio.newSource("assets/sfx/Zombie_Spawn_2.1.mp3", "stream"),
}
for _, audio in ipairs(audioZombie) do
audio:setVolume(game.conf.volume.voices)
end
audioFea = {
love.audio.newSource("assets/sfx/Fee_Spawn_1.mp3", "stream"),
love.audio.newSource("assets/sfx/Fee_Spawn_2.mp3", "stream"),
love.audio.newSource("assets/sfx/Fee_Spawn_3.mp3", "stream"),
}
for _, audio in ipairs(audioFea) do
audio:setVolume(game.conf.volume.voices)
end
-- initialize empty state where nothing can be summoned
game.state.summoning = {
isSummoning = false,
types = {},
}
end
--- callback to update game state
function game.summoning.update(dt) end
--- callback to draw the current game state
function game.summoning.draw()
if game.state.summoning.isSummoning then
local rowHeight = game.conf.ui.summoning.rowHeight
local tableHeight = (rowHeight + 1) * #game.state.summoning.types
local tableWidth = game.conf.ui.summoning.rowWidth
local titleHeight = game.conf.ui.summoning.titleHeight
local menuBorder = game.conf.ui.summoning.border
local menuHeight = tableHeight + titleHeight + menuBorder * 2
local menuWidth = tableWidth + menuBorder * 2
local x1 = love.graphics.getWidth() / 2 - menuWidth / 2
local y1 = love.graphics.getHeight() / 2 - menuHeight / 2
-- Draw Menu Frame
love.graphics.setColor(1, 1, 1)
love.graphics.rectangle("fill", x1, y1, menuWidth, menuHeight)
love.graphics.setColor(0, 0, 0)
love.graphics.rectangle("fill", x1 + menuBorder, y1 + menuBorder, tableWidth, tableHeight + titleHeight)
-- Print Title
local titleFont = titleHeight * 0.8
local titleMargin = titleHeight * 0.05
love.graphics.setColor(1, 1, 1)
love.graphics.setNewFont(titleFont)
love.graphics.printf(
game.conf.ui.summoning.menuTitle,
x1 + menuBorder,
y1 + menuBorder + titleMargin,
tableWidth,
"center"
)
-- Print Minion Table
local tableX = x1 + menuBorder
local tableY = y1 + menuBorder + titleHeight
love.graphics.setNewFont(rowHeight * 0.6)
local fontOffset = rowHeight * 0.2
local keyNumberWidth = rowHeight
local availableWidth = 80
for i, v in ipairs(game.state.summoning.types) do
local minionState = game.state.summoning.types[i]
local minionType = game.conf.minions.presets[minionState.presetId]
local yOffset = (rowHeight + 1) * (i - 1)
local textColor = { 1, 1, 1 }
if minionState.summoned == minionState.totalAvailable then
textColor = { 0.5, 0.5, 0.5 }
end
love.graphics.line(tableX, tableY + yOffset, tableX + tableWidth, tableY + yOffset)
love.graphics.printf({ textColor, i }, tableX, tableY + yOffset + fontOffset, keyNumberWidth, "center")
love.graphics.printf(
{ textColor, minionType.name },
tableX + keyNumberWidth,
tableY + yOffset + fontOffset,
tableWidth - keyNumberWidth - availableWidth,
"left"
)
love.graphics.printf(
{ textColor, tostring(minionState.totalAvailable - minionState.summoned) .. " left" },
tableX + tableWidth - availableWidth - fontOffset,
tableY + yOffset + fontOffset,
availableWidth,
"right"
)
end
end
end
--- callback when a key is pressed
function game.summoning.keypressed(key, scancode, isrepeat)
if scancode == "q" then
game.state.summoning.isSummoning = not game.state.summoning.isSummoning
print("show summoning menu: " .. tostring(game.state.summoning.isSummoning))
end
local number = tonumber(key)
if game.state.summoning.isSummoning and number and number > 0 and number <= #game.state.summoning.types then
-- check if there are still enough summons left and do the summoning
local minionState = game.state.summoning.types[number]
if minionState.summoned < minionState.totalAvailable then
game.state.summoning.isSummoning = false
minionState.summoned = minionState.summoned + 1
game.minions.summon(minionState.presetId, game.summoning.getSummonLocation())
if minionState.presetId == "homunculus" then
local randomIndex = love.math.random(1, #audioGarry)
audioGarry[randomIndex]:stop()
audioGarry[randomIndex]:play()
end
if minionState.presetId == "fae" then
local randomIndex = love.math.random(1, #audioFea)
audioFea[randomIndex]:stop()
audioFea[randomIndex]:play()
end
if minionState.presetId == "zombie" then
local randomIndex = love.math.random(1, #audioZombie)
audioZombie[randomIndex]:stop()
audioZombie[randomIndex]:play()
end
end
end
end
--- callback when the level with the given index is loaded
function game.summoning.loadLevel(id)
-- reset state
game.state.summoning.isSummoning = false
game.state.summoning.types = {}
-- rebuild state to store summonable minion types
print("loading summoning options for level " .. tostring(id))
for presetId, amount in pairs(game.conf.level_sequence[id].minions) do
if amount > 0 then
print(" " .. amount .. " " .. presetId)
table.insert(game.state.summoning.types, {
presetId = presetId,
summoned = 0,
totalAvailable = amount,
})
end
end
end
--- get the default summoning location for the current level
function game.summoning.getSummonLocation()
return game.tilemap.getSpawn()
end
--- refresh one summonable minion of the given type
---
--- Parameters:
--- presetId: The id of a preset defined in game.conf.minions.presets
function game.summoning.refreshSummon(presetId)
print("refreshing 1 summon of preset " .. presetId)
for _, iTyp in pairs(game.state.summoning.types) do
if iTyp.presetId == presetId then
if iTyp.summoned > 0 then
iTyp.summoned = iTyp.summoned - 1
else
error("Cannot refresh summon because no minions have been summoned")
end
return
end
end
error("presetId " .. tostring(presetId) .. " not available in current level")
end