Skip to content

javascript

mithrendal edited this page Apr 4, 2021 · 19 revisions

scripting complex control of vc64web

in the previous chapter scripting support for simple tasks you learned how to trigger a sequence of keypress or joystick controller actions. With that you could do your own auto fire button or key on the keyboard.

In this chapter we want to bring that on a higher level. We want to automatically react on the movemnts of sprites for example. For this we clearly need another language ... we will use javascript.

pure javascript can be used for more complex control ...

API

function sprite_xpos(0..7) //returns the current x-position of the selected sprite... 
function sprite_ypos(0..7) //returns the current x-position of the selected sprite... 
function not_stopped(this_id) //returns whether a the user asked the script to stop ... 
//set_port_owner gives exclusive control of the port so that signals do not mix   
function set_port_owner(1..2,PORT_ACCESSOR.MANUAL or PORT_ACCESSOR.BOT); returns previous port owner
//wait until the c64 shows its ready cursor after reset
await wasm_ready_after_reset();
//simulates in the correct order a runstop restore key press
wasm_runstop_restore();

template for a game loop

//repeats action until action button is pressed again
while(not_stopped(this_id))
{
  //do some action
  await action("A=>500ms"); // presses Keyboard 'A' and waits 500ms
}

example

var x=sprite_xpos(1);

var y=sprite_ypos(1);

while(not_stopped(this_id))

{

  console.log('------'+x + ' '+sprite_xpos(1));

  await my_move_strategy1(1);  

  await my_aim_and_shoot(1);

  console.log('------'+x + ' '+sprite_xpos(1));

}

async function my_move_strategy1(port)

{ 

  await action("j"+port+"right1=>850ms");

  await action("j"+port+"right0=>800ms");

  await action("j"+port+"left1=>450ms");

  await action("j"+port+"left0=>800ms");

}

async function my_aim_and_shoot(port)

{ 

  await action("j"+port+"fire1=>j"+port+"left1");

  await action("300ms");

  await action("j"+port+"left0=>j"+port+"fire0");

}

alert('finished the script');

Clone this wiki locally