-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #394 from Jameskmonger/smelting-task
refactor: ♻️ use new task system for smithing plugin
- Loading branch information
Showing
4 changed files
with
233 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { ActorTask } from '@engine/task/impl'; | ||
import { widgets } from '@engine/config/config-handler'; | ||
import { Player, Skill } from '@engine/world/actor'; | ||
import { Smithable } from './forging-types'; | ||
|
||
/** | ||
* A task that handles the forging of an item. | ||
* | ||
* Operates repeatedly every 4 ticks, and stops when the player has forged the amount they wanted. | ||
* | ||
* @author jameskmonger | ||
*/ | ||
export class ForgingTask extends ActorTask<Player> { | ||
private elapsedTicks = 0; | ||
private amountForged = 0; | ||
|
||
constructor(player: Player, private readonly smithable: Smithable, private readonly amount: number) { | ||
super(player); | ||
} | ||
|
||
public execute(): void { | ||
const taskIteration = this.elapsedTicks++; | ||
|
||
// completed the task if we've forged the amount we wanted | ||
if (this.amountForged >= this.amount) { | ||
this.stop(); | ||
return; | ||
} | ||
|
||
// TODO (Jameskmonger) remove magic number | ||
this.actor.playAnimation(898); | ||
|
||
// only do something every 4 ticks | ||
if (taskIteration % 4 !== 0) { | ||
return; | ||
} | ||
|
||
// can't continue | ||
if (!this.hasMaterials()) { | ||
this.stop(); | ||
// TODO (Jameskmonger) send message | ||
return; | ||
} | ||
|
||
// Remove ingredients | ||
for (let i = 0; i < this.smithable.ingredient.amount; i++) { | ||
this.actor.inventory.removeFirst(this.smithable.ingredient.itemId); | ||
} | ||
|
||
// Add item to inventory | ||
this.actor.inventory.add({ | ||
itemId: this.smithable.item.itemId, | ||
amount: this.smithable.item.amount | ||
}); | ||
|
||
this.actor.outgoingPackets.sendUpdateAllWidgetItems(widgets.inventory, this.actor.inventory); | ||
this.actor.skills.addExp(Skill.SMITHING, this.smithable.experience); | ||
|
||
this.amountForged++; | ||
} | ||
|
||
/** | ||
* Whether the player has the required materials to forge the item. | ||
* @returns {boolean} True if the player has the required materials, false otherwise. | ||
*/ | ||
private hasMaterials() { | ||
return this.smithable.ingredient.amount <= this.actor.inventory.findAll(this.smithable.ingredient.itemId).length | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { ActorTask } from '@engine/task/impl'; | ||
import { findItem } from '@engine/config/config-handler'; | ||
import { Player, Skill } from '@engine/world/actor'; | ||
import { Smeltable } from './smelting-types'; | ||
import { animationIds, soundIds } from '@engine/world/config'; | ||
|
||
/** | ||
* A task that handles the smelting of an item. | ||
* | ||
* Operates repeatedly every 3 ticks, and stops when the player has smelted the amount they wanted. | ||
* | ||
* @author jameskmonger | ||
*/ | ||
export class SmeltingTask extends ActorTask<Player> { | ||
private elapsedTicks = 0; | ||
private amountSmelted = 0 | ||
|
||
constructor(player: Player, private readonly smeltable: Smeltable, private readonly amount: number) { | ||
super(player); | ||
} | ||
|
||
public execute(): void { | ||
const taskIteration = this.elapsedTicks++; | ||
|
||
// completed the task if we've smelted the amount we wanted | ||
if (this.amountSmelted >= this.amount) { | ||
this.stop(); | ||
return; | ||
} | ||
|
||
const bar = this.smeltable.bar; | ||
const barItem = findItem(bar.barId); | ||
|
||
if (!barItem) { | ||
this.actor.sendMessage(`Could not find item with id ${bar.barId}. Please tell a dev.`); | ||
this.stop(); | ||
return; | ||
} | ||
|
||
if (!this.hasMaterials()) { | ||
this.actor.sendMessage(`You don't have enough ${barItem.name.toLowerCase()}.`, true); | ||
this.stop(); | ||
return; | ||
} | ||
|
||
if (!this.hasLevel()) { | ||
this.actor.sendMessage(`You need a smithing level of ${bar.requiredLevel} to smelt ${barItem.name.toLowerCase()}s.`, true); | ||
return; | ||
} | ||
|
||
// Smelting takes 3 ticks for each item | ||
if (taskIteration % 3 !== 0) { | ||
return; | ||
} | ||
|
||
bar.ingredients.forEach((item) => { | ||
for (let i = 0; i < item.amount; i++) { | ||
this.actor.removeFirstItem(item.itemId); | ||
} | ||
}); | ||
|
||
this.actor.giveItem(bar.barId); | ||
this.actor.skills.addExp(Skill.SMITHING, bar.experience); | ||
this.amountSmelted++; | ||
|
||
this.actor.playAnimation(animationIds.smelting); | ||
this.actor.outgoingPackets.playSound(soundIds.smelting, 5); | ||
} | ||
|
||
/** | ||
* Whether the player has the required materials to smelt the item. | ||
* @returns {boolean} True if the player has the required materials, false otherwise. | ||
*/ | ||
private hasMaterials() { | ||
return this.smeltable.bar.ingredients.every((item) => { | ||
const itemIndex = this.actor.inventory.findIndex(item); | ||
if (itemIndex === -1 || this.actor.inventory.amountInStack(itemIndex) < item.amount) { | ||
return false; | ||
} | ||
|
||
return true; | ||
}); | ||
} | ||
|
||
/** | ||
* Whether the player has the required level to smelt the item. | ||
* @returns {boolean} True if the player has the required level, false otherwise. | ||
*/ | ||
private hasLevel() { | ||
return this.actor.skills.hasLevel(Skill.SMITHING, this.smeltable.bar.requiredLevel); | ||
} | ||
} |
Oops, something went wrong.