unit GRL;
@use demoscreens


var
//   font : incbin("font_8x8.chr");
	const bank_sprites:byte = 2;
   	sprdata : incbin("images/data.bin") bank(bank_sprites);   
	sprGirl : array[3*2*8] of byte sprram aligned;
	
	set_idle : array[8] of byte = (0,0,4,4,0,0,4,4);
	set_move : array[8] of byte = (1,2,3,2,1,2,3,2);
	set_kick : array[8] of byte = (5,6,7,7,5,6,7,7);

	const STATE_IDLE : byte = 0;
	const STATE_MOVETO : byte = 1;
	const STATE_KICK : byte = 2;
	


	
	// State pointer
	
	p1,stateP,mp : pointer;	
	


//@donotremove sprites

   	sprites : incbin("images/sprites.bin");
  
	time,prev_x, prev_y, x,y,t,t2,frame,frameLookup, pos, orientationRight,i,j : byte wram;
	// State variables
	currentState, curTick, maxTick, target_x, target_y : byte wram;




procedure ReadState();
begin
	// Read current state
	time:=0;
	currentState:=stateP[0];
	if (currentState = STATE_IDLE) then
	begin
		curTick:=0;
		maxTick:=stateP[1];
		// Advance counter
		stateP:=stateP+2;
		return();
	end;
	if (currentState = STATE_KICK) then
	begin
		curTick:=0;
		maxTick:=stateP[1];
		// Advance counter
		stateP:=stateP+2;
		return();
	end;
	if (currentState = STATE_MOVETO) then
	begin
		curTick:=0;
		target_x:=stateP[1];
		target_y:=stateP[2];
		// Advance counter
		stateP:=stateP + 3;
		return();
	end;
end;


	
procedure UpdateState();
begin
	if (currentState = STATE_IDLE) then
	begin
		if (t2=0) then
			curTick:=curTick+1;
		if (curTick>maxTick) then
			ReadState();
		return();
	end;
	
	if (currentState = STATE_KICK) then
	begin
		if (t2=0) then
			curTick:=curTick+1;
		if (curTick>maxTick) then
			ReadState();
		return();
	end;

	if (currentState = STATE_MOVETO)  then
	begin
		i:=target_x-1;
		j:=target_y-1;
		if (x=i) then
			if (y=j) then
			begin
				ReadState();
			end;

		i:=time&1;
		if (i=0) then
		begin
			prev_x:=x;
			prev_y:=y;			

			if (x<target_x) then x:=x+1;
			if (x>target_x) then x:=x-1;
			if (y<target_y) then y:=y+1;
			if (y>target_y) then y:=y-1;
			
	i:=x+1;
	if (currentState = STATE_MOVETO) then
	if (prev_x<i) then orientationRight:=0 else orientationRight:=1;
		end;		
		return();

	end;


end;


Procedure Move();
begin
	t2:=time&7;

	UpdateState();

	frame:=time>>3;
	frame:=frame &7;
	
	if (currentState=STATE_IDLE) then mp:=set_idle;
	if (currentState=STATE_MOVETO) then mp:=set_move;
	if (currentState=STATE_KICK) then mp:=set_kick;
	
	frame:=mp[frame];
		
		// *6
	pos:=frame<<1;
	pos:=pos+frame;
	pos:=pos<<1;

		
	if (orientationRight=0) then
		initspritefromdata(sprGirl,sprites,pos,6,2,sprite_flip_x)
	else
		initspritefromdata(sprGirl,sprites,pos,6,2 ,0);

	setsprite(sprGirl,x,y,2,3);

//	background_palette:=$FF;
	
	time:=time+1;
end;







procedure Setup();
begin
	memcpyonhblank(sprdata,vram,1776);
	Screens::ClearSprites(64);
	maxTick:=0;
	curTick:=0;
	time:=0;
	ReadState();

end;



end.