Skip to content

Commit

Permalink
poc of knockback ability
Browse files Browse the repository at this point in the history
  • Loading branch information
thmsndk committed Oct 15, 2024
1 parent 8c4c281 commit 73d6a67
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 4 deletions.
42 changes: 42 additions & 0 deletions js/common_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,48 @@ function is_in_front(observer,entity)
return false;
}

function knockback(player, monster, knockbackDistance) {
const dx = player.x - monster.x;
const dy = player.y - monster.y;

const dist = Math.sqrt(dx * dx + dy * dy);

// Avoid division by zero
if (dist === 0) {
return [[player.x, player.y]];
}

const unitDx = dx / dist;
const unitDy = dy / dist;

// Check points along the path
// const stepSize = 15;
const steps = 100; // Number of steps to check along the path
const validPoints = [];
let previousPoint = player;
for (let i = 1; i <= steps; i++) {
const intermediateX = player.x + unitDx * (knockbackDistance * i / steps);
const intermediateY = player.y + unitDy * (knockbackDistance * i / steps);
const intermediatePoint = { x: intermediateX, y: intermediateY };

if(can_move({
map:player.map,
x:previousPoint.x,
y:previousPoint.y,
going_x:intermediatePoint.x,
going_y:intermediatePoint.y,
base:player.base})) {

validPoints.push(intermediatePoint)
} else {
break;
}
previousPoint = intermediatePoint;
}

return validPoints.map(p => [p.x,p.y]);
}

function calculate_movex(map, cur_x, cur_y, target_x, target_y) {
if(target_x==Infinity) target_x=CINF;
if(target_y==Infinity) target_y=CINF;
Expand Down
44 changes: 44 additions & 0 deletions js/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -2668,6 +2668,50 @@ function init_socket(args)
}
}
}
else if(data.type=="polygon"){

// var entity=get_entity(data.name);


// TODO: styles from event
const graphics = new PIXI.Graphics();


// Set the line style (width, color, alpha)
graphics.lineStyle(1, 0x0000FF, 1);

// Set the fill color
graphics.beginFill(0xDE3249);

// Draw the polygon
const points = data.points.reduce((res, p) => {
res.push(p.x, p.y)
return res;
},[])

graphics.drawPolygon(points);

graphics.endFill();

map.addChild(graphics);

function disappear(step,sprite)
{
return function(){
sprite.alpha-=0.08;
// we render 10 steps, each step is 1000 ms
if(step<10) draw_timeout(disappear(step+1,sprite), 100);
else
{
remove_sprite(sprite);
try{ sprite.destroy(); }catch(e){}
}
}
}

draw_timeout(disappear(0, graphics), 1000);

}
else if(is_sdk)
console.log("Unhandled 'ui': "+data.type);
});
Expand Down
8 changes: 4 additions & 4 deletions node/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -11728,7 +11728,8 @@ function update_instance(instance) {

// TODO: be able to offset the polygon start point by N lenght from the monster
// Frontal Cone, using the monsters own angle
const cone = generatePolygon(monster, 100, monster.angle /* south */);
const cone = generatePolygon(monster, 200, monster.angle /* south */);
// TODO: should the polygon handle collisions as "line of sight" so it can't effect them

// is_point_inside expects an array of tubles [[x1,y1],[x2,y2]]
const polygon = cone.map((p) => [p.x, p.y]);
Expand All @@ -11749,11 +11750,10 @@ function update_instance(instance) {
setTimeout(() => {
// TODO: this calculation seems kinda off when lookin at the client and the server decision
if (is_point_inside([player.x, player.y], polygon)) {
// TODO: knockback alculationbbbbbbbbbb depending on angles and such
console.log(`${player.name} is inside the polygon!`);

const knockbackPoints = knockback(player, monster, 250);
// TODO: We can also supply an effect to transport?
transport_player_to(player, monster.map, [player.x, player.y - 150]);
transport_player_to(player, monster.map, knockbackPoints[knockbackPoints.length - 1]);
resend(player, "u+cid");

monster.moving = true;
Expand Down

0 comments on commit 73d6a67

Please sign in to comment.