program VicWilly;
var  

// Take a look at the VBM commands in the help (press F1)
// and also in the platform section of the help file
//
// Note that TRSE for VIC 20 is using zero pages from $8F downwards
// and VBM uses zero pages from $00 upwards.
// The Zero page memory in between can be used by your programs.
// See VBM Memory in the Platforms help section for details. 

// Example showing 8x16 sprite and tilemap

// these constants are used to calculate positions of sprite data
	// pre-shift sprite address offests for 8x16 sprites
	const sprHeight16:byte = 16;
	// side A
	const O0a:byte = 0;
	const O1a:byte = O0a+(sprHeight16*2);
	const O2a:byte = O1a+(sprHeight16*2);
	const O3a:byte = O2a+(sprHeight16*2);

	// side B
	const O0b:byte = sprHeight16;
	const O1b:byte = O0b+(sprHeight16*2);
	const O2b:byte = O1b+(sprHeight16*2);
	const O3b:byte = O2b+(sprHeight16*2);



// declare all your include files here
@vbmexport "resources/charsets/sprWilly.flf" "resources/charsets/sprWillyR.bin" 0 8 2 0
@vbmexport "resources/charsets/sprWilly.flf" "resources/charsets/sprWillyL.bin" 40 48 2 0
@vbmexport "resources/charsets/chrWilly.flf" "resources/charsets/chrWilly.bin" 0 46 1 0

	const adrWillyR : address = $4000; // willy = 4 frames * 16 rows * 2 chars = 128 bytes
	const adrWillyL : address = adrWillyR + 128; // 128 bytes
	const adrChars   : address = adrWillyL +128;
	const adrLevel  : address = adrChars + (46 * 8);
	const lvlData   : address = adrLevel + 45;

	willyR: incbin("resources/charsets/sprWillyR.bin", adrWillyR);
	willyL: incbin("resources/charsets/sprWillyL.bin", adrWillyL);
	chars : incbin("resources/charsets/chrWilly.bin", adrChars);
	level : incbin("resources/tilemap/lvlWilly.flf", adrLevel);

	// can use some bytes in ZERO PAGE memory ($35 to $63)
	xp : byte at $35;
	yp : byte at $36;
	dir : byte at $37;
	i, j : byte at $38;



	// pre-shifted addresses for Player Right - side A
	adrWillyR_A: array[] of integer = (
		adrWillyR+O0a, adrWillyR+O1a, adrWillyR+O2a, adrWillyR+O3a
	);
	// pre-shifted addresses for Player Right - side B
	adrWillyR_B: array[] of integer = (
		adrWillyR+O0b, adrWillyR+O1b, adrWillyR+O2b, adrWillyR+O3b
	);

	// pre-shifted addresses for Player Left - side A
	adrWillyL_A: array[] of integer = (
		adrWillyL+O0a, adrWillyL+O1a, adrWillyL+O2a, adrWillyL+O3a
	);
	// pre-shifted addresses for Player Left - side B
	adrWillyL_B: array[] of integer = (
		adrWillyL+O0b, adrWillyL+O1b, adrWillyL+O2b, adrWillyL+O3b
	);


begin
	// set the default display mode showing all 20 columns
	vbmSetDisplayMode( 0 );
	vbmClear( 0 );
	vbmClearColor( WHITE );
	SCREEN_BG_COLOR := BLACK + SCREEN_BG_BLACK;

	vbmDrawTileMap(#lvlData, #adrChars, 0,0, 20,184);

	xp := 64; yp := 160; dir := 1;
	
	i := 0;

	while ( true ) offpage do
	begin
	
		vbmsetposition2( xp, yp );
		if dir = 1 then
			vbmDrawSpriteE( #adrWillyR_A, #adrWillyR_B )
		else
			vbmDrawSpriteE( #adrWillyL_A, #adrWillyL_B );

	
		waitForRaster( 122 );


		vbmsetposition2( xp, yp );
		if dir = 1 then
			vbmDrawSpriteE( #adrWillyR_A, #adrWillyR_B )
		else
			vbmDrawSpriteE( #adrWillyL_A, #adrWillyL_B );
		
		i := i + 1;
		
		if (i & 3 = 3) then
		begin
		
			// update Willy
			readjoy1();
		
			if (joy1 & joy_right) then
			begin
			
				dir := 1;
				if xp< 136 then xp := xp + 2;
				
			end;
			
			if (joy1 & joy_left) then
			begin
			
				dir := 0;
				if xp> 0 then xp := xp - 2;
				
			end;

		end;
		

	end;
	
	loop();

end.
