-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasteroid.lua
More file actions
138 lines (120 loc) · 2.67 KB
/
Copy pathasteroid.lua
File metadata and controls
138 lines (120 loc) · 2.67 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
local class = require('libraries.middleclass')
local helpers = require('helpers')
local min_x = love.graphics.getWidth() / 2
local max_x = love.graphics.getWidth() * 1.5
local Asteroid = class('Asteroid')
local asteroids = {
{
sheet_x = 0,
sheet_y = 224,
diameter = 32,
shape_diameter = 32,
},
{
sheet_x = 32,
sheet_y = 224,
width = 64,
height = 32,
shape_width = 64,
shape_height = 32,
},
{
sheet_x = 96,
sheet_y = 224,
diameter = 32,
shape_diameter = 26,
},
{
sheet_x = 128,
sheet_y = 224,
diameter = 32,
shape_diameter = 16,
}
}
function Asteroid:initialize()
local asteroid = asteroids[math.random(1, #asteroids)]
self.width = asteroid.width or asteroid.diameter
self.height = asteroid.height or asteroid.diameter
self.shape_width = asteroid.shape_width or asteroid.shape_diameter
self.shape_height = asteroid.shape_height or asteroid.shape_diameter
self.x = math.random(min_x, max_x)
self.y = -self.height
self.angle = 0
self.speed = math.random(100, 300)
self.opacity = 1
self.quad = love.graphics.newQuad(
asteroid.sheet_x,
asteroid.sheet_y,
self.width,
self.height,
celestial_objects_sheet:getWidth(),
celestial_objects_sheet:getHeight()
)
-- quack
self.shape = asteroid.diameter and
collider:circle(
self.x,
self.y,
self.shape_width / 2
) or
collider:rectangle(
self.x,
self.y,
self.shape_width,
self.shape_height
)
end
function Asteroid:update(dt)
self.x = self.x - dt * 2 * self.speed
self.y = self.y + dt * self.speed - player.angle / 70
if self.is_hit then
self.angle = self.angle + self.speed / 20
end
if self.shape then
self.shape:moveTo(self.x, self.y)
self.shape:setRotation(math.rad(self.angle))
end
end
function Asteroid:draw()
if self.shape then
-- for debugging:
-- self.shape:draw('line')
end
helpers.setColor(255, 255, 255, self.opacity)
love.graphics.draw(
celestial_objects_sheet,
self.quad,
self.x,
self.y,
math.rad(self.angle),
1,
1,
self.width / 2,
self.height / 2
)
helpers.resetColor()
end
function Asteroid:fade_out(done)
self.fade_out_timer = timer:tween(
0.2,
self,
{ opacity = 0 },
'linear',
done
)
end
function Asteroid:isOffScreen()
return self.x + self.width <= 0
end
function Asteroid:collidesWith(...)
if not self.shape then return false end
return self.shape:collidesWith(...)
end
function Asteroid:onCollide()
-- ignore multiple hits at once
if self.is_hit then return end
self.is_hit = true
collider:remove(self.shape)
self.shape = nil
end
return Asteroid