-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprojectile.lua
87 lines (73 loc) · 2.82 KB
/
projectile.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
-- 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/>.
Projectile = Entity:extend()
function Projectile:new(image, sound, x, y)
Projectile.super.new(self, image, x, y)
self.width = PROJECTILES_WIDTH
self.height = PROJECTILES_HEIGHT
self.speed = PROJECTILES_SPEED
self.sound = sound
self:setQuads()
self.current_texture = 1
self.texture = self.quads[self.current_texture]
self.texture_change = 0.1
self.texture_timer = 0
end
function Projectile:update(dt, direction)
self.y = self.y + self.speed * direction * dt
-- Update projectile texture every "texture_change" seconds
self:textureUpdate(dt)
end
function Projectile:tutorialUpdate(dt)
self.y = self.y - self.speed * dt
if (self.y <= self.start_y - PROJECTILES_TUTORIAL_HEIGHT) then
self.y = self.start_y
end
self:textureUpdate(dt)
end
function Projectile:draw()
love.graphics.draw(self.image, self.texture, self.x, self.y)
end
-- Projectile Animation
function Projectile:textureUpdate(dt)
if (self.texture_timer >= self.texture_change) then
self.current_texture = self.current_texture + 1
if (self.current_texture > #self.quads) then
self.current_texture = 1
end
self.texture = self.quads[self.current_texture]
self.texture_timer = self.texture_timer - self.texture_change
else
self.texture_timer = self.texture_timer + dt
end
end
function Projectile:collision(e, direction)
-- Player's Projectile collision
if (direction == -1) then
if (self.y < e.y + e.height and self.y > e.y) then
if ((self.x > e.x and self.x < e.x + e.width) or (self.x + self.width < e.x + e.width and self.x + self.width > e.x)) then
return true
end
return false
end
return false
-- Enemy's Projectile collision
else
if (self.y + self.height > e.y) then
if ((self.x > e.x and self.x < e.x + e.width) or (self.x + self.width < e.x + e.width and self.x + self.width > e.x)) then
return true
end
end
return false
end
end