action player_move() // control the player on its client, move it on the server, animate it on all clients
{
VECTOR* cpos;
my.event = player_remove;
my.emask |= ENABLE_DISCONNECT;
my.smask |= NOSEND_FRAME; // don't send animation
var walk_percentage = 0;
var skill1_old = 0, skill2_old = 0;
while (1)
{
if (my.client_id == dplay_id) // player created by this client?
{
vec_set(cpos,vector(-150,0,50));
vec_rotate(cpos,vector(my.pan,my.tilt,0));
vec_add(cpos,my.x);
vec_set(camera.x,cpos);
vec_set(cpos,vector(0,0,16));
vec_add(cpos,my.x);
vec_set(cpos,my.x);
cpos.z += 15;
vec_sub(cpos,camera.x);
vec_to_angle(camera.pan,cpos);
if (key_w-key_s != my.skill1) { // forward/backward key state changed?
my.skill1 = key_w-key_s;
send_skill(my.skill1,0); // send the key state in reliable mode
}
if (key_a-key_d != my.skill2) { // rotation key changed?
my.skill2 = key_a-key_d;
send_skill(my.skill2,0); // send rotation state in reliable mode
}
}
if (connection & CONNECT_SERVER) // running on the server?
{
if (my.skill1 != skill1_old) { // if movement changed
send_skill(my.skill1,SEND_ALL); // send movement to all clients
skill1_old = my.skill1;
}
if (my.skill2 != skill2_old) { // if rotation changed
send_skill(my.skill2,SEND_ALL); // send rotation to all clients
skill2_old = my.skill2;
}
}
my.pan += my.skill2*5*time_step; // rotate the entity using its skill2
var distance = my.skill1*5*time_step;
c_move(me, vector(distance,0,0), NULL, GLIDE); // move it using its skill1
walk_percentage += distance;
ent_animate(me,"walk",walk_percentage,ANM_CYCLE); // animate the entity
wait (1);
}
}