Make players mobs too !
This commit is contained in:
+77
-15
@@ -1,7 +1,11 @@
|
||||
import { PLAYER_EYE_HEIGHT, PLAYER_HEIGHT, PLAYER_WIDTH } from "$/common/constants.ts";
|
||||
import { Container, Cursor, ItemData, ItemStack } from "$/common/inventory.ts";
|
||||
import { PlayerInfo, ScreenLayout } from "$/common/protocol.ts";
|
||||
import { type SavedVitals, Vitals } from "./vitals.ts";
|
||||
import { EXHAUSTION, type SavedVitals, Vitals } from "./vitals.ts";
|
||||
import { PlayerChunks } from "./player_chunks.ts";
|
||||
import { ATTRIBUTES, type AttributeType } from "./entity/attributes.ts";
|
||||
import type { DamageSource, EntityLevel } from "./entity/entity.ts";
|
||||
import { LivingEntity } from "./entity/living_entity.ts";
|
||||
|
||||
export const INVENTORY_SIZE = 9 * 4;
|
||||
export const CRAFTING_SIZE = 10;
|
||||
@@ -35,21 +39,22 @@ export interface SavedPlayer {
|
||||
slim?: boolean;
|
||||
}
|
||||
|
||||
export class ServerPlayer {
|
||||
// a player is a living entity, like minecraft's Player: they have health, can be hurt and pushed around by mobs and
|
||||
// other players, and have effects. their client moves them though, so they don't walk, fall or think here. they're
|
||||
// kept apart from the entity manager's entities, clients hear about players through their own messages
|
||||
export class ServerPlayer extends LivingEntity {
|
||||
readonly type = "bworld:player";
|
||||
readonly conn: number;
|
||||
readonly id = crypto.randomUUID();
|
||||
readonly name: string;
|
||||
|
||||
x = 0;
|
||||
y = 100;
|
||||
z = 0;
|
||||
yaw = 0;
|
||||
pitch = 0;
|
||||
override y = 100;
|
||||
override width = PLAYER_WIDTH;
|
||||
override height = PLAYER_HEIGHT;
|
||||
override eye_height = PLAYER_EYE_HEIGHT;
|
||||
|
||||
inventory = new Container(INVENTORY_SIZE);
|
||||
// hunger and saturation, health is the entity's
|
||||
vitals = new Vitals();
|
||||
// blocks fallen since last standing on something, for fall damage
|
||||
fall_distance = 0;
|
||||
// when the last move came, to tell walking from sprinting
|
||||
last_move_tick = 0;
|
||||
crafting = new Container(CRAFTING_SIZE);
|
||||
@@ -68,17 +73,73 @@ export class ServerPlayer {
|
||||
// the chunks their client has and still needs
|
||||
chunks: PlayerChunks;
|
||||
|
||||
constructor(conn: number, name: string, view_distance: number) {
|
||||
constructor(level: EntityLevel, conn: number, name: string, view_distance: number) {
|
||||
super(level);
|
||||
this.conn = conn;
|
||||
this.name = name;
|
||||
this.chunks = new PlayerChunks(view_distance);
|
||||
}
|
||||
|
||||
override default_attributes(): [AttributeType, number?][] {
|
||||
return [
|
||||
...super.default_attributes(),
|
||||
// a fist does half a heart, like minecraft
|
||||
[ATTRIBUTES.attack_damage, 1],
|
||||
[ATTRIBUTES.physical_shielding],
|
||||
[ATTRIBUTES.arcane_shielding],
|
||||
];
|
||||
}
|
||||
|
||||
// they're saved by name with the players, not with the world's entities
|
||||
override get persistent() {
|
||||
return false;
|
||||
}
|
||||
|
||||
// there's no air bar to show yet, so players don't drown
|
||||
override get can_breathe_underwater() {
|
||||
return true;
|
||||
}
|
||||
|
||||
// their client moves them, this only burns, falls out of the world and counts effects and hurt time down
|
||||
override tick() {
|
||||
this.base_tick();
|
||||
if (!this.dead) {
|
||||
this.tick_effects();
|
||||
}
|
||||
}
|
||||
|
||||
// everything that hurts a player goes through the game server first, which lets mods change or cancel it
|
||||
override hurt(amount: number, source: DamageSource): boolean {
|
||||
return this.level.hurt_player(this, amount, source);
|
||||
}
|
||||
|
||||
// hurts them for real, once mods have had their say
|
||||
apply_hurt(amount: number, source: DamageSource): boolean {
|
||||
return super.hurt(amount, source);
|
||||
}
|
||||
|
||||
override on_hurt(_source: DamageSource) {
|
||||
this.vitals.add_exhaustion(EXHAUSTION.damage);
|
||||
}
|
||||
|
||||
// they don't drop loot or fall over, the game server drops what they carry and respawns them
|
||||
override die(source: DamageSource) {
|
||||
this.level.on_death(this, source);
|
||||
}
|
||||
|
||||
// knockback is how fast their client should move them, sent with the next sync and cleared
|
||||
take_motion(): [number, number, number] | undefined {
|
||||
if (!this.vx && !this.vy && !this.vz) return undefined;
|
||||
const motion: [number, number, number] = [this.vx, this.vy, this.vz];
|
||||
this.vx = this.vy = this.vz = 0;
|
||||
return motion;
|
||||
}
|
||||
|
||||
get held_item(): ItemStack | undefined {
|
||||
return this.inventory.get_item(this.selected_slot);
|
||||
}
|
||||
|
||||
info(): PlayerInfo {
|
||||
player_info(): PlayerInfo {
|
||||
return {
|
||||
id: this.id,
|
||||
name: this.name,
|
||||
@@ -92,7 +153,7 @@ export class ServerPlayer {
|
||||
};
|
||||
}
|
||||
|
||||
save(): SavedPlayer {
|
||||
save_player(): SavedPlayer {
|
||||
return {
|
||||
x: this.x,
|
||||
y: this.y,
|
||||
@@ -101,13 +162,13 @@ export class ServerPlayer {
|
||||
pitch: this.pitch,
|
||||
selected_slot: this.selected_slot,
|
||||
inventory: this.inventory.to_data(),
|
||||
vitals: this.vitals.save(),
|
||||
vitals: { health: this.health, ...this.vitals.save() },
|
||||
skin: this.skin,
|
||||
slim: this.slim || undefined,
|
||||
};
|
||||
}
|
||||
|
||||
load(saved: SavedPlayer) {
|
||||
load_player(saved: SavedPlayer) {
|
||||
this.x = saved.x;
|
||||
this.y = saved.y;
|
||||
this.z = saved.z;
|
||||
@@ -116,6 +177,7 @@ export class ServerPlayer {
|
||||
this.selected_slot = saved.selected_slot;
|
||||
this.inventory.load(saved.inventory);
|
||||
this.vitals.load(saved.vitals);
|
||||
this.health = saved.vitals?.health ?? this.max_health;
|
||||
this.skin = saved.skin;
|
||||
this.slim = saved.slim ?? false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user