local cond_qgb_result= not GAMESTATE:IsPlayerEnabled(PLAYER_1)

local cond_rgb_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_sgb",
  Def.ActorFrame{
    Name= "actor_tgb",
    Def.Quad{
      Name= "actor_ugb",
      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_mmm_held = {0,0,0,0,0,0,0,0};
			
			mawaru_mmm_beat = 0;
			mawaru_mmm_next = 558;
			
			mawaru_mmm_balls = {{},{}}
			
			mawaru_mmm_lives = {2,2}
			mawaru_mmm_score = {0,0}
			
			function mawaru_mmm_loselife(pn)
				if GAMESTATE:IsPlayerEnabled(pn-1) then
					mawaru_mmm_lives[pn] = mawaru_mmm_lives[pn]-1;
					Sound('ddrsuck');
					if mawaru_mmm_lives[pn] == 0 then
						Sound('thlaugh');
					end
				end
			end
			
		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_mmm_lockinput = {.3,.3};
			
			self:sleep(0.02);
			self:queuecommand('Update');

		end,
      UpdateCommand= function(self)
			
			if GAMESTATE:GetSongBeat() > mawaru_mmm_next-.5 then
				mawaru_mmm_next = mawaru_mmm_next+1
			end
			
			--game logic
			
			for pn=1,2 do
				
				if mawaru_mmm_lockinput[pn]>0 then
					mawaru_mmm_lockinput[pn] = mawaru_mmm_lockinput[pn]-0.02
				end
				
				if mawaru_mmm_lives[pn] == 2 then
					mawaru_mmm_score[pn] = 5;
				elseif mawaru_mmm_lives[pn] == 1 then
					mawaru_mmm_score[pn] = 2;
				else
					mawaru_mmm_score[pn] = 0;
				end
			
				local bpn = pn-1;
				
				----------------------handle button presses-------------------
				if mawaru_mmm_lockinput[pn] <= 0 then
					if mawaru_keys[3+(bpn*4)] == 1 and mawaru_mmm_held[3+(bpn*4)] == 0 then
						MESSAGEMAN:Broadcast('mmmJumpP'..pn);
						local beat = GAMESTATE:GetSongBeat()
						if math.abs(beat-0.1-560) < 0.2 or math.abs(beat-0.1-562) < 0.2 or math.abs(beat-0.1-568) < 0.2 or math.abs(beat-0.1-570) < 0.2 then
							mawaru_mmm_balls[pn][1]:stoptweening();
							mawaru_mmm_balls[pn][1]:playcommand('Bounce');
						end
						if math.abs(beat-0.1-565) < 0.2 or math.abs(beat-0.1-567) < 0.2 or math.abs(beat-0.1-573) < 0.2 or math.abs(beat-0.1-575) < 0.2 then
							mawaru_mmm_balls[pn][2]:stoptweening();
							mawaru_mmm_balls[pn][2]:playcommand('Bounce');
						end
						mawaru_mmm_lockinput[pn] = 0.1;
						mawaru_mmm_held[3+(bpn*4)] = 1
					end
					
				end
				
				----------------------don't touch this------------------------
				--release all buttons - used for detecting if a button is newly pressed, as opposed to held
				if mawaru_keys[3+(bpn*4)] == 0 then
					mawaru_mmm_held[3+(bpn*4)] = 0
				end
				---------------------end don't touch this----------------------
					
			end
			
			self:sleep(0.02);
			if mawaru_curgame == 14 and mawaru_gamestate == 1 then
				self:queuecommand('Update');
			end
			
		end,
    },
    Def.Quad{
      Name= "actor_vgb",
      InitCommand= function(self) mawaru_mmm_controller = self end,
    },
    --For all P1 parts, Unconverted x & y pos. -Kid
    Def.ActorFrame{
      Name= "actor_wgb",
      OnCommand= cmd(),
      Def.Sprite{
        Name= "actor_xgb",
        OnCommand= cmd(customtexturerect,0,0,5,8;zoomto,320,512;x,160;y,240;texcoordvelocity,0.6,0.3),
        Texture= "redbg.png",
      },
      Def.Sprite{
        Name= "actor_ygb",
        OnCommand= cmd(zoom,.66;x,32;y,266),
        Texture= "plat.png",
      },
      Def.Sprite{
        Name= "actor_zgb",
        OnCommand= cmd(zoom,.66;x,32+64;y,266),
        Texture= "plat.png",
      },
      Def.Sprite{
        Name= "actor_Agb",
        OnCommand= cmd(zoom,.66;x,32+128;y,266),
        Texture= "plat.png",
      },
      Def.Sprite{
        Name= "actor_Bgb",
        OnCommand= cmd(zoom,.66;x,32+256;y,266),
        Texture= "plat.png",
      },
      Def.Sprite{
        Name= "actor_Cgb",
        OnCommand= cmd(zoom,.5;vertalign,top;x,32+192+10;y,240),
        Texture= "taro.png",
        mmmJumpP1MessageCommand= cmd(finishtweening;addy,-16;accelerate,0.3;addy,16),
      },
      Def.Sprite{
        Name= "actor_Dgb",
        OnCommand= cmd(x,32+192;y,360;zoom,0.75),
        Texture= "box.png",
      },
      Def.Sprite{
        Name= "actor_Egb",
        OnCommand= cmd(x,32+192;y,360;zoom,0.75;rotationz,180),
        Texture= "arrow1.png",
        mmmJumpP1MessageCommand= cmd(finishtweening;zoom,0.95;linear,0.2;zoom,0.75),
      },
      Def.ActorFrame{
        Name= "actor_Fgb",
        OnCommand= cmd(x,32;sleep,0.02;queuecommand,"SetMe"),
        SetMeCommand= function(self) table.insert(mawaru_mmm_balls[1],self:GetChild('')) end,
        StartCommand= cmd(sleep,120/200;sleep,60/200;linear,240/210;addx,256;linear,240/210;addx,-256;linear,240/210;addx,256;linear,240/210;addx,-256),
        Def.Sprite{
          --Name= "actor_ahb",--Names are causing us trouble. -Kid
          BounceCommand= function(self)
				local tonext = (math.max(mawaru_mmm_next - GAMESTATE:GetSongBeat(),0.1))*(60/210)
				self:decelerate(tonext/2);
				self:addy(-90);
				self:accelerate(tonext/2);
				self:y(240);
				local beat = GAMESTATE:GetSongBeat()
				if math.abs(beat-559) < 0.5 or math.abs(beat-561) < 0.5 or math.abs(beat-567) < 0.5 or math.abs(beat-569) < 0.5 then
					self:linear(tonext*0.2);
					self:addy(300*0.2);
					self:linear(tonext*0.8);
					self:addy(300*0.8);
					self:queuecommand('GiveUp');
				else
					self:queuecommand('Bounce');
				end
			end,
          GiveUpCommand= function(self)
				mawaru_mmm_loselife(1);
			end,
          HideGame14MessageCommand= cmd(stoptweening),
          OnCommand= cmd(y,-40),
          StartCommand= cmd(sleep,120/200;linear,60/210;y,240;queuecommand,"Bounce"),
          Texture= "ball.png",
        },
      },
      Def.ActorFrame{
        Name= "actor_bhb",
        OnCommand= cmd(x,32;sleep,0.02;queuecommand,"SetMe"),
        SetMeCommand= function(self) table.insert(mawaru_mmm_balls[1],self:GetChild('')) end,
        StartCommand= cmd(sleep,120/200;sleep,60/210;sleep,300/210;
		linear,240/210;addx,256;linear,240/210;addx,-256;linear,240/210;addx,256;linear,240/210;addx,-256),
        Def.Sprite{
          --Name= "actor_chb",--Names are causing us trouble. -Kid
          BounceCommand= function(self)
				local tonext = (math.max(mawaru_mmm_next - GAMESTATE:GetSongBeat(),0.1))*(60/210)
				self:decelerate(tonext/2);
				self:addy(-90);
				self:accelerate(tonext/2);
				self:y(240);
				local beat = GAMESTATE:GetSongBeat()
				if math.abs(beat-564) < 0.5 or math.abs(beat-566) < 0.5 or math.abs(beat-572) < 0.5 or math.abs(beat-574) < 0.5 then
					self:linear(tonext*0.2);
					self:addy(300*0.2);
					self:linear(tonext*0.8);
					self:addy(300*0.8);
					self:queuecommand('GiveUp');
				else
					self:queuecommand('Bounce');
				end
			end,
          GiveUpCommand= function(self)
				mawaru_mmm_loselife(1);
			end,
          HideGame14MessageCommand= cmd(stoptweening),
          OnCommand= cmd(y,-40),
          StartCommand= cmd(sleep,120/200;sleep,300/210;linear,60/210;y,240;queuecommand,"Bounce"),
          Texture= "ball.png",
        },
      },
    },
    --For all P2 parts, Unconverted x & y pos. -Kid
    Def.ActorFrame{
      Name= "actor_dhb",
      OnCommand= cmd(x,320),
      Def.Sprite{
        Name= "actor_ehb",
        OnCommand= cmd(customtexturerect,0,0,5,8;zoomto,320,512;x,160;y,240;texcoordvelocity,0.6,0.3),
        Texture= "bluebg.png",
      },
      Def.Sprite{
        Name= "actor_fhb",
        OnCommand= cmd(zoom,.66;x,32;y,266),
        Texture= "plat.png",
      },
      Def.Sprite{
        Name= "actor_ghb",
        OnCommand= cmd(zoom,.66;x,32+64;y,266),
        Texture= "plat.png",
      },
      Def.Sprite{
        Name= "actor_hhb",
        OnCommand= cmd(zoom,.66;x,32+128;y,266),
        Texture= "plat.png",
      },
      Def.Sprite{
        Name= "actor_ihb",
        OnCommand= cmd(zoom,.66;x,32+256;y,266),
        Texture= "plat.png",
      },
      Def.Sprite{
        Name= "actor_jhb",
        OnCommand= cmd(zoom,.5;vertalign,top;x,32+192+10;y,240),
        Texture= "steph.png",
        mmmJumpP2MessageCommand= cmd(finishtweening;addy,-16;accelerate,0.3;addy,16),
      },
      Def.Sprite{
        Name= "actor_khb",
        OnCommand= cmd(x,32+192;y,360;zoom,0.75),
        Texture= "box.png",
      },
      Def.Sprite{
        Name= "actor_lhb",
        OnCommand= cmd(x,32+192;y,360;zoom,0.75;rotationz,180),
        Texture= "arrow2.png",
        mmmJumpP2MessageCommand= cmd(finishtweening;zoom,0.95;linear,0.2;zoom,0.75),
      },
      Def.ActorFrame{
        Name= "actor_mhb",
        OnCommand= cmd(x,32;sleep,0.02;queuecommand,"SetMe"),
        SetMeCommand= function(self) table.insert(mawaru_mmm_balls[2],self:GetChild('')) end,
        StartCommand= cmd(sleep,120/200;sleep,60/200;linear,240/210;addx,256;linear,240/210;addx,-256;linear,240/210;addx,256;linear,240/210;addx,-256),
        Def.Sprite{
          --Name= "actor_nhb",--Names are causing us trouble. -Kid
          BounceCommand= function(self)
				local tonext = (math.max(mawaru_mmm_next - GAMESTATE:GetSongBeat(),0.1))*(60/210)
				self:decelerate(tonext/2);
				self:addy(-90);
				self:accelerate(tonext/2);
				self:y(240);
				local beat = GAMESTATE:GetSongBeat()
				if math.abs(beat-559) < 0.5 or math.abs(beat-561) < 0.5 or math.abs(beat-567) < 0.5 or math.abs(beat-569) < 0.5 then
					self:linear(tonext*0.2);
					self:addy(300*0.2);
					self:linear(tonext*0.8);
					self:addy(300*0.8);
					self:queuecommand('GiveUp');
				else
					self:queuecommand('Bounce');
				end
			end,
          GiveUpCommand= function(self)
				mawaru_mmm_loselife(2);
			end,
          HideGame14MessageCommand= cmd(stoptweening),
          OnCommand= cmd(y,SCREEN_CENTER_Y + -280),
          StartCommand= cmd(sleep,120/200;linear,60/210;y,-40;queuecommand,"Bounce"),
          Texture= "ball.png",
        },
      },
      Def.ActorFrame{
        Name= "actor_ohb",
        OnCommand= cmd(x,32;sleep,0.02;queuecommand,"SetMe"),
        SetMeCommand= function(self) table.insert(mawaru_mmm_balls[2],self:GetChild('')) end,
        StartCommand= cmd(sleep,120/200;sleep,60/210;sleep,300/210;
		linear,240/210;addx,256;linear,240/210;addx,-256;linear,240/210;addx,256;linear,240/210;addx,-256),
        Def.Sprite{
          --Name= "actor_phb",--Names are causing us trouble. -Kid
          BounceCommand= function(self)
				local tonext = (math.max(mawaru_mmm_next - GAMESTATE:GetSongBeat(),0.1))*(60/210)
				self:decelerate(tonext/2);
				self:addy(-90);
				self:accelerate(tonext/2);
				self:y(240);
				local beat = GAMESTATE:GetSongBeat()
				if math.abs(beat-564) < 0.5 or math.abs(beat-566) < 0.5 or math.abs(beat-572) < 0.5 or math.abs(beat-574) < 0.5 then
					self:linear(tonext*0.2);
					self:addy(300*0.2);
					self:linear(tonext*0.8);
					self:addy(300*0.8);
					self:queuecommand('GiveUp');
				else
					self:queuecommand('Bounce');
				end
			end,
          GiveUpCommand= function(self)
				mawaru_mmm_loselife(2);
			end,
          HideGame14MessageCommand= cmd(stoptweening),
          OnCommand= cmd(y,SCREEN_CENTER_Y + -280),
          StartCommand= cmd(sleep,120/200;sleep,300/210;linear,60/210;y,240;queuecommand,"Bounce"),
          Texture= "ball.png",
        },
      },
    },
    Def.Quad{
      Name= "actor_qhb",--Unconverted x & y pos. -Kid
      OnCommand= cmd(x,320;y,240;zoomto,6,480;diffuse,color('0,0,0,1')),
    },
    optional_actor(cond_qgb_result,
    Def.Quad{
      Name= "actor_rhb",--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_rgb_result,
    Def.Quad{
      Name= "actor_shb",--Unconverted x & y pos. -Kid
      OnCommand= cmd(x,640*0.75;y,240;zoomto,640/2,480;diffuse,color('0,0,0,1')),
    }),
  },
}
