local curOIcon = 'xeno';
local lastOIcon = curOIcon;

local curPIcon = 'zephthanks';
local lastPIcon = curPIcon;

local format = string.format;
function RGBToHex(tabl) return format('%.2x%.2x%.2x', tabl[1], tabl[2], tabl[3]); end

local colIcon = {
	['zephthanks'] = RGBToHex({102, 68, 154}),
	['godz'] = RGBToHex({78, 74, 102}),
	['tenma'] = RGBToHex({163, 73, 164}),
	
	['xeno'] = RGBToHex({0, 60, 170}),
	['lordx'] = RGBToHex({52, 75, 90}),
	['mazin'] = RGBToHex({35, 34, 165})
};
local dir = {[0] = 'singLEFT', 'singDOWN', 'singUP', 'singRIGHT'};
function onCreate()
	luaDebugMode = true;
	makeLuaSprite('bg', 'feast/GODFEAST', -700, -600);
	addLuaSprite('bg');
	
	makeAnimatedLuaSprite('sunkyAndMitee', 'feast/sunkyAndMitee', 450);
	addAnimationByIndices('sunkyAndMitee', 'danceLeft', 'Idle', {1, 2, 3, 4, 5, 6, 7, 8, 9});
	addAnimationByIndices('sunkyAndMitee', 'danceRight', 'Idle', {15, 16, 17, 18, 19, 20, 21, 22, 23});
	addLuaSprite('sunkyAndMitee');
	
	makeLuaSprite('table', 'feast/table', -300, 154);
	addLuaSprite('table');
	
	
	createInstance('mazin', 'objects.Character', {129, -131, 'feast-mazin'});
	addLuaSprite('mazin');
	
	createInstance('lordx', 'objects.Character', {-319, -107, 'feast-x'});
	addLuaSprite('lordx');
	
	
	createInstance('tenma', 'objects.Character', {816, 26, 'feast-tenma'});
	addLuaSprite('tenma');
	
	createInstance('godz', 'objects.Character', {821, -310, 'feast-z'});
	addLuaSprite('godz');
	
	
	makeAnimatedLuaSprite('fleetway', 'feast/fleetway', -810 - 64, -100 - 78);
	addAnimationByPrefix('fleetway', 'idle', 'Idle');
	playAnim('fleetway', 'idle', true);
	setProperty('fleetway.animation.curAnim.loopPoint', 8);
	setScrollFactor('fleetway', 1.2, 1.2);
	addLuaSprite('fleetway', true);
	
	makeAnimatedLuaSprite('scourge', 'feast/scourge', 1600 + 42, -300 - 78);
	addAnimationByPrefix('scourge', 'idle', 'Idle');
	playAnim('scourge', 'idle', true);
	setProperty('scourge.animation.curAnim.loopPoint', 8);
	setScrollFactor('scourge', 1.2, 1.2);
	addLuaSprite('scourge', true);
	
	
	makeLuaText('lyrics', '', 1280, 0, 570);
	setTextSize('lyrics', 24);
	setTextBorder('lyrics', 1, '000000');
	setTextFont('lyrics', 'PressStart2P.ttf');
	setObjectCamera('lyrics', 'camOther');
	addLuaText('lyrics');
	
	makeLuaSprite('fadeSpr', nil, -20, -20);
	makeGraphic('fadeSpr', 1, 1, '000000');
	scaleObject('fadeSpr', 1320, 760);
	setObjectCamera('fadeSpr', 'other');
	addLuaSprite('fadeSpr');
	setProperty('fadeSpr.alpha', 0.00001);
	
	
	cacheStuff();
end

function cacheStuff()
	makeAnimatedLuaSprite('zephRhyme', 'characters/mighty/rhyme', 300);
	addLuaSprite('zephRhyme');
	setProperty('zephRhyme.alpha', 0.00001);
	
	
	makeLuaSprite('godIconCache', 'icons/icon-godz');
	setObjectCamera('godIconCache', 'hud');
	addLuaSprite('godIconCache');
	setProperty('godIconCache.alpha', 0.00001);
	
	makeLuaSprite('tenmaIconCache', 'icons/icon-tenma');
	setObjectCamera('tenmaIconCache', 'hud');
	addLuaSprite('tenmaIconCache');
	setProperty('tenmaIconCache.alpha', 0.00001);
	
	
	makeLuaSprite('lordIconCache', 'icons/icon-lordx');
	setObjectCamera('lordIconCache', 'hud');
	addLuaSprite('lordIconCache');
	setProperty('lordIconCache.alpha', 0.00001);
	
	makeLuaSprite('mazinIconCache', 'icons/icon-mazin');
	setObjectCamera('mazinIconCache', 'hud');
	addLuaSprite('mazinIconCache');
	setProperty('mazinIconCache.alpha', 0.00001);
end

function onCreatePost()
	setProperty('camGame.scroll.x', 755 - 640);
	setProperty('camGame.scroll.y', 30 - 360);
	
	setProperty('camFollow.x', 755);
	setProperty('camFollow.y', 140);
end

function onSongStart()
	setProperty('zephRhyme.alpha', 0);
	
	setProperty('godIconCache.alpha', 0);
	setProperty('tenmaIconCache.alpha', 0);
	setProperty('lordIconCache.alpha', 0);
	setProperty('mazinIconCache.alpha', 0);
	
	setTimeBarColors(colIcon['xeno']);
end

function onUpdatePost()
	if lastOIcon ~= curOIcon then
		lastOIcon = curOIcon;
		callMethod('iconP2.changeIcon', {curOIcon});
		setHealthBarColors(colIcon[curOIcon]);
		setHealth(getHealth());
	end
	
	if lastPIcon ~= curPIcon then
		lastPIcon = curPIcon;
		callMethod('iconP1.changeIcon', {curPIcon});
		setHealthBarColors(nil, colIcon[curPIcon]);
		setHealth(getHealth());
	end
end

function onCountdownTick(t) checkBeat(t); end
function onBeatHit() checkBeat(curBeat); end

local beatCheck = {'tenma', 'godz', 'mazin', 'lordx'};
local danced = false;
function checkBeat(b)
	if b % 2 == 0 then
		for _, s in pairs(beatCheck) do
			if not getProperty(s .. '.animation.name'):startsWith('sing') then callMethod(s .. '.dance'); end
		end
		
		danced = not danced;
		playAnim('sunkyAndMitee', (danced and 'danceRight' or 'danceLeft'), true);
		
		playAnim('fleetway', 'idle', true);
		playAnim('scourge', 'idle', true);
	end
end

function onMoveCamera(d)
	setProperty('camFollow.y', 140);
	if d == 'dad' then setProperty('camFollow.x', 622);
	else setProperty('camFollow.x', 872); end
end

local goodNote = {
	[''] = function(_, d)
		curPIcon = 'zephthanks';
		for _, s in pairs({'godz', 'tenma'}) do
			playAnim(s, dir[d], true);
			setProperty(s .. '.holdTimer', 0);
		end
	end,
	
	['Zeph Feast'] = function(_, d)
		curPIcon = 'zephthanks';
		playAnim('boyfriend', dir[d], true);
		setProperty('boyfriend.holdTimer', 0);
	end,
	['GodZ Feast'] = function(_, d)
		curPIcon = 'godz';
		playAnim('godz', dir[d], true);
		setProperty('godz.holdTimer', 0);
	end,
	['Tenma Feast'] = function(_, d)
		curPIcon = 'tenma';
		playAnim('tenma', dir[d], true);
		setProperty('tenma.holdTimer', 0);
	end
};
goodNote['Xeno Feast'] = function(_, d) goodNote['Zeph Feast'](_, d); end
goodNote['LordX Feast'] = function(_, d) goodNote['GodZ Feast'](_, d); end
goodNote['Mazin Feast'] = function(_, d) goodNote['Tenma Feast'](_, d); end

function goodNoteHit(i, d, n, s)
	local a = goodNote[n];
	if a then a(i, d, s); end
end

local oppNote = {
	[''] = function(_, d)
		curOIcon = 'xeno';
		for _, s in pairs({'lordx', 'mazin'}) do
			playAnim(s, dir[d], true);
			setProperty(s .. '.holdTimer', 0);
		end
	end,
	
	['Xeno Feast'] = function(_, d)
		curOIcon = 'xeno';
		playAnim('dad', dir[d], true);
		setProperty('dad.holdTimer', 0);
	end,
	['LordX Feast'] = function(_, d)
		curOIcon = 'lordx';
		playAnim('lordx', dir[d], true);
		setProperty('lordx.holdTimer', 0);
	end,
	['Mazin Feast'] = function(_, d)
		curOIcon = 'mazin';
		playAnim('mazin', dir[d], true);
		setProperty('mazin.holdTimer', 0);
	end
};
oppNote['Zeph Feast'] = function(_, d) oppNote['Xeno Feast'](_, d); end
oppNote['GodZ Feast'] = function(_, d) oppNote['LordX Feast'](_, d); end
oppNote['Tenma Feast'] = function(_, d) oppNote['Mazin Feast'](_, d); end
function opponentNoteHit(i, d, n, s)
	local a = oppNote[n];
	if a then a(i, d, s); end
end

local missNote = {
	[''] = function(_, d)
		curPIcon = 'zephthanks';
		for _, s in pairs({'godz', 'tenma'}) do
			playAnim(s, dir[d] .. 'miss', true);
			setProperty(s .. '.holdTimer', 0);
		end
	end,
	
	['Zeph Feast'] = function(_, d) curPIcon = 'zephthanks'; end,
	['GodZ Feast'] = function(_, d)
		curPIcon = 'godz';
		playAnim('godz', dir[d] .. 'miss', true);
		setProperty('godz.holdTimer', 0);
	end,
	['Tenma Feast'] = function(_, d)
		curPIcon = 'tenma';
		playAnim('tenma', dir[d] .. 'miss', true);
		setProperty('tenma.holdTimer', 0);
	end
};
function noteMiss(i, d, n, s)
	local a = missNote[n];
	if a then a(i, d, s); end
end

local events = {
	['Feast Rhyme'] = function(v1)
		if v1 == 'yay' then
			curPIcon = 'zephthanks';
			setProperty('boyfriend.skipDance', true);
			playAnim('boyfriend', 'rhyme', true);
			
			setProperty('defaultCamZoom', 0.8);
			setProperty('camZoomingMult', 0);
			doTweenAlpha('camOut', 'camHUD', 0, (crochet / 2000) / playbackRate);
		elseif v1 == 'hey' then
			doTweenAlpha('camOut', 'camHUD', 1, (crochet / 250) / playbackRate);
		elseif v1 == 'nay' then
			setProperty('camZoomingMult', 1);
			setProperty('boyfriend.skipDance', false);
			setProperty('defaultCamZoom', 0.65);
		end
	end,
	
	['Lyrics'] = function(v1, v2)
		local c = ((v2 == '') and 'ffffff' or v2);
		setTextColor('lyrics', c);
		setTextString('lyrics', v1);
	end,
	
	['Dark Fade'] = function() doTweenAlpha('fadeOut', 'fadeSpr', 1, 2 / playbackRate); end
};
function onEvent(n, v1, v2)
	local a = events[n];
	if a then a(v1, v2); end
end

function eventEarlyTrigger(n)
	if n == 'Feast Rhyme' then return (2 / 24); end
end

function onGameOver() callOnLuas('killCheck', {curPIcon}); return Function_Continue; end

local sub = string.sub;
function string.startsWith(b, s) return sub(b, 1, #s) == s; end
