forked from LockDzn/rpg-character-sheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
characterSheet.js
403 lines (351 loc) · 9.4 KB
/
characterSheet.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
const data = {
name: 'Claudio',
player: 'Ryan',
occupation: 'Caçador',
age: 21,
sex: 'male',
birthplace: 'São paulo',
residence: 'São paulo',
life: {
current: 12,
max: 12,
},
sanity: {
current: 62,
max: 62,
},
weapons: [
{
name: 'Balestra',
type: 'Arco',
damage: '1d20',
numCurrent: 1,
numMax: 1,
attack: 5,
reach: '10 m',
defect: 1,
area: '',
},
{
name: 'Canivete',
type: 'Briga',
damage: '1d10',
numCurrent: '',
numMax: '',
attack: '1/2',
reach: '',
defect: 1,
area: '',
},
],
attributes: [
{
type: 'Aparência',
amount: 10,
},
{
type: 'Constituição',
amount: 10,
},
{
type: 'Destreza',
amount: 10,
},
{
type: 'Educação',
amount: 10,
},
{
type: 'Força',
amount: 10,
},
{
type: 'Inteligência',
amount: 10,
},
{
type: 'Poder',
amount: 10,
},
{
type: 'Sorte',
amount: 10,
},
{
type: 'Movimento',
amount: 10,
},
{
type: '?',
amount: 10,
},
],
}
data.weapons.map((weapon, index) => {
addWeaponToTable(weapon, index)
})
data.attributes.map((attribute, index) => {
addAttribute(attribute, index)
})
$('#name').val(data.name)
$('#player').val(data.player)
$('#occupation').val(data.occupation)
$('#age').val(data.age)
$('#sex').val(data.sex)
$('#birthplace').val(data.birthplace)
$('#residence').val(data.residence)
$('.lifeBar').css('width', `${calculateBar(data.life.current, data.life.max)}%`)
$('#lifeCount').text(`${data.life.current}/${data.life.max}`)
$('#lifeCurrent').val(data.life.current)
$('#lifeMax').val(data.life.max)
$('.sanityBar').css(
'width',
`${calculateBar(data.sanity.current, data.sanity.max)}%`
)
$('#sanityCount').text(`${data.sanity.current}/${data.sanity.max}`)
$('#sanityCurrent').val(data.sanity.current)
$('#sanityMax').val(data.sanity.max)
const diceModal = $('#diceAttributes')
const lifeModal = $('#lifeModal')
const sanityModal = $('#sanityModal')
$(window).click(function (event) {
if (event.target.id == 'diceAttributes') {
diceModal.css('display', 'none')
$('#diceNumber').text('')
$('#diceType').text('')
$('.modalDice').css('transform', 'rotate(0deg)')
$('.modalDice').css('-webkit-transform', 'rotate(0deg)')
} else if (event.target.id == 'lifeModal') {
lifeModal.css('display', 'none')
} else if (event.target.id == 'sanityModal') {
sanityModal.css('display', 'none')
} else if (event.target.id == 'addWeaponModal') {
closeModal('#addWeaponModal')
}
})
function rollAtribute(atribute, amount) {
console.log(this)
diceModal.css('display', 'block')
setTimeout(() => {
$('.modalDice').css('transform', 'rotate(360deg)')
$('.modalDice').css('-webkit-transform', 'rotate(360deg)')
}, 1000)
setTimeout(() => {
const diceNumber = rollDice('1d20')
const diceType = calcDice(amount, diceNumber)
$('#diceNumber').text(diceNumber)
$('#diceType').text(diceType)
setTimeout(() => {
diceModal.css('display', 'none')
$('#diceNumber').text('')
$('#diceType').text('')
$('.modalDice').css('transform', 'rotate(0deg)')
$('.modalDice').css('-webkit-transform', 'rotate(0deg)')
}, 20000)
}, 2000)
}
$('.lifeBar').click(function () {
console.log(this)
lifeModal.css('display', 'block')
})
$('.sanityBar').click(function () {
console.log(this)
sanityModal.css('display', 'block')
})
$('#addWeapon').click(function () {
openModal('#addWeaponModal')
})
$('#lesion').change(function () {
if (this.checked) {
console.log('Modo lesionamento grave ativado!')
} else {
console.log('Modo lesionamento grave desativado!')
}
})
$('#injury').change(function () {
if (this.checked) {
console.log('Modo lesionamento ativado!')
} else {
console.log('Modo lesionado desativado!')
}
})
$('#dying').change(function () {
if (this.checked) {
console.log('Modo morrendo ativado!')
} else {
console.log('Modo morrendo desativado!')
}
})
$('#traumatized').change(function () {
if (this.checked) {
console.log('Modo traumatizado ativado!')
} else {
console.log('Modo traumatizado desativado!')
}
})
$('#crazed').change(function () {
if (this.checked) {
console.log('Modo enlouquecido ativado!')
} else {
console.log('Modo enlouquecido desativado!')
}
})
$('#addWeaponForm').submit(function (event) {
var weaponType = ''
if ($('#weaponType').val() == 'fire') {
weaponType = 'Fogo'
} else if ($('#weaponType').val() == 'arch') {
weaponType = 'Arco'
} else if ($('#weaponType').val() == 'fight') {
weaponType = 'Briga'
}
const weapon = {
name: $('#weaponName').val(),
type: weaponType,
damage: $('#weapondamage').val(),
numCurrent: $('#weaponNumCurrent').val(),
numMax: $('#weaponNumMax').val(),
attack: $('#weaponAttack').val(),
reach: $('#weaponReach').val(),
defect: $('#weaponDefect').val(),
area: $('#weaponArea').val(),
}
data.weapons.push(weapon)
const id = data.weapons.length - 1
addWeaponToTable(weapon, id)
closeModal('#addWeaponModal')
event.preventDefault()
})
$('#changeLife').submit(function (event) {
let current = Number($('#lifeCurrent').val())
const max = Number($('#lifeMax').val())
if (current > max) {
alert('A vida atual não pode ser maior que a maxima!')
current = max
}
data.life.current = current
data.life.max = max
$('.lifeBar').css('width', `${calculateBar(current, max)}%`)
$('#lifeCount').text(`${current}/${max}`)
closeModal('#lifeModal')
event.preventDefault()
})
$('#changeSanity').submit(function (event) {
let current = Number($('#sanityCurrent').val())
const max = Number($('#sanityMax').val())
if (current > max) {
alert('A sanidade atual não pode ser maior que a maxima!')
current = max
}
data.sanity.current = current
data.sanity.max = max
$('.sanityBar').css('width', `${calculateBar(current, max)}%`)
$('#sanityCount').text(`${current}/${max}`)
closeModal('#sanityModal')
event.preventDefault()
})
function calculateBar(current, max) {
if (current > max) {
return 100
} else if (current < 0) {
return 0
} else {
const value = (100 / max) * current
const string = value.toString().split('.')[0]
const percentage = Number(string)
return percentage
}
}
function calcDice(ability, dice) {
// Não encontrei uma forma mais fácil, então fiz assim
const table = [
{ normal: 20 },
{ normal: 19, good: 20 },
{ normal: 18, good: 20 },
{ normal: 17, good: 19 },
{ normal: 16, good: 19, extreme: 20 },
{ normal: 15, good: 18, extreme: 20 },
{ normal: 14, good: 18, extreme: 20 },
{ normal: 13, good: 17, extreme: 20 },
{ normal: 12, good: 17, extreme: 20 },
{ normal: 11, good: 16, extreme: 20 },
{ normal: 10, good: 16, extreme: 19 },
{ normal: 9, good: 16, extreme: 19 },
{ normal: 8, good: 15, extreme: 19 },
{ normal: 7, good: 14, extreme: 19 },
{ normal: 6, good: 14, extreme: 18 },
{ normal: 5, good: 13, extreme: 18 },
{ normal: 4, good: 13, extreme: 18 },
{ normal: 3, good: 12, extreme: 18 },
{ normal: 2, good: 12, extreme: 18 },
{ normal: 1, good: 11, extreme: 17 },
]
const type = table[ability - 1]
if (type.extreme) {
if (dice >= type.extreme) return 'Extremo'
if (dice >= type.good) return 'Sucesso Bom'
if (dice >= type.normal) return 'Sucesso Normal'
if (dice <= type.normal) return 'Fracasso'
} else if (type.good) {
if (dice >= type.good) return 'Sucesso Bom'
if (dice >= type.normal) return 'Sucesso Normal'
if (dice <= type.normal) return 'Fracasso'
} else if (type.normal) {
if (dice >= type.normal) return 'Sucesso Normal'
if (dice <= type.normal) return 'Fracasso'
}
}
function rollDice(dice) {
let [count, max] = dice.split('d')
if (Number(count) && Number(max)) {
count = Number(count)
max = Number(max)
let total = 0
for (let i = 0; i < count; i++) {
total += Math.floor(Math.random() * max + 1)
}
return total
} else {
return null
}
}
function openModal(modal) {
const Modal = $(modal)
Modal.css('display', 'block')
}
function closeModal(modal) {
const Modal = $(modal)
Modal.css('display', 'none')
}
function addWeaponToTable(weapon, id) {
const newWeapon = $(`<tr id="weapon_${id}">
<td>
<button onclick="deleteWeapon(${id})">
<i class="fa fa-trash-o trashcan"></i>
</button>
${weapon.name}
</td>
<td>${weapon.type}</td>
<td>${weapon.damage}</td>
<td>${weapon.numCurrent}</td>
<td>${weapon.numMax}</td>
<td>${weapon.attack}</td>
<td>${weapon.reach}</td>
<td>${weapon.defect}</td>
<td>${weapon.area}</td>
</tr>`)
$('table#weapons').append(newWeapon)
}
function addAttribute(attribute, id) {
const newAttribute = $(`<div class="attribute" id="attribute_${id}">
<a onclick="rollAtribute('${attribute.type}', ${attribute.amount})">
<img class="attributeDice" src="./img/dado.png" alt="Dado">
</a>
<h3>${attribute.type}</h3>
<input type="text" name="appearance" value="${attribute.amount}" id="attribute_input_${id}" disabled>
</div>`)
$('#attributesList').append(newAttribute)
}
function deleteWeapon(id) {
$(`tr#${id}`).remove()
}