The action() is invoked for each action separately, with different k and v (you can name the parameters as you wish). You handle it depending on the action type. If you need to keep some state variables for some actions, just attach them to "this". Usually, you want the actions to start/stop a movement. There's a helper class included that will automatically handle the position integration, axis_integrator, usually used for turrets but also other things:
function radians(v){return v*Math.PI/180.0}
//axis integrator takes max speed and acceleration values, optionally min/max clamp values
function init_vehicle(){
this.turx = new axis_integrator(radians(MaxTurretXSpeed), radians(MaxTurretXAccel));
this.tury = new axis_integrator(radians(MaxTurretYSpeed), radians(MaxTurretYAccel), radians(MinTurretAngle), radians(MaxTurretAngle));
this.turretsnd = 0;
}
function action(k,v,dt)
{
switch(k){
case ATurretX: this.turx.set(v); break; //pass value from the action to the integrator
case ATurretY: this.tury.set(v); break;
}
}
function update_frame(dt, engine, brake, steering)
{
//turret handling, changed(dt) returns true if the axis_integrator value changed
var tmov=false;
if(this.turx.changed(dt)){
this.geom.rotate_joint_orig(turret, this.turx.value, {x:0,y:0,z:-1});
tmov = true;
}
if(this.tury.changed(dt)){
this.geom.rotate_joint_orig(mantlet, this.tury.value, {x:1,y:0,z:0});
tmov = true;
}
//play turret sound while it moves
if(tmov) {
if(!this.turretsnd)
this.snd.play_loop(1, 3);
}
else this.snd.stop(1);
this.turretsnd = tmov;