WIP: Player Saving

This commit is contained in:
Holly Stubbs 2023-11-09 17:04:11 +00:00
parent e303cbc5df
commit 5a250cb601
5 changed files with 59 additions and 6 deletions

View File

@ -1,3 +1,4 @@
import { IReader, IWriter } from "bufferstuff";
import AABB from "../AABB";
import { Chunk } from "../Chunk";
import { MetadataEntry, MetadataWriter } from "../MetadataWriter";
@ -37,8 +38,6 @@ export class Entity implements IEntity {
public absRotation:Rotation;
public lastAbsRotation:Rotation;
public velocity:Vec3;
public health:number;
public wasHurt:boolean;
public isDead:boolean;
@ -85,8 +84,6 @@ export class Entity implements IEntity {
this.absRotation = new Rotation();
this.lastAbsRotation = new Rotation();
this.velocity = new Vec3();
this.crouching = this.lastCrouchState = this.lastFireState = this.queuedChunkUpdate = false;
this.chunk = world.getChunk(this.position.x >> 4, this.position.z >> 4);
@ -96,6 +93,24 @@ export class Entity implements IEntity {
this.isDead = false;
}
public fromSave(reader:IReader) {
this.position.set(reader.readDouble(), reader.readDouble(), reader.readDouble());
this.motion.set(reader.readFloat(), reader.readFloat(), reader.readFloat());
this.rotation.set(reader.readFloat(), reader.readFloat());
this.fire = reader.readShort();
this.fallDistance = reader.readFloat();
this.health = reader.readByte();
}
public toSave(writer:IWriter) {
writer.writeDouble(this.position.x).writeDouble(this.position.y).writeDouble(this.position.z) // Position
.writeDouble(this.motion.x).writeDouble(this.motion.y).writeDouble(this.motion.z) // Motion
.writeFloat(this.rotation.x).writeFloat(this.rotation.y) // Rotation
.writeShort(this.fire)
.writeFloat(this.fallDistance)
.writeByte(this.health);
}
sendToNearby(buffer:Buffer) {
this.world.sendToNearbyClients(this, buffer);
}

View File

@ -3,8 +3,8 @@ import Vec3 from "../Vec3"
export interface IEntity {
entityId:number,
position:Vec3,
motion:Vec3,
lastPosition:Vec3,
velocity:Vec3,
crouching:boolean,
updateMetadata:() => void,
distanceTo:(entity:IEntity) => number,

View File

@ -1,6 +1,9 @@
import { IReader, IWriter } from "bufferstuff";
import { ItemStack } from "./ItemStack";
export default interface IInventory {
fromSave:(reader:IReader) => void,
toSave:(writer:IWriter) => void,
getInventoryName:() => string,
getInventorySize:() => number,
getSlotItemStack:(slotId:number) => ItemStack | null

View File

@ -1,4 +1,4 @@
import { Endian, createWriter } from "bufferstuff";
import { Endian, IReader, IWriter, createWriter } from "bufferstuff";
import { ItemStack } from "./ItemStack";
import IInventory from "./IInventory";
@ -18,6 +18,25 @@ export class Inventory implements IInventory {
this.name = name;
}
public fromSave(reader:IReader) {
const inventorySize = reader.readByte();
for (let i = 0; i < inventorySize; i++) {
this.itemStacks[i] = ItemStack.FromSave(reader);
}
}
public toSave(writer:IWriter) {
writer.writeByte(this.size);
for (const itemStack of this.itemStacks) {
if (itemStack === null) {
writer.writeShort(-1);
continue;
}
itemStack.toSave(writer);
}
}
addItemStack(itemStack:ItemStack) {
// Check bottom inventory row (hotbar) first.
/*let workingItemStack:ItemStack | null;

View File

@ -1,3 +1,4 @@
import { IReader, IWriter } from "bufferstuff";
import { Block } from "../blocks/Block";
import { IEntity } from "../entities/IEntity";
import { Player } from "../entities/Player";
@ -65,6 +66,21 @@ export class ItemStack {
this.itemStackId = ItemStack.ITEMSTACK_ID_ADDER++;
}
public static FromSave(reader:IReader) {
const itemId = reader.readShort();
if (itemId === -1) {
return null;
}
return new ItemStack(itemId, reader.readByte(), reader.readShort());
}
public toSave(writer:IWriter) {
writer.writeShort(this.itemID)
.writeByte(this.size)
.writeShort(this.damage);
}
public static Compare(itemStack1:ItemStack, itemStack2:ItemStack) {
return itemStack1.itemStackId === itemStack2.itemStackId;
}