Thread: Point-n-click
View Single Post
  #1  
Old 09-03-2009, 04:08 PM
Codein Codein is offline
jwd
Codein's Avatar
Join Date: Oct 2005
Location: Greater Manchester
Posts: 2,423
Codein has a spectacular aura aboutCodein has a spectacular aura about
Send a message via AIM to Codein Send a message via MSN to Codein
Point-n-click

I have provided the point-n-click movement system I am using on Noctorious. Now, you may be thinking "Yes, this has been done" but I wanted to provide a version that wasn't as complex as Mallard's system for those who haven't the skill or knowledge to hack his system.

This system moves the player at a static speed, rather than taking
acceleration into account, thus acting as an intermediate solution between classic static-speed arrowkey movement and Mallard's point-n-click movement system.

Not only that, but I'd say it'd be slightly easier to learn from this system if you're new-ish to the GScript language.

The collision detection isn't the greatest, however, I've managed to get the player pretty flush against walls.

Credits:
Mallard - Creator of the system which gave me inspiration and guidance in constructing this system.

PHP Code:
/*
  Script: Simple Point-n-Click Movement
  Author: Codein
  
  Description:
  A point-n-click movement system that moved the player in a static speed.
*/
//#CLIENTSIDE

function onCreated() {
  
disableDefMovement();
  
  
this.moveSpeed 12/16;
  
  
setTimer(0.05);
}

/*
  Main Loop.
*/
function onTimeout() {    
  
updateVisuals();
  
  if (
leftmousebutton) {
    
//Set the position in which to move to.   
    
this.position = new TStaticVar();
    
this.position.mousex;
    
this.position.mousey;
    
    
updateUnitVectors();
    
    
this.moving true;
  }
  
  
/*
    Check if player is not moving in one direction.
    This is because the unit vectors would stay the same,
    causing the player to miss the target position, otherwise.
  */ 
  
if (player.== this.playerX || player.== this.playerY) {
    
updateUnitVectors();
  }
  
  
this.playerX player.x;
  
this.playerY player.y;
  
  
/*
    Check whether the player has reached the target
  */
  
this.distance = ((this.position.player.x) ^ + (this.position.player.y) ^ 2) ^ 0.5;
  
  if (
this.moving == true && this.distance 2) {
    
movePlayer();
  }
  
  else {
    
this.moving false;
  }
  
  
setDirection();
  
setAnimation();
  
  
setTimer(0.05);
}

/*
  Display a crosshair that turns red when over a blocking tile
  and green when you're able to move there.
*/
function updateVisuals() {
    
temp.aimCross showtext(201mousex 0.5mousey 1"b""Arial""+");
    
temp.aimCross.zoom 1.5;
  
    if (
onwall(mousexmousey)) {
      
temp.aimCross.red 255;
      
temp.aimCross.blue 0;
      
temp.aimCross.green 0;
    }
  
    elseif (
leftmousebutton) {
      
temp.aimCross.red 0;
      
temp.aimCross.blue 0;
      
temp.aimCross.green 255;
    }
  
    else {
      
temp.aimCross.blue 255;
      
temp.aimCross.green 255;
      
temp.aimCross.red 255;
    }
}

/*
  Set the unit vectors, which we will use to move the player
  in the x/y axis, with the appropriate magnitude,
  to reach the target.
*/
function updateUnitVectors() {
    
this.displacement = new TStaticVar();
    
this.displacement.this.position.player.x;
    
this.displacement.this.position.player.y;
    
    
this.unitVectors vectornormalize({this.displacement.xthis.displacement.y0});
}

/*
  Move the player on the x/y axis.
  
  We check and move the player on the x/y axis independently, so that
  you can slide along a wall.
*/
function movePlayer() {  
  
  
//X axis wall check
  
for (0<= this.moveSpeed+= 1/16) {
    
temp.xCheck player.this.unitVectors[0] * i;
    
    if (
onwall2(temp.xCheck 0.4player.1/162.21)) {
      break;
    }
  }
  
  
//X axis movement
  
player.+= this.unitVectors[0] * (- (this.moveSpeed 1/16 0));
  
  
//Y Axis wall check
  
for (0this.moveSpeed+= 1/16) {
    
temp.yCheck player.this.unitVectors[1] * i;
    
    if (
onwall2(player.1temp.yCheck 1/161.61)) {
      break;
    }
  }
  
  
//Y axis movement
  
player.+= this.unitVectors[1] * (- (this.moveSpeed 1/16 0));
}

function 
setDirection() {
  
player.dir getdir(this.displacement.xthis.displacement.y);
}

/*
  Set the gani of the player, depending on whether they are
    - Swimming
    - Walking
    - Idle
*/
function setAnimation() {
  if (
onwater(player.1.5player.2)) {
    if (
player.ani != "swim"setAni("swim"null);
  }
  
  elseif (
this.moving) {
    if (
player.ani != "walk"setAni("walk"null);
  }
  
  else {
    
setAni("idle"null);
  }

Reply With Quote