[Codebox=javascript file=Untitled.js]//---------------------------------------------------------------------------------
//---------------------------------------------------------------------------------
//---------------------------------------------------------------------------------
// rutina para seguir caminos prefabricados, gracias hokuto por la idea!
//---------------------------------------------------------------------------------
//---------------------------------------------------------------------------------
class path{
//----------------------------------------------
constructor(){
this.points = []; // vector list of path..
this.lastDist = 999999999999; // last distance to next vector..
this.vel = 3; // velocity of movement..
this.pos = 0; // actual point of path..
this.x = 0;
this.y = 0;
this.pause = false;
this.last = -666;
}
//----------------------------------------------
add(x, y){
var vec = createVector(x, y);
this.points.push(vec);
}
//----------------------------------------------
length(){
return this.points.length-1;
}
//----------------------------------------------
reset(){
this.pos = 0;
this.lastDist = 999999999999;
}
//----------------------------------------------
setPoint(num){
this.pos = num;
}
//----------------------------------------------
setVelocity(vel){
this.vel = vel;
}
//----------------------------------------------
//----------------------------------------------
move(type_){
this.x = _id_actual_sprite_runing_.x;
this.y = _id_actual_sprite_runing_.y;
if(this.last == -666){
this.last = type_;
}
switch(type_){
//--------------------------------------------------------------------------------------------------------------------------------------
case FORWARD:
if(this.pause){
return this.pos;
}
if(type_ != this.last){
switch(this.last){
case FORWARD:
break;
case FORWARD_LOOP:
break;
case REVERSE:
if(this.pos<this.points.length-1){
this.pos++;
} else{
this.pos = 0;
}
break;
case REVERSE_LOOP:
if(this.pos<this.points.length-1){
this.pos++;
} else{
this.pos = 0;
}
break;
}
this.last = type_;
}
if(this.x == this.points[this.points.length-1].x){
if(this.y == this.points[this.points.length-1].y){
return this.pos;
}
}
var a = this.getAngle(_id_actual_sprite_runing_.x, _id_actual_sprite_runing_.y, this.points[this.pos].x, this.points[this.pos].y);
if(this.pos < this.points.length){
this.advance(this.vel, a);
}
var d = dist(this.x, this.y, this.points[this.pos].x, this.points[this.pos].y);
if( d < this.vel ){
if(this.pos < this.length()){
this.pos++;
} else{
this.x = this.points[this.points.length-1].x;
this.y = this.points[this.points.length-1].y;
}
}
if(_id_actual_sprite_runing_.body_created){
_id_actual_sprite_runing_.translate(this.x, this.y);
} else{
_id_actual_sprite_runing_.x = this.x;
_id_actual_sprite_runing_.y = this.y;
}
return this.pos;
break;
//--------------------------------------------------------------------------------------------------------------------------------------
case FORWARD_LOOP:
if(this.pause){
return this.pos;
}
if(type_ != this.last){
switch(this.last){
case FORWARD:
break;
case FORWARD_LOOP:
break;
case REVERSE:
if(this.pos<this.points.length-1){
this.pos++;
} else{
this.pos = 0;
}
break;
case REVERSE_LOOP:
if(this.pos<this.points.length-1){
this.pos++;
} else{
this.pos = 0;
}
break;
}
this.last = type_;
}
var a = this.getAngle(_id_actual_sprite_runing_.x, _id_actual_sprite_runing_.y, this.points[this.pos].x, this.points[this.pos].y);
if(this.pos < this.points.length){
this.advance(this.vel, a);
}
var d = dist(this.x, this.y, this.points[this.pos].x, this.points[this.pos].y);
if( d < this.vel ){
if(this.pos < this.length()){
this.pos++;
} else{
this.pos = 0;
}
}
if(_id_actual_sprite_runing_.body_created){
_id_actual_sprite_runing_.translate(this.x, this.y);
} else{
_id_actual_sprite_runing_.x = this.x;
_id_actual_sprite_runing_.y = this.y;
}
return this.pos;
break;
//--------------------------------------------------------------------------------------------------------------------------------------
case REVERSE:
if(this.pause){
return this.pos;
}
if(type_ != this.last){
switch(this.last){
case FORWARD:
if(this.pos>=0){
this.pos--;
} else{
this.pos = this.points.length-1;
}
break;
case FORWARD_LOOP:
if(this.pos>=0){
this.pos--;
} else{
this.pos = this.points.length-1;
}
break;
case REVERSE:
break;
case REVERSE_LOOP:
break;
}
this.last = type_;
}
if(this.x == this.points[0].x){
if(this.y = this.points[0].y){
this.last = type_;
return this.pos;
}
}
var a = this.getAngle(_id_actual_sprite_runing_.x, _id_actual_sprite_runing_.y, this.points[this.pos].x, this.points[this.pos].y);
if(this.pos >= 0){
this.advance(this.vel, a);
}
var d = dist(this.x, this.y, this.points[this.pos].x, this.points[this.pos].y);
if( d < this.vel ){
if(this.pos > 0){
this.pos--;
} else{
this.x = this.points[0].x;
this.y = this.points[0].y;
}
}
if(_id_actual_sprite_runing_.body_created){
_id_actual_sprite_runing_.translate(this.x, this.y);
} else{
_id_actual_sprite_runing_.x = this.x;
_id_actual_sprite_runing_.y = this.y;
}
return this.pos;
break;
//--------------------------------------------------------------------------------------------------------------------------------------
case REVERSE_LOOP:
if(this.pause){
return this.pos;
}
if(type_ != this.last){
switch(this.last){
case FORWARD:
if(this.pos>0){
this.pos--;
} else{
this.pos = this.points.length-1;
}
break;
case FORWARD_LOOP:
if(this.pos>0){
this.pos--;
} else{
this.pos = this.points.length-1;
}
break;
case REVERSE:
break;
case REVERSE_LOOP:
break;
}
this.last = type_;
}
if(this.x == this.points[0].x){
if(this.y = this.points[0].y){
return this.pos;
}
}
var a = this.getAngle(_id_actual_sprite_runing_.x, _id_actual_sprite_runing_.y, this.points[this.pos].x, this.points[this.pos].y);
if(this.pos >= 0){
this.advance(this.vel, a);
}
var d = dist(this.x, this.y, this.points[this.pos].x, this.points[this.pos].y);
if( d < this.vel ){
if(this.pos > 0){
this.pos--;
} else{
this.pos = this.points.length-1;
}
}
if(_id_actual_sprite_runing_.body_created){
_id_actual_sprite_runing_.translate(this.x, this.y);
} else{
_id_actual_sprite_runing_.x = this.x;
_id_actual_sprite_runing_.y = this.y;
}
return this.pos;
break;
//--------------------------------------------------------------------------------------------------------------------------------------
}
}
//----------------------------------------------
getAngle (x1, y1, x2, y2){
var a = degrees(atan2(y2-y1, x2-x1 ));
if(a<0){a=360.0+a;}
if(a>=360.0){a=0;}
return (360-a);
}
//----------------------------------------------
advance(dist, angle){
this.x = this.x + dist * cos(radians(-angle));
this.y = this.y - dist * sin(radians(angle));
}
//----------------------------------------------
}[/Codebox]
En el post anterior te he dejado la libreria para probar