Alright, so I have a knockback system that's been plaguing me for perhaps a good year. So I've finally broken down and decided to share it to see if I can get any help with it. Jerret was kind enough to point out that I was doing part of the onwall wrong, so I fixed that and now have been currently fighting with the remaining problem in which it either stops too short, or sticks me in a wall (often I've had it stop too short, then, before adjusting anything, throwing me at the exact same angle would sometimes stick me a little bit into the wall. seemingly at random)
I've tried things like using a for loop to precalculate the distance between me and a wall. Unfortunately this came with two problems. unless I set the player's coordinates to integers (shifting the player at the beginning), the smaller decimals of the player's coordinates would stop me too short. secondly, if a player was in front of the detection before it flinged me, then that would be the limit. even if they moved.
I've also resorted to things like manually basing the onwall detection based on the player's direction as well (i.e. if you're facing this way, check for onwall here, and here. if you're facing that way, instead check for onwall there, and there.). but that also produced wonky results. Part of this headache is that I need this to be able to fling players at variable speeds. I've looked at using one of dusty's posted movement systems that i've incorporated, and importing part of the script into the knockback system, and tweaking it in from there, but that didn't go to well either. (part of the problem being that it doesn't work basing it off of the opposite of the player's direction when I tried it, and my initial idea of converting the movex and movey coordinates into directions wouldn't work as I needed to have access to 0-4, including every number in between. getdir() doesn't do this as it only gives integers, and I don't know of any other built-in way to do such a thing.)
Any help, tips, or hints would be greatly appreciated..
Here's the script:
PHP Code:
//#CLIENTSIDE
function onCreated(){
this = KnockbackSystem;
}
public function onActionKnockback(account, ax, ay, az, ad, speed, distance, stuntime, hurtonimpact){
this.kbparams = {account, ax, ay, az, ad, speed, distance, stuntime, hurtonimpact};
this.kbcounter = distance;
setTimer(0.05);
}
function onTimeout(){
doKnockback(this.kbparams[0], this.kbparams[1], this.kbparams[2], this.kbparams[3], this.kbparams[4], this.kbparams[5], this.kbparams[6], this.kbparams[7]);
setTimer(0.05);
}
function doKnockback(account, ax, ay, az, ad, speed, distance, stuntime){
if(this.kbcounter > 0){
temp.to = {this.kbparams[1], this.kbparams[2]};
temp.angle = getangle(player.x - to[0], player.y - to[1]);
temp.movex = (cos(angle)) * speed;
temp.movey = (sin(angle)) * speed;
temp.onwallx = (player.x + 1) + temp.movex;
temp.onwally = (player.y + 2) - temp.movey;
temp.oldx = player.x;
temp.oldy = player.y;
this.kdir = getdir(player.x - to[0],player.y - to[1]);
if(this.kdir == 0){player.dir = 2;}
elseif(this.kdir == 1){player.dir = 3;}
elseif(this.kdir == 2){player.dir = 0;}
elseif(this.kdir == 3){player.dir = 1;}
showimg(200, "block.png", temp.onwallx, temp.onwally); findimg(200).alpha = 0.5;
if(!onwall(temp.onwallx, temp.onwally)){
player.x += temp.movex;
player.y -= temp.movey;
this.kbcounter -= 1*speed;
}
else{
player.x = temp.oldx;
player.y = temp.oldy;
setani("hurt",NULL);
this.kbcounter = 0;
if(this.kbparams[8] > 0){
onActionDamage(speed);
}
}
if(this.kbcounter <= 0){
doExtraStuff();
return;
}
setTimer(0.05);
}
}
function onActionDamage(speed){
WeaponSystem.onActionDamage(null, this.kbparams[0], (client.maxhealth/100) * speed, 100, "Physical", 100, null, client.playerlevel, this.kbparams[1], this.kbparams[2], this.kbparams[3], this.kbparams[4], 1, 1);
}
function doExtraStuff(){
if(stuntime > 0){
player.freezetime += stuntime;
}
}
function onPlayerchats(){
if(player.chat == "/kbtest"){
onActionKnockback(null, player.x + 0.5, player.y, 0, null, 1, 10, 0, 1);
}
}