-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity-spawner.lua
53 lines (44 loc) · 1.3 KB
/
entity-spawner.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
EntitySpawner = class("EntitySpawner")
---
-- EntitySpawner:initialize
-- A simple entity spawner.
--
-- @param x X position
-- @param y Y position
-- @param entity The name of the entity to spawn
-- @param params Parameters used to spawn the entity
-- @param interval Interval between spawning. Also accepts a table {min, max} for randomized intervals
-- @param amount Amount of entities to spawn. Also accepts a table {min, max} for randomized amount
--
function EntitySpawner:initialize(data)
self.x = data.x
self.y = data.y
self.entity = data.type
self.params = data.params
self.interval = data.interval
self.amount = data.amount
self.objectid = game.addObject(self)
self.nextFire = 0
end
---
-- EntitySpawner:update
-- Spawns an entity if needed.
--
function EntitySpawner:update(dt)
if game.time >= self.nextFire then
for i=1, math.random(unpack(self.amount)) do
_G[self.entity]:new(self.x, self.y)
end
if self.interval[1] == 0 and self.interval[2] == 0 then
self.nextFire = 8999
else
self.nextFire = game.time + math.random() * (self.interval[2] - self.interval[1]) + self.interval[1]
end
end
end
---
-- EntitySpawner:draw
-- Does nothing.
--
function EntitySpawner:draw()
end