-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbulleter.lua
More file actions
86 lines (67 loc) · 1.88 KB
/
bulleter.lua
File metadata and controls
86 lines (67 loc) · 1.88 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
Object = require "classic"
------
local Bullet = Object:extend()
function Bullet:new(x, y, velocity)
self.x = x
self.y = y
-- Siempre debe ser un vector2 {x, y}
self.velocity = velocity
end
function Bullet:update(dt)
self.x = self.x + self.velocity.x * dt
self.y = self.y + self.velocity.y * dt
end
function Bullet:draw()
love.graphics.circle("line", self.x, self.y, 5)
end
-----
Bulleter = Object:extend()
function Bulleter:new(x, y, density, freq, speed)
self.x = x or 0
self.y = y or 0
self.density = density or 10
self.start = false
self.freq = freq or 10
self.speed = speed or 200
self.rotation = 0
self.angularSpeed = 0
self.time = 0
self.lastfire = 0
self.bullets = {}
end
function Bulleter:rotate(dt, angularSpeed)
self.rotation = self.rotation + angularSpeed * dt
end
function Bulleter:fire()
local normal = (2*math.pi) / self.density
for i = 1, self.density do
local bullet = Bullet(self.x, self.y, {
['x'] = math.cos(i * normal + self.rotation) * self.speed,
['y'] = math.sin(i * normal + self.rotation) * self.speed
})
table.insert(self.bullets, bullet)
end
print('Balas: ', #self.bullets)
end
function Bulleter:update(dt)
self:rotate(dt, self.angularSpeed)
self.time = self.time + dt
if (self.time - self.lastfire) > self.freq then
self:fire()
self.lastfire = self.time
end
for i, bullet in ipairs(self.bullets) do
if bullet.x < 0 or bullet.y < 0 or bullet.x > 800 or bullet.y > 600 then
table.remove(self.bullets, i)
end
bullet:update(dt)
end
end
function Bulleter:draw()
for i, bullet in ipairs(self.bullets) do
-- TODO: Borrar balas para que no explote la pc
bullet:draw()
end
-- love.graphics.circle("fill", self.x, self.y, 40)
end
return Bulleter