local cond_cb_result= not GAMESTATE:IsPlayerEnabled(PLAYER_1)

local cond_db_result= not GAMESTATE:IsPlayerEnabled(PLAYER_2)

local function optional_actor(cond, actor)
	if cond then return actor end
	return Def.Actor{}
end

return Def.ActorFrame{
  Name= "actor_eb",
  Def.ActorFrame{
    Name= "actor_fb",
    Def.Quad{
      Name= "actor_gb",
      InitCommand= function(self)
		
			--this is where variables go when the simfile starts up
			--function declarations, and things that will never change even
			--if this particular minigame is restarted after being played once before
		
			--for storing if a button is held down
			mawaru_anders_held = {0,0,0,0,0,0,0,0};
			
			mawaru_anders_xspd = {0,0}
			mawaru_anders_maxxspd = {0.2,0.2}
			mawaru_anders_xaccel = {0.06,0.06}
			mawaru_anders_x = {13,13}
			
			mawaru_anders_flashtimer = 0
			mawaru_anders_flashval = 0;
			
			mawaru_anders_players = {}
			mawaru_anders_playersI = {}
			
			mawaru_anders_dead = {false,false}
			
			mawaru_anders_pointers = {}
			
		end,
      KillCommand= function(self)
			--any cleanup, if necessary
		end,
      StartCommand= function(self)
			
			--variables set up when a game is started
			--wait 2 seconds (for the curtain or whatever) before accepting any inputs
			mawaru_anders_lockinput = {.3,.3};
			
			mawaru_anders_speedcaps = {0.2,0.2,0.3,0.4,1,999}
			mawaru_anders_accels = {0.02,0.02,0.03,0.04,0.07,0.1}
			mawaru_anders_xposes = {15,14,13,13,12,10}
			
			for pn = 1,2 do
				local d = mawaru_difficulty[pn]
				if d > -1 then
					mawaru_anders_maxxspd[pn] = mawaru_anders_speedcaps[d+1]
					mawaru_anders_xaccel[pn] = mawaru_anders_accels[d+1]
					mawaru_anders_x[pn] = mawaru_anders_xposes[d+1]
				end
			end
			
			self:sleep(0.02);
			self:queuecommand('Update');

		end,
      UpdateCommand= function(self)
			
			--game logic
			
			local beat = GAMESTATE:GetSongBeat();
			
			if mawaru_anders_flashtimer <= 0 then
				if mawaru_anders_flashval == 1 then
					mawaru_anders_flashval = 0;
				else
					mawaru_anders_flashval = 1
				end
				mawaru_anders_flashtimer = 0.04
			else
				mawaru_anders_flashtimer = mawaru_anders_flashtimer-0.02
			end
			
			for pn=1,2 do
				
				if GAMESTATE:IsPlayerEnabled(pn-1) then
				
					if mawaru_anders_lockinput[pn]>0 then
						mawaru_anders_lockinput[pn] = mawaru_anders_lockinput[pn]-0.02
					end
				
					local bpn = pn-1;
						
					----------------------handle button presses-------------------
					if mawaru_anders_lockinput[pn] <= 0 then
						if mawaru_keys[1+(bpn*4)] == 1 then
							mawaru_anders_xspd[pn] = mawaru_anders_xspd[pn]-mawaru_anders_xaccel[pn]
						end
						if mawaru_keys[4+(bpn*4)] == 1 then
							mawaru_anders_xspd[pn] = mawaru_anders_xspd[pn]+mawaru_anders_xaccel[pn]
						end
					end
					
					mawaru_anders_xspd[pn] = mawaru_clamp(mawaru_anders_xspd[pn]+mawaru_clamp(((mawaru_anders_x[pn]-15)/400),-mawaru_anders_maxxspd[pn]/2,mawaru_anders_maxxspd[pn]/2),mawaru_anders_maxxspd[pn]*-1,mawaru_anders_maxxspd[pn])
					
					mawaru_anders_x[pn] = mawaru_anders_x[pn]+mawaru_anders_xspd[pn]
					
					local adjustx = (mawaru_anders_x[pn]-15)*(200/15)
					mawaru_anders_pointers[pn]:x(160+mawaru_clamp(adjustx,-100,100))
					
					if not mawaru_anders_dead[pn] then
						mawaru_anders_playersI[pn]:setstate(29-mawaru_clamp(math.floor(mawaru_anders_x[pn])+mawaru_round(0*math.sin(beat*math.pi)),0,29))
						local v = math.abs(mawaru_anders_x[pn]-15)/4
						mawaru_anders_players[pn]:effectmagnitude(v,v,0);
						local fv = mawaru_anders_flashval
						if mawaru_anders_x[pn] < 5 or mawaru_anders_x[pn] > 24 then
							mawaru_anders_playersI[pn]:diffuse(1,fv+0.6,fv+0.6,1);
						else
							mawaru_anders_playersI[pn]:diffuse(1,1,1,1);
						end
						if mawaru_anders_x[pn] < -6 then
							mawaru_anders_dead[pn] = true
							mawaru_anders_players[pn]:visible(false);
							MESSAGEMAN:Broadcast('AndersDieFrontP'..pn);
							MESSAGEMAN:Broadcast('AndersDieP'..pn);
							Sound('hit_wacky');
						end
						if mawaru_anders_x[pn] > 35 then
							mawaru_anders_dead[pn] = true
							mawaru_anders_players[pn]:visible(false);
							MESSAGEMAN:Broadcast('AndersDieBackP'..pn);
							MESSAGEMAN:Broadcast('AndersDieP'..pn);
							Sound('hit_wacky');
						end
					end
				
				end
				
			end
			
			self:sleep(0.02);
			if mawaru_curgame == 10 and mawaru_gamestate == 1 then
				self:queuecommand('Update');
			end
			
		end,
    },
    Def.Quad{
      Name= "actor_hb",
      InitCommand= function(self) mawaru_anders_controller = self end,
    },
    --For all p1 parts, Unconverted x & y pos. -Kid
    Def.ActorFrame{
      Name= "actor_ib",
      OnCommand= cmd(),
      Def.Sprite{
        Name= "actor_jb",
        OnCommand= cmd(x,160;y,240),
        Texture= "bg.png",
      },
      Def.Sprite{
        Name= "actor_kb",
        OnCommand= cmd(x,160;y,240),
        Texture= "pad.png",
      },
      Def.ActorFrame{
        Name= "actor_lb",
        OnCommand= cmd(x,188;y,168;sleep,0.02;queuecommand,"SetMe"),
        SetMeCommand= function(self) table.insert(mawaru_anders_players,self) end,
        Def.Sprite{
          Name= "actor_mb",
          OnCommand= cmd(animate,0;sleep,0.02;queuecommand,"SetMe2"),
          SetMe2Command= function(self) table.insert(mawaru_anders_playersI,self) end,
          Texture= "anders 6x5.png",--Got extended to andersbar by accident. -Kid
        },
      },
      Def.Sprite{
        Name= "actor_nb",
        Frames= {
          {Frame= 0, Delay= 0.08},
          {Frame= 1, Delay= 999},
          {Frame= 2, Delay= 999},
      },
        AndersDieBackP1MessageCommand= cmd(visible,true;setstate,2;vibrate;effectmagnitude,10,10,0;sleep,0.4;queuecommand,"StopVib"),
        AndersDieFrontP1MessageCommand= cmd(visible,true;setstate,0;vibrate;effectmagnitude,10,10,0;sleep,0.4;queuecommand,"StopVib"),
        OnCommand= cmd(x,195;y,330;cropright,8/288;visible,false),
        StopVibCommand= cmd(stopeffect),
        Texture= "./_dead 2x2.png",
      },
      Def.Sprite{
        Name= "actor_ob",
        OnCommand= cmd(x,160;y,240),
        Texture= "front_bar.png",
      },
      Def.Sprite{
        Name= "actor_pb",
        AndersDieP1MessageCommand= cmd(sleep,0.3;linear,0.3;x,-1),
        OnCommand= cmd(horizalign,left;x,-150;y,190),
        Texture= "rob.png",
      },
      Def.Sprite{
        Name= "actor_qb",
        AndersDieP1MessageCommand= cmd(sleep,0.9;bounceend,0.3;zoom,1),
        OnCommand= cmd(horizalign,left;animate,0;vertalign,bottom;x,93;y,149;zoom,0),
        Texture= "uok 1x2.png",
      },
      Def.Sprite{
        Name= "actor_rb",
        AndersDieP1MessageCommand= cmd(sleep,1.8;bounceend,0.3;zoom,1),
        OnCommand= cmd(x,160;y,245;animate,0;setstate,math.random(0,1);zoom,0),
        Texture= "response 1x2.png",
      },
      Def.ActorFrame{
        Name= "actor_sb",
        AndersDieP1MessageCommand= cmd(visible,false),
        Def.Sprite{
          Name= "actor_tb",
          OnCommand= cmd(x,160;y,80;diffusealpha,0.5),
          Texture= "andersbar.png",
        },
        Def.Sprite{
          Name= "actor_ub",
          OnCommand= cmd(x,160;y,80;zoom,0.5;rotationz,-90;horizalign,right;sleep,0.02;queuecommand,"SetMe"),
          SetMeCommand= function(self) table.insert(mawaru_anders_pointers,self) end,
          Texture= "pointer.png",
        },
      },
    },
    --For all p2 parts, Unconverted x & y pos. -Kid
    Def.ActorFrame{
      Name= "actor_vb",
      OnCommand= cmd(x,320),
      Def.Sprite{
        Name= "actor_wb",
        OnCommand= cmd(x,160;y,240),
        Texture= "bg.png",
      },
      Def.Sprite{
        Name= "actor_xb",
        OnCommand= cmd(x,160;y,240),
        Texture= "pad.png",
      },
      Def.ActorFrame{
        Name= "actor_yb",
        OnCommand= cmd(x,188;y,168;sleep,0.02;queuecommand,"SetMe"),
        SetMeCommand= function(self) table.insert(mawaru_anders_players,self) end,
        Def.Sprite{
          Name= "actor_zb",
          OnCommand= cmd(animate,0;sleep,0.02;queuecommand,"SetMe2"),
          SetMe2Command= function(self) table.insert(mawaru_anders_playersI,self) end,
          Texture= "anders 6x5.png",--Got extended to andersbar by accident. -Kid
        },
      },
      Def.Sprite{
        Name= "actor_Ab",
        Frames= {
          {Frame= 0, Delay= 0.08},
          {Frame= 1, Delay= 999},
          {Frame= 2, Delay= 999},
      },
        AndersDieBackP2MessageCommand= cmd(visible,true;setstate,2;vibrate;effectmagnitude,10,10,0;sleep,0.4;queuecommand,"StopVib"),
        AndersDieFrontP2MessageCommand= cmd(visible,true;setstate,0;vibrate;effectmagnitude,10,10,0;sleep,0.4;queuecommand,"StopVib"),
        OnCommand= cmd(x,195;y,330;cropright,8/288;visible,false),
        StopVibCommand= cmd(stopeffect),
        Texture= "./_dead 2x2.png",
      },
      Def.Sprite{
        Name= "actor_Bb",
        OnCommand= cmd(x,160;y,240),
        Texture= "front_bar.png",
      },
      Def.Sprite{
        Name= "actor_Cb",
        AndersDieP2MessageCommand= cmd(sleep,0.3;linear,0.3;x,320-1),
        OnCommand= cmd(horizalign,left;x,320+150;zoomx,-1;y,190),
        Texture= "rob.png",
      },
      Def.Sprite{
        Name= "actor_Db",
        AndersDieP2MessageCommand= cmd(sleep,0.9;bounceend,0.3;zoom,1),
        OnCommand= cmd(horizalign,right;animate,0;setstate,1;vertalign,bottom;x,320-93;y,149;zoom,0),
        Texture= "uok 1x2.png",
      },
      Def.Sprite{
        Name= "actor_Eb",
        AndersDieP2MessageCommand= cmd(sleep,1.8;bounceend,0.3;zoom,1),
        OnCommand= cmd(x,160;y,245;animate,0;setstate,math.random(0,1);zoom,0),
        Texture= "response 1x2.png",
      },
      Def.ActorFrame{
        Name= "actor_Fb",
        AndersDieP2MessageCommand= cmd(visible,false),
        Def.Sprite{
          Name= "actor_ac",
          OnCommand= cmd(x,160;y,80;diffusealpha,0.5),
          Texture= "andersbar.png",
        },
        Def.Sprite{
          Name= "actor_bc",
          OnCommand= cmd(x,160;y,80;zoom,0.5;rotationz,-90;horizalign,right;sleep,0.02;queuecommand,"SetMe"),
          SetMeCommand= function(self) table.insert(mawaru_anders_pointers,self) end,
          Texture= "pointer.png",
        },
      },
    },
    Def.Quad{
      Name= "actor_cc",--Unconverted x & y pos. -Kid
      OnCommand= cmd(x,320;y,240;zoomto,6,480;diffuse,color('0,0,0,1')),
    },
    optional_actor(cond_cb_result,
    Def.Quad{
      Name= "actor_dc",--Unconverted x & y pos. -Kid
      OnCommand= cmd(x,640*0.25;y,240;zoomto,640/2,480;diffuse,color('0,0,0,1')),
    }),
    optional_actor(cond_db_result,
    Def.Quad{
      Name= "actor_ec",--Unconverted x & y pos. -Kid
      OnCommand= cmd(x,640*0.75;y,240;zoomto,640/2,480;diffuse,color('0,0,0,1')),
    }),
  },
}
