From 73d6a6712c72958d861298706aca6387dfaf6e19 Mon Sep 17 00:00:00 2001 From: thmsn <9084377+thmsndk@users.noreply.github.com> Date: Sun, 16 Jun 2024 22:43:43 +0200 Subject: [PATCH] poc of knockback ability --- js/common_functions.js | 42 ++++++++++++++++++++++++++++++++++++++++ js/game.js | 44 ++++++++++++++++++++++++++++++++++++++++++ node/server.js | 8 ++++---- 3 files changed, 90 insertions(+), 4 deletions(-) diff --git a/js/common_functions.js b/js/common_functions.js index 05828040..f8598cca 100644 --- a/js/common_functions.js +++ b/js/common_functions.js @@ -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; diff --git a/js/game.js b/js/game.js index 2a4e7548..dfe62486 100644 --- a/js/game.js +++ b/js/game.js @@ -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); }); diff --git a/node/server.js b/node/server.js index 3351cb4c..69c538db 100644 --- a/node/server.js +++ b/node/server.js @@ -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]); @@ -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;