-
Notifications
You must be signed in to change notification settings - Fork 0
/
spaceship.lua
110 lines (93 loc) · 2.69 KB
/
spaceship.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
local shooting_interval = 0.2
local sp = { x = 0, y = 0, speed = 150, img = nil, score = 0, alive = true,
shooting_timer = shooting_interval, allow_shooting = true,
active_lasers = {}, shooting_sound = nil}
local laser = { x = 0, y = 0, speed = 200, img = nil }
spaceship = sp
function sp.load(arg)
sp.img = love.graphics.newImage('images/spaceship.png')
laser.img = love.graphics.newImage('images/laser.png')
sp.x = (love.graphics:getWidth() / 2) - (sp.img:getWidth() / 2)
sp.shooting_sound = love.audio.newSource('sounds/pitiu.ogg', 'static')
--sp.shooting_sound = love.audio.newSource("sounds/pitiu.wav", 'static')
end
function sp.move_right(dt)
if sp.x < (love.graphics.getWidth() - sp.img:getWidth()) then
sp.x = sp.x + (sp.speed * dt)
end
end
function sp.move_left(dt)
if sp.x > 0 then
sp.x = sp.x - (sp.speed * dt)
end
end
function sp.draw(dt)
if spaceship.alive then
sp.y = love.graphics:getHeight() - sp.img:getHeight()
love.graphics.draw(sp.img, sp.x, sp.y)
for i, laser in ipairs(sp.active_lasers) do
love.graphics.draw(laser.img, laser.x, laser.y)
end
else
love.graphics.print("Press 'R' to restart", love.graphics:getWidth()/2-50,
love.graphics:getHeight()/2-10)
end
end
function sp.increase_score()
if sp.alive then
sp.score = sp.score + 1
end
end
function sp.decrease_score()
if sp.alive and sp.score > 0 then
sp.score = sp.score - 1
end
end
function sp.update_shooting_interval(dt)
sp.shooting_timer = sp.shooting_timer - (1 * dt)
if sp.shooting_timer < 0 then
sp.allow_shooting = true
end
end
function sp.shoot(dt)
if sp.allow_shooting and sp.alive then
-- Lets shoot
new_laser = {
x = (sp.x + ((sp.img:getWidth()/2) - (laser.img:getWidth()/2))),
y = (sp.y - (laser.img:getHeight()/2)), speed = laser.speed,
img = laser.img }
if sp.shooting_sound ~= nil then
sp.shooting_sound:rewind()
sp.shooting_sound:play()
end
table.insert(sp.active_lasers, new_laser)
sp.shooting_timer = shooting_interval
sp.allow_shooting = false
end
end
function sp.move_lasers(dt)
for i, d_laser in ipairs(sp.active_lasers) do
d_laser.y = d_laser.y - (d_laser.speed * dt)
if d_laser.y < 0 then
table.remove(sp.active_lasers, i)
end
end
end
function sp.get_lasers()
return sp.active_lasers;
end
function sp.remove_laser(pos)
table.remove(sp.active_lasers, pos)
end
function sp.killed()
sp.alive = false
sp.active_lasers = {}
end
function sp.revive()
if not sp.alive then
sp.x = (love.graphics:getWidth() / 2) - (sp.img:getWidth() / 2)
sp.alive = true
sp.score = 0
end
end
return sp