View Single Post
  #32  
Old 06-25-2008, 02:59 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
Since this thread is already kind of bumped:

PHP Code:
function onActionServerSide()
{
  switch (
params[0]) {    
    case 
"freezeplayer":
      
clientr.frozen true;
    break;
    
    case 
"unfreezeplayer":
      
clientr.frozen false;
    break;
  }
}

//#CLIENTSIDE

//**************************************************
function onCreated()
{
  
this.baseSpeed 12;
  
this.acceleration 2;
  
this.friction.plain 0.75// <- Ordinary, which means 'normal ground'.
  
this.friction.water 0.5;
  
this.friction.chair 0.5;
  
this.friction.air 0.95;
  
this.gravity 2;
  
  
this.baseJumpPower.xy 25;
  
this.baseJumpPower.7;
  
this.doublePressDelay 0.25;
  
  
this.gani.idle "idle";
  
this.gani.walk "walk";
  
this.gani.slowWalk "slowwalk";
  
this.gani.swim "swim";
  
this.gani.sit "sit";
  
  
disabledefmovement();
  
  
onTimeout();
}


//**************************************************
function onKeyPressed(keycodekeynamescancode)
{
  switch (
keyname) {
    case 
"n":
      
this.noclip = !this.noclip;
      
player.chat "Noclip: " SPC this.noclip;
    break;
  }
}


//**************************************************
function onTimeout()
{
  
temp.dirArr = {falsefalsefalsefalse};
  if (
this.freezetime <= 0)
  {
    if (
keydown(0)) temp.dirArr[0] = true;
    if (
keydown(1)) temp.dirArr[1] = true;
    if (
keydown(2)) temp.dirArr[2] = true;
    if (
keydown(3)) temp.dirArr[3] = true;
  }
  
  
// *****Loop throught the keys (temp.dirArr).
  
for (i=0i<4i++) {
    
temp.dir i;
    if (
temp.dirArr[i] == true) {
      if (
this.("keyMode" temp.dir) == 0) {
        
this.("lastKeyPress" temp.dir) = timevar2;
        
this.("keyMode" temp.dir) = 1;
      }
      elseif (
this.("keyMode" temp.dir) == 2) {
        
Jump(temp.dir);
        
this.("keyMode" temp.dir) = 0;
      }
    }
    else {
      if (
this.("keyMode" temp.dir) == 1) {
          
this.("keyMode" temp.dir) = 2;
      }
      if (
timevar2 this.("lastKeyPress" temp.dir) > this.doublePressDelay) {
        
this.("keyMode" temp.dir) = 0;
      }
    }
  }
  
  
// *****Get current friction
  
if (player.== 0) {
    
temp.curtiletype tiletype(player.1.5player.2);
    if (
temp.curtiletype == 11temp.friction this.friction.water;
    elseif (
temp.curtiletype == 3temp.friction this.friction.chair;
    else 
temp.friction this.friction.plain;
  }
  else 
temp.friction this.friction.air;
  
  
// *****Apply friction
  
this.speed.*= temp.friction;
  
this.speed.*= temp.friction;
  if (
this.speed.0.25 && this.speed.> -0.25this.speed.0;
  if (
this.speed.0.25 && this.speed.> -0.25this.speed.0;
  
  
// *****Accelerate so we'll eventually reach that target position.
  
if (player.== && this.freezetime <= 0) {
    if (
keydown(0)) Accelerate(0, -this.accelerationthis.baseSpeed);
    if (
keydown(1)) Accelerate(-this.acceleration0this.baseSpeed);
    if (
keydown(2)) Accelerate(0this.accelerationthis.baseSpeed);
    if (
keydown(3)) Accelerate(this.acceleration0this.baseSpeed);
  }
  
  
// *****Actually move the player according to current speed.
  
Move(this.speed.xthis.speed.y);
  if (!
clientr.frozen && (this.speed.!= || this.speed.!= 0)) player.dir getdir(this.speed.xthis.speed.y);
  
  
// *****Gravity
  
if (player.0this.speed.-= this.gravity;
  elseif (
this.speed.0this.speed.0;
  
player.+= this.speed.16;
  
  
// *****Freeze
  
if (this.freezetime 0this.freezetime -= 0.05;
  
  
// *****Set animation.
  
if (Distance(this.speed.xthis.speed.y) > 0.1) {
    
temp.groundtype GetGroundType();
    if (
temp.groundtype == "water"setani(this.gani.swim"");
    elseif (
temp.groundtype == "chair"setani(this.gani.sit"");
    elseif (!
clientr.frozen && Distance(this.speed.xthis.speed.y) > 0.5) {
      if (
Distance(this.speed.xthis.speed.y) < 7.5setani(this.gani.slowWalk"");
      else 
setani(this.gani.walk"");
    }
    else 
setani(this.gani.idle"");
  }
  
  
setTimer(0.05);
}


//**************************************************
// 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 || clientr.frozen) 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 0.75 temp.xDir * (1/16), player.1.751.51.25) || this.nocliptemp.+= 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.0.75temp.16 1.75 temp.yDir * (1/16), 1.51.25) || this.nocliptemp.+= temp.yDir;
      else break;
    }
    
player.temp.16;
  }
}


//**************************************************
// Jump in the specified direction.
function Jump(direction)
{
  if (
player.== 0) {
    if (
this.speed.== NULLthis.speed.+= this.baseJumpPower.z;
    if (
direction == 0this.speed.-= this.baseJumpPower.xy;
    elseif (
direction == 1this.speed.-= this.baseJumpPower.xy;
    elseif (
direction == 2this.speed.+= this.baseJumpPower.xy;
    elseif (
direction == 3this.speed.+= this.baseJumpPower.xy;
  }
}


//**************************************************
// Prevent the player form accelerating or jumping via keydown() for the specified amount of time.
public function freeze(time) {
  if (
time != NULLthis.freezetime time;
  else return 
this.freezetime;
}


//**************************************************
// Replace ganis to be displayed when walking etc. Read onCreated() for list of variables.
public function replaceGani(typegani)
{
  
makevar("this.gani." type) = gani;
  
player.chat makevar("this.gani." type);
}


//**************************************************
// Returns a string representing the ground type the player is standing on. We need this in order to figure out what gani to use.
function GetGroundType()
{
  
temp.tiletype tiletype(player.1.5player.2);
  if (
temp.tiletype == 11) return "water";
  elseif (
temp.tiletype == 3) return "chair";
  else return 
"ground";
}


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

It's a later revision of the original code that I completely forgot about and found again only recently.
Note: the animations might not work properly since I didn't bother with extensive debugging after modifying it slightly for Code Gallery use.

Like the previous modification posted, this one lets you use the keyboard to move around. Unlike the previous, it uses Accelerate() instead of SetTarget() (*gasp*).
In addition, as a result of the great mind leakage of 1978*, it implements a UT style jump/dodge function which is activated when you tap a direction button twice. It will usually keep you entertained for about sixty seconds.

It's been fooled around with by at least two other coders but I'm unable to deduce who did what exactly.

[Footnote*]
This disastrous event, which killed many, was a result of Xzirox running into the Barsebäck reactor to save a baby from radiation poisoning. The baby had already been burnt to a crisp by the intense heat of course, so he was faced with nought but carbon fumes. The combination of fumes, radiation and a conveniently occurring discharge of electricity momentarily mutated Xzirox into a Mindflayer with great telepathic abilities who lost control of it's telepathic wave transmitters, resulting in all of his most terrible ideas being leaked into a nearby town. I was observing a pane of lead at the time which blocked most of the waves, so I was fortunately only struck with a somewhat innocent idea. However, even this limited exposure caused me to lose a whole night of sleep by forcing me to compulsively finish the feature before finally allowing me to sleep. The Swedish government agreed to pay for the medical bills for the humanification if Xzirox would promise never to attempt to save a baby again. Nowadays the survivors of that terrible accident annually pay respects to the less lucky ones who had their heads explode.
Reply With Quote