-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameMap.lua
More file actions
236 lines (212 loc) · 6.37 KB
/
Copy pathgameMap.lua
File metadata and controls
236 lines (212 loc) · 6.37 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
local resources = require("resources")
local input = require("input")
local utility = require("utility")
local warpDrive = resources.sounds.warpDrive
--[[
Game Map module.
Exposes functions for creating map nodes and game maps.
]]
local gameMap = {}
function gameMap.init(enterCombat, enterCity, enterMenu, enterTextScene)
gameMap.enterCombat = enterCombat
gameMap.enterCity = enterCity
gameMap.enterMenu = enterMenu
gameMap.enterTextScene = enterTextScene
end
--[[
Local fields and functions.
]]
local imgStarMap = resources.images.starMap
local nodeHighlight = resources.images.nodeHighlight
local yellowNode = resources.images.yellowNode
local blueNode = resources.images.blueNode
local redNode = resources.images.redNode
local greenNode = resources.images.greenNode
local darkNode = resources.images.darkNode
local mermaidShip = resources.images.mermaidShip
local nodeSize = 32
local nodeRadius = nodeSize / 2
local nodeClickRadius = nodeRadius + 8
--[[
MapNode class.
A node on the map. Has an id and links to other nodes.
Has x and y positions for drawing a representation of the map.
]]
local MapNode = {}
MapNode.type = "normal"
MapNode.id = 0
MapNode.links = {}
function MapNode:new(id, name, xPos, yPos, links)
local o = {id = id, name = name, xPos = xPos, yPos = yPos, links = links}
setmetatable(o, self)
self.__index = self
return o
end
--[[
GameMap class.
A collection of nodes.
Keeps track of the current node and allows movement to other linked nodes.
]]
local GameMap = {}
GameMap.nodes = {}
GameMap.buttons = {
utility.UI.newButton(
40,
700,
"Menu",
true,
true,
function()
gameMap.enterMenu()
end,
"smallFont"
)
}
function GameMap:new(nodes)
local o = {nodes = nodes}
o.currentNode = o.nodes[1]
setmetatable(o, self)
self.__index = self
return o
end
function GameMap:moveToNode(node)
if self.currentNode.links[node.id] then
self.currentNode = node
return true
end
return false
end
function GameMap:showBoss()
local normalStars = {}
for i = 1, table.getn(self.nodes) do
if self.nodes[i].type == "normal" then
table.insert(normalStars, self.nodes[i])
end
end
normalStars[math.random(1, table.getn(normalStars))].type = "boss"
end
local function getEncounterType(node)
local roll = math.random(1, 100)
if node.type == "city" then
return "city"
elseif node.type == "normal" then
if roll < 51 then
return "decision"
elseif roll > 90 then
return "merchantShip"
else
return "pirate"
end
elseif node.type == "dangerZone" then
if roll < 61 then
return "highLevelPirate"
else
return "dangerousDecision"
end
elseif node.type == "key" then
return "key"
elseif node.type == "boss" then
return "boss"
end
end
local function enterScene(node)
local type = getEncounterType(node)
if type == "pirate" then
gameMap.enterCombat("pirate")
elseif type == "highLevelPirate" then
gameMap.enterCombat("highLevelPirate")
elseif type == "merchantShip" then
gameMap.enterCombat("merchantShip")
elseif type == "key" then
gameMap.enterCombat("keyStarPirate")
elseif type == "boss" then
gameMap.enterCombat("boss")
elseif type == "city" then
gameMap.enterCity(node)
elseif type == "decision" then
gameMap.enterTextScene("normal")
elseif type == "dangerousDecision" then
gameMap.enterTextScene("dangerous")
end
end
function GameMap:update(dt)
utility.UI.updateButtons(self.buttons)
self.hoveredNode = nil
for i = 1, table.getn(self.nodes) do
local node = self.nodes[i]
local rect = {
xPos = node.xPos - nodeClickRadius,
yPos = node.yPos - nodeClickRadius,
width = nodeClickRadius * 2,
height = nodeClickRadius * 2
}
if input.mouseOver(rect) then
self.hoveredNode = self.nodes[i]
break
end
end
if self.hoveredNode and input.getLeftClick() then
local canTravel = false
for i = 1, table.getn(self.currentNode.links) do
if self.currentNode.links[i] == self.hoveredNode.id then
canTravel = true
break
end
end
if canTravel then
resources.playSound(warpDrive)
self.currentNode = self.hoveredNode
enterScene(self.currentNode)
end
end
end
function GameMap:draw()
love.graphics.draw(imgStarMap, 0, 0)
utility.UI.drawButtons(self.buttons)
for i = 1, table.getn(self.nodes) do
local node = self.nodes[i]
if node.type == "city" then
love.graphics.draw(blueNode, node.xPos - nodeRadius, node.yPos - nodeRadius)
elseif node.type == "dangerZone" then
love.graphics.draw(redNode, node.xPos - nodeRadius, node.yPos - nodeRadius)
elseif node.type == "boss" then
love.graphics.draw(darkNode, node.xPos - nodeRadius, node.yPos - nodeRadius)
else
love.graphics.draw(greenNode, node.xPos - nodeRadius, node.yPos - nodeRadius)
end
end
local x, y = input.getMouse()
if self.hoveredNode then
love.graphics.draw(
nodeHighlight,
self.hoveredNode.xPos - nodeRadius - 2,
self.hoveredNode.yPos - nodeRadius - 2
)
resources.printWithFont(
"smallFont",
function()
local node = self.hoveredNode
local name = node.name
if node.type == "dangerZone" then
name = "Danger Zone"
elseif node.type == "city" then
name = "Merchant City"
elseif node.type == "beacon" then
name = "Distress Beacon"
end
love.graphics.print(name, 750, 20)
end
)
end
love.graphics.draw(mermaidShip, self.currentNode.xPos, self.currentNode.yPos, 0, 0.1, 0.1)
end
--[[
Module interface.
]]
function gameMap.newMapNode(id, name, xPos, yPos, links)
return MapNode:new(id, name, xPos, yPos, links)
end
function gameMap.newGameMap(nodes)
return GameMap:new(nodes)
end
return gameMap