-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilding.lua
More file actions
150 lines (124 loc) · 4.18 KB
/
Copy pathbuilding.lua
File metadata and controls
150 lines (124 loc) · 4.18 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
local utility = require("utility")
local Building = {}
Building.__index = Building
function Building.new(x,y,width,height)
local self = setmetatable({}, Building)
self.type = "building"
self.x = x or 200
self.y = y or 100
self.initialx = x or 200
self.initialy = y or 100
self.width = width or 75
self.height = height or 50
self.color = {0.2,0.2,0.2}
self.selected = false
return self
end
function Building:update(dt)
--No update on default buildings
end
function Building:draw()
love.graphics.setColor(self.color)
love.graphics.rectangle("fill",self.x,self.y,self.width,self.height)
end
function Building:resize(w,h)
-- self.x = utility.clamp(initialx,0,w)
-- self.y = utility.clamp(initialy,0,h)
-- self.x = utility.clamp(self.initialx,0,w - self.width)
-- self.y = utility.clamp(self.initialy,0,h - self.height)
end
function Building:handleClick(x, y)
if self:isInside(x, y) then
for _,b in ipairs(buildings) do
b.selected = false
end
self.selected = not self.selected
return true
else
self.selected = false
return false
end
end
function Building:isInside(x, y)
return x >= self.x and x <= self.x + self.width and y >= self.y and y <= self.y + self.height
end
function Building:drawMenu()
if not self.selected then
return
end
local menuX = self.x + self.width + 5
local menuY = self.y
local menuWidth = 160
local menuHeight = 60
-- clamp so it stays onscreen
local screenW, screenH = love.graphics.getWidth(), love.graphics.getHeight()
if menuX + menuWidth > screenW then
menuX = self.x - menuWidth - 5 -- shift to left of building
end
if menuY + menuHeight > screenH then
menuY = screenH - menuHeight - 5 -- shift up a bit
end
if menuY < 0 then
menuY = 5
end
-- background
love.graphics.setColor(0, 0, 0, 0.8)
love.graphics.rectangle("fill", menuX, menuY, menuWidth, menuHeight, 4, 4)
-- outline
love.graphics.setColor(1, 1, 1)
love.graphics.rectangle("line", menuX, menuY, menuWidth, menuHeight, 4, 4)
-- clear buttons for this frame
self.menuButtons = {}
-- option 1: human spawner
love.graphics.print("Survivor", menuX + 8, menuY + 8)
local spawnBtnX = menuX + 80
local spawnBtnY = menuY + 5
local spawnBtnW = 60
local spawnBtnH = 20
if suppliesCollected >= spawnerCost then
love.graphics.setColor(1, 1, 1)
else
love.graphics.setColor(0.5, 0.5, 0.5) -- gray if can’t afford
end
love.graphics.rectangle("line", spawnBtnX, spawnBtnY, spawnBtnW, spawnBtnH)
love.graphics.print(spawnerCost, spawnBtnX + 5, spawnBtnY + 3)
table.insert(self.menuButtons, {
x = spawnBtnX, y = spawnBtnY,
w = spawnBtnW, h = spawnBtnH,
action = "buySpawner",
enabled = (suppliesCollected >= spawnerCost)
})
-- option 2: police station
love.graphics.print("Police", menuX + 8, menuY + 30)
local policeBtnX = menuX + 80
local policeBtnY = menuY + 27
local policeBtnW = 60
local policeBtnH = 20
love.graphics.rectangle("line", policeBtnX, policeBtnY, policeBtnW, policeBtnH)
love.graphics.print(policeCost, policeBtnX + 5, policeBtnY + 3)
table.insert(self.menuButtons, {
x = policeBtnX, y = policeBtnY,
w = policeBtnW, h = policeBtnH,
action = "buyPolice",
enabled = (suppliesCollected >= policeCost)
})
if suppliesCollected >= policeCost then
love.graphics.setColor(1, 1, 1)
else
love.graphics.setColor(0.5, 0.5, 0.5)
end
-- reset color
love.graphics.setColor(1, 1, 1)
end
function Building:handleMenuClick(mx, my)
if not self.menuButtons then return nil end
for _, btn in ipairs(self.menuButtons) do
if mx >= btn.x and mx <= btn.x + btn.w and
my >= btn.y and my <= btn.y + btn.h and btn.enabled then
self.selected = false
return btn.action
end
end
return nil
end
return Building