Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace loopingEvent with ActorTask for spinning wheel #414

Merged
merged 3 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/engine/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './content-plugin';
export * from './loader';
export * from './util';
110 changes: 0 additions & 110 deletions src/engine/plugins/util.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/engine/world/skill-util/harvest-skill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { soundIds } from '@engine/world/config/sound-ids';
import { Skill } from '@engine/world/actor/skills';
import { getBestAxe, HarvestTool } from '@engine/world/config/harvest-tool';
import { findItem } from '@engine/config/config-handler';
import { activeWorld } from '@engine/world';
import { loopingEvent } from '@engine/plugins';
import { logger } from '@runejs/common';

/**
Expand Down
5 changes: 2 additions & 3 deletions src/plugins/buttons/magic-attack.plugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Player } from '@engine/world/actor';
import { TaskExecutor, MagicOnNPCActionHook, MagicOnNPCAction } from '@engine/action';
import { logger } from '@runejs/common';
import { loopingEvent } from '@engine/plugins';

const buttonIds: number[] = [
0, // Home Teleport
Expand Down Expand Up @@ -31,13 +30,13 @@ export const activate = (task: TaskExecutor<MagicOnNPCAction>, elapsedTicks: num
player.walkingQueue.clear();

//npc world index would be -1 for players
player.outgoingPackets.sendProjectile(player.position, offsetX, offsetY, 250, 40, 36, 100, npc.worldIndex + 1, 1);
player.outgoingPackets.sendProjectile(player.position, offsetX, offsetY, 250, 40, 36, 100, npc.worldIndex + 1, 1);
console.info(`${player.username} smites ${npc.name} with ${spells[buttonId]}`);
};

export default {
pluginId: 'rs:magic',
hooks:
hooks:
{
type: 'magic_on_npc',
widgetId: 192,
Expand Down
14 changes: 2 additions & 12 deletions src/plugins/commands/player-animation-command.plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { commandActionHandler, PlayerCommandAction } from '@engine/action';
import { Player } from '@engine/world/actor/player/player';
import { loopingEvent } from '@engine/plugins';
import { commandActionHandler, PlayerCommandActionHook } from '@engine/action';

const action: commandActionHandler = (details): void => {
const { player, args } = details;
Expand All @@ -22,14 +20,6 @@ export default {
}
],
handler: action
},
{
type: 'player_command',
commands: [ 'yeet' ],
handler: ({ player }: PlayerCommandAction): void => {
loopingEvent({ ticks: 3, player })
.event.asObservable().subscribe(() => (player as Player).playAnimation(866))
}
}
} as PlayerCommandActionHook
]
};
118 changes: 81 additions & 37 deletions src/plugins/skills/crafting/spinning-wheel.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import { Skill } from '@engine/world/actor/skills';
import { animationIds } from '@engine/world/config/animation-ids';
import { objectIds } from '@engine/world/config/object-ids';
import { findItem, widgets } from '@engine/config/config-handler';
import { loopingEvent } from '@engine/plugins';
import { logger } from '@runejs/common';
import { ActorTask } from '@engine/task/impl';
import { Player } from '@engine/world/actor';

interface Spinnable {
input: number | number[];
Expand Down Expand Up @@ -76,63 +77,106 @@ export const openSpinningInterface: objectInteractionActionHandler = (details) =
});
};

const spinProduct: any = (details: ButtonAction, spinnable: Spinnable, count: number) => {
let elapsedTicks = 0;

let created = 0;

// As an multiple items can be used for one of the recipes, check if its an array
let currentItem: number;
let currentItemIndex: number = 0;
let isArray = false;
if (Array.isArray(spinnable.input)) {
isArray = true;
currentItem = spinnable.input[0];
} else {
currentItem = spinnable.input;
/**
* A task to (repeatedly if needed) spin a product from a spinnable.
*/
class SpinProductTask extends ActorTask<Player> {
/**
* The number of ticks that `execute` has been called inside this task.
*/
private elapsedTicks = 0;

/**
* The number of items that should be spun.
*/
private count: number;

/**
* The number of items that have been spun.
*/
private created = 0;

/**
* The spinnable that is being used.
*/
private spinnable: Spinnable;

/**
* The currently being spun input.
*/
private currentItem: number;

/**
* The index of the current input being spun.
*/
private currentItemIndex = 0;

constructor(
player: Player,
spinnable: Spinnable,
count: number,
) {
super(player);
this.spinnable = spinnable;
this.count = count;
}
// Create a new tick loop
const loop = loopingEvent({ player: details.player });
loop.event.subscribe(() => {
if (created === count) {
loop.cancel();

public execute(): void {
if (this.created === this.count) {
this.stop();
return;
}

// As an multiple items can be used for one of the recipes, check if its an array
let isArray = false;
if (Array.isArray(this.spinnable.input)) {
isArray = true;
this.currentItem = this.spinnable.input[0];
} else {
this.currentItem = this.spinnable.input;
}

// Check if out of input material
if (!details.player.hasItemInInventory(currentItem)) {
if (!this.actor.hasItemInInventory(this.currentItem)) {
let cancel = false;
if (isArray) {
if (currentItemIndex < (<number[]> spinnable.input).length) {
currentItemIndex++;
currentItem = (<number[]> spinnable.input)[currentItemIndex];
if (this.currentItemIndex < (<number[]> this.spinnable.input).length) {
this.currentItemIndex++;
this.currentItem = (<number[]> this.spinnable.input)[this.currentItemIndex];
} else {
cancel = true;
}
} else {
cancel = true;
}
if (cancel) {
const itemName = findItem(currentItem)?.name || '';
details.player.sendMessage(`You don't have any ${itemName.toLowerCase()}.`);
loop.cancel();
const itemName = findItem(this.currentItem)?.name || '';
this.actor.sendMessage(`You don't have any ${itemName.toLowerCase()}.`);
this.stop();
return;
}
}

// Spinning takes 3 ticks for each item
if (elapsedTicks % 3 === 0) {
details.player.removeFirstItem(currentItem);
details.player.giveItem(spinnable.output);
details.player.skills.addExp(Skill.CRAFTING, spinnable.experience);
created++;
if (this.elapsedTicks % 3 === 0) {
this.actor.removeFirstItem(this.currentItem);
this.actor.giveItem(this.spinnable.output);
this.actor.skills.addExp(Skill.CRAFTING, this.spinnable.experience);
this.created++;
}

// animation plays once every two items
if (elapsedTicks % 6 === 0) {
details.player.playAnimation(animationIds.spinSpinningWheel);
details.player.outgoingPackets.playSound(soundIds.spinWool, 5);
if (this.elapsedTicks % 6 === 0) {
this.actor.playAnimation(animationIds.spinSpinningWheel);
this.actor.outgoingPackets.playSound(soundIds.spinWool, 5);
}

elapsedTicks++;
});
this.elapsedTicks++;
}
}

const spinProduct: any = (details: ButtonAction, spinnable: Spinnable, count: number) => {
details.player.enqueueTask(SpinProductTask, [spinnable, count]);
};

export const buttonClicked: buttonActionHandler = (details) => {
Expand Down
3 changes: 1 addition & 2 deletions src/plugins/skills/fletching/fletching.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ import { colors } from '@engine/util/colors';
import { findItem, widgets } from '@engine/config/config-handler';
import { Fletchable } from '@plugins/skills/fletching/fletching-types';
import { itemInteractionActionHandler } from '@engine/action';
import { loopingEvent } from '@engine/plugins';

//fletching stuff goes below this! lets do it!

export default {
pluginId: 'rs:fletching',

};