Player api
This commit is contained in:
+15
-9
@@ -29,10 +29,6 @@ import { EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
import type { EntityJson } from "$/common/mod_data.ts";
|
||||
import { tick_texture_animations } from "./texture_animations.ts";
|
||||
|
||||
// how far from the eyes entities can be hit, like minecraft
|
||||
const ENTITY_REACH = 3;
|
||||
// how far from the eyes blocks can be broken and used
|
||||
const BLOCK_REACH = 6;
|
||||
// holding the use button uses the held item on blocks again this often, like minecraft's 4 ticks
|
||||
const USE_REPEAT_MS = 200;
|
||||
|
||||
@@ -66,6 +62,8 @@ export class Client {
|
||||
#attacking = false;
|
||||
// when holding the use button on a block uses it again, see USE_REPEAT_MS
|
||||
#next_use = 0;
|
||||
// a use was sent and the use button hasn't been let go of since, see #handle_block_interaction
|
||||
#use_held = false;
|
||||
#stopped = false;
|
||||
// the render distance the server was last told, it sends chunks for it
|
||||
#sent_view_distance: number;
|
||||
@@ -293,7 +291,7 @@ export class Client {
|
||||
player.z,
|
||||
player.yaw,
|
||||
player.pitch,
|
||||
ENTITY_REACH,
|
||||
player.entity_interaction_range,
|
||||
(entity) =>
|
||||
entity instanceof RemotePlayer ||
|
||||
(entity instanceof RemoteEntity && !entity.dead && is_hittable(entity)),
|
||||
@@ -305,7 +303,7 @@ export class Client {
|
||||
player.z,
|
||||
player.yaw,
|
||||
player.pitch,
|
||||
picked?.distance ?? BLOCK_REACH,
|
||||
picked?.distance ?? player.block_interaction_range,
|
||||
this.game_mode.held_item()?.targets_liquids,
|
||||
);
|
||||
this.entity_hit = picked && !this.hit_result ? picked.entity : undefined;
|
||||
@@ -320,16 +318,24 @@ export class Client {
|
||||
if (!attacking || !hit) {
|
||||
this.game_mode.stop_destroy_block();
|
||||
}
|
||||
// letting go stops using an item over time, like shooting a bow, even with a screen open by then. the server
|
||||
// may not have said it's using one yet, so it's sent after every use
|
||||
if (this.#use_held && !InputManager.is_mouse_down(2)) {
|
||||
this.#use_held = false;
|
||||
this.connection.send({ type: "release_use" });
|
||||
}
|
||||
if (this.screen) {
|
||||
return;
|
||||
}
|
||||
|
||||
// a click uses right away, holding it on blocks keeps using every USE_REPEAT_MS
|
||||
// a click uses right away, holding it on blocks keeps using every USE_REPEAT_MS. not while it's using an item
|
||||
const now = performance.now();
|
||||
const use_pressed = InputManager.is_mouse_pressed(2);
|
||||
const using = use_pressed || (hit && InputManager.is_mouse_down(2) && now >= this.#next_use);
|
||||
const use_pressed = InputManager.is_mouse_pressed(2) && !this.player.using_item;
|
||||
const using = use_pressed ||
|
||||
(hit && InputManager.is_mouse_down(2) && now >= this.#next_use && !this.player.using_item);
|
||||
if (using) {
|
||||
this.#next_use = now + USE_REPEAT_MS;
|
||||
this.#use_held = true;
|
||||
}
|
||||
|
||||
// the held item swings on every click and use, and keeps swinging while breaking, like minecraft
|
||||
|
||||
@@ -38,6 +38,8 @@ const SPRINT_FOV = (1 + SPRINT_SPEED) / 2;
|
||||
const FOV_EASE = 0.5;
|
||||
// like minecraft: sneaking is 30% of walking speed and lowers the eyes 0.35, which ease there by half each tick
|
||||
const SNEAK_SPEED = 0.3;
|
||||
// using an item over time, like drawing a bow, slows walking to a fifth and stops sprinting, like minecraft
|
||||
const USING_SPEED = 0.2;
|
||||
const SNEAK_EYE_HEIGHT = PLAYER_EYE_HEIGHT - 0.35;
|
||||
const EYE_EASE = 0.5;
|
||||
// sneaking keeps it from walking off anything taller than this
|
||||
@@ -102,6 +104,11 @@ export class LocalPlayer extends Player {
|
||||
// the attack speed attribute the server sent, and when they last attacked or switched slots, for the indicator
|
||||
attack_speed = 4;
|
||||
last_attack = -Infinity;
|
||||
// how far it reaches blocks and entities, the attributes the server sent
|
||||
block_interaction_range = 6;
|
||||
entity_interaction_range = 3;
|
||||
// using its held item over time, as the server says, see ctx's player.start_using
|
||||
using_item = false;
|
||||
|
||||
// blocks per second, peaks about 1.25 blocks up with the entity's gravity and drag
|
||||
jump_force = 9.23;
|
||||
@@ -111,7 +118,10 @@ export class LocalPlayer extends Player {
|
||||
#ticks_since_sent = 0;
|
||||
// counts down every tick, letting go of jump clears it
|
||||
#jump_delay = 0;
|
||||
// the sneaking pose, which flying doesn't have
|
||||
sneaking = false;
|
||||
// holding the sneak key, flying or not, which is what the server is told for placing and using things
|
||||
holding_sneak = false;
|
||||
// stays on after letting go of the key, see #update_sprinting
|
||||
sprinting = false;
|
||||
// what the server says, see /fly
|
||||
@@ -139,6 +149,7 @@ export class LocalPlayer extends Player {
|
||||
// flying ignores water
|
||||
this.#in_water = !this.flying && this.in_water;
|
||||
const was_swimming = this.swimming;
|
||||
const was_holding_sneak = this.holding_sneak;
|
||||
const input = this.client.screen ? NO_INPUT : this.#read_input();
|
||||
this.#apply_input(input);
|
||||
// after sprinting is, so letting go stands it up the same tick
|
||||
@@ -185,7 +196,10 @@ export class LocalPlayer extends Player {
|
||||
if (Math.abs(this.vx) < MIN_SPEED) this.vx = 0;
|
||||
if (Math.abs(this.vz) < MIN_SPEED) this.vz = 0;
|
||||
this.update_swim_animation();
|
||||
this.#send_position((this.on_ground && !was_on_ground) || this.swimming !== was_swimming);
|
||||
this.#send_position(
|
||||
(this.on_ground && !was_on_ground) || this.swimming !== was_swimming ||
|
||||
this.holding_sneak !== was_holding_sneak,
|
||||
);
|
||||
}
|
||||
|
||||
// like voxelibre: sprinting with the eyes under water starts it, and it keeps going while it sprints in water, along
|
||||
@@ -277,12 +291,14 @@ export class LocalPlayer extends Player {
|
||||
let { x: input_x, z: input_z } = input;
|
||||
const size = Math.hypot(input_x, input_z);
|
||||
if (size > 0) {
|
||||
input_x /= size;
|
||||
input_z /= size;
|
||||
const scale = (this.using_item ? USING_SPEED : 1) / size;
|
||||
input_x *= scale;
|
||||
input_z *= scale;
|
||||
}
|
||||
|
||||
// sneaking while flying goes down instead
|
||||
this.sneaking = input.sneak && !this.flying;
|
||||
this.holding_sneak = input.sneak;
|
||||
const sin = Math.sin(this.yaw);
|
||||
const cos = Math.cos(this.yaw);
|
||||
this.#update_sprinting(input, sin, cos);
|
||||
@@ -325,7 +341,8 @@ export class LocalPlayer extends Player {
|
||||
// until it stops moving forward, sneaks, gets hungry or runs into a wall, even after letting go of the key
|
||||
#update_sprinting(input: MoveInput, sin: number, cos: number) {
|
||||
// like minecraft, flying sprints even hungry, and sneaking to go down doesn't stop it
|
||||
const can_sprint = input.z < 0 && (this.flying || (!input.sneak && this.food > SPRINT_MIN_FOOD));
|
||||
const can_sprint = input.z < 0 && !this.using_item &&
|
||||
(this.flying || (!input.sneak && this.food > SPRINT_MIN_FOOD));
|
||||
// how far it's looking into what it hit in the last move, the way it's facing is -sin, -cos
|
||||
const into_wall = sin * this.colliding_x + cos * this.colliding_z;
|
||||
if (!can_sprint || into_wall > SPRINT_WALL_ANGLE) {
|
||||
@@ -357,6 +374,7 @@ export class LocalPlayer extends Player {
|
||||
pitch: this.pitch,
|
||||
teleport: this.teleport_id,
|
||||
...(this.swimming && { swimming: true }),
|
||||
...(this.holding_sneak && { sneaking: true }),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
+9
-3
@@ -88,8 +88,13 @@ export class MultiPlayerGameMode {
|
||||
const held = this.held_item();
|
||||
const value = level.get_block_value(hit.x, hit.y, hit.z);
|
||||
const used = (held?.targets_liquids && liquid_block(value, block_info)) || block_info;
|
||||
const interaction = { face: hit.face, hit: hit.hit };
|
||||
if (block_rules.interact(used, hit.x, hit.y, hit.z, interaction, this.client.player.yaw)) return;
|
||||
// sneaking with an item skips the block, like on the server
|
||||
const sneaking = this.client.player.holding_sneak;
|
||||
const skips_block = sneaking && held !== undefined;
|
||||
const interaction = { face: hit.face, hit: hit.hit, sneaking };
|
||||
if (!skips_block && block_rules.interact(used, hit.x, hit.y, hit.z, interaction, this.client.player.yaw)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// guess that it places the held block, unless the block does something when used. the same rules as the server:
|
||||
// into the clicked block when it's replaceable, and only where place_in and place_on allow
|
||||
@@ -109,6 +114,7 @@ export class MultiPlayerGameMode {
|
||||
...target,
|
||||
face: hit.face,
|
||||
hit: hit.hit,
|
||||
sneaking,
|
||||
}),
|
||||
place_block,
|
||||
level.get_block_value(target.x, target.y, target.z),
|
||||
@@ -120,7 +126,7 @@ export class MultiPlayerGameMode {
|
||||
level.get_block_value(target.x, target.y, target.z),
|
||||
below_id,
|
||||
) && !(place_block?.has_collision && this.#occupied(target.x, target.y, target.z, place_block, place_value!));
|
||||
if (!block_info.interactive && place_id && placeable) {
|
||||
if ((!block_info.interactive || skips_block) && place_id && placeable) {
|
||||
level.add_block({ ...target, id: place_id }, place_value! >>> 16);
|
||||
predict_neighbors(target.x, target.y, target.z);
|
||||
const slot = this.#held_slot();
|
||||
|
||||
@@ -286,6 +286,7 @@ function client_context(listing: ModListing): ClientContext {
|
||||
return rules_world.season_at(x, z);
|
||||
},
|
||||
is_swimming: () => need_client().player.swimming,
|
||||
is_sneaking: () => need_client().player.holding_sneak,
|
||||
},
|
||||
world: {
|
||||
get_block(x, y, z) {
|
||||
|
||||
@@ -84,8 +84,13 @@ export class ClientPacketListener {
|
||||
client.player.saturation = message.saturation;
|
||||
client.player.air = message.air;
|
||||
break;
|
||||
case "attack_speed":
|
||||
client.player.attack_speed = message.value;
|
||||
case "attributes":
|
||||
client.player.attack_speed = message.attack_speed;
|
||||
client.player.block_interaction_range = message.block_interaction_range;
|
||||
client.player.entity_interaction_range = message.entity_interaction_range;
|
||||
break;
|
||||
case "using_item":
|
||||
client.player.using_item = message.using;
|
||||
break;
|
||||
case "flying":
|
||||
client.player.flying = message.flying;
|
||||
|
||||
Reference in New Issue
Block a user