-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.lua
More file actions
339 lines (262 loc) · 11.6 KB
/
Copy pathgame.lua
File metadata and controls
339 lines (262 loc) · 11.6 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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
love = require("love")
mathheader = require("mathheader")
projectile = require("projectile")
enemy = require("enemy")
vector = require("libs.vector.vector")
player = require("player")
environment = require("environment")
function ReturnGameTable(player, game_environment)
-- Initialisation, getting some of the base stats ready
local _,_,flags = love.window.getMode()
gun_equipped_filepath = "Weapons.Guns.G19"
_G.REFRESH_RATE = flags.refreshrate
local game = {}
game.type = "game"
game.environment = {}
if player == nil then
game.player = ReturnPlayer(player, vector.new(0,0))
else
game.player = ReturnPlayer(nil, vector.new(0,0))
end
game.player.id = 0
game.projectiles = {}
game.enemies = {}
local game_environment = {}
game_environment.ground = {}
game_environment.ground.texture = love.graphics.newImage("Assets/Textures/Ground/TileableCarpet.jpg")
game_environment.ground.zoom = 0.3
game_environment.ground.resolution = vector.new(game_environment.ground.texture:getWidth(),game_environment.ground.texture:getHeight())
game_environment.ground.Render = function ()
--TODO program system that finds, based on given texture size, player pos, resolution and zoom, how many times to repeat the texture in the X and Y direction and where to start from
local tile_offset, treq = FindBestFloorPlacementZoom(game_environment.ground) -- top left of ground placement, tile reqiurement in order to cover the whole screen
for x=1,treq.x do
local x_coord = ((game_environment.ground.resolution.x*game_environment.ground.zoom*ZOOM_MULT) * (x-1)) - tile_offset.x
for y=1,treq.y do
local y_coord = ((game_environment.ground.resolution.y*game_environment.ground.zoom*ZOOM_MULT) * (y-1)) - tile_offset.y
love.graphics.draw(game_environment.ground.texture, x_coord, y_coord, 0, game_environment.ground.zoom * ZOOM_MULT, game_environment.ground.zoom * ZOOM_MULT, -x/2, -y/2)
end
end
end
game.environment = game_environment
-- Camera info and functions
game.player.camera = {}
local camera = {}
camera.frame_lag = math.floor(REFRESH_RATE/2) -- Number of frames the camera will average in order to lag behind, making the movement feel more "movementy"
camera.stored_positions = {}
camera.stored_positions.player = {}
--print(game.player.state.position)
for i=1,camera.frame_lag do camera.stored_positions.player[i] = game.player.state.position end
camera.stored_positions.mouse = {}
for i=1,camera.frame_lag do camera.stored_positions.mouse[i] = vector.new(0,0) end
camera.stored_positions.zoom = {}
for i=1,camera.frame_lag do camera.stored_positions.zoom[i] = ZOOM end
camera.total_denominator = 0
for i=1,camera.frame_lag do
camera.total_denominator = camera.total_denominator + i
end
camera.total_denominator = camera.total_denominator/camera.frame_lag
camera.UpdateStoredPositions = function ()
if camera.frame_lag > 0 then
if love.mouse.isDown(2) then
if game.player.equips and game.player.equips.gun and game.player.equips.gun ~= nil then
if game.player.equips.gun.aiming_vision_increase then
local mouse_pos = vector.new(love.mouse.getPosition())
mouse_pos = mouse_pos-(RES/2)
table.insert(camera.stored_positions.mouse, 1, (mouse_pos/(RES/2)*game.player.equips.gun.aiming_vision_increase))
end
end
else
table.insert(camera.stored_positions.mouse, 1, vector.new(0,0))
end
table.insert(camera.stored_positions.player, 1, game.player.state.position)
table.insert(camera.stored_positions.zoom, 1, ZOOM)
if #camera.stored_positions.player > camera.frame_lag then
table.remove(camera.stored_positions.player)
end
if #camera.stored_positions.mouse > camera.frame_lag then
table.remove(camera.stored_positions.mouse)
end
if #camera.stored_positions.zoom > camera.frame_lag then
table.remove(camera.stored_positions.zoom)
end
end
end
-- Calculate the camera offset
camera.Offset = function()
if camera.frame_lag > 0 then
local total = vector.new(0,0)
local zoom_total = 0
for i,coordinate in ipairs(camera.stored_positions.player) do
total = total + (coordinate*((camera.frame_lag-(i-1))/camera.frame_lag))
end
for i,mouse_offset in ipairs(camera.stored_positions.mouse) do
total = total + (mouse_offset*((camera.frame_lag-(i-1))/camera.frame_lag)) * GAME.player.equips.gun.aiming_vision_increase
end
for i,zoom in ipairs(camera.stored_positions.zoom) do
zoom_total = zoom_total + zoom
end
total = game.player.state.position - total/camera.total_denominator + (RES/2)
local zoom_lagged = zoom_total/camera.frame_lag
return total, zoom_lagged
end
return game.player.state.position
end
game.player.camera = camera
-- Controls, will have different sections for movement, shop ect just to simplify things for me lol
local controls = {}
controls.movement = {}
controls.attack = {}
controls.movement.up = function () return love.keyboard.isDown("w") end
controls.movement.down = function () return love.keyboard.isDown("s") end
controls.movement.left = function () return love.keyboard.isDown("a") end
controls.movement.right = function () return love.keyboard.isDown("d") end
controls.movement.sprint = function () return love.keyboard.isDown("lshift") end
controls.attack.fire = function () return love.mouse.isDown(1) end
game.controls = controls
--Code for equipping items
local equips = {}
local gunequipped = require(gun_equipped_filepath)
equips.gun = ReturnGun()
game.player.equips = equips
--Where a ton of the calculations for the game will be stored, IE movement calculations and acceleration
game.functions = {}
local functions = {}
functions.Acceleration = function (dt)
game.player.Acceleration(dt)
for i,enemy in ipairs(game.enemies) do
if game.enemies[i].state.movementConditionsMet() then
game.enemies[i].Acceleration(dt, enemy.DesiredMovementVector(game.player.state.position))
else
game.enemies[i].Decelerate(dt)
end
--print("enemy speed:", game.enemies[i].state.speed)
end
end
--Movement
functions.Movement = function (dt)
-- player movement
game.player.state.position = game.player.state.position + (game.player.state.speed * dt)
--print("player pos", game.player.state.position)
--print("cam offset", CAMERA_OFFSET)
--print("cam pos", CAMERA_POSITION)
--print("player screen pos", PLAYER_SCREEN_POSITION)
-- enemy movement
for i,enemy in ipairs(game.enemies) do
game.enemies[i].Movement(dt)
--print("enemy position:", enemy.state.position)
end
end
--Projectile based functions
functions.Fire = function (start_pos,end_pos,weapon_type,thing)
if (love.timer.getTime() - weapon_type.last_fired) > weapon_type.fire_rate then
weapon_type.last_fired = love.timer.getTime()
local projectile = CreateProjectile(start_pos,end_pos,weapon_type,thing)
if projectile ~= nil then
table.insert(game.projectiles, projectile)
end
end
end
functions.KillExpiredProjectiles = function ()
for pos,projectile in pairs(game.projectiles) do
if projectile.dead() then
table.remove(game.projectiles, pos)
end
end
end
functions.checkCircleCollision = function(thing, projectile)
local thingposition = thing.state.position
local distance = thingposition:dist(projectile.position)
--If the distance between the two objects is less than their radii added together, they are colliding
return (thing.state.size + projectile.size) > distance
end
functions.PerformRequestedActions = function ()
if game.player.state.fireConditionsMet() then
game.player.UsePrimary()
end
if game.player.state.secondaryConditonsMet() then
if game.player.equips.gun and game.player.equips.gun.aiming_supported then
game.player.state.scoped_in = true
end
game.player.UseSecondary()
else
game.player.state.scoped_in = false
end
for i, enemy in pairs(game.enemies) do
if enemy.state.fireConditionsMet() then
game.functions.Fire(enemy.state.position,game.player.state.position, enemy.equips.gun, enemy)
end
end
end
functions.ProgressProjectiles = function (dt)
for _,projectile in pairs(game.projectiles) do
projectile.Progress(dt)
end
functions.KillExpiredProjectiles()
end
functions.RenderProjectiles = function ()
for _,projectile in pairs(game.projectiles) do
projectile.Render()
end
end
functions.SpawnEnemy = function ()
local enemy = ReturnEnemy()
table.insert(game.enemies, enemy)
end
functions.KillEnemies = function ()
for enemy_pos, enemy in pairs(game.enemies) do
for proj_pos, projectile in pairs(game.projectiles) do
if not projectile.idInHitList(enemy.id) then
if not enemy.state.dead() and not projectile.dead() and projectile.shooter_id ~= enemy.id then
if functions.checkCircleCollision(enemy, projectile) then
game.enemies[enemy_pos].state.health = enemy.state.health - game.projectiles[proj_pos].damage
game.projectiles[proj_pos].pierce = game.projectiles[proj_pos].pierce - 1
table.insert(game.projectiles[proj_pos].hit_list, enemy.id)
end
end
end
end
end
for pos, enemy in pairs(game.enemies) do
if enemy.state.dead() then
table.remove(game.enemies, pos)
end
end
end
functions.RenderEnemies = function ()
for i, enemy in pairs(game.enemies) do
enemy.Render()
end
end
functions.RenderFloor = function ()
game.environment.ground.Render()
end
functions.RenderPlayer = function ()
game.player.Render()
end
functions.getScreenPosition = function (vector)
return CAMERA_POSITION + (vector * ZOOM_MULT)
end
game.functions = functions
-- Proceed to next frame
game.Progress = function (dt)
game.functions.Acceleration(dt)
game.functions.Movement(dt)
game.functions.ProgressProjectiles(dt)
game.functions.PerformRequestedActions()
game.functions.KillEnemies()
game.player.camera.UpdateStoredPositions()
end
game.Render = function ()
game.functions.RenderFloor()
game.functions.RenderProjectiles()
game.functions.RenderEnemies()
game.functions.RenderPlayer()
end
game.End = function ()
local results = {}
results.player = game.player
return results
end
-- Return everything
return game
end