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.z = 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(keycode, keyname, scancode)
{
switch (keyname) {
case "n":
this.noclip = !this.noclip;
player.chat = "Noclip: " SPC this.noclip;
break;
}
}
//**************************************************
function onTimeout()
{
temp.dirArr = {false, false, false, false};
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=0; i<4; i++) {
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.z == 0) {
temp.curtiletype = tiletype(player.x + 1.5, player.y + 2);
if (temp.curtiletype == 11) temp.friction = this.friction.water;
elseif (temp.curtiletype == 3) temp.friction = this.friction.chair;
else temp.friction = this.friction.plain;
}
else temp.friction = this.friction.air;
// *****Apply friction
this.speed.x *= temp.friction;
this.speed.y *= temp.friction;
if (this.speed.x < 0.25 && this.speed.x > -0.25) this.speed.x = 0;
if (this.speed.y < 0.25 && this.speed.y > -0.25) this.speed.y = 0;
// *****Accelerate so we'll eventually reach that target position.
if (player.z == 0 && this.freezetime <= 0) {
if (keydown(0)) Accelerate(0, -this.acceleration, this.baseSpeed);
if (keydown(1)) Accelerate(-this.acceleration, 0, this.baseSpeed);
if (keydown(2)) Accelerate(0, this.acceleration, this.baseSpeed);
if (keydown(3)) Accelerate(this.acceleration, 0, this.baseSpeed);
}
// *****Actually move the player according to current speed.
Move(this.speed.x, this.speed.y);
if (!clientr.frozen && (this.speed.x != 0 || this.speed.y != 0)) player.dir = getdir(this.speed.x, this.speed.y);
// *****Gravity
if (player.z > 0) this.speed.z -= this.gravity;
elseif (this.speed.z < 0) this.speed.z = 0;
player.z += this.speed.z / 16;
// *****Freeze
if (this.freezetime > 0) this.freezetime -= 0.05;
// *****Set animation.
if (Distance(this.speed.x, this.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.x, this.speed.y) > 0.5) {
if (Distance(this.speed.x, this.speed.y) < 7.5) setani(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(xSpeed, ySpeed, maxSpeed)
{
if (Distance(this.speed.x, this.speed.y) < maxSpeed || (Distance(this.speed.x + xSpeed, this.speed.y + ySpeed) - Distance(this.speed.x, this.speed.y)) <= 0) {
this.speed.x += xSpeed;
this.speed.y += ySpeed;
if (Distance(this.speed.x, this.speed.y) > maxSpeed) {
temp.angle = getangle(this.speed.x, this.speed.y);
this.speed.x = cos(temp.angle) * maxSpeed;
this.speed.y = -sin(temp.angle) * maxSpeed;
}
}
}
//**************************************************
// Move the player by specified amount of pixels.
function Move(xSpeed, ySpeed)
{
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.x = int(player.x * 16);
temp.xDir = (abs(xSpeed) / xSpeed);
for (i=0; i<abs(xSpeed); i++) {
if (!onwall2(temp.x / 16 + 0.75 + temp.xDir * (1/16), player.y + 1.75, 1.5, 1.25) || this.noclip) temp.x += temp.xDir;
else break;
}
player.x = temp.x / 16;
}
// --- 'y' movement of the player --- \\
if (ySpeed != 0) {
temp.y = int(player.y * 16);
temp.yDir = (abs(ySpeed) / ySpeed);
for (i=0; i<abs(ySpeed); i++) {
if (!onwall2(player.x + 0.75, temp.y / 16 + 1.75 + temp.yDir * (1/16), 1.5, 1.25) || this.noclip) temp.y += temp.yDir;
else break;
}
player.y = temp.y / 16;
}
}
//**************************************************
// Jump in the specified direction.
function Jump(direction)
{
if (player.z == 0) {
if (this.speed.z == NULL) this.speed.z += this.baseJumpPower.z;
if (direction == 0) this.speed.y -= this.baseJumpPower.xy;
elseif (direction == 1) this.speed.x -= this.baseJumpPower.xy;
elseif (direction == 2) this.speed.y += this.baseJumpPower.xy;
elseif (direction == 3) this.speed.x += this.baseJumpPower.xy;
}
}
//**************************************************
// Prevent the player form accelerating or jumping via keydown() for the specified amount of time.
public function freeze(time) {
if (time != NULL) this.freezetime = time;
else return this.freezetime;
}
//**************************************************
// Replace ganis to be displayed when walking etc. Read onCreated() for list of variables.
public function replaceGani(type, gani)
{
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.x + 1.5, player.y + 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(dx, dy)
{
return ((dx)^2 + (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.