Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 03-22-2007, 01:40 AM
DrakilorP2P DrakilorP2P is offline
Registered User
DrakilorP2P's Avatar
Join Date: Apr 2006
Posts: 755
DrakilorP2P is just really niceDrakilorP2P is just really nice
Point n click movement system

This script implements a Diablo 2 style movement system. You click on a location and the player will attempt to move there. Included is acceleration and friction, which are configurable from onCreated().
This script used to have stamina and walking/running, but I removed it since most people would probably want to implement their own such system anyway (And to avoid bloating the script with features no one will use which only make it harder to read.).

Here's some documentation to help you expand it if necessary.

onCreated():
  • Variable initialisation.
onTimeout():
  • Acceleration, friction and movement.
SetTarget(x, y):
  • Properly sets this.target.[x/y] which is where the player will accelerate towards.
Accelerate(xspeed, yspeed, maxspeed):
  • Properly increases this.speed.[x/y] to avoid accelerating further than maxspeed.
Move(x, y):
  • Moves the player with block detection.
Distance(x, y):
  • Helper function; returns distance between (0, 0) and (x, y).

Installation: Used as a weapon.
PHP Code:
//#CLIENTSIDE

//**************************************************
function onCreated()
{
  
this.baseSpeed 8;
  
this.acceleration 4;
  
this.baseFriction 0.5;
  
  
this.target.player.1.5;
  
this.target.player.2;
  
  
onTimeout();
}


//**************************************************
function onTimeout()
{
  
// *****Set target position so we know where to move
  
if (leftmousebutton) {
    
SetTarget(mousexmousey);
  }
  
  
// *****Friction (We aren't walking on ice)
  
this.speed.*= this.baseFriction;
  
this.speed.*= this.baseFriction;
  
  
// *****Accelerate so we'll eventually reach that target position.
  
if (Distance(this.target.player.1.5this.target.player.2) > && !this.stopped) {
    
temp.angle getangle(this.target.player.1.5this.target.player.2);
    
temp.speed.cos(temp.angle) * this.acceleration;
    
temp.speed.= -sin(temp.angle) * this.acceleration;
    
    
Accelerate(temp.speed.xtemp.speed.ythis.baseSpeed);
    
    
setani("walk""");
  }
  else
  {
    if (
this.speed.0.5 && this.speed.> -0.5this.speed.0;
    if (
this.speed.0.5 && this.speed.> -0.5this.speed.0;
    
this.stopped true;
    
setani("idle""");
  }
  
  
// *****Actually move the player according to current speed.
  
Move(this.speed.xthis.speed.y);
  if (
this.speed.!= && this.speed.!= 0player.dir getdir(this.speed.xthis.speed.y);

  
setTimer(0.05);
}


//**************************************************
function onMouseDown(button)
{
  
// Set target position so we know where to move.
  
if (button == "left") {
    
SetTarget(mousexmousey);
  }
}


//**************************************************
// Set the position the system will try to reach.
function SetTarget(targetxtargety)
{
    if (
Distance(targetx player.1.5targety player.2) > 1) {
      
this.target.targetx;
      
this.target.targety;
      
this.stopped false;
    }
}


//**************************************************
// Increase the speed in pixels per frame.
function Accelerate(xSpeedySpeedmaxSpeed)
{
  if (
Distance(this.speed.xthis.speed.y) < maxSpeed || (Distance(this.speed.xSpeedthis.speed.ySpeed) - Distance(this.speed.xthis.speed.y)) <= 0) {
    
this.speed.+= xSpeed;
    
this.speed.+= ySpeed;
    
    if (
Distance(this.speed.xthis.speed.y) > maxSpeed) {
      
temp.angle getangle(this.speed.xthis.speed.y);
      
this.speed.cos(temp.angle) * maxSpeed;
      
this.speed.= -sin(temp.angle) * maxSpeed;
    }
  }
}


//**************************************************
// Move the player by specified amount of pixels.
function Move(xSpeedySpeed)
{
  if (!
isapplicationactive) return;
  
  
xSpeed xSpeed<0?int(xSpeed)+1:int(xSpeed);
  
ySpeed ySpeed<0?int(ySpeed)+1:int(ySpeed);
  
  
// --- 'x' movement of the player --- \\
  
if (xSpeed != 0) {
    
temp.int(player.16);
    
temp.xDir = (abs(xSpeed) / xSpeed);
    for (
i=0i<abs(xSpeed); i++) {
      if (!
onwall2(temp.16 temp.xDir * (1/16), player.211)) temp.+= temp.xDir;
      else break;
    }
    
player.temp.16;
  }
  
  
// --- 'y' movement of the player --- \\
  
if (ySpeed != 0) {
    
temp.int(player.16);
    
temp.yDir = (abs(ySpeed) / ySpeed);
    for (
i=0i<abs(ySpeed); i++) {
      if (!
onwall2(player.1temp.16 temp.yDir * (1/16), 11)) temp.+= temp.yDir;
      else break;
    }
    
player.temp.16;
  }
  
  
//Hack
  
if (tiletype(player.1.5player.2) == 3) {
    
setani("sit""");
  }
}


//**************************************************
//Calculate the distance between (0, 0) and (dx, dy).
function Distance(dxdy)
{
  return ((
dx)^+ (dy)^2)^0.5

Reply With Quote
 


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 04:38 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.