Creation/Dev/Script/Vector Position Functions

From Graal Bible
Revision as of 14:34, 24 September 2011 by Cbk1994 (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Under Construction.png

This article is currently being written or rewritten by Chris Vimes.


vecx and vecy are two commonly-used GScript functions which help calculate positions based on a direction.

 vecx(dir) - returns the amount a player would move on the x axis if moving in direction dir
 vecy(dir) - returns the amount a player would move on the y axis if moving in direction dir

The amount is based on a player speed of one tile per movement. You can multiply the returned value by your desired speed to find the appropriate movement delta for your situation.

Reference Table

Direction vecx(dir) vecy(dir)
0 (up) 0 -1
1 (left) -1 0
2 (down) 0 1
3 (right) 1 0

Example (Staff Boots)

//#CLIENTSIDE 
function onCreated() {
  this.speed = 1;
}  

function onKeyPressed(code, key) {  
  if (key == "b") { 
    // toggle boots
    this.on = ! this.on;
      
    if (this.on) {
      this.trigger("timeout", null);
    }  
  }  
} 

function onTimeOut() {  
  if (! this.on) { 
    return;
  }  
    
  for (temp.key = 0; key < 4; key ++) {  
    if (keydown(key)) { // is the key down?  
      // move the player in the appropriate direction using vecx and vecy, multiplying
      // them by the current speed
      player.x += (vecx(key) * this.speed);  
      player.y += (vecy(key) * this.speed);  
    }  
  }  
    
  this.setTimer(0.05);  
}  

function onPlayerChats() {  
  if (player.chat.starts("/speed ")) {  
    this.speed = player.chat.substring(7).trim();
  }  
}

See Also