2024-10-26 14:24:38 +01:00
|
|
|
import Entity from "./Entity";
|
|
|
|
import ItemStack from "../inventories/ItemStack";
|
|
|
|
import World from "../World";
|
2023-04-17 02:05:11 +01:00
|
|
|
|
2024-10-26 14:24:38 +01:00
|
|
|
export default class EntityItem extends Entity {
|
2023-04-17 02:05:11 +01:00
|
|
|
public age:number;
|
2023-06-26 09:57:40 +01:00
|
|
|
public itemStack:ItemStack;
|
2023-04-17 02:05:11 +01:00
|
|
|
|
2023-11-09 16:30:40 +00:00
|
|
|
public pickupDelay:number;
|
|
|
|
|
2023-11-02 08:31:43 +00:00
|
|
|
public constructor(world:World, itemStack:ItemStack) {
|
2023-04-17 02:05:11 +01:00
|
|
|
super(world);
|
2023-06-26 09:57:40 +01:00
|
|
|
|
|
|
|
this.itemStack = itemStack;
|
|
|
|
|
2023-11-09 16:30:40 +00:00
|
|
|
this.entitySize.set(0.2, 0.2);
|
|
|
|
|
|
|
|
this.pickupDelay = 0;
|
|
|
|
|
|
|
|
this.motion.set(Math.random() * 0.2 - 0.1, 0.2, Math.random() * 0.2 - 0.1);
|
|
|
|
|
2023-04-17 02:05:11 +01:00
|
|
|
this.age = 0;
|
|
|
|
this.health = 5;
|
|
|
|
}
|
2023-11-09 16:30:40 +00:00
|
|
|
|
2023-12-24 17:47:20 +00:00
|
|
|
async onTick() {
|
2023-11-09 16:30:40 +00:00
|
|
|
super.onTick();
|
|
|
|
if (this.pickupDelay > 0) {
|
|
|
|
this.pickupDelay--;
|
2023-12-18 01:23:52 +00:00
|
|
|
} else {
|
2023-12-24 17:47:20 +00:00
|
|
|
let playerCollided = await this.collidesWithPlayer(this.entityAABB);
|
|
|
|
if (playerCollided !== undefined) {
|
2023-12-18 01:23:52 +00:00
|
|
|
playerCollided.inventory.addItemStack(this.itemStack);
|
|
|
|
playerCollided.itemPickup(this, this.itemStack.size);
|
|
|
|
if (this.itemStack.size <= 0) {
|
|
|
|
this.kill();
|
|
|
|
}
|
|
|
|
}
|
2023-11-09 16:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.motion.add(0, -0.04, 0);
|
|
|
|
this.moveEntity(this.motion.x, this.motion.y, this.motion.z);
|
|
|
|
|
|
|
|
let xyMult = 0.98;
|
|
|
|
if (this.onGround) {
|
|
|
|
xyMult = 0.59;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Change the x and z based on the slipperiness of a block
|
|
|
|
this.motion.mult(xyMult, 0.98, xyMult);
|
|
|
|
|
|
|
|
if (this.onGround) {
|
|
|
|
this.motion.y *= -0.5;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.age++;
|
|
|
|
if (this.age >= 6000) {
|
2023-12-06 00:04:57 +00:00
|
|
|
this.kill();
|
2023-11-09 16:30:40 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-17 02:05:11 +01:00
|
|
|
}
|