@ -31,7 +31,7 @@ const maxHandSize = 4;
const maxBoardSize = 3 ;
const maxBoardSize = 3 ;
let inspectCard = null ;
let inspectCard = null ;
let attackingCard = null ;
// Gonna need lots of refactoring, and sorting
// Gonna need lots of refactoring, and sorting
class Board {
class Board {
@ -78,9 +78,9 @@ class Board{
// Draw Invidual Cards, called by other deck stuff
// Draw Invidual Cards, called by other deck stuff
// Might be put into a card class, makes sense, eh.
// Might be put into a card class, makes sense, eh.
drawCard ( array , arrayKey , name , positionX , positionY , width , height , fill ){
drawCard ( array , arrayKey , name , positionX , positionY , width , height , fill , border ){
// Card Colour
// Card Colour
console . log ( 'drawCard card: ' + JSON . stringify ( array [ arrayKey ] ) ) ;
//console.log('drawCard card: '+JSON.stringify(array[arrayKey]));
let colourId = array [ arrayKey ] . colour ;
let colourId = array [ arrayKey ] . colour ;
if ( colourId == 0 ) { fill = '#EEE' }
if ( colourId == 0 ) { fill = '#EEE' }
else if ( colourId == 1 ) { fill = '#0033EE' }
else if ( colourId == 1 ) { fill = '#0033EE' }
@ -91,7 +91,8 @@ class Board{
y : positionY ,
y : positionY ,
width : width ,
width : width ,
height : height ,
height : height ,
fillStyle : fill
fillStyle : fill ,
strokeStyle : border
} ) ;
} ) ;
array [ arrayKey ] [ 'clickable' ] = cardClickable ;
array [ arrayKey ] [ 'clickable' ] = cardClickable ;
@ -296,8 +297,12 @@ class Board{
let positionY = canvas . height - cardHeight - 30 - ( cardHeight ) ;
let positionY = canvas . height - cardHeight - 30 - ( cardHeight ) ;
let width = cardWidth ;
let width = cardWidth ;
let height = cardHeight ;
let height = cardHeight ;
let border = false ;
this . drawCard ( playerBoard , i , name , positionX , positionY , width , height , fill ) ;
if ( attackingCard !== null && playerBoard [ i ] . name == attackingCard [ 0 ] . name ) {
border = '#FF0000' ;
}
this . drawCard ( playerBoard , i , name , positionX , positionY , width , height , fill , border ) ;
}
}
}
}
@ -373,6 +378,37 @@ class Board{
console . log ( 'inspect' ) ;
console . log ( 'inspect' ) ;
}
}
}
}
// Selects the card that will be attacking
// Stop other actions besides selecting opponent/opponent unit
// Can cancel, will do later
startAttack ( index ) {
// Can probably combine attacking/inspect, and set another array element to 'attacking', 'inspecting', etc.
attackingCard = [ playerBoard [ index ] , index ] ;
this . drawBoard ( ) ;
}
// Do the attack
makeAttack ( index ) {
// If card attacked
// Compare attackingCard and defendingCard
let defendingCard = opponentBoard [ index ] ;
if ( defendingCard . atk <= attackingCard [ 0 ] . atk ) {
opponentBoard . splice ( index , 1 ) ;
// Need to push to grave, etc. here in future too
}
if ( attackingCard [ 0 ] . atk <= defendingCard . atk ) {
playerBoard . splice ( attackingCard [ 1 ] , 1 ) ;
}
console . log ( 'attacking' ) ;
this . endAttack ( ) ;
// If player attacked
// Compare life/remove life cards 5/10/15,1/2/3?
}
endAttack ( ) {
attackingCard = null ;
this . drawBoard ( ) ;
}
}
}
// TEMP!!
// TEMP!!
@ -392,13 +428,21 @@ canvas.addEventListener('click', function(event) {
console . log ( 'EVENT LISTENER' ) ;
console . log ( 'EVENT LISTENER' ) ;
console . log ( '' ) ;
console . log ( '' ) ;
// specialEvent used to prevent eventTriggers if something specific
// is being done, attack needs to be made, inspecting card.
// Prevents user from doing other actions until completed or cancelled event
let specialEvent = false ;
if ( inspectCard !== null || attackingCard !== null ) {
specialEvent = true ;
}
var x = event . pageX - canvasLeft ,
var x = event . pageX - canvasLeft ,
y = event . pageY - canvasTop ;
y = event . pageY - canvasTop ;
// Collision detection between clicked offset and clickableItems
// Collision detection between clicked offset and clickableItems
// https://stackoverflow.com/a/9880302
// https://stackoverflow.com/a/9880302
if ( inspectCard != null ) {
if ( inspectCard != = null ) {
console . log ( inspectCard ) ;
console . log ( inspectCard ) ;
clickable = inspectCard [ 0 ] [ inspectCard [ 1 ] ] . clickable ;
clickable = inspectCard [ 0 ] [ inspectCard [ 1 ] ] . clickable ;
console . log ( clickable ) ;
console . log ( clickable ) ;
@ -422,14 +466,14 @@ canvas.addEventListener('click', function(event) {
// TODO: potentially loop all clickables, and do a check on clickable.name to differ event handling per-item
// TODO: potentially loop all clickables, and do a check on clickable.name to differ event handling per-item
// For now this will be fine, as it functions
// For now this will be fine, as it functions
if ( clickableCheck ( x , y , clickable ) ) {
if ( clickableCheck ( x , y , clickable ) && ! specialEvent ) {
board . drawACard ( ) ;
board . drawACard ( ) ;
}
}
// # OPPONENT DECK
// # OPPONENT DECK
clickable = clickableItems [ 'deckOpponentSprite' ] ;
clickable = clickableItems [ 'deckOpponentSprite' ] ;
if ( clickableCheck ( x , y , clickable ) ) {
if ( clickableCheck ( x , y , clickable ) && ! specialEvent ) {
board . drawACardOpponent ( ) ;
board . drawACardOpponent ( ) ;
}
}
@ -438,7 +482,7 @@ canvas.addEventListener('click', function(event) {
let clickable = card . clickable ;
let clickable = card . clickable ;
if ( clickableCheck ( x , y , clickable ) ) {
if ( clickableCheck ( x , y , clickable ) && ! specialEvent ) {
board . playCardToBoard ( index ) ;
board . playCardToBoard ( index ) ;
@ -455,7 +499,12 @@ canvas.addEventListener('click', function(event) {
let clickable = card . clickable ;
let clickable = card . clickable ;
if ( clickableCheck ( x , y , clickable ) ) {
if ( clickableCheck ( x , y , clickable ) ) {
board . startAttack ( index ) ;
if ( attackingCard !== null && card == attackingCard [ 0 ] ) {
board . endAttack ( ) ;
}
if ( ! specialEvent ) {
board . startAttack ( index ) ;
}
board . drawBoard ( ) ;
board . drawBoard ( ) ;
}
}
} ) ;
} ) ;
@ -465,7 +514,14 @@ canvas.addEventListener('click', function(event) {
let clickable = card . clickable ;
let clickable = card . clickable ;
if ( clickableCheck ( x , y , clickable ) ) {
if ( clickableCheck ( x , y , clickable ) ) {
board . inspectOpponentBoard ( index ) ;
// Check if card if getting attacked
if ( attackingCard !== null ) {
board . makeAttack ( index ) ;
}
if ( ! specialEvent ) {
board . inspectOpponentBoard ( index ) ;
}
board . drawBoard ( ) ;
board . drawBoard ( ) ;
}
}
} ) ;
} ) ;