VECTOR newPos;
var newPosSet = OFF;
PANEL* theSpot = {
bmap = "stone.tga";
layer = 2;
flags = SHOW;
}
void set_new_pos()
{
newPos.x = mouse_pos.x; //store the current mouse position.
newPos.y = mouse_pos.y;
newPos.x -= bmap_width(theSpot.bmap)/2; //make sure the center of the player reaches the position, not the corner of the player
newPos.y -= bmap_height(theSpot.bmap)/2;
newPosSet = ON;
}
VECTOR temp;
var theSpotSpeed = 10; //play with this value
void move_to_mouse_pos()
{
VECTOR myVelocity;
vec_set(myVelocity,nullvector); //make player not move at beginning
while(1)
{
//if there's a new position: calculate the direction from my position to the new position.
if(newPosSet == ON)
{
vec_diff(myVelocity,newPos,vector(theSpot.pos_x,theSpot.pos_y,0));
newPosSet = OFF;
}
vec_set(temp,myVelocity);
vec_normalize(temp,time_step*theSpotSpeed); //pointing in the direction that I should move in, correct length
//speed*time = distance to be covered
theSpot.pos_x += temp.x; //add the to the position
theSpot.pos_y += temp.y;
if(vec_dist(vector(theSpot.pos_x,theSpot.pos_y,0),newPos) < theSpotSpeed*time_step) vec_set(myVelocity,nullvector);
wait(1);
}
}
void main()
{
//put these lines in your main function:
mouse_mode = 4;
vec_set(newPos,vector(-1,-1,0)); //new position.
on_mouse_left = set_new_pos;
move_to_mouse_pos();
}