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;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// entities mods define (entities/<name>.json), drawn with their entity model and a texture from the atlas. there are no
|
||||
// animations yet: the model turns to the entity's yaw, and a bone named "head" to where it looks. entities without a
|
||||
// model are their texture facing the camera, like minecraft's fishing hook
|
||||
// animation files yet: the model turns to the entity's yaw, a bone named "head" to where it looks, and bones named
|
||||
// like minecraft's humanoid ones (right_arm, left_arm, right_leg, left_leg) swing as it walks, the right arm when it
|
||||
// attacks too. entities without a model are their texture facing the camera, like minecraft's fishing hook
|
||||
import { AssetManager } from "$/client/assets.ts";
|
||||
import type { Camera } from "$/client/camera.ts";
|
||||
import type { RemoteEntity } from "$/client/entity/remote_entity.ts";
|
||||
@@ -39,6 +40,7 @@ export function render_mod_entity(entity: RemoteEntity, camera: Camera, partial_
|
||||
const yaw_sin = Math.sin(entity.yaw);
|
||||
const yaw_cos = Math.cos(entity.yaw);
|
||||
|
||||
const limbs = limb_angles(entity, partial_tick);
|
||||
const bones = new Map<string, Matrix>();
|
||||
for (const bone of model.bones) {
|
||||
let matrix = bone_matrix(bone);
|
||||
@@ -46,6 +48,8 @@ export function render_mod_entity(entity: RemoteEntity, camera: Camera, partial_
|
||||
const look = rotation_around(model_pivot(bone), entity.pitch, entity.head_yaw - entity.yaw, 0);
|
||||
matrix = multiply(look, matrix);
|
||||
}
|
||||
const limb = limbs[bone.name];
|
||||
if (limb) matrix = multiply(rotation_around(model_pivot(bone), limb, 0, 0), matrix);
|
||||
if (bone.parent) matrix = multiply(bones.get(bone.parent) ?? IDENTITY, matrix);
|
||||
bones.set(bone.name, matrix);
|
||||
|
||||
@@ -65,6 +69,22 @@ export function render_mod_entity(entity: RemoteEntity, camera: Camera, partial_
|
||||
}
|
||||
}
|
||||
|
||||
// how far each humanoid limb turns forward (radians), like minecraft's HumanoidModel.setupAnim: arms and legs swing
|
||||
// opposite each other as it walks, and the right arm comes down in an attack like undergarden's rotwalker's
|
||||
function limb_angles(entity: RemoteEntity, partial_tick: number): Record<string, number> {
|
||||
const walk = entity.walk_animation(partial_tick);
|
||||
const swing = Math.cos(walk.position * 0.6662) * 1.4 * walk.speed;
|
||||
const attack = entity.attack_animation(partial_tick);
|
||||
// ease out quart, so it comes down fast
|
||||
const eased = 1 - (1 - attack) ** 4;
|
||||
return {
|
||||
right_arm: -swing + Math.sin(eased * Math.PI) * 1.2,
|
||||
left_arm: swing,
|
||||
right_leg: swing,
|
||||
left_leg: -swing,
|
||||
};
|
||||
}
|
||||
|
||||
// the whole texture, 16 pixels to a block, facing the camera around the middle of the entity's box
|
||||
function render_sprite(
|
||||
entity: RemoteEntity,
|
||||
|
||||
Reference in New Issue
Block a user