program part7_RenderLevel;
@use "gfx/Levels"
var  
/*
	In this tutorial, we combine part 3 (sprites) with level rendering

*/

@define spriteLoc $3200
// levels are loaded to/from $5000
@define levelPosition $5000
// User $2000 for charset (vic 0)
@define charsetPosition $2000
// Turn off kernal
@define useKernal 0

// Automatically export sprites on each build, so you don't have to manually export
@export "sprites/Sprites.flf" "sprites/sprites.bin" 256
// Automatically export charsets 
@export "charsets/charset1.flf" "charsets/charset1.bin" 256

@define @levelpointer levelp

   	charset1: incbin("charsets/charset1.bin", @charsetPosition);
   	colors: incbin("charsets/charset1_color.bin", @charsetPosition+$800);
	// Sprite locations as in part 3 (at $3200)
	mySprites:incbin("sprites/sprites.bin", @spriteLoc);
	levels: incbin("levels/level1.flf", @levelPosition);

	i,j:byte = 0;
	curLevel_x, curLevel_y : byte=0;

	// Player position x and y. x must be integer since the sprite x coordinate can vary between 0 and 320
	player_x: integer;
	player_y: byte;

// Max / min movement position for player sprite x,y	
	@define miny 66
	@define maxy 210
	@define minx 16
	@define maxx 320 

	// Let's always use sprite "0" as our player sprite
	@define playerSprite 0



// Sets up sprite
procedure InitMainSprite();
begin
	// Set sprite position at bank 0 to $C8=$3200/64
	setspriteloc(@playerSprite,@spriteLoc/64,0);
	player_x:=80;
	player_y:=120;
	// Set sprite x,y pos
	spritepos(player_x,player_y,@playerSprite);

	// Turn on only player sprite
	sprite_bitmask:=%00000001;

 	// All sprites are multicolor
	sprite_multicolor:=$FF;
	sprite_multicolor_reg1:=light_green;
	sprite_multicolor_reg2:=grey;

	sprite_color[@playerSprite]:=red;
end;

// This method initializes charset data, and is similar to the one in part 6
procedure Initialize();
begin
	clearscreen(light_blue, screen_col_loc);
	clearscreen(key_space, screen_char_loc);
	screen_bg_col:=black;
	screen_fg_col:=black;
	// Pointer to character color definitions
	Levels::cdp:=#colors;

	multicolor_char_col[1]:=grey;
	multicolor_char_col[2]:=brown;

	setmulticolormode();

	setcharsetlocation(@charsetPosition);

	Levels::ReadHeader(@levelPosition);
	Levels::starty:=2;
end;

/*	
	Traverselevels checkes whether the player has moved close to a border.
    If true, then the neighbouring screen is loaded and the player sprite is placed
	on the opposite part of the screen. 

*/
procedure TraverseLevels();
var 
   redraw:byte=0;
begin

	if (player_x<@minx) then begin
		if (curLevel_x<>0) then begin
			// decrease level counter
			dec(curLevel_x);
			// move player x to the other side of the screen
			player_x:=@maxx-1;
			redraw:=1;
		end
		else
			player_x:=@minx+1;
	end;
	if (player_x>@maxx) then begin
		if (curLevel_x+1<>Levels::width) then begin
			inc(curLevel_x);
			player_x:=@minx+1;
			redraw:=1;
		end
		else 
			player_x:=@maxx-1;

	end;

	if (player_y<@miny) then begin
		if (curLevel_y<>0) then begin
			dec(curLevel_y);
			player_y:=@maxy-1;
			redraw:=1;
		end
		else
			player_y:=@miny+1;
	end;
	if (player_y>@maxy) then begin
		if (curLevel_y+1<>Levels::height) then begin
			inc(curLevel_y);
			player_y:=@miny;
			redraw:=1;
		end
		else
			player_y:=@maxy-1;
	end;

	if redraw=1 then begin
		Levels::RenderCharsetColorLevel(curLevel_x,curLevel_y,hi(screen_char_loc));
		redraw:=0;
	end;


end;

/*
	 Moves the player
*/
procedure MovePlayerSprite();
begin
	joystick(2);
	player_x:=player_x+joystickright-joystickleft;
	player_y:=player_y+joystickdown-joystickup;
	spritepos(player_x,player_y,@playerSprite);
end;


// Main raster function: Contains all high-level procedure calls to sprite & entity handling
interrupt RasterRenderLevels();
begin
	StartIRQ(@useKernal);
	setbank(vic_bank0);
	MovePlayerSprite();	
	TraverseLevels();
	CloseIRQ();
end;



// Main block
begin
	Initialize();
	InitMainSprite();
	redraw:=1;
//	RenderCharsetColorLevel(curLevel_x,curLevel_y,hi(screen_char_loc));
	// Turn off all interrupts
	DisableCIAInterrupts();
	// Turn off kernal
	SetMemoryConfig(1,@useKernal,0);
	// Set up raster IRQ
	RasterIRQ(RasterRenderLevels(),$00,@useKernal);
	EnableRasterIRQ();
	enableirq();
	Loop(); 
end.
