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-- Simon Says: "Eight Eyed Monster!"
24-- BOROMAL
25-- idle : waiting for user to click
26-- playSeg : playing segment of song
27-- wait : wait for user input
28-- game over : user was too slow
29-- victory
30
31local STATE_PLAYSEG		= 1000
32local STATE_WAIT		= 1001
33local STATE_GAMEOVER	= 1002
34local STATE_VICTORY		= 1003
35
36v.songLen = 8
37v.waitTime = 4.5
38v.curNote = 1
39v.onNote = 1
40v.noteDelay = 0
41v.userNote = 1
42
43v.song = nil
44v.eye = nil
45v.center = 0
46v.centerEye = 0
47v.body = 0
48
49v.idolWeight = 200
50
51local function generateSong()
52	for i=1, 10 do
53		v.song[i] = math.random(8)-1
54    end
55end
56
57function init(me)
58	v.song = {}
59	v.eye = {}
60
61	setupEntity(me)
62	entity_initSkeletal(me, "Simon")
63	entity_setEntityType(me, ET_NEUTRAL)
64
65	entity_setActivation(me, AT_CLICK, 64, 512)
66	for	i=1, 8 do
67		v.eye[i] = entity_getBoneByIdx(me, i)
68		--entity_getBoneByName(string.format("Eye%d",i))
69		bone_setColor(v.eye[i], getNoteColor(i-1))
70		bone_alpha(v.eye[i], 0)
71	end
72	v.center = entity_getBoneByName(me, "Center")
73	v.centerEye = entity_getBoneByName(me, "CenterEye")
74	v.body = entity_getBoneByName(me, "Body")
75	v.skirtLeft = entity_getBoneByName(me, "SkirtLeft")
76	v.skirtRight = entity_getBoneByName(me, "SkirtRight")
77
78	entity_animate(me, "idle")
79
80	entity_setState(me, STATE_IDLE)
81
82	entity_offset(me, 0, 20, 2, -1, 1, 1)
83
84	entity_setCullRadius(me, 1024)
85
86	bone_setSegs(v.body, 2, 32, 0.3, 0.3, -0.018, 0, 6, 1)
87	bone_setSegs(v.skirtLeft, 2, 32, -0.3, 0.3, -0.018, 0, 6, 1)
88	bone_setSegs(v.skirtRight, 2, 32, -0.3, 0.3, -0.018, 0, 6, 1)
89	entity_setUpdateCull(me, 2500)
90end
91
92function postInit(me)
93	if entity_isFlag(me, 1) then
94		-- FormUpgradeEnergy2
95		local ent = createEntity("upgrade-wok", "", entity_getPosition(me))
96		entity_setWeight(ent, v.idolWeight)
97	end
98end
99
100function update(me, dt)
101	if entity_isState(me, STATE_PLAYSEG) then
102		v.noteDelay = v.noteDelay + dt
103		if v.noteDelay > 0.5 - (v.onNote*0.02) then
104			v.noteDelay = 0
105			playSfx(string.format("MenuNote%d", v.song[v.curNote]))
106			local theEye = v.eye[v.song[v.curNote]+1]
107			--bone_alpha(theEye, 0)
108			bone_scale(theEye, 1, 0)
109			bone_scale(theEye, 1, 1, 0.25, 1, -1)
110			bone_alpha(theEye, 1)
111			--bone_alpha(, 1, 0.25, 1, -1)
112			if v.curNote >= v.onNote then
113				entity_setState(me, STATE_WAIT, v.waitTime)
114			end
115			v.curNote = v.curNote + 1
116		end
117	end
118	local nx,ny = entity_getPosition(getNaija())
119	local sx,sy = entity_getPosition(me)
120	local x = (nx-sx)*0.75
121	local y = (ny-sy)*0.75
122	x,y = vector_cap(x, y, 20)
123	bone_setPosition(v.centerEye, x, y, 1.0)
124end
125
126function activate(me)
127	--debugLog("ACTIVATE!")
128	entity_setActivationType(me, AT_NONE)
129	v.curNote = 1
130	v.userNote = 1
131	entity_setState(me, STATE_PLAYSEG)
132	musicVolume(0.5, 0.5)
133end
134
135function enterState(me)
136	local colorT = 0.2
137	if entity_isState(me, STATE_IDLE) then
138		--debugLog("IDLE!")
139		entity_setActivationType(me, AT_CLICK)
140		generateSong()
141		v.onNote = 1
142		v.curNote = 1
143		v.userNote =1
144		bone_setColor(v.center, 0.5, 0.5, 1, colorT)
145	elseif entity_isState(me, STATE_VICTORY) then
146		--debugLog("VICTORY!")
147		musicVolume(1, 1)
148		if entity_isFlag(me, 0) then
149			playSfx("secret")
150			local ent = createEntity("upgrade-wok", "", entity_getPosition(me))
151			entity_alpha(ent, 0)
152			entity_alpha(ent, 1, 0.2)
153			entity_setWeight(ent, v.idolWeight)
154			entity_setFlag(me, 1)
155		else
156			playSfx("secret")
157			local r = randRange(1, 70)
158			if r < 10 then
159				spawnIngredient("PlantLeaf", entity_getPosition(me))
160			elseif r < 20 then
161				spawnIngredient("FishOil", entity_getPosition(me))
162			elseif r < 30 then
163				spawnIngredient("SmallEgg", entity_getPosition(me))
164			elseif r < 40 then
165				spawnIngredient("SmallEye", entity_getPosition(me))
166			elseif r < 50 then
167				spawnIngredient("SeaCake", entity_getPosition(me))
168			elseif r < 60 then
169				spawnIngredient("HandRoll", entity_getPosition(me))
170			else
171				spawnIngredient("SmallBone", entity_getPosition(me))
172			end
173		end
174		entity_setStateTime(me, 3)
175	elseif entity_isState(me, STATE_GAMEOVER) then
176		musicVolume(1, 1)
177		shakeCamera(10, 1)
178		for	i=1, 8 do
179			bone_alpha(v.eye[i], 0)
180		end
181		--debugLog("GAMEOVER!")
182		playSfx("BeastForm")
183		entity_setStateTime(me, 2)
184		bone_setColor(v.center, 1, 0.5, 0.5, colorT)
185	elseif entity_isState(me, STATE_WAIT) then
186		--[[
187		for	i=1, 8 do
188			bone_alpha(v.eye[i], 0)
189		end
190		]]--
191		--debugLog("WAIT!")
192		v.userNote = 1
193		bone_setColor(v.center, 0.5, 1, 0.5, colorT)
194	elseif entity_isState(me, STATE_PLAYSEG) then
195		v.curNote = 1
196		--debugLog("PLAYSEG!")
197		bone_setColor(v.center, 0.5, 0.5, 1, colorT)
198	end
199end
200
201function exitState(me)
202	if entity_isState(me, STATE_WAIT) then
203		entity_setState(me, STATE_GAMEOVER)
204	elseif entity_isState(me, STATE_GAMEOVER) or entity_isState(me, STATE_VICTORY) then
205		entity_setState(me, STATE_IDLE)
206	end
207end
208
209function songNote(me, note)
210	--debugLog("songNote!")
211	if entity_isState(me, STATE_WAIT) then
212		if note == v.song[v.userNote] then
213			v.userNote = v.userNote + 1
214			if v.userNote > v.onNote then
215				if v.userNote > v.songLen then
216					entity_setState(me, STATE_VICTORY)
217				else
218					v.onNote = v.onNote + 1
219					entity_setState(me, STATE_PLAYSEG)
220				end
221			end
222		else
223			entity_setState(me, STATE_GAMEOVER)
224		end
225	end
226end
227
228function hitSurface(me)
229	--entity_sound(me, "rock-hit")
230end
231
232function damage(me, attacker, bone, damageType, dmg)
233	return false
234end
235