forked from j-pandeirada/tunicounter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
541 lines (480 loc) · 17.6 KB
/
index.js
File metadata and controls
541 lines (480 loc) · 17.6 KB
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
import Card from './card.js'
import Dice from './dice.js'
import LifeCounter from './lifecounter.js'
import gameZone from './gamezone.js'
import gameObject from './gameObject.js'
import animations from './animations.js'
var config = {
type: Phaser.AUTO,
width: 1600,
height: 1000,
draggable: true,
scene: {
preload: preload,
create: create,
update: update
},
}
var game = new Phaser.Game(config)
function preload ()
{
this.load.image("bg", 'assets/FABPlayMat.webp')
this.load.image("cardback", "assets/back.png")
this.load.image("ARC000", "assets/ARC000.png")
this.load.image("CRU000-CF", "assets/CRU000-CF.png")
this.load.image("ELE000-CF", "assets/ELE000-CF.png",)
this.load.image("EVR000-CF", "assets/EVR000-CF.png",)
this.load.image("MON000-CF", "assets/MON000-CF.png",)
this.load.image("UPR000", "assets/UPR000.png",)
this.load.image("WTR000-CF", "assets/WTR000-CF.png",)
this.load.image("UPR182-CF", "assets/UPR182-CF.png",)
this.load.image("WTR150-CF", "assets/WTR150-CF.png",)
this.load.image("endTurn", "assets/endTurn.png",)
this.load.image("0", "assets/0.png",)
this.load.image("1", "assets/1.png",)
this.load.image("2", "assets/2.png",)
this.load.image("3", "assets/3.png",)
this.load.image("4", "assets/4.png",)
this.load.spritesheet("dice", "assets/dice_sheet.png", { frameWidth: 128, frameHeight: 128 })
this.load.spritesheet("nums", "assets/nums.png", { frameWidth: 314, frameHeight: 500 })
this.load.spritesheet("glowHover", "assets/glowRed.png", { frameWidth: 586, frameHeight: 802 })
this.load.spritesheet("glowTint", "assets/glowOrange.png", { frameWidth: 586, frameHeight: 802 })
this.load.spritesheet("glowSelection", "assets/glowBlue.png", { frameWidth: 586, frameHeight: 802 })
this.load.spritesheet("glowSink", "assets/glowGreen.png", { frameWidth: 586, frameHeight: 802 })
}
var keys, numbers
const cardZoneSize = [114, 160]
const cardSize = [109, 152]
function create ()
{
/* Private Variables */
var objectTag = 0
/* Public Variables*/
this.clickDuration = 0
this.zones = []
this.cardPiles = new Map()
this.cards_on_board = []
this.selectedCards = new Map()
this.drawingBox = false
this.isBdown = false
this.keyEvent
this.newKeyDown
this.newKeyUp
this.longclicked = false
/* Environment objects */
this.bg = this.add.image(1280/2-100, 720/2-35, 'bg').setScale(1.5)
this.endTurn = new gameObject(this, 859, 470, 'endTurn').setScale(0.5).setInteractive()
this.selectionBox = this.add.rectangle(0,0,0,0).setStrokeStyle(2, 0x962726, 1).setOrigin(0,0)
this.bounds = [this.bg.x+this.bg.displayWidth, this.bg.y+this.bg.displayHeight]
/* Glow effects */
animations.createGlowEffect(this, 'glowHover', 'glowHover')
animations.createGlowEffect(this, 'glowTint', 'glowTint')
animations.createGlowEffect(this, 'glowSelection', 'glowSelection')
animations.createGlowEffect(this, 'glowSink', 'glowSink')
/* Game objects */
this.dice = new Dice(this,200,200, 'dice')
this.lifecounter = new LifeCounter(this, 420, 590)
/* Keyboard inputs */
keys = this.input.keyboard.addKeys('T,F,R,S,P,G,B,D,NUMPAD_ADD,NUMPAD_SUBTRACT')
numbers = this.input.keyboard.addKeys('ZERO,ONE,TWO,THREE,FOUR,FIVE,SIX,SEVEN,EIGHT,NINE')
/* Game Zones */
this.zones.push(new gameZone(this, 92, 228-35, cardZoneSize[0], cardZoneSize[1], "head"))
this.zones.push(new gameZone(this, 92, 398-35, cardZoneSize[0], cardZoneSize[1], "chest"))
this.zones.push(new gameZone(this, 220, 398-35, cardZoneSize[0], cardZoneSize[1], "arms"))
this.zones.push(new gameZone(this, 92, 569-35, cardZoneSize[0], cardZoneSize[1], "legs"))
this.zones.push(new gameZone(this, 410, 398-35, cardZoneSize[0], cardZoneSize[1], "weapon1"))
this.zones.push(new gameZone(this, 539, 398-35, cardZoneSize[0], cardZoneSize[1], "hero"))
this.zones.push(new gameZone(this, 666, 398-35, cardZoneSize[0], cardZoneSize[1], "weapon2"))
this.zones.push(new gameZone(this, 539, 569-35, cardZoneSize[0], cardZoneSize[1], "arsenal"))
this.zones.push(new gameZone(this, 988, 228-35, cardZoneSize[0], cardZoneSize[1], "grave"))
this.zones.push(new gameZone(this, 988, 398-35, cardZoneSize[0], cardZoneSize[1], "deck"))
this.zones.push(new gameZone(this, 859, 398-35, cardZoneSize[0], cardZoneSize[1], "pitch"))
this.zones.push(new gameZone(this, 988, 569-35, cardZoneSize[0], cardZoneSize[1], "banished"))
//var board = new gameZone(this, this.bg.displayWidth/2, this.bg.displayHeight/2, this.bg.displayWidth, this.bg.displayHeight, "board")
//this.children.sendToBack(board)
//this.zones.push(board)
/*
this.input.on('pointerdown', function(pointer, currentlyOver) {
if(this.scene.endTurn.pointerover){
pitchToDeck(this.scene)
}
// Set origin position for selection box
var onTopOf = ""
try{
onTopOf = currentlyOver[0].type
}
catch{}
if(onTopOf != "card" && onTopOf != "dice" && onTopOf != "lifecounter" && onTopOf != "object" && onTopOf != ""){
this.scene.drawingBox = true
this.scene.selectionBox.setPosition(pointer.x,pointer.y)
this.scene.selectedCards.clear()
}
else{
this.drawingBox = false
}
console.log(this.scene.cardPiles)
})
this.input.on('pointerup', function(pointer, currentlyOver) {
// Reset selection box size
this.scene.selectionBox.setSize(0, 0)
this.scene.drawingBox = false
})
*/
this.input.keyboard.on('keydown', function (event) {
this.scene.keyEvent = event.key
this.scene.newKeyDown = true
})
this.input.keyboard.on('keyup', function (event) {
this.scene.keyEvent = event.key
this.scene.newKeyUp = true
})
/*
this.input.on('dragend', function (pointer, gameObject, dropped) {
if(gameObject.type == "card")
if(!dropped){
snapCardToBoard(this.scene, gameObject)
}
})
*/
/*
this.GOD = function(card, placeOnTop){
// Remove from previous list
if(this.cardPiles.has(card.previousZone)){
var list = this.cardPiles.get(card.previousZone)
var idx = list.indexOf(card.objectTag)
if(idx >= 0 ){ // Por algum motivo isto estava a ser -1 às vezes e o splice dava merda
list.splice(idx,1)
}
if (list.length === 0){
this.cardPiles.delete(card.previousZone)
}
}
// Add to new (existing) list
if(this.cardPiles.has(card.zoneTag)){
let list = this.cardPiles.get(card.zoneTag)
if(placeOnTop){
list.push(card.objectTag)
//this.cardPiles.set(card.zoneTag, list)
}
else{
list.unshift(card.objectTag)
}
this.cardPiles.set(card.zoneTag, list)
orderPileVisually(this, list)
}
// Create a new list
else{
this.cardPiles.set(card.zoneTag, [card.objectTag])
let list = this.cardPiles.get(card.zoneTag)
orderPileVisually(this, list)
}
card.previousZone = card.zoneTag
}
*/
//var cardToSpawn = ['ARC000', 'ELE000-CF', 'EVR000-CF', 'MON000-CF', 'UPR000', 'WTR000-CF', 'CRU000-CF']
var cardToSpawn = ['0', '1', '2', '3', '4']
for (let index = 0; index < 1; index++) {
for(var cardName of cardToSpawn){
var card = new Card(this, 718/2, 420/2, cardName, 'cardback', "glow", "deck", (objectTag++).toString())
this.cards_on_board.push(card)
//this.GOD(card, true)
}
}
for(var card of cardToSpawn){
//this.cards_on_board.push(new Card(this, 718/2, 420/2, card, 'cardback', "glow", "pitch", (objectTag++).toString()))
}
//this.cards_on_board.push(new Card(this, 718/2, 420/2, 'UPR182-CF', 'cardback', "glow", "head", (objectTag++).toString()))
//this.cards_on_board.push(new Card(this, 718/2, 420/2, 'WTR150-CF', 'cardback', "glow", "chest", (objectTag++).toString()))
}
function update (time)
{
//console.log(this.input.activePointer.x, this.input.activePointer.y)
var active_card = checkHoveredCard(this.cards_on_board)
keyboardHandler(this, active_card)
}
function checkHoveredCard(cards){
var return_card = false
var card
for (card of cards){
if (card.pointerover){
return_card = card
break
}
}
return return_card
}
/*
function flipPile(scene, active_card){
var cardPile = scene.cardPiles.get(active_card.zoneTag)
for(var card of cardPile.reverse()){
if(scene.cards_on_board[+card].texture.key != scene.cards_on_board[+card].cardback){
animations.flipCard(scene, scene.cards_on_board[+card], function(){scene.children.bringToTop(scene.cards_on_board[+card])})
}
else{
scene.children.bringToTop(scene.cards_on_board[+card])
animations.flipCard(scene, scene.cards_on_board[+card], function(){})
}
}
}
function shufflePile(scene, zoneTag){
var list = scene.cardPiles.get(zoneTag)
for (let i = list.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1))
const temp = list[i]
list[i] = list[j]
list[j] = temp
}
orderPileVisually(scene, list)
}
function orderPileVisually(scene, cardPile){
for(var cardIdx of cardPile){
scene.children.bringToTop(scene.cards_on_board[+cardIdx])
}
}
*/
/*
function pitchToDeck(scene){
var zone, card
var cardPile = scene.cardPiles.get("pitch")
if(cardPile){
cardPile = cardPile.slice()
}
else{
return
}
// Grab zone object
for(zone of scene.zones){
if(zone.zoneTag == "deck"){
break
}
}
// Move cards to deck zone
for(var cardIdx of cardPile){
card = scene.cards_on_board[+cardIdx]
animations.flipAndMoveCardToZone(scene, card, zone.x, zone.y, zone.zoneTag)
scene.GOD(card, false) // Place on bottom
}
// Reorder deck visually
var cardPile = scene.cardPiles.get("deck")
orderPileVisually(scene, cardPile)
}
function snapCardToBoard(scene, card){
var cardPile = scene.cardPiles.get(card.zoneTag)
var cardinPile
for(var cardIdx of cardPile){
cardinPile = scene.cards_on_board[+cardIdx]
cardinPile.updatePosition(card.input.dragStartX, card.input.dragStartY, card.zoneTag)
}
}
*/
/*
function groupSelectedCards(scene){
var zoneTag = scene.selectedCards[0].objectTag
var newX, newY
for (var zone of scene.zones) {
// Bounds verifications to see if cards are being placed in a zone
var ver1 = (zone.border.x-zone.border.width/2 <= scene.input.activePointer.x)
var ver2 = (zone.border.x+zone.border.width/2 >= scene.input.activePointer.x)
var ver3 = (zone.border.y-zone.border.height/2 <= scene.input.activePointer.y)
var ver4 = (zone.border.y+zone.border.height/2 >= scene.input.activePointer.y)
// Get new XY position for cards
if(zone.zoneTag != "board"){
if(ver1 && ver2 && ver3 && ver4){
newX = zone.x
newY = zone.y
break
}
else{
// Por algum motivo o bounds aqui não corresponde ao tamanho do campo
if(scene.input.activePointer.x-cardSize[0]/2 < 0){
newX = cardSize[0]/2+20
}
else if(scene.input.activePointer.x+cardSize[0]/2 > scene.bounds[0]) {
//console.log("this")
newX = scene.bounds[0]-20
}
else{
newX = scene.input.activePointer.x
}
if(scene.input.activePointer.y-cardSize[1]/2 < 0){
newY = cardSize[1]/2+20
}
else if(scene.input.activePointer.y+cardSize[1]/2 > scene.bounds[1]){
newY = scene.bounds[1]-20
}
else{
newY = scene.input.activePointer.y
}
}
}
}
// Update cardPiles
for (var card of scene.selectedCards) {
card.setGlowEffect(0)
animations.moveCardToPosition(scene, card, newX, newY)
if(zone.zoneTag != "board"){
card.zoneTag = zone.zoneTag
}
else{
card.zoneTag = zoneTag
}
scene.GOD(card, true)
}
}
*/
/*
function spreadPile(scene, selectedCards){
var originX = scene.input.activePointer.x
var originY = scene.input.activePointer.y
var offsetX = 0
var offsetY = 0
// Check if too close to bounds
if(originX-cardSize[0]/2 < 0){
originX = cardSize[0]/2+20
}
else if(originX+cardSize[0]/2 > scene.bounds[0]) {
originX = scene.bounds[0]-20
}
if(originY-cardSize[1]/2 < 0){
originY = cardSize[1]/2+20
}
else if(originY+cardSize[0]/2 > scene.bounds[1]){
originY = scene.bounds[1]-20
}
for(var card of selectedCards){
card.zoneTag=card.objectTag
animations.moveCardToPosition(scene, card, originX+offsetX, originY+offsetY)
scene.GOD(card, true)
card.setGlowEffect(0)
scene.children.bringToTop(scene.cards_on_board[+card])
card.selected = false
// Calculate position of next card
offsetX+=0.33*card.displayWidth
if(originX+offsetX+card.displayWidth/2 > scene.bg.displayWidth){
offsetX = 0
offsetY += 0.66*card.displayHeight
}
}
}
function updateSelectionBox(scene, pointer){
if(!scene.drawingBox){
return
}
if(pointer.x <= scene.selectionBox.x){
scene.selectionBox.width += scene.selectionBox.x - pointer.x
scene.selectionBox.x = pointer.x
}
else{
var oldX = scene.selectionBox.x
scene.selectionBox.x = pointer.x
scene.selectionBox.width += oldX-scene.selectionBox.x
}
if(pointer.y <= scene.selectionBox.y){
scene.selectionBox.height += scene.selectionBox.y - pointer.y
scene.selectionBox.y = pointer.y
}
else{
var oldY = scene.selectionBox.y
scene.selectionBox.y = pointer.y
scene.selectionBox.height += oldY - scene.selectionBox.y
}
scene.selectionBox.setPosition(scene.selectionBox.x, scene.selectionBox.y)
scene.selectionBox.setSize(scene.selectionBox.width, scene.selectionBox.height)
for (var card of scene.cards_on_board) {
if(Phaser.Geom.Rectangle.ContainsRect(scene.selectionBox.getBounds(), card.getBounds())){ //Top left
card.selected = true
}
else{
card.selected = false
}
}
}
function checkSelectedCards(scene){
var selectedCards = []
for (var card of scene.cards_on_board) {
if(card.selected){
selectedCards.push(card)
}
}
scene.selectedCards = selectedCards
}
*/
/*
function clickTimer(scene){
if(scene.input.activePointer.isDown){
scene.clickDuration++
}
else{
scene.clickDuration = 0
}
}
function longClickHandler(scene, active_card){
if (active_card != false && scene.clickDuration > 25){
scene.longclicked = true
if(!scene.isBdown){
//sink glow has privilege over selection glow
active_card.setGlowEffect('glowSelection')
}
//Am i moving a pile or not?
//get x,y position of pile (last card of pile always works)
var aux_card = scene.cards_on_board[+scene.cardPiles.get(active_card.zoneTag)[0]]
if(aux_card.getBounds().contains(scene.input.x, scene.input.y)){
//i am moving a pile, all cards in pile are selected
for(var cardIdx of scene.cardPiles.get(active_card.zoneTag)){
if(!scene.selectedCards.has(cardIdx)){
scene.selectedCards.set(cardIdx,scene.cards_on_board[cardIdx])
}
else{
scene.selectedCards.delete(cardIdx)
scene.selectedCards.set(cardIdx,scene.cards_on_board[cardIdx])
}
}
}
//do nothing more , drag does everything you need :)
}
}
*/
function keyboardHandler(scene, active_card){
if(scene.newKeyDown){
var key = scene.keyEvent.toLowerCase()
switch (key) {
case 't':
active_card.tap()
break
case 'f':
active_card.flip()
break
case 's':
active_card.scry()
break
case 'r':
break
case 'p':
if(active_card != false && active_card.zoneTag === "pitch"){
//pitchToDeck(scene)
}
break
default:
break
}
scene.newKeyDown=false
}
if(scene.newKeyUp){
var key = scene.keyEvent.toLowerCase()
switch (key) {
case 's':
if (active_card != false){
active_card.unscry()
}
break
case 'b':
scene.isBdown = false
if (active_card != false && active_card.selected){
active_card.setGlowEffect('glowHover')
}
default:
break
}
scene.newKeyUp=false
}
}