-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjelly.lua
106 lines (93 loc) · 2.46 KB
/
jelly.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
local parent = require 'actor'
local Bullet = require 'bullet'
local Upgrade = require 'upgrade'
local new
local function toOther(self, other)
local vx, vy = other.hx - self.hx, other.hy - self.hy
local px, py = grid:toPixel(vx, vy)
len2 = px*px + py*py
local dir
if len2 > 0.001 then
dir = math.atan2(py, px) / (math.pi/3)
dir = math.floor(dir) % 6
else
dir = 0
end
return dir, math.sqrt(len2) / (2*grid.a)
end
local function update(self, G)
self.turns = self.turns - 1
if self.turns < 0 then
self.turns = self.timeout
local dir, dist = toOther(self, player)
if dist < 8 then
dir = (dir + math.random(-1,1)) % 6
local dx, dy = unpack(grid.dirs[1+dir])
local a = 0.25
local vx, vy = grid:toPixel(self.vx + a*dx, self.vy + a*dy)
local v = self.vMax * 2*grid.a
if vx*vx + vy*vy <= v*v then
self.ax, self.ay = self.ax + a*dx, self.ay + a*dy
end
end
end
parent.methods.update(self, G)
end
local function die(self)
world:remove(self)
if self.whole then
local w = self.whole
w.pieces = w.pieces - 1
if w.pieces <= 0 then
w.spawn[w.spawnIndex] = false
end
end
end
local function split(self, dir)
self.pieces = 0
local dirs = { (dir + 2) % 6, (dir - 2) % 6 }
for _,d in ipairs(dirs) do
local dx, dy = unpack(grid.dirs[1+d])
local j = new(self.hx + 0.5*dx, self.hy + 0.5*dy, true, self.timeout, self.vMax)
j.vx, j.vy = self.vx + 0.25*dx, self.vy + 0.25*dy
j.whole = self
self.pieces = self.pieces + 1
end
die(self)
end
local function collisionResponse(self)
local other = self.collider.other
if other then
if instanceOf(other, Bullet) or other == player then
if self.small then
die(self)
else
local dir = toOther(self, other)
split(self, dir)
end
elseif instanceOf(other, Upgrade) then
return false
end
end
return true
end
local methods = {
update = update,
collisionResponse = collisionResponse
}
local class = { __index = setmetatable(methods, parent.class) }
new = function(hx, hy, small, timeout, vMax)
local dir = math.random(0, 5)
local ch = small and 'o' or 'O'
local self = parent.new(ch, hx, hy, dir, {0.43, 0.08, 0.35})
self.tip = "Acid Jelly"
self.small = small or false
self.timeout = timeout or 5
self.turns = math.random(1, self.timeout)
self.vMax = vMax or 0.5
local x, y = grid:toPixel(hx, hy)
self.collider = { x=x, y=y, r=0.7*grid.a, e=0.5 }
table.insert(newActors, self)
return setmetatable(self, class)
end
return { new = new, methods = methods, class = class }