//////////////////////////////////////////////////////////////////
// turn towards an absolute target angle with given angular speed
// use wait_for_my(ent_turnto) for determining when the angle is reached
function ent_turnto(ENTITY* ent,ANGLE* to,var aspeed)
{
proc_kill2(ent_turnto,ent);
// store the "to" angle for the wait loop - it could be a temporary vector!
ANGLE atarget;
vec_set(atarget,to);
while(aspeed) {
// calculate the difference between the current and the target angle
ANGLE adiff;
ang_diff(adiff,atarget,ent.pan);
// check if we're already there
if(vec_length(adiff) <= aspeed*time_step) {
vec_set(ent.pan,atarget);
return;
}
// scale the difference angle by the angular speed,
// and add it to the entity angle
vec_normalize(adiff,aspeed*time_step);
ang_add(ent.pan,adiff);
wait(1);
}
}
// turn towards a target position with given speed
// use wait_for_my(ent_turnto) for determining when the angle is reached
function ent_faceto(ENTITY* ent,VECTOR* pos,var aspeed)
{
if(!ent || !pos) return;
VECTOR* diff = vec_diff(NULL,pos,ent.x);
vec_to_angle(diff,diff);
ent_turnto(ent,diff,aspeed);
}