Skip to content

Commit 19a131b

Browse files
committed
implement player spawning, movement, animation, metadata and destroying, implement NBT serialization, version 1.1.7
1 parent 4ac61ad commit 19a131b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1476
-133
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Click [here](https://labystudio.de/page/minecraft/) for a demo!
3535
- World
3636
- 16x16x16 Chunks
3737
- Block type, data, sky & block lightning
38+
- Entities
3839
- Minecraft Alpha Generator
3940
- 64 bits seed
4041
- Perlin terrain generation
@@ -73,12 +74,14 @@ Click [here](https://labystudio.de/page/minecraft/) for a demo!
7374
- Hot-Bar
7475
- Chat
7576
- Debug
77+
- Player list
7678
- Multiplayer
7779
- Networking
7880
- RSA Encryption
7981
- AES Encryption
8082
- Compression
8183
- Splitting
84+
- NBT Serialization
8285
- Sub-Protocols
8386
- Handshake
8487
- Status
@@ -89,6 +92,7 @@ Click [here](https://labystudio.de/page/minecraft/) for a demo!
8992
- Movement Packets
9093
- Block Update Packets
9194
- Chat Packets
95+
- Player Packets
9296
- Commands
9397
- /help
9498
- /time

src/js/net/minecraft/client/Minecraft.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import PlayerControllerMultiplayer from "./network/controller/PlayerControllerMu
2626

2727
export default class Minecraft {
2828

29-
static VERSION = "1.1.6"
29+
static VERSION = "1.1.7"
3030
static URL_GITHUB = "https://github.com/labystudio/js-minecraft";
3131
static PROTOCOL_VERSION = 47; //758;
3232

@@ -124,6 +124,7 @@ export default class Minecraft {
124124

125125
if (this.world !== null) {
126126
this.world.getChunkProvider().getChunks().clear();
127+
this.world.clearEntities();
127128
this.world = null;
128129
this.player = null;
129130
this.loadingScreen = null;
@@ -135,6 +136,14 @@ export default class Minecraft {
135136
this.loadingScreen.setTitle("Building terrain...");
136137
this.displayScreen(this.loadingScreen);
137138

139+
// Clear previous world
140+
if (this.world !== null) {
141+
this.world.getChunkProvider().getChunks().clear();
142+
this.world.clearEntities();
143+
this.worldRenderer.reset();
144+
this.itemRenderer.reset();
145+
}
146+
138147
// Create world
139148
this.world = world;
140149
this.worldRenderer.scene.add(this.world.group);
@@ -277,9 +286,6 @@ export default class Minecraft {
277286
// Tick renderer
278287
this.worldRenderer.onTick();
279288

280-
// Tick the player
281-
this.player.onUpdate();
282-
283289
// Tick particle renderer
284290
this.particleRenderer.onTick();
285291
}

src/js/net/minecraft/client/entity/Entity.js

+66-12
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import Random from "../../util/Random.js";
44

55
export default class Entity {
66

7-
constructor(minecraft, world) {
7+
constructor(minecraft, world, id) {
88
this.minecraft = minecraft;
99
this.world = world;
10+
this.id = id;
11+
1012
this.random = new Random();
1113
this.renderer = null;
1214

@@ -24,7 +26,6 @@ export default class Entity {
2426
this.stepHeight = 0.0;
2527

2628
this.onGround = false;
27-
this.sneaking = false;
2829

2930
this.rotationYaw = 0;
3031
this.rotationPitch = 0;
@@ -43,6 +44,12 @@ export default class Entity {
4344
this.ticksExisted = 0;
4445
this.isDead = false;
4546

47+
this.serverPositionX = 0;
48+
this.serverPositionY = 0;
49+
this.serverPositionZ = 0;
50+
51+
this.metaData = {};
52+
4653
this.boundingBox = new BoundingBox();
4754
this.setPosition(this.x, this.y, this.z);
4855
}
@@ -70,19 +77,36 @@ export default class Entity {
7077
y + this.height,
7178
z + width
7279
);
80+
}
7381

74-
this.motionX = 0;
75-
this.motionY = 0;
76-
this.motionZ = 0;
82+
setRotation(yaw, pitch) {
83+
this.rotationYaw = yaw % 360;
84+
this.rotationPitch = pitch % 360;
85+
}
7786

78-
this.prevX = this.x;
79-
this.prevY = this.y;
80-
this.prevZ = this.z;
87+
setTargetPositionAndRotation(x, y, z, yaw, pitch, increments) {
88+
this.setPosition(x, y, z);
89+
this.setRotation(yaw, pitch);
8190
}
8291

83-
setRotation(yaw, pitch) {
84-
this.rotationYaw = yaw;
85-
this.rotationPitch = pitch;
92+
setPositionAndRotation(x, y, z, yaw, pitch) {
93+
this.prevX = this.x = x;
94+
this.prevY = this.y = y;
95+
this.prevZ = this.z = z;
96+
97+
this.prevRotationYaw = this.rotationYaw = yaw;
98+
this.prevRotationPitch = this.rotationPitch = pitch;
99+
100+
let diffYaw = (this.prevRotationYaw - yaw);
101+
if (diffYaw < -180) {
102+
this.prevRotationYaw += 360;
103+
}
104+
if (diffYaw >= 180) {
105+
this.prevRotationYaw -= 360;
106+
}
107+
108+
this.setPosition(this.x, this.y, this.z);
109+
this.setRotation(yaw, pitch);
86110
}
87111

88112
onUpdate() {
@@ -119,7 +143,7 @@ export default class Entity {
119143
let originalTargetY = targetY;
120144
let originalTargetZ = targetZ;
121145

122-
if (this.onGround && this.sneaking) {
146+
if (this.onGround && this.isSneaking()) {
123147
for (; targetX !== 0.0 && this.world.getCollisionBoxes(this.boundingBox.offset(targetX, -this.stepHeight, 0.0)).length === 0; originalTargetX = targetX) {
124148
if (targetX < 0.05 && targetX >= -0.05) {
125149
targetX = 0.0;
@@ -214,4 +238,34 @@ export default class Entity {
214238
|| this.rotationPitch !== this.prevRotationPitch;
215239
}
216240

241+
isSneaking() {
242+
return this.getFlag(1);
243+
}
244+
245+
setSneaking(sneaking) {
246+
this.setFlag(1, sneaking);
247+
}
248+
249+
updateMetaData(metaData) {
250+
for (const [id, value] of Object.entries(metaData)) {
251+
this.metaData[value.id] = value;
252+
}
253+
}
254+
255+
getFlag(flag) {
256+
return typeof this.metaData[0] !== "undefined" && (this.metaData[0].value & 1 << flag) !== 0;
257+
}
258+
259+
setFlag(flag, value) {
260+
if (typeof this.metaData[0] === "undefined") {
261+
this.metaData[0] = {id: 0, type: 0, value: 0};
262+
}
263+
264+
if (value) {
265+
this.metaData[0].value |= 1 << flag;
266+
} else {
267+
this.metaData[0].value &= ~(1 << flag);
268+
}
269+
}
270+
217271
}

src/js/net/minecraft/client/entity/EntityLiving.js

+38-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import MathHelper from "../../util/MathHelper.js";
33

44
export default class EntityLiving extends Entity {
55

6-
constructor(minecraft, world) {
7-
super(minecraft, world);
6+
constructor(minecraft, world, id) {
7+
super(minecraft, world, id);
88

99
this.jumpTicks = 0;
1010

@@ -70,6 +70,28 @@ export default class EntityLiving extends Entity {
7070
--this.jumpTicks;
7171
}
7272

73+
if (this.rotationPositionIncrements > 0) {
74+
// Interpolate the position and rotation
75+
let x = this.x + (this.targetX - this.x) / this.rotationPositionIncrements;
76+
let y = this.y + (this.targetY - this.y) / this.rotationPositionIncrements;
77+
let z = this.z + (this.targetZ - this.z) / this.rotationPositionIncrements;
78+
79+
// Update yaw and pitch
80+
let yaw = MathHelper.wrapAngleTo180(this.targetYaw - this.rotationYaw);
81+
this.rotationYaw = this.rotationYaw + yaw / this.rotationPositionIncrements;
82+
this.rotationPitch = (this.rotationPitch + (this.targetPitch - this.rotationPitch) / this.rotationPositionIncrements);
83+
84+
// Decrement position increments
85+
this.rotationPositionIncrements--;
86+
87+
// Update position
88+
this.setPosition(x, y, z);
89+
this.setRotation(this.rotationYaw, this.rotationPitch);
90+
}
91+
92+
// TODO Find the right spot to update this
93+
this.rotationYawHead = this.rotationYaw;
94+
7395
// Stop if too slow
7496
if (Math.abs(this.motionX) < 0.003) {
7597
this.motionX = 0.0;
@@ -81,8 +103,6 @@ export default class EntityLiving extends Entity {
81103
this.motionZ = 0.0;
82104
}
83105

84-
this.rotationYawHead = this.rotationYaw;
85-
86106
// Jump
87107
if (this.jumping) {
88108
if (this.isInWater()) {
@@ -174,6 +194,15 @@ export default class EntityLiving extends Entity {
174194
}
175195
}
176196

197+
setTargetPositionAndRotation(x, y, z, yaw, pitch, increments) {
198+
this.targetX = x;
199+
this.targetY = y;
200+
this.targetZ = z;
201+
this.targetYaw = yaw;
202+
this.targetPitch = pitch;
203+
this.rotationPositionIncrements = increments;
204+
}
205+
177206
swingArm() {
178207
let swingAnimationEnd = 6;
179208
if (!this.isSwingInProgress || this.swingProgressInt >= swingAnimationEnd / 2 || this.swingProgressInt < 0) {
@@ -217,4 +246,9 @@ export default class EntityLiving extends Entity {
217246
return value - wrapped;
218247
}
219248

249+
setRotationYawHead(yaw) {
250+
this.targetYaw = yaw; // TODO should be rotationYawHead
251+
// this.rotationYawHead = yaw;
252+
}
253+
220254
}

0 commit comments

Comments
 (0)