The getHitBox() function now only requires you call it whenever you want to change
your hitbox size||position. This is a far better way to go than my first example.
getHitBox( box, offset x, offset y)
box is a 4 element array, { width, height, offset x, offset y}
all these are re-oriented depending on the way the player is facing.
ie, if you offset the hitbox so it always hits on the players right,
it will always be on the players right, no matter how your facing.
The function returns a 2d where hbox =
{{x,y,w,h},{x,y,w,h},{x,y,w,h},{x,y,w,h}};
where each set of coordinates represents the 4 directions the player/npc is facing.
This way instead of running the function whenever the player changes directions,
it can just lookup the proper coordinates we stored here.
For use with players, you will want to add offset x/y as 1.5,2 to account for the fact the
players x and y are at his top left. You can also leave it blank, this is optional. If you use it
with an npc you may want to adjust this to center the box on your npc.
As for the speed of this script, I'd say it can't get much faster serverside. Certainly only running getHitBox()
once isn't slowing things down at all. Only way to increase the speed of this script seems to be bringing
the player/npc detection to the client. The way it is now might get laggy with 50 players or more
pking constantly. Hard to say without some more experienced insights.
PHP Code:
//Use to generate coordinates for a HitBox centered around x,y + offx,offy
//box = {w,h,offx,offy} which are all adjusted by direction
function getHitBox(box,offx,offy){
temp.hbox = new[4];
temp.ox = {box[2],box[3],-box[2],-box[3]};//created custom arrays for the offsets
temp.oy = {box[3],-box[2],-box[3],box[2]};// because it was getting confusing otherwise.
for (temp.d=0;temp.d<4;temp.d++){
temp.wi = (d in {1,3}) ? box[1]:box[0];// swap width/height if direction is different.
temp.hi = (d in {1,3}) ? box[0]:box[1];
temp.x = offx - wi/2 + vecx(d)*(wi/2) - ox[d]; // the magic happening here also.
temp.y = offy - hi/2 + vecy(d)*(hi/2) - oy[d];
temp.hbox[d] = {x,y,wi,hi};
}
return temp.hbox;
}
PHP Code:
//add the functions to the script with join, or just put them in the script I suppose.
function onCreated(){
this.join("public_adam_func");
}
function onActionServerside(){// Make sure we got a hitbox set.
if (params[0] == "newhbox"){
player.hbox = getHitBox({2,4,0,0},1.5,2);
}
if (params[0] == "hitthings"){
// use player.dir as the paramater of the hbox array. and 0,1,2,3 in place of x,y,w,h. Be sure and add the players x,y to the hbox's x,y.
for (temp.obj: findAreaPlayers(player.x + player.hbox[player.dir][0],player.y + player.hbox[player.dir][1],player.hbox[player.dir][2],player.hbox[player.dir][3],player.account)){
//do stuff with the players you hit here!
}
for (temp.obj: findAreaNpcs(player.x + player.hbox[player.dir][0],player.y + player.hbox[player.dir][1],player.hbox[player.dir][2],player.hbox[player.dir][3])){
// Do stuff with the NPC's you hit here!
}
}
}
//#CLIENTSIDE
function onCreated(){
triggerServer("gui",this.name,"newhbox");
}
function onWeaponFired(){
triggerServer("gui",this.name,"hitthings");
}
Also, the "box" itself doesn't do anything except be a well placed, direction oriented box. This function just takes the need to do any calculating out of your hands, and generates the coordinates for a good hitbox for you.
You could use the box for various random things, but it was initially intended for a fully custom melee dmg system. With various shaped hitbox's for different shaped/size melee weapons. Daggers, staves, swords, etc... punch. kick. Left handed daggers, right handed daggers, that not only look different, but hit differently.