Undergarden mod !
This commit is contained in:
@@ -36,6 +36,10 @@ export abstract class Entity {
|
||||
step_grace_ticks = 0;
|
||||
// ticks since it was last on the ground
|
||||
#air_ticks = 0;
|
||||
// how far the limbs have swung and how hard they swing (0 to 1), like minecraft's WalkAnimationState
|
||||
walk_position = 0;
|
||||
walk_speed = 0;
|
||||
prev_walk_speed = 0;
|
||||
|
||||
// which way it hit something on each axis in the last move: 1 or -1, 0 for nothing.
|
||||
// 1 on y means it's standing on something
|
||||
@@ -55,6 +59,21 @@ export abstract class Entity {
|
||||
return this.colliding_y === 1;
|
||||
}
|
||||
|
||||
// after moving each tick. full speed is 5 blocks per second, a bit faster than walking
|
||||
update_walk_animation() {
|
||||
const moved = Math.hypot(this.x - this.prev_x, this.z - this.prev_z);
|
||||
this.prev_walk_speed = this.walk_speed;
|
||||
this.walk_speed += (Math.min(moved * 4, 1) - this.walk_speed) * 0.4;
|
||||
this.walk_position += this.walk_speed;
|
||||
}
|
||||
|
||||
walk_animation(partial_tick: number) {
|
||||
return {
|
||||
position: this.walk_position - this.walk_speed * (1 - partial_tick),
|
||||
speed: this.prev_walk_speed + (this.walk_speed - this.prev_walk_speed) * partial_tick,
|
||||
};
|
||||
}
|
||||
|
||||
// jumps there, without interpolating from where it was
|
||||
set_position(x: number, y: number, z: number) {
|
||||
this.x = this.prev_x = x;
|
||||
|
||||
@@ -17,11 +17,6 @@ export abstract class Player extends Entity {
|
||||
slim = false;
|
||||
#skin_version = 0;
|
||||
|
||||
// how far the limbs have swung and how hard they swing (0 to 1), like minecraft's WalkAnimationState
|
||||
walk_position = 0;
|
||||
walk_speed = 0;
|
||||
prev_walk_speed = 0;
|
||||
|
||||
constructor(level: ClientLevel, id: string, name: string) {
|
||||
super(level, id, PLAYER_WIDTH, PLAYER_HEIGHT, PLAYER_EYE_HEIGHT);
|
||||
this.name = name;
|
||||
@@ -40,19 +35,4 @@ export abstract class Player extends Entity {
|
||||
this.skin = texture;
|
||||
this.slim = slim;
|
||||
}
|
||||
|
||||
// after moving each tick. full speed is 5 blocks per second, a bit faster than walking
|
||||
update_walk_animation() {
|
||||
const moved = Math.hypot(this.x - this.prev_x, this.z - this.prev_z);
|
||||
this.prev_walk_speed = this.walk_speed;
|
||||
this.walk_speed += (Math.min(moved * 4, 1) - this.walk_speed) * 0.4;
|
||||
this.walk_position += this.walk_speed;
|
||||
}
|
||||
|
||||
walk_animation(partial_tick: number) {
|
||||
return {
|
||||
position: this.walk_position - this.walk_speed * (1 - partial_tick),
|
||||
speed: this.prev_walk_speed + (this.walk_speed - this.prev_walk_speed) * partial_tick,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { EntityData, EntityInfo } from "$/common/protocol.ts";
|
||||
import type { EntityData, EntityEvent, EntityInfo } from "$/common/protocol.ts";
|
||||
import type { EntityJson } from "$/common/mod_data.ts";
|
||||
import { EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
import type { ClientLevel } from "../level/client_level.ts";
|
||||
@@ -9,6 +9,8 @@ const LERP_TICKS = 3;
|
||||
// how long it flashes red after being hurt, and falls over after dying, like minecraft
|
||||
const HURT_TICKS = 10;
|
||||
const DEATH_TICKS = 20;
|
||||
// how long its attack swing takes, like minecraft's mobs
|
||||
const SWING_TICKS = 6;
|
||||
|
||||
// an entity the server simulates, like a mob. this only follows where it's told it is and keeps what it was told
|
||||
// it looks like, for drawing it
|
||||
@@ -24,6 +26,8 @@ export class RemoteEntity extends Entity {
|
||||
dead = false;
|
||||
// what's riding it, by id
|
||||
passengers: string[] = [];
|
||||
// ticks into its attack swing, undefined when it isn't swinging
|
||||
swing_ticks: number | undefined;
|
||||
|
||||
#target = { x: 0, y: 0, z: 0, yaw: 0, pitch: 0, head_yaw: 0 };
|
||||
#lerp_ticks = 0;
|
||||
@@ -46,9 +50,12 @@ export class RemoteEntity extends Entity {
|
||||
this.#lerp_ticks = LERP_TICKS;
|
||||
}
|
||||
|
||||
on_event(event: "hurt" | "death") {
|
||||
on_event(event: EntityEvent) {
|
||||
if (event === "hurt") {
|
||||
this.hurt_time = HURT_TICKS;
|
||||
} else if (event === "swing") {
|
||||
// swinging again halfway through doesn't restart it, like minecraft
|
||||
if (this.swing_ticks === undefined || this.swing_ticks >= SWING_TICKS / 2) this.swing_ticks = 0;
|
||||
} else {
|
||||
this.dead = true;
|
||||
}
|
||||
@@ -69,6 +76,13 @@ export class RemoteEntity extends Entity {
|
||||
this.head_yaw += wrap_angle(target.head_yaw - this.head_yaw) * t;
|
||||
this.#lerp_ticks -= 1;
|
||||
}
|
||||
this.update_walk_animation();
|
||||
if (this.swing_ticks !== undefined && ++this.swing_ticks >= SWING_TICKS) this.swing_ticks = undefined;
|
||||
}
|
||||
|
||||
// how far through its attack swing it is, 0 to 1, like minecraft's attackTime. 0 when it isn't swinging
|
||||
attack_animation(partial_tick: number) {
|
||||
return this.swing_ticks === undefined ? 0 : Math.min(1, (this.swing_ticks + partial_tick) / SWING_TICKS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { PlayerInfo } from "$/common/protocol.ts";
|
||||
import type { EntityEvent, PlayerInfo } from "$/common/protocol.ts";
|
||||
import { TICK_DELTA } from "$/common/constants.ts";
|
||||
import type { ClientLevel } from "../level/client_level.ts";
|
||||
import { Player } from "./player.ts";
|
||||
@@ -38,7 +38,7 @@ export class RemotePlayer extends Player {
|
||||
}
|
||||
|
||||
// players respawn straight away, so dying doesn't show
|
||||
on_event(event: "hurt" | "death") {
|
||||
on_event(event: EntityEvent) {
|
||||
if (event === "hurt") {
|
||||
this.hurt_time = HURT_TICKS;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user