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-- AGGRO HOPPER
25-- ================================================================================================
26
27-- specific
28local STATE_JUMP			= 1000
29local STATE_TRANSITION		= 1001
30local STATE_JUMPPREP		= 1002
31local STATE_WALK			= 1003
32
33-- ================================================================================================
34-- L O C A L  V A R I A B L E S
35-- ================================================================================================
36
37v.jumpDelay = 0
38v.moveTimer = 0
39v.rotateOffset = 0
40v.angry = false
41v.enraged = false
42v.moving = 0.2
43v.n = 0
44
45v.fireDelay = 0
46
47v.lx = 0
48v.ly = 0
49
50v.bubbleRelease = 0
51
52-- ================================================================================================
53-- FUNCTIONS
54-- ================================================================================================
55
56--[[
57function land(me)
58	entity_clampToSurface(me)
59	entity_moveAlongSurface(me, dt, 1, 6, 24)
60	entity_rotateToSurfaceNormal(me, 0.1)
61end
62]]--
63
64function init(me)
65
66	setupBasicEntity(
67	me,
68	"",								-- texture
69	12,								-- health
70	2,								-- manaballamount
71	2,								-- exp
72	10,								-- money
73	40,								-- collideRadius
74	STATE_IDLE,						-- initState
75	128,							-- sprite width
76	128,							-- sprite height
77	1,								-- particle "explosion" type, 0 = none
78	0,								-- 0/1 hit other entities off/on (uses collideRadius)
79	4000							-- updateCull -1: disabled, default: 4000
80	)
81	entity_initSkeletal(me, "Mermog")
82
83	entity_setDeathParticleEffect(me, "TinyGreenExplode")
84
85	--land(me)
86	entity_setWeight(me, 1000)
87	entity_setState(me, STATE_IDLE)
88
89	entity_setBounceType(me, BOUNCE_REAL)
90	entity_setBounce(me, 0.9)
91	--entity_setClampOnSwitchDir(me, false)
92	esetv(me, EV_SWITCHCLAMP, 0)
93	esetv(me, EV_WALLOUT, 64)--18)
94
95	entity_clampToSurface(me)
96
97	entity_setCullRadius(me, 512)
98
99	entity_setDeathSound(me, "mermog-die")
100
101	loadSound("mermog-die")
102	loadSound("mermog-jump")
103end
104
105function postInit(me)
106	v.n = getNaija()
107end
108
109function update(me, dt)
110	v.lx, v.ly = entity_getPosition(me)
111
112	if v.enraged then
113		dt = dt * 1.25
114	end
115	--[[
116	if entity_hasTarget(me) then
117		if entity_isTargetInRange(me, 64) then
118			entity_hurtTarget(me, 1)
119			entity_pushTarget(me, 500)
120		end
121	end
122	]]--
123	if entity_isState(me, STATE_IDLE) or entity_isState(me, STATE_TRANSITION) then
124		entity_moveAlongSurface(me, dt, 0.5, 6)
125		--entity_switchSurfaceDirection(me)
126	end
127	if entity_getState(me)==STATE_IDLE then
128		if not(entity_hasTarget(me)) then
129			entity_findTarget(me, 1200)
130		else
131			if entity_isTargetInRange(me, 900) then
132				v.jumpDelay = v.jumpDelay - dt
133				if v.jumpDelay < 0 then
134					v.angry = true
135					v.jumpDelay = 0.2
136					entity_setState(me, STATE_JUMPPREP)
137				end
138			end
139		end
140	elseif entity_isState(me, STATE_WALK) then
141		if entity_isfh(me) then
142			entity_switchSurfaceDirection(me, 0)
143		else
144			entity_switchSurfaceDirection(me, 1)
145		end
146		entity_moveAlongSurface(me, dt, 1000*v.moving, 6)
147		entity_rotateToSurfaceNormal(me, 0.1)
148	elseif entity_getState(me)==STATE_JUMPPREP then
149		if not entity_isAnimating(me) then
150			entity_setState(me, STATE_JUMP)
151		end
152	elseif entity_getState(me)==STATE_JUMP then
153		--entity_flipToVel(me)
154		entity_updateCurrents(me, dt)
155		entity_updateMovement(me, dt)
156
157		v.bubbleRelease = v.bubbleRelease - dt
158		if v.bubbleRelease < 0 then
159			v.bubbleRelease = 0.4
160			spawnParticleEffect("bubble-release-short", entity_x(me), entity_y(me))
161		end
162
163		--[[
164		v.fireDelay = v.fireDelay - dt
165		if v.fireDelay < 0 then
166			v.fireDelay = 0.4
167			s = createShot("mermog-shot", me, v.n, entity_x(me), entity_y(me))
168		end
169		]]--
170	elseif not(entity_getState(me)==STATE_TRANSITION or entity_isState(me, STATE_WALK)) then
171		entity_updateMovement(me, dt)
172	end
173
174
175	--[[
176	if isObstructed(entity_x(me), entity_y(me)) then
177		entity_setPosition(me, v.lx, v.ly)
178		entity_clampToSurface(me)
179	end
180	]]--
181
182	entity_handleShotCollisions(me)
183	entity_touchAvatarDamage(me, 64, 1, 500)
184
185	if isObstructed(entity_x(me), entity_y(me)) then
186		entity_adjustPositionBySurfaceNormal(me, 1)
187	end
188end
189
190function damage(me, attacker, bone, damageType, dmg)
191	if entity_isState(me, STATE_IDLE) then
192		entity_setState(me, STATE_JUMPPREP)
193	end
194	return true
195end
196
197v.bounces = 0
198function hitSurface(me)
199	if entity_getState(me)==STATE_JUMP then
200		local t = egetvf(me, EV_CLAMPTRANSF)
201		if entity_checkSurface(me, 6, STATE_TRANSITION, t) then
202			entity_rotateToSurfaceNormal(me, 0)
203			entity_scale(me, 1, 0.5)
204			entity_scale(me, 1, 1, t)
205			--[[
206			entity_setInternalOffset(me, 0, 64)
207			entity_setInternalOffset(me, 0, 0, t)
208			]]--
209		else
210			local nx, ny = getWallNormal(entity_getPosition(me))
211			nx, ny = vector_setLength(nx, ny, 400)
212			entity_addVel(me, nx, ny)
213		end
214		--[[
215		if entity_isNearObstruction(me, 4, OBSCHECK_4DIR) then
216			entity_clampToSurface(me)
217			entity_setState(me, STATE_TRANSITION, 0.001)
218		end
219		]]--
220	end
221	--[[
222	if entity_getState(me)==STATE_JUMP then
223		local nx, ny = getWallNormal(cx, cy)
224		if ny < 0 then
225			land(me)
226			entity_setState(me, STATE_TRANSITION)
227		else
228			v.bounces = v.bounces + 1
229			if v.bounces > 8 then
230				land(me)
231				entity_setState(me, STATE_TRANSITION)
232			end
233		end
234	end
235	]]--
236end
237
238function enterState(me)
239	if entity_getState(me)==STATE_IDLE then
240		entity_animate(me, "idle", LOOP_INF)
241		entity_setMaxSpeed(me, 1200)
242	elseif entity_getState(me)==STATE_JUMPPREP then
243
244		local nx, ny = entity_getNormal(me)
245		for i=20,400,20 do
246
247			local nx1, ny1 = vector_setLength(nx, ny, i)
248			local tx = entity_x(me) + nx1
249			local ty = entity_y(me) + ny1
250			--debugLog(string.format("t(%d, %d)", tx, ty))
251			if isObstructed(tx, ty) then
252				--debugLog("idle!")
253				entity_setState(me, STATE_WALK, 2, 1)
254				return
255			end
256		end
257
258		entity_animate(me, "jumpPrep")
259		entity_flipToEntity(me, entity_getTarget(me))
260	elseif entity_isState(me, STATE_TRANSITION) then
261		entity_setStateTime(me, entity_animate(me, "land"))
262	elseif entity_isState(me, STATE_WALK) then
263		entity_animate(me, "walk", -1)
264	elseif entity_getState(me)==STATE_JUMP then
265		v.bounces = 0
266		entity_flipToEntity(me, entity_getTarget(me))
267		entity_animate(me, "jump")
268
269		v.rotateOffset = 0
270		entity_applySurfaceNormalForce(me, 1000)
271		if entity_isfh(me) then
272			entity_addVel(me, 400, 0)
273		else
274			entity_addVel(me, -400, 0)
275		end
276		entity_adjustPositionBySurfaceNormal(me, 10)
277
278		entity_sound(me, "mermog-jump")
279		--entity_adjustPositionBySurfaceNormal(me, 32)
280	end
281end
282
283function animationKey(me, key)
284end
285
286function exitState(me)
287	if entity_getState(me)==STATE_TRANSITION then
288		entity_setState(me, STATE_IDLE)
289		if chance(50) then
290			entity_setState(me, STATE_WALK, 2)
291		end
292	elseif entity_getState(me)==STATE_WALK then
293		entity_setState(me, STATE_IDLE)
294	end
295end
296
297function dieNormal(me)
298	if chance(20) then
299		local x, y = entity_getNormal(me)
300		x, y = vector_setLength(x, y, 64)
301		spawnIngredient("PlantLeaf", entity_x(me)+x, entity_y(me)+y)
302	end
303end
304