From ffcffcc714a9c754655f1523cdcffb9115a07550 Mon Sep 17 00:00:00 2001 From: zakarya Date: Thu, 9 Mar 2023 10:14:54 -0800 Subject: [PATCH] added tick delta and punching --- img/world/biome/forest/boulder.svg | 8 ++--- script/client.js | 7 +++- script/game.js | 51 ++++++++++++++++++++++++++---- 3 files changed, 54 insertions(+), 12 deletions(-) diff --git a/img/world/biome/forest/boulder.svg b/img/world/biome/forest/boulder.svg index e70a439..90a727b 100644 --- a/img/world/biome/forest/boulder.svg +++ b/img/world/biome/forest/boulder.svg @@ -59,7 +59,7 @@ id="layer1"> + transform="matrix(0.87102117,0.28747162,-0.28747162,0.87102117,38.565043,-14.677095)" + inkscape:transform-center-y="-2.8935958e-06" /> diff --git a/script/client.js b/script/client.js index 7112015..ea274ca 100644 --- a/script/client.js +++ b/script/client.js @@ -2,6 +2,7 @@ var canvas; var canvas_center = new vector(0, 0); var mouse_position = new vector(0, 0); var camera = new vector(0, 0); +var lastupdate = Date.now(); function load_image(path) { let image = new Image(); @@ -63,8 +64,12 @@ function init() { } function tick() { + let now = Date.now(); + let delta = now - lastupdate; + lastupdate = now; + resize_canvas(canvas); - game_tick(); + game_tick(delta); } function game_ready() { diff --git a/script/game.js b/script/game.js index e3be4bb..a4b690c 100644 --- a/script/game.js +++ b/script/game.js @@ -174,22 +174,26 @@ function do_collisions() { }); } -function do_movement() { +function do_movement(delta) { if (keys_pressed["KeyW"]) { - player.position.y -= player_speed / fps; + player.position.y -= player_speed / delta; } if (keys_pressed["KeyS"]) { - player.position.y += player_speed / fps; + player.position.y += player_speed / delta; } if (keys_pressed["KeyA"]) { - player.position.x -= player_speed / fps; + player.position.x -= player_speed / delta; } if (keys_pressed["KeyD"]) { - player.position.x += player_speed / fps; + player.position.x += player_speed / delta; } //rotation player.rotation = canvas_center.rotation_to(mouse_position) + rotation_offset; + + if (mouse_down && !punching) { + interact(); + } } document.onmousemove = function(e) { @@ -199,9 +203,9 @@ document.onmousemove = function(e) { mouse_position.y = e.pageY; } -function game_tick() { +function game_tick(delta) { //calculations - do_movement(); + do_movement(delta); do_collisions(); move_camera(); @@ -310,4 +314,37 @@ function send_position_to_server() { clearInterval(update_server_tick); console.log("disconnected from server"); } +} + +//interaction +var interact_interval; +var punching = false; +var mouse_down = false; +document.body.onmousedown = function() { + mouse_down = true; +} +document.body.onmouseup = function() { + mouse_down = false; +} +function interact() { + if (!punching) { + punching = true; + interact_interval = setInterval(interaction_animation, 1000/fps, Date.now()); + } +} + +function inter_anim_curve(x) { + return -15*Math.pow(x-0.25, 2)+1; +} + +function interaction_animation(start_time) { + let now = Date.now(); + let x = (now - start_time)/1000; + //console.log(inter_anim_curve(x)); + rotation_offset = inter_anim_curve(x)/2; + if (x >= 0.5) { + clearInterval(interact_interval); + rotation_offset = 0; + punching = false; + } } \ No newline at end of file