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

feat: add cosmere initiative system #33

Merged
merged 3 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 9 additions & 0 deletions src/declarations/foundry/common/documents/combat.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace foundry {
namespace documents {
declare class BaseCombat<
Schema extends
foundry.abstract.DataSchema = foundry.abstract.DataSchema,
Parent extends Document | null = null,
> extends foundry.abstract.Document<Schema, Parent> {}
}
}
9 changes: 9 additions & 0 deletions src/declarations/foundry/common/documents/combatant.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace foundry {
namespace documents {
declare class BaseCombatant<
Schema extends
foundry.abstract.DataSchema = foundry.abstract.DataSchema,
Parent extends Document | null = null,
> extends foundry.abstract.Document<Schema, Parent> {}
}
}
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ Hooks.once('init', async () => {
CONFIG.Item.dataModels = dataModels.item.config;
CONFIG.Item.documentClass = documents.CosmereItem;

CONFIG.Combat.documentClass = applications.combat.CosmereCombat;
CONFIG.Combatant.documentClass = applications.combat.CosmereCombatant;
CONFIG.ui.combat = applications.combat.CosmereCombatTracker;

Actors.unregisterSheet('core', ActorSheet);
Actors.registerSheet('cosmere-rpg', applications.actor.CharacterSheet, {
types: ['character'],
Expand Down
12 changes: 10 additions & 2 deletions src/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"placeholder": "[Path]"
},
"Specialties": {
"placeholder": "[Specialty]"
"placeholder": "[Specialty]"
},
"Expertise": {
"name": "Expertise",
Expand Down Expand Up @@ -352,7 +352,15 @@
"Vital": "Vital",
"Healing": "Healing"
},
"Currencies": {}
"Currencies": {},
"Combat": {
"FastPlayers": "Fast Characters",
"SlowPlayers": "Slow Characters",
"FastAdversaries": "Fast Adversaries",
"SlowAdversaries": "Slow Adversaries",
"ToggleTurn": "Toggle Turn Speed",
"ResetActivation": "Reset Activation"
}
},
"DICE": {
"RollMode": "Roll Mode",
Expand Down
7 changes: 4 additions & 3 deletions src/style.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@import './style/sheets/module.scss';
// @import './style/chat/module.scss';
@import './style/sidebar/combat.scss';

// @import './style/chat/module.scss';
/* ----------------------------------------- */
/* Globals */
/* ----------------------------------------- */
Expand Down Expand Up @@ -34,10 +35,10 @@

@font-face {
font-family: 'cosmere-dingbats';
src: url("https://dl.dropboxusercontent.com/scl/fi/9909gen4fd0oveyzfposx/CosmereDingbats-Regular.otf?rlkey=ig6odq9hxyo1st8kt3ujp1czz&st=72qrads3&raw=1")
src: url('https://dl.dropboxusercontent.com/scl/fi/9909gen4fd0oveyzfposx/CosmereDingbats-Regular.otf?rlkey=ig6odq9hxyo1st8kt3ujp1czz&st=72qrads3&raw=1');
}

i.cosmere-icon {
font-family: 'cosmere-dingbats';
font-style: normal;
}
}
37 changes: 37 additions & 0 deletions src/style/sidebar/combat.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
.cosmere-combatant-buttons {
display: flex;
text-align: center;
gap: 6px;
}

.cosmere-turn-speed-control,
.cosmere-activate-control,
.cosmere-activate-control-activated {
display: block;
width: 40px;
height: var(--sidebar-item-height);
background-size: 32px;
background-position: center;
background-repeat: no-repeat;
margin: 0 0.5em;
}

.cosmere-turn-speed-control {
background-image: url(/icons/svg/clockwork.svg);
}

.cosmere-activate-control,
.cosmere-activate-control-activated {
background-image: url(/icons/svg/combat.svg);
}

.cosmere-activate-control-activated {
filter: brightness(40%);
}

.combat-phase {
padding: 0.5rem 0px;
display: flex;
align-items: center;
justify-content: center;
}
156 changes: 156 additions & 0 deletions src/system/applications/combat/combat-tracker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import CosmereCombatant from './combatant';

/**
* Overrides default tracker template to implement slow/fast buckets and activation button.
*/
export default class CosmereCombatTracker extends CombatTracker {
// eslint-disable-next-line @typescript-eslint/class-literal-property-style
jsc17 marked this conversation as resolved.
Show resolved Hide resolved
override get template() {
return 'systems/cosmere-rpg/templates/combat/combat-tracker.hbs';
}

//modifies data being sent to the combat tracker template to add turn speed, type and activation status and splitting turns between the initiative phases.
override async getData(
options?: Partial<ApplicationOptions> | undefined,
): Promise<object> {
const data = (await super.getData(options)) as {
turns: CosmereTurn[];
fastPlayers: CosmereTurn[];
slowPlayers: CosmereTurn[];
fastNPC: CosmereTurn[];
slowNPC: CosmereTurn[];
};
data.turns = data.turns.map((turn) => {
jsc17 marked this conversation as resolved.
Show resolved Hide resolved
const combatant: CosmereCombatant =
this.viewed!.getEmbeddedDocument(
'Combatant',
turn.id,
{},
) as CosmereCombatant;
const newTurn: CosmereTurn = {
...turn,
turnSpeed: combatant.getFlag(
'cosmere-rpg',
'turnSpeed',
) as string,
type: combatant.actor.type,
activated: combatant.getFlag(
'cosmere-rpg',
'activated',
) as boolean,
};
//strips active player formatting
newTurn.css = '';
return newTurn;
});
data.fastPlayers = data.turns.filter((turn) => {
return turn.type == 'character' && turn.turnSpeed == 'fast';
});
data.slowPlayers = data.turns.filter((turn) => {
return turn.type == 'character' && turn.turnSpeed == 'slow';
});
data.fastNPC = data.turns.filter((turn) => {
return turn.type == 'adversary' && turn.turnSpeed == 'fast';
});
data.slowNPC = data.turns.filter((turn) => {
return turn.type == 'adversary' && turn.turnSpeed == 'slow';
});
jsc17 marked this conversation as resolved.
Show resolved Hide resolved

return data;
}

override activateListeners(html: JQuery<HTMLElement>): void {
super.activateListeners(html);
html.find('.cosmere-turn-speed-control').on(
'click',
this._onClickToggleTurnSpeed.bind(this),
);
html.find('.cosmere-activate-control').on(
'click',
this._onActivateCombatant.bind(this),
);
}

//toggles combatant turn speed on clicking the "fast/slow" text on the combat tracker window
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can also be set up as a doc comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not exactly sure what you mean here. Can you elaborate a bit?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you use JSDoc comments (comments starting with /**) instead of regular comments, then your comment will show up alongside intellisense on most IDEs. Have a look at the Actor document for some examples.

Broadly: Any comments that explain the functionality (for someone who might want to use that function/variable/parameter/etc.) should be doc comments. Any comments that explain the implementation should be regular comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a pretty good explanation. I hadn't used JSDoc comments before so I didn't realize they did that for intellisense. I've expanded the comments in the code I've added.

protected _onClickToggleTurnSpeed(event: Event) {
event.preventDefault();
event.stopPropagation();
const btn = event.currentTarget as HTMLElement;
const li = btn.closest<HTMLElement>('.combatant')!;
const combatant: CosmereCombatant = this.viewed!.getEmbeddedDocument(
'Combatant',
li.dataset.combatantId!,
{},
) as CosmereCombatant;
void combatant.toggleTurnSpeed();
}

//activates the combatant when clicking the activation icon
protected _onActivateCombatant(event: Event) {
event.preventDefault();
event.stopPropagation();
const btn = event.currentTarget as HTMLElement;
const li = btn.closest<HTMLElement>('.combatant')!;
const combatant: CosmereCombatant = this.viewed!.getEmbeddedDocument(
'Combatant',
li.dataset.combatantId!,
{},
) as CosmereCombatant;
void combatant.setFlag('cosmere-rpg', 'activated', true);
}

//toggles combatant turn speed on clicking the "fast/slow" option in the turn tracker context menu
protected _onContextToggleTurnSpeed(li: JQuery<HTMLElement>) {
const combatant: CosmereCombatant = this.viewed!.getEmbeddedDocument(
'Combatant',
li.data('combatant-id') as string,
{},
) as CosmereCombatant;
combatant.toggleTurnSpeed();
}

//resets combatants activation status to hasn't activated
protected _onContextResetActivation(li: JQuery<HTMLElement>) {
const combatant: CosmereCombatant = this.viewed!.getEmbeddedDocument(
'Combatant',
li.data('combatant-id') as string,
{},
) as CosmereCombatant;
void combatant.setFlag('cosmere-rpg', 'activated', false);
}

_getEntryContextOptions(): ContextMenuEntry[] {
const menu: ContextMenuEntry[] = [
{
name: 'COSMERE.Combat.ToggleTurn',
icon: '',
callback: this._onContextToggleTurnSpeed.bind(this),
},
{
name: 'COSMERE.Combat.ResetActivation',
icon: '<i class="fas fa-undo"></i>',
callback: this._onContextResetActivation.bind(this),
},
];
menu.push(
...super
._getEntryContextOptions()
.filter(
(i) =>
i.name !== 'COMBAT.CombatantReroll' &&
i.name !== 'COMBAT.CombatantClear',
),
);
return menu;
}
}

interface CosmereTurn {
id: string;
css: string;
pending: number;
finished: number;
type?: string;
turnSpeed?: string;
jsc17 marked this conversation as resolved.
Show resolved Hide resolved
jsc17 marked this conversation as resolved.
Show resolved Hide resolved
activated?: boolean;
}
22 changes: 22 additions & 0 deletions src/system/applications/combat/combat.ts
jsc17 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export default class CosmereCombat extends Combat {
//sets all defeated combatants activation status to true (already activated), and all others to false(hasn't activated yet)
resetActivations() {
for (const combatant of this.turns) {
void combatant.setFlag(
'cosmere-rpg',
'activated',
combatant.isDefeated ? true : false,
);
}
}

override async startCombat(): Promise<this> {
this.resetActivations();
return super.startCombat();
}

override async nextRound(): Promise<this | undefined> {
this.resetActivations();
return super.nextRound();
}
}
54 changes: 54 additions & 0 deletions src/system/applications/combat/combatant.ts
jsc17 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { DocumentModificationOptions } from '@league-of-foundry-developers/foundry-vtt-types/src/foundry/common/abstract/document.mjs';
import { SchemaField } from '@league-of-foundry-developers/foundry-vtt-types/src/foundry/common/data/fields.mjs';
import { CosmereActor } from '../../documents';
import { ActorType, TurnSpeed } from '@src/system/types/cosmere';

/**
*
*/

export default class CosmereCombatant extends Combatant {
//sets actor type to CosmereActor, to get typescript to stop yelling. Might be a better way to do this but I'm newish to TS and don't know how.
override get actor(): CosmereActor {
return super.actor as CosmereActor;
}
jsc17 marked this conversation as resolved.
Show resolved Hide resolved

//on creation, combatants turn speed is set to slow, activation status to false, and then sets the initiative, bypassing the need to roll
protected override _onCreate(
data: SchemaField.InnerAssignmentType<DataSchema>,
options: DocumentModificationOptions,
userID: string,
) {
super._onCreate(data, options, userID);
void this.setFlag('cosmere-rpg', 'turnSpeed', 'slow');
void this.setFlag('cosmere-rpg', 'activated', false);
void this.combat?.setInitiative(
this.id!,
this.generateInitiative(
this.actor.type as ActorType,
TurnSpeed.Slow,
jsc17 marked this conversation as resolved.
Show resolved Hide resolved
),
);
}

generateInitiative(type: ActorType, speed: TurnSpeed): number {
let initiative: number = this.actor.system.attributes.spd.value;
jsc17 marked this conversation as resolved.
Show resolved Hide resolved
if (type == ActorType.Character) initiative += 500;
if (speed == TurnSpeed.Fast) initiative += 1000;
return initiative;
}

toggleTurnSpeed() {
const currentSpeed = this.getFlag(
'cosmere-rpg',
'turnSpeed',
) as TurnSpeed;
const newSpeed =
currentSpeed == TurnSpeed.Slow ? TurnSpeed.Fast : TurnSpeed.Slow;
void this.setFlag('cosmere-rpg', 'turnSpeed', newSpeed);
void this.combat?.setInitiative(
this.id!,
this.generateInitiative(this.actor.type as ActorType, newSpeed),
);
}
}
5 changes: 5 additions & 0 deletions src/system/applications/combat/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import CosmereCombat from './combat';
import CosmereCombatTracker from './combat-tracker';
import CosmereCombatant from './combatant';

export { CosmereCombat, CosmereCombatTracker, CosmereCombatant };
1 change: 1 addition & 0 deletions src/system/applications/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * as actor from './actor';
export * as combat from './combat';
5 changes: 5 additions & 0 deletions src/system/types/cosmere.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,8 @@ export const enum ItemType {

Injury = 'injury',
}

export const enum TurnSpeed {
Fast = 'fast',
Slow = 'slow',
}
1 change: 1 addition & 0 deletions src/system/util/handlebars/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ export async function preloadHandlebarsTemplates() {
'systems/cosmere-rpg/templates/actors/parts/actions.hbs',
'systems/cosmere-rpg/templates/actors/parts/inventory.hbs',
// 'systems/cosmere-rpg/templates/chat/parts/roll-details.hbs',
'systems/cosmere-rpg/templates/combat/combatant.hbs',
];

return await loadTemplates(
Expand Down
Loading