-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcookie.lua
131 lines (105 loc) · 2.54 KB
/
cookie.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
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
cookie = class:new()
cookies = {}
local model = polygon.new("soda/cookie.soda")
local movetimermax = 1/30
local primedtimermax = 3
function cookie:init(x, y)
self.x = x
self.y = y
self.xv = 0
self.yv = 0
self.px = x
self.py = y
self.loader = loader.step
self.isEnemy = true
self.type = "cookie"
self.movetimer = 0
self.state = "normal" -- normal, primed
self.primedtimer = 0
self.deleteself = false
bumpwrld:add(self, self.x, self.y, 50, 50)
end
function cookie:update(dt)
if self.state == "normal" then
self.movetimer = self.movetimer + dt
if self.movetimer > movetimermax then
self.px = self.x + math.random()*10
self.py = self.y + math.random()*10
self.movetimer = 0
end
elseif self.state == "primed" then
self.primedtimer = self.primedtimer + dt
if self.primedtimer >= primedtimermax then
self.deleteself = true
end
self.x = self.x + self.xv * dt
self.y = self.y + self.yv * dt
self.yv = self.yv + 30*60 * dt
self.px = self.x
self.py = self.y
end
-- collision
local function filter(i, o)
if o.type == "player" or o.isEnemy or o.type == "pickup" then
return "cross"
end
if self.state == "normal" then
return "slide"
else
self.xv = 0
self.yv = 0
return "touch"
end
end
-- run collision detection with the filter
local ax, ay, cols, len = bumpwrld:move(self, self.px, self.py, filter)
self.px, self.py = ax, ay
end
function cookie:draw()
lg.push()
lg.translate(self.px, self.py)
lg.scale(1.5,1.5)
polygon.draw(model)
lg.pop()
end
function cookie:damage(o)
local dx = 0
if o and o.player_facing then dx = o.player_facing
elseif o and o.x then dx = lume.sign(self.x - o.x)
else dx = 1 end
if self.state == "normal" then
self.state = "primed"
self.xv = dx * 600
self.yv = -200
else
self.xv = dx * 600
self.yv = -200
self.y = self.y-2
end
sfx_enemy_pop:stop()
sfx_enemy_pop:setPitch(0.9 + math.random(4)/10)
sfx_enemy_pop:play()
end
function cookie:delete()
bumpwrld:remove(self)
if not game_over then
if not self.deletenoscore then
local spawndrop = lume.weightedchoice({
[true] = 1,
[false] = 4*2
})
if spawndrop then
pickups[#pickups+1] = pickup:new(self.x, self.y+10)
end
-- spawn gibs
for i=1,20 do
gibs[#gibs+1] = gib:new(self.x+30, self.y,
math.pi*math.random(), math.random(-60*6, 60*6),
-math.random(500, 1000), math.pi/2*math.random(-60, 60))
end
-- spawn explosion
explosions[#explosions+1] = explosion:new(self.x, self.y, 360, 750)
ent_player.score = ent_player.score + 10
end
end
end