-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
executable file
·284 lines (264 loc) · 9.58 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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
local orders = require"orders"
function love.load()
ui = require"ui"
camera = require"lib/camera"
vector = require"lib/vector"
local ship = require "ship"
local waypoint = require "waypoint"
local projectile = require "projectile"
local panels = require "panels"
local panel_elements = require"panel_elements"
local turret = require"turret"
require"inputcallbacks"
love.graphics.setLineWidth(1.5)
game = {}
game.things = {}
game.selected = {}
game.manualship = nil
game.paused = false
game.wallthickness = 100
game.worldemptyoutside = 100
game.collgroups = {}
game.collgroups.walls = 1
game.collgroups.ships = 2
game.collgroups.projectiles = 4
game.collgroups.other = 8
--Collision rules: Projectiles pass through walls.
--Nothing of the same player collides with itself.
game.selstartx = 0
game.selstarty = 0
game.mousepressed = false
game.cam = camera(vector(0,0),1,0)
game.cam:attach()
game.startscenario = "scenarios/testscenario.lua"
function game.makeworld()
game.world = love.physics.newWorld(game.worldminx - game.wallthickness - game.worldemptyoutside,game.worldminy - game.wallthickness - game.worldemptyoutside,game.worldmaxx + game.wallthickness + game.worldemptyoutside,game.worldmaxy + game.wallthickness + game.worldemptyoutside)
game.world:setCallbacks(add, persist, rem)
game.static = love.physics.newBody(game.world,0,0,0,0)
game.topwall = love.physics.newRectangleShape(game.static, 0, game.worldminy - game.wallthickness/2 + 1, game.worldmaxx - game.worldminx + game.wallthickness * 2 - 2, game.wallthickness - 1, 0)
game.leftwall = love.physics.newRectangleShape(game.static, game.worldminx - game.wallthickness/2+1, 0, game.wallthickness - 1, game.worldmaxy - game.worldminy + game.wallthickness * 2 - 2, 0)
game.bottomwall = love.physics.newRectangleShape(game.static, 0, game.worldmaxy + game.wallthickness/2 - 1, game.worldmaxx - game.worldminx + game.wallthickness * 2 - 2, game.wallthickness - 1, 0)
game.rightwall = love.physics.newRectangleShape(game.static, game.worldmaxx + game.wallthickness/2 - 1, 0, game.wallthickness - 1, game.worldmaxy - game.worldminy + game.wallthickness*2 - 2, 0)
game.topwall:setData("wall")
game.bottomwall:setData("wall")
game.leftwall:setData("wall")
game.rightwall:setData("wall")
game.topwall:setFilterData(game.collgroups.walls, game.collgroups.ships, 0)
game.bottomwall:setFilterData(game.collgroups.walls, game.collgroups.ships, 0)
game.leftwall:setFilterData(game.collgroups.walls, game.collgroups.ships, 0)
game.rightwall:setFilterData(game.collgroups.walls, game.collgroups.ships, 0)
end
love.filesystem.load(game.startscenario)()
--[[
local modes = love.graphics.getModes()
for k,v in pairs(modes) do
print("w=" .. v.width .. " h=" .. v.height)
end
--]]
function game:drawthings()
for k,v in pairs(self.things) do
if v.isalive then
v:draw()
end
end
end
function game:drawselected()
for k,v in pairs(self.selected) do
v:drawselected()
end
end
function game:camupdate(dt)
if game.manualship and ui.followmanualship then
game.cam.pos.x = game.manualship.body:getX()
game.cam.pos.y = game.manualship.body:getY()
game.cam.rot = -game.manualship.body:getAngle() - math.pi/2
end
end
function game:drawwalls()
local Ul = game.cam:cameraCoords(game.worldminx,game.worldminy)
local Ur = game.cam:cameraCoords(game.worldmaxx, game.worldminy)
local Dr = game.cam:cameraCoords(game.worldmaxx,game.worldmaxy)
local Dl = game.cam:cameraCoords(game.worldminx, game.worldmaxy)
love.graphics.setColor(ui.wallcolour)
love.graphics.line(Ul.x, Ul.y, Ur.x, Ur.y, Dr.x, Dr.y, Dl.x, Dl.y, Ul.x, Ul.y)
end
function game:drawselbox()
if game.mousepressed then
love.graphics.setColor(ui.selboxcolour)
love.graphics.rectangle( "line", game.selstartx, game.selstarty, love.mouse:getX() - game.selstartx, love.mouse:getY() - game.selstarty)
end
end
function game:draw()
game:drawwalls()
game:drawthings()
game:drawselected()
game:drawselbox()
end
function game:clickdown(x, y, button)
if button == "l" then
game.mousepressed = true
game.selstartx = x
game.selstarty = y
end
end
function game:clickup(x, y, button)
if button == "l" then
game.selected = nil
game.selected = {}
game.mousepressed = false
local selstart = game.cam:worldCoords(game.selstartx, game.selstarty)
local selend = game.cam:worldCoords(x,y)
--constrain to within the active area
if selstart.x > game.worldmaxx then selstart.x = game.worldmaxx end
if selstart.x < game.worldminx then selstart.x = game.worldminx end
if selstart.y > game.worldmaxy then selstart.y = game.worldmaxy end
if selstart.y < game.worldminy then selstart.y = game.worldminy end
if selend.x > game.worldmaxx then selend.x = game.worldmaxx end
if selend.x < game.worldminx then selend.x = game.worldminx end
if selend.y > game.worldmaxy then selend.y = game.worldmaxy end
if selend.y < game.worldminy then selend.y = game.worldminy end
if (math.abs(selend.x - selstart.x) < 3) or (math.abs(selend.y - selstart.y) < 3) then --selection is small enough to be a click
for k,v in pairs(game.things) do
if v.shape:testPoint(selend.x,selend.y) then
if v.isship then table.insert(game.selected, v) end
end
end
else
for k,v in pairs(game.things) do
local xmax
local xmin
local ymax
local ymin
if selend.x > selstart.x then
xmax = selend.x
xmin = selstart.x
else
xmax = selstart.x
xmin = selend.x
end
if selend.y > selstart.y then
ymax = selend.y
ymin = selstart.y
else
ymax = selstart.y
ymin = selend.y
end
if v.body:getX() <= xmax and v.body:getX() >= xmin and v.body:getY() <= ymax and v.body:getY() >= ymin then
if v.isship then table.insert(game.selected, v) end
end
end
end
elseif button == "r" then
local clickedthing = false --Checks if the click hits anything
local selend = game.cam:worldCoords(x,y)
--restrict coordinates to within the world
if selend.x > game.worldmaxx then selend.x = game.worldmaxx end
if selend.x < game.worldminx then selend.x = game.worldminx end
if selend.y > game.worldmaxy then selend.y = game.worldmaxy end
if selend.y < game.worldminy then selend.y = game.worldminy end
for k,v in pairs(game.things) do
if v.shape:testPoint(selend.x, selend.y) then --v is target
if v.parent then v = v.parent end --Right-clicking a turret targets its parent instead.
clickedthing = true
for k2,v2 in pairs (game.selected) do --v2 is actor
if v2 ~= v and v2.team == ui.team then
if v.team == v2.team then
v2.order.func = orders.follow --Currently, ordered to follow whatever is clicked
v2.order.data = v
else
v2.order.func = orders.attack
v2.order.data = v
end
end
end
end
end
if clickedthing == false then
--Right-clicked empty space
if game.selected[1] then --I have some ships selected to give orders to.
for k,v in pairs(game.selected) do
if v.team == ui.team then
v.order.func = orders.move --Currently, ordered to move to wherever was clicked
v.order.data = waypoint.newwaypoint(selend.x, selend.y)
end
end
end
end
end
end
function game:getcollide(x,y)
return true
end
end
function love.draw()
ui:draw()
end
function love.update(dt)
ui:update(dt)
game:camupdate(dt)
local camspeed = 100
local turnspeed = 1000
if not (game.manualship and ui.followmanualship) then
if love.keyboard.isDown("left") then
local vx = camspeed*dt*math.sin(game.cam.rot-math.pi/2)/game.cam.zoom
local vy = camspeed*dt*math.cos(game.cam.rot-math.pi/2)/game.cam.zoom
game.cam:move(vx, vy)
end
if love.keyboard.isDown("right") then
local vx = -camspeed*dt*math.sin(game.cam.rot-math.pi/2)/game.cam.zoom
local vy = -camspeed*dt*math.cos(game.cam.rot-math.pi/2)/game.cam.zoom
game.cam:move(vx, vy)
end
if love.keyboard.isDown("up") then
local vx = -camspeed*dt*math.sin(game.cam.rot)/game.cam.zoom
local vy = -camspeed*dt*math.cos(game.cam.rot)/game.cam.zoom
game.cam:move(vx, vy)
end
if love.keyboard.isDown("down") then
local vx = camspeed*dt*math.sin(game.cam.rot)/game.cam.zoom
local vy = camspeed*dt*math.cos(game.cam.rot)/game.cam.zoom
game.cam:move(vx, vy)
end
if love.keyboard.isDown("e") then
game.cam:rotate(dt*0.5)
end
if love.keyboard.isDown("q") then
game.cam:rotate(-dt*0.5)
end
end
if love.keyboard.isDown("kpenter") then
game.cam.zoom = 1
game.cam.rot = 0
end
if love.keyboard.isDown("kp+") then
game.cam.zoom = game.cam.zoom + game.cam.zoom*dt
end
if love.keyboard.isDown("kp-") then
game.cam.zoom = game.cam.zoom - game.cam.zoom*dt
end
ui:update(dt)
if game.paused == false then
game.world:update(dt)
for k,v in pairs(game.things) do
if v.isalive then
v:update(dt)
elseif v.body:isFrozen() then
table.remove(game.things, k)
end
end
if game.manualship then
if love.keyboard.isDown("w") then
game.manualship.body:applyForce(game.manualship.thrust * dt * math.cos(game.manualship.body:getAngle()), game.manualship.thrust * dt * math.sin(game.manualship.body:getAngle()))
end
if love.keyboard.isDown("s") then
game.manualship.body:applyForce(-game.manualship.thrust * dt * math.cos(game.manualship.body:getAngle()), -game.manualship.thrust * dt * math.sin(game.manualship.body:getAngle()))
end
if love.keyboard.isDown("a") then
game.manualship.body:applyTorque(-game.manualship.torque*dt)
end
if love.keyboard.isDown("d") then
game.manualship.body:applyTorque(game.manualship.torque*dt)
end
end
end
end