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
23-- ================================================================================================
24-- M A N T I S
25-- ================================================================================================
26
27-- ================================================================================================
28-- L O C A L  V A R I A B L E S
29-- ================================================================================================
30
31local STATE_SCREECH			= 1000
32
33local STATE_FIREBOMBS		= 1001
34local STATE_FALL			= 1002
35local STATE_FLOAT			= 1003
36local STATE_SHOOT			= 1004
37local STATE_ATTACK			= 1005
38local STATE_HURT			= 1006
39local STATE_RECOVER			= 1007
40local STATE_DIE				= 1008
41
42v.n = 0
43v.fight = false
44v.cut = false
45
46v.spawnPoint = 0
47
48
49v.phaseDelayTime			= 1.5
50v.phaseDelay				= v.phaseDelayTime
51
52v.phase					= 0
53
54local PHASE_NONE			= 0
55local PHASE_SHOTS			= 2
56local PHASE_FIREBOMBS		= 1
57local PHASE_MAX				= 2
58
59v.startx = 0
60v.starty = 0
61
62v.hits = 3
63
64v.body = 0
65
66v.item = 0
67
68-- ================================================================================================
69-- FUNCTIONS
70-- ================================================================================================
71
72function init(me)
73	setupBasicEntity(
74	me,
75	"",								-- texture
76	999,							-- health
77	1,								-- manaballamount
78	1,								-- exp
79	1,								-- money
80	32,								-- collideRadius (for hitting entities + spells)
81	STATE_IDLE,						-- initState
82	512,							-- sprite width
83	512,							-- sprite height
84	1,								-- particle "explosion" type, 0 = none
85	0,								-- 0/1 hit other entities off/on (uses collideRadius)
86	5500							-- updateCull -1: disabled, default: 4000
87	)
88
89	entity_initSkeletal(me, "mantis")
90	entity_animate(me, "idle", -1)
91
92	entity_generateCollisionMask(me)
93
94	entity_setState(me, STATE_IDLE)
95
96	entity_setDeathParticleEffect(me, "TinyGreenExplode")
97
98	entity_setCanLeaveWater(me, true)
99
100
101
102	entity_scale(me, 2, 2)
103
104	--entity_setAllDamageTargets(me, false)
105	entity_setDamageTarget(me, DT_AVATAR_VINE, true)
106
107	entity_setDamageTarget(me, DT_ENEMY, true)
108	entity_setDamageTarget(me, DT_ENEMY_MANTISBOMB, true)
109
110
111	loadSound("mantis-fall")
112	loadSound("mantis-bomb")
113	loadSound("mantis-die")
114	loadSound("mantis-fire")
115	loadSound("mantis-roar")
116
117
118	v.body = entity_getBoneByName(me, "body")
119	v.spawnPoint = entity_getBoneByName(me, "spawnpoint")
120
121	bone_alpha(v.spawnPoint, 0)
122
123
124end
125
126function postInit(me)
127	v.n = getNaija()
128
129	v.item = entity_getNearestEntity(me, "healthupgrade4")
130	--debugLog(string.format("item: %d", v.item))
131
132	if not entity_isFlag(me, 0) then
133		entity_delete(me)
134	else
135		entity_alpha(v.item, 0)
136	end
137
138	v.startx = entity_x(me)
139	v.starty = entity_y(me)
140end
141
142function update(me, dt)
143	--entity_updateMovement(me, dt)
144	if not v.fight and entity_isEntityInRange(me, v.n, 1400) then
145		if not v.cut then
146			v.cut = true
147			v.fight = true
148			entity_idle(v.n)
149			playMusic("MiniBoss")
150			cam_toEntity(me)
151			watch(0.5)
152			playSfx("mantis-roar")
153			watch(1.5)
154			cam_toEntity(v.n)
155			v.cut = false
156		end
157	end
158
159	if v.fight and not entity_isEntityInRange(me, getNaija(), 3800) then
160		v.fight = false
161		updateMusic()
162		v.phase = 0
163	end
164
165
166	if v.fight then
167		if entity_isState(me, STATE_IDLE) then
168			v.phaseDelay = v.phaseDelay - dt
169			if v.phaseDelay < 0 then
170				debugLog("phaseDelay 0")
171				v.phase = v.phase + 1
172				if v.phase == PHASE_FIREBOMBS then
173					debugLog("firebombs")
174					entity_setState(me, STATE_FIREBOMBS)
175				end
176				if v.phase == PHASE_MAX then
177					v.phase = 0
178				end
179				v.phaseDelay = v.phaseDelayTime
180			end
181		end
182	end
183
184	if v.fight then
185		entity_handleShotCollisionsSkeletal(me)
186
187		local bone = entity_collideSkeletalVsCircle(me, v.n)
188		if bone ~= 0 then
189			entity_touchAvatarDamage(me, 0, 1, 500)
190		end
191	end
192end
193
194function hitSurface(me)
195end
196
197function enterState(me)
198	if entity_isState(me, STATE_IDLE) then
199		entity_offset(me, 0, 0, 0.1)
200		entity_animate(me, "idle", -1)
201	--[[
202	elseif entity_isState(me, STATE_) then
203	elseif entity_isState(me, STATE_) then
204	]]--
205
206	elseif entity_isState(me, STATE_FIREBOMBS) then
207		entity_setStateTime(me, entity_animate(me, "firebombs"))
208	elseif entity_isState(me, STATE_FALL) then
209		playSfx("mantis-fall")
210
211		entity_animate(me, "hurt", 4, 1)
212		local nd = getNode("mantisfall")
213		local nx = node_x(nd)
214		local ny = node_y(nd)
215		entity_setPosition(me, nx, ny, 1)
216
217		entity_setStateTime(me, 6)
218
219
220
221	elseif entity_isState(me, STATE_RECOVER) then
222		entity_setPosition(me, v.startx, v.starty, 1, 0, 0, 1)
223		entity_setStateTime(me, 1)
224		for i=1,3 do
225			v = entity_getNearestEntity(me, "UberVine")
226			if v ~= 0 then
227				entity_delete(v)
228			end
229		end
230	elseif entity_isState(me, STATE_HURT) then
231		playSfx("mantis-roar")
232
233		entity_offset(me, -10, 0)
234		entity_offset(me, 10, 0, 0.1, -1, 1)
235		bone_damageFlash(v.body)
236		v.hits = v.hits - 1
237
238		shakeCamera(10, 3)
239
240		entity_setStateTime(me, 1)
241	elseif entity_isState(me, STATE_DIE) then
242		entity_idle(v.n)
243		entity_flipToEntity(v.n, me)
244
245		local e = getFirstEntity()
246		while e ~= 0 do
247			if entity_isName(e, "mantis-bomb") then
248				entity_msg(e, "exp")
249				watch(0.2)
250			end
251			e = getNextEntity()
252		end
253
254		spawnParticleEffect("gateway-die", entity_x(me), entity_y(me))
255		fadeOutMusic(1)
256		cam_toEntity(me)
257		playSfx("mantis-roar")
258		entity_setStateTime(me, entity_animate(me, "die"))
259		entity_setFlag(me, 1)
260		setFlag(FLAG_MINIBOSS_MANTISSHRIMP, 1)
261	end
262end
263
264function exitState(me)
265	if entity_isState(me, STATE_FIREBOMBS) or entity_isState(me, STATE_RECOVER) then
266		entity_setState(me, STATE_IDLE, -1)
267	elseif entity_isState(me, STATE_HURT) then
268		entity_offset(me, 0, 0, 0.1)
269		if v.hits <= 0 then
270			entity_setState(me, STATE_DIE)
271		else
272			entity_setState(me, STATE_RECOVER)
273		end
274
275	elseif entity_isState(me, STATE_DIE) then
276		playSfx("mantis-die")
277		spawnParticleEffect("gateway-die", entity_x(me), entity_y(me))
278		cam_toEntity(getNaija())
279		entity_delete(me)
280
281		entity_alpha(v.item, 1)
282
283		pickupGem("boss-mantis")
284	elseif entity_isState(me, STATE_FALL) then
285		entity_setState(me, STATE_RECOVER)
286	end
287end
288
289function animationKey(me, key)
290	if entity_isState(me, STATE_FIREBOMBS) then
291		if key == 1 or key == 3 or (key == 5 and chance(100)) or (key==6 and chance(50)) then
292
293
294			local sx, sy = bone_getWorldPosition(v.spawnPoint)
295			local e = createEntity("mantis-bomb", "", sx, sy)
296			if key == 1 then
297				entity_moveTowards(e, entity_x(v.n), entity_y(v.n), 1000, 1)
298			else
299				entity_addVel(e, math.random(500)-250, 300)
300			end
301
302			playSfx("mantis-fire")
303		end
304	end
305end
306
307function damage(me, attacker, bone, damageType, dmg)
308	if damageType == DT_ENEMY_MANTISBOMB then
309		--debugLog("MANTIS BOMB DAMAGE TYPE")
310		if entity_isState(me, STATE_FALL) then
311			entity_setState(me, STATE_HURT)
312			shakeCamera(10, 2)
313			return false
314		end
315	end
316	if damageType == DT_AVATAR_VINE then
317		if entity_isState(me, STATE_IDLE) or entity_isState(me, STATE_FIREBOMBS) then
318			entity_setState(me, STATE_FALL)
319		end
320	end
321	if damageType == DT_AVATAR_ENERGYBLAST or damageType == DT_AVATAR_SHOCK then
322		playNoEffect()
323		return false
324	end
325	return false
326end
327
328