-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
163 lines (140 loc) · 4.45 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
-- Copyright (C) 2023 Alessandro Amatucci Girlanda
-- This file is part of Invaders in Space.
-- Invaders in Space is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
-- Invaders in Space is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
-- You should have received a copy of the GNU General Public License
-- along with Invaders in Space. If not, see <https://www.gnu.org/licenses/>.
function love.load()
-- Set Game Icon
local icon = love.image.newImageData("pictures/icon.png")
love.window.setIcon(icon)
-- Include files
require("globals")
Object = require("classic/classic")
require("text")
require("entity")
require("background")
require("menu")
require("game")
require("player")
require("projectile")
require("enemy")
require("explosion")
require("tutorial")
require("button")
require("gameover")
-- Load External Media
loadPictures()
loadSounds()
math.randomseed(os.time())
-- Setup the different pages
background = Background()
menu = Menu()
game = Game()
tutorial = Tutorial()
gameover = Gameover()
current_page = "Menu"
current_music = back_music
love.audio.setVolume(0.5)
end
function love.update(dt)
if (current_page == "Menu") then
menu:update(dt)
elseif (current_page == "Start") then
background:update(dt)
game:update(dt)
elseif (current_page == "Tutorial") then
tutorial:update(dt)
elseif (current_page == "Game Over") then
gameover:update(dt)
elseif (current_page == "Quit") then
love.event.quit()
end
end
function love.draw()
if (current_page == "Menu") then
background:draw()
background:titleDraw()
menu:draw()
elseif (current_page == "Start") then
background:draw()
game:draw()
elseif (current_page == "Tutorial") then
tutorial:draw()
elseif (current_page == "Game Over") then
love.audio.stop(back_music)
gameover:draw()
end
end
-- Drawing functions
function arrayUpdate(dt, array)
for i,obj in ipairs(array) do
obj:update(dt)
end
end
function arrayDraw(array)
for i,obj in ipairs(array) do
-- Check if it is list
if (#obj > 0) then
for j,quad in ipairs(obj) do
quad:draw()
end
else
obj:draw()
end
end
end
-- Sound playing functions
function setupSound()
back_music:setVolume(0.3)
end
function playSound(sound)
love.audio.stop(sound)
love.audio.play(sound)
end
-- Window control keys
function love.keypressed(key)
-- Back to menu
if key == "escape" then
current_page = "Menu"
love.audio.pause(back_music)
-- Close the game
elseif key == "q" then
love.event.quit()
-- Reset game
elseif key == "r" then
love.event.quit('restart')
end
end
-- LOAD FUNCTIONS --
function loadPictures()
title_pic = love.graphics.newImage("pictures/title.png")
player_pic = love.graphics.newImage("pictures/player.png")
projectile_pic = love.graphics.newImage("pictures/projectile.png")
enemies_pic = love.graphics.newImage("pictures/enemies.png")
e_projectile_pic = love.graphics.newImage("pictures/e_projectile.png")
explosion_pic = love.graphics.newImage("pictures/explosion.png")
win_pic = love.graphics.newImage("pictures/win.png")
lose_pic = love.graphics.newImage("pictures/lose.png")
end
function loadSounds()
back_music = love.audio.newSource("sounds/game.mp3", "stream")
projectile_sound = love.audio.newSource("sounds/projectile.mp3", "static")
e_projectile_sound = love.audio.newSource("sounds/e_projectile.mp3", "static")
player_hit_sound = love.audio.newSource("sounds/player_hit.mp3", "static")
explosion_sound = love.audio.newSource("sounds/explosion.mp3", "static")
win_sound = love.audio.newSource("sounds/win.mp3", "static")
lose_sound = love.audio.newSource("sounds/lose.mp3", "static")
end
-- RESET GAME --
function reset()
background = Background()
game = Game()
gameover = Gameover()
end