Skip to content

Commit

Permalink
feat(world): added ticks param to entity.setOnFire
Browse files Browse the repository at this point in the history
  • Loading branch information
PMK744 committed Jul 15, 2024
1 parent de7c683 commit df9cc3a
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 10 deletions.
30 changes: 25 additions & 5 deletions packages/world/src/components/entity/attribute/health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -120,8 +142,6 @@ class EntityHealthComponent extends EntityAttributeComponent {
this.entity.kill();
}
}

public onTick(): void {}
}

export { EntityHealthComponent };
33 changes: 33 additions & 0 deletions packages/world/src/components/entity/flag/on-fire.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class EntityOnFireComponent extends EntityFlagComponent {

public defaultValue = false;

public onFireRemainingTicks = 300;

/**
* Creates a new entity breathing component.
*
Expand All @@ -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 };
29 changes: 24 additions & 5 deletions packages/world/src/entity/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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`.
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit df9cc3a

Please sign in to comment.