Skip to content

javascript

mithrendal edited this page Apr 13, 2021 · 17 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();
//waits after a load"*",8,1 until the IEC bus is free and the c64 is ready to accept typing again
await disk_loading_finished();
//so that one can type run
await action("'run'=>Enter");
//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(still_active())
{
  //do some action
  await action("A=>500ms"); // presses Keyboard 'A' and waits 500ms
}

another example shows a script that stops when you are pausing the emulator

while(still_active())
{
  if (is_running())
  {
    await action(`'${this_id}'`);  
  }
  await action("500ms");
}

instead of writing the A letter on the C64-screen it will output the id of the button

The script is still_active as long as the action button is visibily red.

ATTENTION: one thing to keep in mind is that all these scripts run in parallel. Yes it is a sort of multitasking, how can this be when javascript is executed singlethreaded only? Well ... it is like the cooperative multitasking of the early days of computing ... e.g. when task here action button script is going crazy and grabs all processing power for it alone then it will block the complete thread. The browser will stall. So you have to suspend your task with a sleep commando ...

you can suspend your script with either

await action("20ms") 

or

await sleep(20);

to give to play well the cooperative multitasking game

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