forked from fvtt-fria-ligan/twilight2000-foundry-vtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
t2k4e.js
238 lines (201 loc) · 7.53 KB
/
t2k4e.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* ============================================================================
* TWILIGHT 2000 4E
* Official website: https://frialigan.se/en/games/twilight-2000/
* ============================================================================
* Contributing: https://github.com/Stefouch/t2k4e
* ============================================================================
* Creator: Stefouch
* Patreon: https://www.patreon.com/Stefouch
* ============================================================================
* Source Code License: GPL-3.0-or-later
*
* Foundry License: Foundry Virtual Tabletop End User License Agreement
* https://foundryvtt.com/article/license/
*
* ============================================================================
*/
// Imports Modules.
import { T2K4E } from './module/config.js';
import { registerDsN, T2KRoller } from './module/dice.js';
import { registerSystemSettings } from './module/settings.js';
import { registerStatusEffects } from './module/statusEffects.js';
import { preloadHandlebarsTemplates, registerHandlebars } from './module/templates.js';
import { createT2KMacro, rollItemMacro } from './module/macros.js';
import displayMessages from './module/message-system.js';
import * as Chat from './module/chat.js';
// Imports Documents.
import ActorT2K from './module/actor/actor.js';
import ItemT2K from './module/item/item.js';
// Imports Applications.
import ActorSheetT2KCharacter from './module/actor/characterSheet.js';
import ActorSheetT2KVehicle from './module/actor/vehicleSheet.js';
import ActorSheetT2KUnit from './module/actor/unitSheet.js';
import ItemSheetT2K from './module/item/itemSheet.js';
// Imports Helpers.
import { checkMigration } from './module/migration.js';
import * as YZUR from './lib/yzur.js';
/* -------------------------------------------- */
/* Foundry VTT Initialization */
/* -------------------------------------------- */
Hooks.once('init', function() {
console.log(`t2k4e | Initializing the Twilight 2000 4E Game System\n${T2K4E.ASCII}`);
// Registers dice.
YZUR.YearZeroRollManager.register('t2k', {
'ROLL.chatTemplate': 'systems/t2k4e/templates/dice/roll.hbs',
'ROLL.tooltipTemplate': 'systems/t2k4e/templates/dice/tooltip.hbs',
'ROLL.infosTemplate': 'systems/t2k4e/templates/dice/infos.hbs',
'CHAT.showInfos': true,
'DICE.ICONS.t2k.ammo.6': '<img src="systems/t2k4e/assets/icons/bullet2.png"/>',
});
game.yzur = YZUR;
// Creates a namespace within the game global.
// Places our classes in their own namespace for later reference.
game.t2k4e = {
applications: {
ActorSheetT2KCharacter,
ActorSheetT2KVehicle,
ActorSheetT2KUnit,
ItemSheetT2K,
},
config: T2K4E,
entities: {
ActorT2K,
ItemT2K,
},
macros: {
rollItemMacro,
},
roller: T2KRoller,
};
// Records configuration values.
CONFIG.T2K4E = T2K4E;
CONFIG.Actor.documentClass = ActorT2K;
CONFIG.Item.documentClass = ItemT2K;
// Patches Core functions.
CONFIG.Combat.initiative = {
formula: '1d10 + (@attributes.agl.value / 100)',
decimals: 2,
};
// Registers sheet application classes.
// This will stop using the core sheets and instead use our customized versions.
Actors.unregisterSheet('core', ActorSheet);
Actors.registerSheet('t2k4e', ActorSheetT2KCharacter, {
types: ['character', 'npc'],
makeDefault: true,
label: 'T2K4E.SheetClassCharacter',
});
Actors.registerSheet('t2k4e', ActorSheetT2KVehicle, {
types: ['vehicle'],
makeDefault: true,
label: 'T2K4E.SheetClassVehicle',
});
Actors.registerSheet('t2k4e', ActorSheetT2KUnit, {
types: ['unit'],
makeDefault: true,
label: 'T2K4E.SheetClassUnit',
});
Items.unregisterSheet('core', ItemSheet);
Items.registerSheet('t2k4e', ItemSheetT2K, { makeDefault: true });
registerSystemSettings();
registerHandlebars();
preloadHandlebarsTemplates();
// Defines custom T2K status effects.
registerStatusEffects();
});
Hooks.once('ready', function() {
// Wait to register hotbar drop hook on ready so that modules could register earlier if they want to.
Hooks.on('hotbarDrop', (bar, data, slot) => createT2KMacro(data, slot));
// Determines whether a system migration is required and feasible.
checkMigration();
// Debugging
if (game.userId === 'OPqJPiI75DlhwfVv') {
CONFIG.debug.hooks = true;
try {
// Renders a starting actor or item.
/** @type {Actor} */
const startingActor = game.actors.getName('Stefouch');
// startingActor.sheet.render(true);
console.warn(startingActor);
/** @type {Actor} */
const startingVehicle = game.actors.getName('T-80');
// startingVehicle.sheet.render(true);
console.warn(startingVehicle);
/** @type {Item} */
const startingItem = game.items.getName('FN FAL');
// startingItem.sheet.render(true);
console.warn(startingItem);
/** @type {JournalEntry} */
// game.journal.getName('Test').sheet.render(true);
}
catch (error) {
console.warn('t2k4e | DEBUG | Cannot find starting Entity.', error);
}
}
// Displays starting messages.
displayMessages();
console.warn('t2k4e | READY!');
});
/* -------------------------------------------- */
/* Foundry VTT Hooks */
/* -------------------------------------------- */
Hooks.once('diceSoNiceReady', dice3d => registerDsN(dice3d));
/* -------------------------------------------- */
Hooks.on('renderChatLog', (app, html, data) => Chat.addChatListeners(html));
/* -------------------------------------------- */
Hooks.on('getChatLogEntryContext', Chat.addChatMessageContextOptions);
/* -------------------------------------------- */
Hooks.on('renderChatMessage', (app, html, data) => {
// Hides chat action buttons.
Chat.hideChatActionButtons(html);
// Automatically closes dice results tooltips.
let delay = game.settings.get('t2k4e', 'closeRollTooltipDelay');
if (delay >= 0) {
delay = Math.min(delay, 15 * 60);
Chat.closeRollTooltip(app, html, delay * 1000);
}
});
/* -------------------------------------------- */
Hooks.on('dropActorSheetData', (actor, sheet, data) => {
// When dropping something on a vehicle sheet.
if (actor.type === 'vehicle') {
// When dropping an actor on a vehicle sheet.
if (data.type === 'Actor') sheet.dropCrew(data.id);
}
});
/* -------------------------------------------- */
Hooks.on('createToken', (token, data, userId) => {
// When creating a Unit token.
if (token.actor.type === 'unit') {
const updateData = {};
// Uses abbreviation (info) in place of name.
const nm = token.actor.data.data.info;
if (nm) updateData['name'] = nm;
// Uses default affiliation.
const afl = token.actor.data.data.unitAffiliation;
if (afl) {
let disposition;
switch (afl) {
case 'friendly':
disposition = CONST.TOKEN_DISPOSITIONS.FRIENDLY;
break;
case 'hostile':
disposition = CONST.TOKEN_DISPOSITIONS.HOSTILE;
break;
case 'neutral':
disposition = CONST.TOKEN_DISPOSITIONS.NEUTRAL;
break;
default:
disposition = CONST.TOKEN_DISPOSITIONS.HOSTILE;
}
if (disposition !== token.data.disposition) updateData['disposition'] = disposition;
}
// Updates the token.
if (!foundry.utils.isObjectEmpty(updateData)) {
token.update(updateData);
}
}
});
/* -------------------------------------------- */
Hooks.on('renderItemSheet', function(app, html) {
app._element[0].style.height = 'auto';
});