-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
267 lines (221 loc) · 6.99 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
require "requires"
function love.load()
guitime = love.timer.getTime()
time_start = guitime
ctime = guitime
pause_time = 0
window = {}
window.w, window.h = 960, 600
love.window.setMode(window.w, window.h)
love.graphics.setBackgroundColor(color.bg)
shader_desaturate = love.graphics.newShader("desaturate.lua")
controller = controls.setup()
love.mouse.setVisible(false)
love.mouse.setGrabbed(true)
mouse = {x = love.mouse.getX(), y = love.mouse.getY()}
font = love.graphics.newImageFont("art/font.png",
" abcdefghijklmnopqrstuvwxyz" ..
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0" ..
"123456789.,!?-+/():;%&`'*#=[]\"")
love.graphics.setFont(font)
love.graphics.setLineWidth(1)
game_state = "play"
mainmap = map:new(48, 32)
mainmap:fill_main()
nextpiece = files.parse_map_file("maps/hi.txt", 7, 7)
img.setup()
-- set up the parallax background
parallax_canvas = love.graphics.newCanvas(512, 512)
love.graphics.setCanvas(parallax_canvas)
for i=0, 15 do
for j=0, 15 do
k = love.math.random(1, 8)
if k <= 4 then
love.graphics.draw(img.tileset, img.tile["backdrop"][k], 32 * i, 32 * j)
end
end
end
love.graphics.setCanvas()
audio.setup()
love.audio.setVolume(0.3)
gravity = 2000
player = actor:new(
{
class = "player", id = 1, name = "player1", faction = "player",
x = 250, y = 250, half_w = 7, half_h = 7,
dx = 0, dy = 0,
sprite = "player", color = color.rouge, flash_color = color.white, flash_time = 0,
facing = 'r', anim_start = ctime,
ai = {control = "player"}, controls = {},
top_speed = 250,
walk_accel = 1200, walk_friction = 500,
jump_speed = 550, air_accel = 700,
dash_speed = 700, dash_dur = 0.3, dash_cooldown = 0.1,
touching_floor = false, double_jumps = 0, double_jumps_max = 2,
hp = 1000, status = {},
weapon = weapon_data.spawn("laser"), weapon2 = weapon_data.spawn("missile"),
shot_cooldown = 0, cof = 0, cof_factor = 0
})
enemies = {}
shots = {}
sparks = {}
beams = {}
spawn(7)
end
function love.update(dt)
guitime = love.timer.getTime() - time_start
-- handle input
controller:update()
if game_state == "pause" then
if controller:pressed('menu') then unpause() end
if controller:pressed('view') then love.event.push("quit") end
elseif game_state == "play" then
if controller:pressed('menu') then pause() end
end
if game_state == "play" then
ctime = guitime - pause_time
camera.update(dt)
-- update everything
player:update(dt, nil)
for _,v in pairs(enemies) do
v:update(dt)
end
for _,v in pairs(shots) do
v:update(dt)
end
for _,v in pairs(sparks) do
v:update(dt)
end
for _,v in pairs(beams) do
v:update(dt)
end
end
end
function love.draw()
if game_state == "pause" then
love.graphics.setShader(shader_desaturate)
end
for i = -1, math.floor(window.w / 512) do
for j = -1, math.floor(window.h / 512) do
love.graphics.draw(parallax_canvas, math.floor(((-camera.x * 0.5) % 512) + 512 * i), math.floor(((-camera.y * 0.5) % 512) + 512 * j))
end
end
img.update_tileset_batch()
love.graphics.draw(img.tileset_batch, -(camera.x % 32), -(camera.y % 32))
for _,v in pairs(enemies) do
if camera.on_screen(v) then
v:draw()
end
end
player:draw()
for _,v in pairs(shots) do
v:draw()
end
for _,v in pairs(sparks) do
v:draw()
end
for _,v in pairs(beams) do
v:draw()
end
-- gui
love.graphics.setColor(player.weapon.color)
if player:check_status("reload") then
love.graphics.arc("line", "open", camera.view_x(player), camera.view_y(player), 20, 0,
2 * math.pi * (player.status.reload - ctime) / player.weapon.reload_time)
end
if game_state == "play" then
love.graphics.draw(img.cursor, mouse.x - 2, mouse.y - 2)
love.graphics.circle("line", mouse.x, mouse.y,
math.sin(player.cof) * mymath.dist(mouse.x, mouse.y, camera.view_x(player), camera.view_y(player)))
end
love.graphics.setColor(player.color)
love.graphics.print(player.hp, 20, 20)
love.graphics.setColor(color.white)
love.graphics.print({player.weapon.color,
"[ " .. player.weapon.name .. " ]\n" .. counter_string(player.weapon.ammo, player.weapon.ammo_glyph),
color.dkgrey,
counter_string(player.weapon.ammo_max - player.weapon.ammo, player.weapon.ammo_glyph)},
20, 40)
-- debug msg
love.graphics.line(-camera.x + 32, -camera.y + mainmap.death_line, -camera.x +(mainmap.width+1) * 32, -camera.y + mainmap.death_line)
love.graphics.print("Time: "..string.format("%.2f", ctime), 20, window.h - 140)
love.graphics.print("FPS: "..love.timer.getFPS(), 20, window.h - 120)
love.graphics.print("p: "..mymath.round(player.x)..", "..mymath.round(player.y), 20, window.h - 100)
love.graphics.print("d: "..mymath.round(player.dx)..", "..mymath.round(player.dy), 20, window.h - 80)
local dc = love.graphics.getStats()
love.graphics.print("draws: "..dc.drawcalls, 20, window.h - 60)
love.graphics.print(map.grid_at_pos(mouse.x + camera.x)..", "..map.grid_at_pos(mouse.y + camera.y), 20, window.h - 40)
-- physics.map_collision_test(player)
if game_state == "pause" then
love.graphics.setShader()
draw_pause_menu()
end
end
function love.keypressed(key, unicode)
-- debug
if key == "1" then
mainmap:print(nextpiece, map.grid_at_pos(mouse.x + camera.x), map.grid_at_pos(mouse.y + camera.y))
end
if key == "2" then
spawn(1)
end
if key == "3" then
mainmap:set_block("slope_45", map.grid_at_pos(mouse.x + camera.x), map.grid_at_pos(mouse.y + camera.y))
redraw = true
end
if key == "8" then
player.dy = player.dy - 50000
end
if key == "9" then
player.dx = player.dx - 50000
end
if key == "0" then
player.dx = player.dx + 50000
player.dy = player.dy - 50000
end
end
function love.joystickadded(joystick)
controller.joystick = joystick
end
function love.focus(f)
if f then
love.mouse.setVisible(false)
love.mouse.setGrabbed(true)
else
if game_state ~= "pause" then pause() end
love.mouse.setVisible(true)
love.mouse.setGrabbed(false)
end
end
function spawn(n)
for i = 1, n do
enemy_data.spawn("bird", love.math.random(64, mainmap.width*32 - 64),
love.math.random(64, mainmap.height*32 - 64), 'r')
enemy_data.spawn("demon", love.math.random(64, mainmap.width*32 - 64),
love.math.random(64, mainmap.height*32 - 64), 'r')
end
end
function counter_string(n, glyph)
if n == 0 then return "" end
glyph = glyph or "/"
return string.rep(glyph, n)
end
local pause_mouse_x, pause_mouse_y, pause_start
function pause()
game_state = "pause"
pause_mouse_x, pause_mouse_y = love.mouse.getPosition()
pause_start = guitime
end
function unpause()
game_state = "play"
love.mouse.setPosition(pause_mouse_x, pause_mouse_y)
pause_time = pause_time + guitime - pause_start
end
function draw_pause_menu()
love.graphics.setColor(color.rouge)
love.graphics.circle("fill", window.w/2, window.h/2, 200)
love.graphics.setColor(color.white)
love.graphics.printf("Press Q to quit", math.floor(window.w/2 - 200), math.floor(window.h/2 - font:getHeight()/2), 400, "center")
love.graphics.setColor(color.white)
love.graphics.draw(img.cursor, love.mouse.getX() - 2, love.mouse.getY() - 2)
end