1-- Copyright (C) 2007, 2010 - Bit-Blot
2--
3-- This file is part of Aquaria.
4--
5-- Aquaria is free software; you can redistribute it and/or
6-- modify it under the terms of the GNU General Public License
7-- as published by the Free Software Foundation; either version 2
8-- of the License, or (at your option) any later version.
9--
10-- This program is distributed in the hope that it will be useful,
11-- but WITHOUT ANY WARRANTY; without even the implied warranty of
12-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13--
14-- See the GNU General Public License for more details.
15--
16-- You should have received a copy of the GNU General Public License
17-- along with this program; if not, write to the Free Software
18-- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20if not v then v = {} end
21if not AQUARIA_VERSION then dofile("scripts/entities/entityinclude.lua") end
22
23v.n = 0
24
25v.dir = -1
26
27v.ing = 0
28
29v.mouthState = 0
30
31local MOUTH_IDLE	= 0
32local MOUTH_OPEN 	= 1
33local MOUTH_CLOSED	= 2
34
35local STATE_SPIT	= 1000
36
37v.holding = 0
38
39v.bite = 0
40
41v.eyeglow = 0
42
43function init(me)
44	setupEntity(me)
45	entity_setEntityType(me, ET_ENEMY)
46	entity_initSkeletal(me, "grouper")
47	--entity_setAllDamageTargets(me, false)
48
49	entity_setCollideRadius(me, 128)
50
51	entity_setHealth(me, 24)
52
53	--entity_setEntityLayer(me, 1)
54
55	entity_offset(me, 0, -40)
56	entity_offset(me, 0, 40, 3, -1, 1, 1)
57
58	entity_setState(me, STATE_IDLE)
59
60	v.eyeglow = entity_getBoneByName(me, "glow")
61	bone_setBlendType(v.eyeglow, BLEND_ADD)
62	bone_alpha(v.eyeglow, 0)
63
64	v.bite = entity_getBoneByName(me, "bite")
65	bone_alpha(v.bite, 0)
66
67	esetv(me, EV_ENTITYDIED, 1)
68
69	entity_setCullRadius(me, 800)
70
71	loadSound("grouper")
72	loadSound("grouper-hurt")
73	loadSound("grouper-die")
74
75	entity_setDeathSound(me, "grouper-die")
76
77	entity_setDeathScene(me, true)
78
79	entity_setDeathParticleEffect(me, "tinygreenexplode")
80
81	--entity_addVel(me, randVector(500))
82end
83
84local function doSetRenderPass(me, pass)
85	for i=0,5 do if i ~= 1 then bone_setRenderPass(entity_getBoneByIdx(me, i), pass) end end
86end
87
88local function closeMouth(me)
89	entity_stopAllAnimations(me)
90	entity_animate(me, "idle", -1)
91	entity_animate(me, "close", 0, 1)
92	v.mouthState = MOUTH_IDLE
93
94	debugLog("set render pass 0")
95	doSetRenderPass(me, 0)
96end
97
98function postInit(me)
99	v.n = getNaija()
100	entity_setTarget(me, v.n)
101end
102
103function entityDied(me, theIng)
104	if theIng == v.ing then
105		entity_stopAllAnimations(me)
106		entity_animate(me, "idle", -1)
107		entity_animate(me, "close", 0, 1)
108		v.ing = 0
109	end
110end
111
112local function checkMouth(me)
113	local bx, by = bone_getWorldPosition(v.bite)
114	if v.mouthState == MOUTH_OPEN then
115		if entity_isPositionInRange(v.n, bx, by, 96) then
116			v.holding = v.n
117
118			closeMouth(me)
119
120
121			entity_setState(me, STATE_WAIT, 2, 1)
122			v.ing = 0
123
124			debugLog("set render pass 3")
125			doSetRenderPass(me, 3)
126		end
127	end
128end
129
130function update(me, dt)
131	entity_updateMovement(me, dt)
132
133	--[[
134	--entity_addVel(me, v.dir*100*dt, 0)
135	entity_doCollisionAvoidance(me, dt, 32, 0.1)
136	entity_doCollisionAvoidance(me, dt, 16, 0.1)
137
138	if math.abs(entity_velx(me)) < 100 or math.abs(entity_vely(me)) < 100 then
139		entity_addVel(me, randVector(500))
140	end
141
142	entity_flipToVel(me)
143	]]--
144
145	local bx, by = bone_getWorldPosition(v.bite)
146
147	if entity_isState(me, STATE_IDLE) then
148		if v.ing == 0 then
149			v.ing = entity_getNearestEntity(me, "", 1024, ET_INGREDIENT, 0)
150			entity_setMaxSpeedLerp(me, 0.01, 0.2)
151			v.mouthState = MOUTH_IDLE
152		else
153			entity_doCollisionAvoidance(me, dt, 16, 0.5)
154			--debugLog(string.format("ing: %s", entity_getName(v.ing)))
155			if v.mouthState ~= MOUTH_OPEN then
156				spawnParticleEffect("bubble-release", bx, by)
157				entity_animate(me, "open", 0, 1)
158				v.mouthState = MOUTH_OPEN
159
160				debugLog("set render pass 3")
161				doSetRenderPass(me, 3)
162
163				entity_sound(me, "grouper")
164			end
165
166			entity_moveTowards(me, entity_x(v.ing)-64, entity_y(v.ing), dt, 2000)
167			if entity_isPositionInRange(me, entity_x(v.ing), entity_y(v.ing), 128) then
168				entity_sound(me, "gulp")
169				entity_delete(v.ing)
170				v.ing = 0
171				entity_stopAllAnimations(me)
172				entity_animate(me, "idle", -1)
173				entity_animate(me, "close", 0, 1)
174			end
175			entity_setMaxSpeedLerp(me, 2, 0.2)
176			checkMouth(me)
177		end
178	elseif entity_isState(me, STATE_OPEN) then
179		checkMouth(me)
180	end
181
182	if v.ing == 0 then
183		if v.mouthState == MOUTH_OPEN then
184			closeMouth(me)
185			entity_setState(me, STATE_IDLE)
186		end
187	end
188
189
190
191	if v.holding ~= 0 then
192		entity_setPosition(v.holding, bx, by)
193	end
194
195	if v.holding ~= 0 and not (entity_isState(me, STATE_WAIT) or entity_isState(me, STATE_SPIT)) then
196		entity_setState(me, STATE_SPIT, 1)
197	end
198
199	if v.holding == 0 and v.ing == 0 and v.mouthState ~= MOUTH_OPEN then
200		doSetRenderPass(me, 0)
201	end
202
203	entity_doCollisionAvoidance(me, dt, 10, 0.5)
204	entity_flipToVel(me)
205
206	if entity_isState(me, STATE_IDLE) then
207		if entity_touchAvatarDamage(me, entity_getCollideRadius(me), 0) then
208			if v.mouthState ~= MOUTH_OPEN and avatar_isBursting() and entity_setBoneLock(v.n, me) then
209				-- yay!
210			else
211				local x, y = entity_getVectorToEntity(me, v.n, 1000)
212				entity_addVel(v.n, x, y)
213			end
214		end
215	end
216
217	entity_handleShotCollisions(me)
218end
219
220function enterState(me)
221	if entity_isState(me, STATE_IDLE) then
222		entity_animate(me, "idle", -1)
223	elseif entity_isState(me, STATE_SPIT) then
224		local bx, by = bone_getWorldPosition(v.bite)
225		spawnParticleEffect("bubble-release", bx, by)
226		entity_animate(me, "open2", 0, 1)
227		if v.holding ~= 0 then
228			entity_clearVel(v.holding)
229			if entity_isfh(me) then
230				entity_push(v.holding, 5000, 0, 2, 5000, 0.5)
231				entity_addVel(v.holding, 5000, 0)
232			else
233				entity_push(v.holding, -5000, 0, 2, 5000, 0.5)
234				entity_addVel(v.holding, -5000, 0)
235			end
236		end
237		v.holding = 0
238	elseif entity_isState(me, STATE_WAIT) then
239		entity_setMaxSpeedLerp(me, 0, 2)
240	elseif entity_isState(me, STATE_OPEN) then
241		if entity_getBoneLockEntity(getNaija()) == me then
242			avatar_fallOffWall()
243		end
244		if v.mouthState ~= MOUTH_OPEN then
245			local bx, by = bone_getWorldPosition(v.bite)
246			spawnParticleEffect("bubble-release", bx, by)
247			entity_animate(me, "open", 0, 1)
248			v.mouthState = MOUTH_OPEN
249
250			debugLog("set render pass 3")
251			doSetRenderPass(me, 3)
252		end
253	elseif entity_isState(me, STATE_DEATHSCENE) then
254		entity_scale(me, 0, 0, 2)
255		entity_setStateTime(me, 2)
256	end
257end
258
259function exitState(me)
260	if entity_isState(me, STATE_WAIT) then
261		entity_setState(me, STATE_SPIT, 1)
262	elseif entity_isState(me, STATE_SPIT) then
263		closeMouth(me)
264		entity_setState(me, STATE_IDLE)
265	elseif entity_isState(me, STATE_OPEN) then
266		closeMouth(me)
267		v.mouthState = MOUTH_IDLE
268		entity_setState(me, STATE_IDLE)
269	end
270end
271
272function damage(me, attacker, bone, damageType, dmg)
273	entity_sound(me, "grouper-hurt")
274	v.n = getNaija()
275	entity_setMaxSpeedLerp(me, 2, 0)
276	entity_setMaxSpeedLerp(me, 0, 2)
277	entity_moveTowards(me, entity_x(v.n), entity_y(v.n), 1, -3000)
278	return true
279end
280
281function animationKey(me, key)
282end
283
284function hitSurface(me)
285end
286
287function songNote(me, note)
288	bone_alpha(v.eyeglow, 0.5, 1)
289	bone_setColor(v.eyeglow, getNoteColor(note))
290end
291
292function songNoteDone(me, note, timer)
293	if timer > 1 then
294		entity_setState(me, STATE_OPEN, 2)
295	end
296	bone_alpha(v.eyeglow, 0, 1)
297end
298
299function song(me, song)
300end
301
302function activate(me)
303end
304
305