Make players mobs too !
This commit is contained in:
+7
-4
@@ -4,6 +4,7 @@ import { LocalPlayer } from "./entity/local_player.ts";
|
||||
import { RemotePlayer } from "./entity/remote_player.ts";
|
||||
import { ItemEntity } from "./entity/item_entity.ts";
|
||||
import { RemoteEntity } from "./entity/remote_entity.ts";
|
||||
import type { Entity } from "./entity/entity.ts";
|
||||
import { Camera } from "./camera.ts";
|
||||
import { Options } from "./options.ts";
|
||||
import { MultiPlayerGameMode } from "./game_mode.ts";
|
||||
@@ -46,8 +47,8 @@ export class Client {
|
||||
screens: GuiScreen[] = [];
|
||||
// what the player is looking at, updated every frame
|
||||
hit_result: BlockHitResult | undefined;
|
||||
// the entity being looked at, closer than any block. hit_result is undefined then
|
||||
entity_hit: RemoteEntity | undefined;
|
||||
// the entity or other player being looked at, closer than any block. hit_result is undefined then
|
||||
entity_hit: Entity | undefined;
|
||||
debugging = false;
|
||||
|
||||
// seconds of game time not ticked yet
|
||||
@@ -255,7 +256,9 @@ export class Client {
|
||||
player.yaw,
|
||||
player.pitch,
|
||||
ENTITY_REACH,
|
||||
(entity) => entity instanceof RemoteEntity && !entity.dead && is_hittable(entity),
|
||||
(entity) =>
|
||||
entity instanceof RemotePlayer ||
|
||||
(entity instanceof RemoteEntity && !entity.dead && is_hittable(entity)),
|
||||
);
|
||||
// a block in front of the entity hides it
|
||||
this.hit_result = this.level.pick(
|
||||
@@ -266,7 +269,7 @@ export class Client {
|
||||
player.pitch,
|
||||
picked?.distance ?? BLOCK_REACH,
|
||||
);
|
||||
this.entity_hit = picked && !this.hit_result ? picked.entity as RemoteEntity : undefined;
|
||||
this.entity_hit = picked && !this.hit_result ? picked.entity : undefined;
|
||||
}
|
||||
|
||||
// clicks happen right away, holding to break advances in tick()
|
||||
|
||||
@@ -4,6 +4,8 @@ import type { ClientLevel } from "../level/client_level.ts";
|
||||
import { Player } from "./player.ts";
|
||||
|
||||
const SMOOTHING = 12;
|
||||
// how long they flash red after being hurt, like minecraft
|
||||
const HURT_TICKS = 10;
|
||||
|
||||
// another player on the server, it moves where the server says instead of simulating anything
|
||||
export class RemotePlayer extends Player {
|
||||
@@ -11,6 +13,8 @@ export class RemotePlayer extends Player {
|
||||
target_x: number;
|
||||
target_y: number;
|
||||
target_z: number;
|
||||
// counting down after a hurt event
|
||||
hurt_time = 0;
|
||||
|
||||
constructor(level: ClientLevel, info: PlayerInfo) {
|
||||
super(level, info.id, info.name);
|
||||
@@ -33,7 +37,15 @@ export class RemotePlayer extends Player {
|
||||
this.pitch = pitch;
|
||||
}
|
||||
|
||||
// players respawn straight away, so dying doesn't show
|
||||
on_event(event: "hurt" | "death") {
|
||||
if (event === "hurt") {
|
||||
this.hurt_time = HURT_TICKS;
|
||||
}
|
||||
}
|
||||
|
||||
tick() {
|
||||
if (this.hurt_time > 0) this.hurt_time -= 1;
|
||||
const t = Math.min(1, TICK_DELTA * SMOOTHING);
|
||||
this.x += (this.target_x - this.x) * t;
|
||||
this.y += (this.target_y - this.y) * t;
|
||||
|
||||
+3
-2
@@ -8,6 +8,7 @@ import { box_overlaps_block, can_place, placement_target } from "$/common/utils.
|
||||
import type { EntityJson } from "$/common/mod_data.ts";
|
||||
import { RemoteEntity } from "./entity/remote_entity.ts";
|
||||
import { Player } from "./entity/player.ts";
|
||||
import type { Entity } from "./entity/entity.ts";
|
||||
|
||||
// breaking and using blocks against a server, like minecraft's MultiPlayerGameMode. the client times breaking
|
||||
// and guesses the results so they feel instant, the server decides what really happens
|
||||
@@ -95,8 +96,8 @@ export class MultiPlayerGameMode {
|
||||
container.set_item(index, item.amount > 0 ? item : undefined);
|
||||
}
|
||||
|
||||
// left click on an entity, the server decides whether it's hurt
|
||||
attack(entity: RemoteEntity) {
|
||||
// left click on an entity or another player, the server decides whether it's hurt
|
||||
attack(entity: Entity) {
|
||||
this.client.player.send_position_now();
|
||||
this.client.connection.send({ type: "attack_entity", id: entity.id });
|
||||
}
|
||||
|
||||
@@ -119,7 +119,7 @@ export class ClientPacketListener {
|
||||
}
|
||||
case "entity_event": {
|
||||
const entity = level.entities.get(message.id);
|
||||
if (entity instanceof RemoteEntity) {
|
||||
if (entity instanceof RemoteEntity || entity instanceof RemotePlayer) {
|
||||
entity.on_event(message.event);
|
||||
}
|
||||
break;
|
||||
@@ -224,6 +224,11 @@ export class ClientPacketListener {
|
||||
case "teleport":
|
||||
client.player.set_position(message.x, message.y, message.z);
|
||||
break;
|
||||
case "set_motion":
|
||||
client.player.vx = message.vx;
|
||||
client.player.vy = message.vy;
|
||||
client.player.vz = message.vz;
|
||||
break;
|
||||
case "close_screen":
|
||||
// closed by the server (the block broke), it already put everything back
|
||||
client.screens = client.screens.filter((s) => !(s instanceof GuiContainer));
|
||||
|
||||
@@ -9,12 +9,10 @@ import { get_sprite_region } from "$/client/sprites.ts";
|
||||
import type { SpriteRegion } from "$/common/constants.ts";
|
||||
import { EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
import type { EntityBoneJson, EntityJson, EntityModelJson } from "$/common/mod_data.ts";
|
||||
import { push_box, type Vec3 } from "./player_renderer.ts";
|
||||
import { HURT_TINT, push_box, type Vec3 } from "./player_renderer.ts";
|
||||
|
||||
// models are in texture pixels, 16 to a block
|
||||
const PIXEL = 1 / 16;
|
||||
// how much of each color is left while it flashes red after being hurt, like minecraft's
|
||||
const HURT_TINT: Vec3 = [1, 0.6, 0.6];
|
||||
|
||||
// the atlas has to be the current texture
|
||||
export function render_mod_entity(entity: RemoteEntity, camera: Camera, partial_tick: number) {
|
||||
|
||||
@@ -7,7 +7,7 @@ import type { Entity } from "$/client/entity/entity.ts";
|
||||
import { RemotePlayer } from "$/client/entity/remote_player.ts";
|
||||
import { ItemEntity } from "$/client/entity/item_entity.ts";
|
||||
import { render_block_items, render_item_entity } from "./item_renderer.ts";
|
||||
import { render_player } from "./player_renderer.ts";
|
||||
import { HURT_TINT, render_player } from "./player_renderer.ts";
|
||||
import { render_mod_entity } from "./entity_renderer.ts";
|
||||
import { RemoteEntity } from "$/client/entity/remote_entity.ts";
|
||||
import {
|
||||
@@ -104,7 +104,7 @@ export class LevelRenderer {
|
||||
flush_batch();
|
||||
set_current_texture(skin.tex);
|
||||
}
|
||||
render_player(entity, skin, partial_tick);
|
||||
render_player(entity, skin, partial_tick, entity.hurt_time > 0 ? HURT_TINT : undefined);
|
||||
}
|
||||
}
|
||||
flush_batch();
|
||||
|
||||
@@ -4,6 +4,9 @@ import { push_vertex, type Texture } from "$/client/renderer/mod.ts";
|
||||
|
||||
export type Vec3 = [number, number, number];
|
||||
|
||||
// how much of each color is left while something flashes red after being hurt, like minecraft's
|
||||
export const HURT_TINT: Vec3 = [1, 0.6, 0.6];
|
||||
|
||||
// minecraft's player model is 32 skin pixels tall, it's scaled to the player's height
|
||||
const PIXEL = PLAYER_HEIGHT / 32;
|
||||
// keeps nearest sampling from picking the texel next to a face's edge
|
||||
@@ -144,8 +147,9 @@ const FACES: { rect: (w: number, h: number, d: number) => number[]; corners: num
|
||||
},
|
||||
];
|
||||
|
||||
// a player with a minecraft skin, like minecraft's PlayerRenderer. the skin has to be the current texture
|
||||
export function render_player(player: Player, skin: Texture, partial_tick: number) {
|
||||
// a player with a minecraft skin, like minecraft's PlayerRenderer. the skin has to be the current texture. tint
|
||||
// multiplies its colors, for flashing red when hurt
|
||||
export function render_player(player: Player, skin: Texture, partial_tick: number, tint?: Vec3) {
|
||||
const { x, y, z } = player.render_position(partial_tick);
|
||||
const yaw_sin = Math.sin(player.yaw);
|
||||
const yaw_cos = Math.cos(player.yaw);
|
||||
@@ -173,8 +177,8 @@ export function render_player(player: Player, skin: Texture, partial_tick: numbe
|
||||
const mz = (py * sin + pz * cos + pivot_z) * PIXEL;
|
||||
return [x + mx * yaw_cos + mz * yaw_sin, y + my, z - mx * yaw_sin + mz * yaw_cos];
|
||||
};
|
||||
push_cube(skin, part, part.texture, 0, transform);
|
||||
push_cube(skin, part, part.overlay, part.inflate, transform);
|
||||
push_cube(skin, part, part.texture, 0, transform, tint);
|
||||
push_cube(skin, part, part.overlay, part.inflate, transform, tint);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -184,10 +188,11 @@ function push_cube(
|
||||
uv: [number, number],
|
||||
inflate: number,
|
||||
transform: (x: number, y: number, z: number) => Vec3,
|
||||
tint?: Vec3,
|
||||
) {
|
||||
const low = part.from.map((n) => n - inflate) as Vec3;
|
||||
const high = part.from.map((n, i) => n + part.size[i] + inflate) as Vec3;
|
||||
push_box(part.size, low, high, uv, (u, v) => [u / skin.width, v / skin.height], transform);
|
||||
push_box(part.size, low, high, uv, (u, v) => [u / skin.width, v / skin.height], transform, tint);
|
||||
}
|
||||
|
||||
// a box of a model, from low to high, textured like minecraft's box uv from [u, v] for a box of size (in texture
|
||||
|
||||
Reference in New Issue
Block a user