One Card! - Multiplayer Card Game
import QtQuick 2.0
import Felgo 3.0
Item {
id: deck
width: 82
height: 134
property int cardsInDeck: 108
property int cardsInStack: 108
property var cardInfo: []
property var cardDeck: []
property var types: ["zero", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine", "draw2", "skip", "reverse", "wild", "wild4"]
property var cardColor: ["yellow", "red", "green", "blue", "black"]
SoundEffect {
volume: 0.5
id: shuffleSound
source: "../../assets/snd/shuffle.wav"
}
function createDeck(){
reset()
fillDeck()
shuffleDeck()
printDeck()
}
function syncDeck(deckInfo){
reset()
for (var i = 0; i < cardsInDeck; i ++){
cardInfo[i] = deckInfo[i]
}
printDeck()
}
function fillDeck(){
var card
var order = 0
for (var i = 0; i < 4; i ++){
card = {variationType: types[0], cardColor: cardColor[i], points: 5, hidden: true, order: order}
cardInfo.push(card)
order ++
for (var j = 1; j <= 9; j ++){
for (var k = 0; k < 2; k ++){
card = {variationType: types[j], cardColor: cardColor[i], points: 5, hidden: true, order: order}
cardInfo.push(card)
order ++
}
}
for (var l = 10; l <= 12; l ++){
for (var m = 0; m < 2; m ++){
card = {variationType: types[l], cardColor: cardColor[i], points: 20, hidden: true, order: order}
cardInfo.push(card)
order ++
}
}
}
for (var n = 13; n <= 14; n ++){
for (var o = 0; o < 4; o ++){
card = {variationType: types[n], cardColor: cardColor[4], points: 50, hidden: true, order: order}
cardInfo.push(card)
order ++
}
}
}
function printDeck(){
shuffleSound.play()
var id
for (var i = 0; i < cardInfo.length; i ++){
id = entityManager.createEntityFromUrlWithProperties(
Qt.resolvedUrl("Card.qml"), {
"variationType": cardInfo[i].variationType,
"cardColor": cardInfo[i].cardColor,
"points": cardInfo[i].points,
"order": cardInfo[i].order,
"hidden": cardInfo[i].hidden,
"z": i,
"state": "stack",
"parent": deck,
"newParent": deck})
cardDeck.push(entityManager.getEntityById(id))
}
offsetStack()
}
function handOutCards(amount){
var handOut = []
for (var i = 0; i < (cardsInStack + i) && i < amount; i ++){
var index = deck.cardDeck.length - (deck.cardDeck.length - deck.cardsInStack) - 1
handOut.push(cardDeck[index])
cardsInStack --
}
passedChance()
depot.effect = false
var userId = multiplayer.activePlayer ? multiplayer.activePlayer.userId : 0
multiplayer.sendMessage(gameLogic.messageSetEffect, {effect: false, userId: userId})
return handOut
}
function passedChance(){
for (var i = 0; i < playerHands.children.length; i++) {
if (playerHands.children[i].player === multiplayer.activePlayer){
if (multiplayer.myTurn || !multiplayer.activePlayer || !multiplayer.activePlayer.connected){
playerHands.children[i].onu = false
var userId = multiplayer.activePlayer ? multiplayer.activePlayer.userId : 0
multiplayer.sendMessage(gameLogic.messagePressONU, {userId: userId, onu: false})
}
}
}
}
function shuffleDeck(){
for (var i = cardInfo.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1))
var temp = cardInfo[i]
cardInfo[i] = cardInfo[j]
cardInfo[j] = temp
}
cardsInStack = cardsInDeck
}
function reset(){
var toRemoveEntityTypes = ["card"]
entityManager.removeEntitiesByFilter(toRemoveEntityTypes)
while(cardDeck.length) {
cardDeck.pop()
cardInfo.pop()
}
cardsInStack = cardsInDeck
for (var i = 0; i < playerHands.children.length; i++) {
playerHands.children[i].reset()
}
}
function getTopCardId(){
reStack()
var index = Math.max(cardDeck.length - (cardDeck.length - cardsInStack) - 1, 0)
return deck.cardDeck[index].entityId
}
function offsetStack(){
for (var i = 0; i < cardDeck.length; i++){
if (cardDeck[i].state == "stack"){
cardDeck[i].y = i * (-0.1)
}
}
}
function markStack(){
if (cardDeck.length <= 0) return
var card = entityManager.getEntityById(getTopCardId())
card.glowImage.visible = true
}
function unmark(){
if (cardDeck.length <= 0) return
var card = entityManager.getEntityById(getTopCardId())
card.glowImage.visible = false
}
function reStack(){
var cardIds = []
if (cardsInStack <= 1){
for (var i = 0; i < cardDeck.length; i ++){
if (cardDeck[i].state === "depot" && cardDeck[i].entityId !== depot.current.entityId){
cardIds.push(cardDeck[i].entityId)
}
}
for (var j = 0; j < cardIds.length; j++) {
for (var k = 0; k < cardDeck.length; k ++){
if (cardDeck[k].entityId == cardIds[j]){
if(cardDeck[k].variationType == "wild" || cardDeck[k].variationType == "wild4"){
cardDeck[k].cardColor = "black"
}
cardDeck[k].hidden = true
cardDeck[k].newParent = deck
cardDeck[k].state = "stack"
moveElement(k, 0)
cardsInStack ++
break
}
}
}
}
offsetStack()
}
function moveElement(from, to){
cardDeck.splice(to,0,cardDeck.splice(from,1)[0])