From df9cc3a481603d452fc3e92e600cc49e39644d5a Mon Sep 17 00:00:00 2001 From: Payton Kerby Date: Mon, 15 Jul 2024 14:54:08 -0500 Subject: [PATCH] feat(world): added ticks param to entity.setOnFire --- .../src/components/entity/attribute/health.ts | 30 ++++++++++++++--- .../src/components/entity/flag/on-fire.ts | 33 +++++++++++++++++++ packages/world/src/entity/entity.ts | 29 +++++++++++++--- 3 files changed, 82 insertions(+), 10 deletions(-) diff --git a/packages/world/src/components/entity/attribute/health.ts b/packages/world/src/components/entity/attribute/health.ts index 008a8198..ad394779 100644 --- a/packages/world/src/components/entity/attribute/health.ts +++ b/packages/world/src/components/entity/attribute/health.ts @@ -47,9 +47,31 @@ class EntityHealthComponent extends EntityAttributeComponent { } /** - * Damages the entity with an attacker. - * @param attacker The entity that is attacking the entity. - * @returns The current health of the entity. + * Applies damage to the entity. + * @param damage The amount of damage to apply to the entity. + */ + public applyDamage(damage: number): void { + // Decrease the health of the entity + this.decreaseValue(damage); + + // Create a new actor event packet + const packet = new ActorEventPacket(); + + // Assign the values to the packet + packet.actorRuntimeId = this.entity.runtime; + packet.eventId = ActorEventIds.HURT_ANIMATION; + packet.eventData = -1; + this.entity.dimension.broadcast(packet); + + // Check if the entity is dead + if (this.getCurrentValue() <= 0) { + // Kill the entity + this.entity.kill(); + } + } + + /** + * @deprecated This method is deprecated and will be removed in the future. */ public damage(attacker: Entity): number { // TODO: Handle the damage based on the attacker @@ -120,8 +142,6 @@ class EntityHealthComponent extends EntityAttributeComponent { this.entity.kill(); } } - - public onTick(): void {} } export { EntityHealthComponent }; diff --git a/packages/world/src/components/entity/flag/on-fire.ts b/packages/world/src/components/entity/flag/on-fire.ts index 635e09bc..b3a32a0f 100644 --- a/packages/world/src/components/entity/flag/on-fire.ts +++ b/packages/world/src/components/entity/flag/on-fire.ts @@ -11,6 +11,8 @@ class EntityOnFireComponent extends EntityFlagComponent { public defaultValue = false; + public onFireRemainingTicks = 300; + /** * Creates a new entity breathing component. * @@ -23,6 +25,37 @@ class EntityOnFireComponent extends EntityFlagComponent { // Set the entity to have breathing this.setCurrentValue(this.defaultValue, false); } + + /** + * Extinquishes the fire on the entity. + */ + public extinguish(): void { + // Set the remaining ticks to 0 + this.onFireRemainingTicks = 0; + } + + public onTick(): void { + // Check if the value is true + if (this.getCurrentValue()) { + // Decrease the remaining ticks + this.onFireRemainingTicks--; + + // Check if the remaining ticks is less than or equal to + if (this.onFireRemainingTicks <= 0) { + // Set the value to false + this.setCurrentValue(false); + + // Remove the component from the entity + this.entity.removeComponent(EntityOnFireComponent.identifier); + } else if ( + this.entity.dimension.world.currentTick % 20n === 0n && + this.entity.hasComponent("minecraft:health") + ) { + // Apply damage to the entity + this.entity.applyDamage(1); + } + } + } } export { EntityOnFireComponent }; diff --git a/packages/world/src/entity/entity.ts b/packages/world/src/entity/entity.ts index 8a655e16..43f1f2a1 100644 --- a/packages/world/src/entity/entity.ts +++ b/packages/world/src/entity/entity.ts @@ -470,9 +470,6 @@ class Entity { // Iterate over the flags set on the entity for (const [flag, enabled] of this.flags) packet.setFlag(flag, enabled); - // Clear the flags cause they only need to be sent once - this.flags.clear(); - // Send the packet to the dimension this.dimension.broadcast(packet); } @@ -684,6 +681,23 @@ class Entity { healthComponent.setCurrentValue(health); } + /** + * Applies damage to the entity. + * @note This method is dependant on the entity having a `minecraft:health` component, if not will result in an `error`. + * @param damage The amount of damage to apply to the entity. + */ + public applyDamage(damage: number): void { + // Check if the entity has a health component + if (!this.hasComponent("minecraft:health")) + throw new Error("The entity does not have a health component."); + + // Get the health component + const health = this.getComponent("minecraft:health"); + + // Apply the damage to the entity + health.applyDamage(damage); + } + /** * Geta the nametag of the entity. * @note This method is dependant on the entity having a `minecraft:nametag` component, if not will result in an `error`. @@ -722,19 +736,24 @@ class Entity { * Sets the entity on fire. * @note This method is dependant on the entity having a `minecraft:on_fire` component, if the component does not exist it will be created. */ - // TODO: add seconds parameter - public setOnFire(): void { + public setOnFire(ticks?: number): void { // Check if the entity has an on fire component if (this.hasComponent("minecraft:on_fire")) { // Get the on fire component const component = this.getComponent("minecraft:on_fire"); + // Set the remaining ticks + component.onFireRemainingTicks = ticks ?? 300; + // Set the entity on fire component.setCurrentValue(true); } else { // Create a new on fire component const component = new EntityOnFireComponent(this); + // Set the remaining ticks + component.onFireRemainingTicks = ticks ?? 300; + // Set the entity on fire component.setCurrentValue(true); }