Hitting !!
This commit is contained in:
@@ -302,6 +302,8 @@ export class Client {
|
||||
}
|
||||
} else if (InputManager.is_mouse_pressed(2)) {
|
||||
this.game_mode.use_item();
|
||||
} else if (!this.entity_hit && InputManager.is_mouse_pressed(0)) {
|
||||
this.game_mode.swing();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -323,6 +325,8 @@ export class Client {
|
||||
|
||||
if (inventories.hotbar_selected !== previous) {
|
||||
this.connection.send({ type: "select_slot", slot: inventories.hotbar_selected });
|
||||
// the server starts the attack cooldown over too
|
||||
this.player.last_attack = performance.now();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,6 +60,9 @@ export class LocalPlayer extends Player {
|
||||
health = 20;
|
||||
food = 20;
|
||||
saturation = 5;
|
||||
// the attack speed attribute the server sent, and when they last attacked or switched slots, for the indicator
|
||||
attack_speed = 4;
|
||||
last_attack = -Infinity;
|
||||
|
||||
// blocks per second, peaks about 1.25 blocks up with the entity's gravity and drag
|
||||
jump_force = 9.23;
|
||||
@@ -81,6 +84,13 @@ export class LocalPlayer extends Player {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
// like the server's ServerPlayer.attack_strength, from real time so the indicator fills smoothly: 0 right after
|
||||
// attacking, 1 after 20 / attack_speed ticks
|
||||
attack_strength(): number {
|
||||
const ticks = (performance.now() - this.last_attack) / 50;
|
||||
return Math.min(1, Math.max(0, (ticks + 0.5) / (20 / this.attack_speed)));
|
||||
}
|
||||
|
||||
tick() {
|
||||
this.#apply_input(this.client.screen ? NO_INPUT : this.#read_input());
|
||||
this.#prev_eye_height = this.eye_height;
|
||||
|
||||
@@ -100,6 +100,13 @@ export class MultiPlayerGameMode {
|
||||
attack(entity: Entity) {
|
||||
this.client.player.send_position_now();
|
||||
this.client.connection.send({ type: "attack_entity", id: entity.id });
|
||||
this.client.player.last_attack = performance.now();
|
||||
}
|
||||
|
||||
// left click on nothing: a swing at the air, which uses up the attack cooldown like an attack
|
||||
swing() {
|
||||
this.client.connection.send({ type: "swing" });
|
||||
this.client.player.last_attack = performance.now();
|
||||
}
|
||||
|
||||
// right click on nothing
|
||||
|
||||
+18
-3
@@ -1,7 +1,7 @@
|
||||
import type { Client } from "$/client/client.ts";
|
||||
import type { ClientInventories } from "$/client/inventory.ts";
|
||||
import { draw_item } from "$/client/rendering/render_utils.ts";
|
||||
import { canvas, draw_text, measure_text } from "$/client/renderer/mod.ts";
|
||||
import { canvas, draw_rect, draw_text, measure_text } from "$/client/renderer/mod.ts";
|
||||
import { display_name } from "$/common/utils.ts";
|
||||
import { TEXT_HEIGHT, TEXT_SCALE } from "./widgets.ts";
|
||||
import { draw_gui_sprite, GUI_SCALE } from "./gui_sprites.ts";
|
||||
@@ -24,6 +24,12 @@ const HELD_NAME_GAP = 12;
|
||||
const CROSSHAIR_SIZE = 15;
|
||||
// under the crosshair so it shows on bright sky and snow, minecraft inverts the colors behind it instead
|
||||
const CROSSHAIR_SHADOW: [number, number, number, number] = [0, 0, 0, 0.4];
|
||||
// the attack indicator, like minecraft's: a bar under the crosshair filling up while the attack cooldown runs
|
||||
const ATTACK_BAR_WIDTH = 16;
|
||||
const ATTACK_BAR_HEIGHT = 2;
|
||||
const ATTACK_BAR_GAP = 2;
|
||||
const ATTACK_BAR_BACK: [number, number, number, number] = [0, 0, 0, 0.4];
|
||||
const ATTACK_BAR_FILL: [number, number, number, number] = [1, 1, 1, 0.9];
|
||||
|
||||
// what's drawn over the world while playing, like minecraft's Gui: hotbar, crosshair and chat
|
||||
export class Hud {
|
||||
@@ -35,7 +41,7 @@ export class Hud {
|
||||
this.#render_hotbar(client.player.inventories);
|
||||
this.#render_vitals(client);
|
||||
this.#render_held_name(client.player.inventories);
|
||||
this.#render_crosshair();
|
||||
this.#render_crosshair(client);
|
||||
client.chat.render(false, icons_y() - HELD_NAME_GAP);
|
||||
}
|
||||
|
||||
@@ -113,12 +119,21 @@ export class Hud {
|
||||
}
|
||||
}
|
||||
|
||||
#render_crosshair() {
|
||||
#render_crosshair(client: Client) {
|
||||
const size = CROSSHAIR_SIZE * GUI_SCALE;
|
||||
const x = Math.round((canvas.width - size) / 2);
|
||||
const y = Math.round((canvas.height - size) / 2);
|
||||
draw_gui_sprite("crosshair", x + GUI_SCALE, y + GUI_SCALE, undefined, undefined, CROSSHAIR_SHADOW);
|
||||
draw_gui_sprite("crosshair", x, y);
|
||||
|
||||
// only while it's charging, like minecraft
|
||||
const strength = client.player.attack_strength();
|
||||
if (strength >= 1) return;
|
||||
const width = ATTACK_BAR_WIDTH * GUI_SCALE;
|
||||
const bar_x = Math.round((canvas.width - width) / 2);
|
||||
const bar_y = y + size + ATTACK_BAR_GAP * GUI_SCALE;
|
||||
draw_rect(bar_x, bar_y, width, ATTACK_BAR_HEIGHT * GUI_SCALE, ATTACK_BAR_BACK);
|
||||
draw_rect(bar_x, bar_y, Math.round(width * strength), ATTACK_BAR_HEIGHT * GUI_SCALE, ATTACK_BAR_FILL);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -78,6 +78,9 @@ export class ClientPacketListener {
|
||||
client.player.food = message.food;
|
||||
client.player.saturation = message.saturation;
|
||||
break;
|
||||
case "attack_speed":
|
||||
client.player.attack_speed = message.value;
|
||||
break;
|
||||
case "player_leave":
|
||||
level.remove_entity(message.id);
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user