One Card! - Multiplayer Card Game
import QtQuick 2.0
import Felgo 3.0
Item {
id: depot
width: 82
height: 134
property var current
property alias effectTimer: effectTimer
property bool effect: false
property bool skipped: false
property bool clockwise: true
property int drawAmount: 1
SoundEffect {
volume: 0.5
id: skipSound
source: "../../assets/snd/skip.wav"
}
SoundEffect {
volume: 0.5
id: reverseSound
source: "../../assets/snd/reverse.wav"
}
Timer {
id: effectTimer
repeat: false
interval: 3000
onTriggered: {
effectTimer.stop()
skipped = false
var userId = multiplayer.activePlayer ? multiplayer.activePlayer.userId : 0
multiplayer.sendMessage(gameLogic.messageSetSkipped, {skipped: false, userId: userId})
console.debug("<<<< Trigger new turn after effect, clockwise: " + clockwise)
gameLogic.triggerNewTurn()
}
}
function createDepot(){
depositCard(deck.getTopCardId())
deck.cardsInStack --
}
function randomIntFromInterval(min,max)
{
return Math.floor(Math.random() * (max - min + 1) + min)
}
function depositCard(cardId){
var card = entityManager.getEntityById(cardId)
changeParent(card)
if (!multiplayer.activePlayer || multiplayer.activePlayer.connected){
card.hidden = false
}
var rotation = randomIntFromInterval(-5, 5)
var xOffset = randomIntFromInterval(-5, 5)
var yOffset = randomIntFromInterval(-5, 5)
card.rotation = rotation
card.x = xOffset
card.y = yOffset
if (!current) {
card.z = 0
}else{
card.z = current.z + 1
}
current = card
var userId = multiplayer.activePlayer ? multiplayer.activePlayer.userId : 0
if(hasEffect()){
effect = true
multiplayer.sendMessage(gameLogic.messageSetEffect, {effect: true, userId: userId})
} else {
effect = false
multiplayer.sendMessage(gameLogic.messageSetEffect, {effect: false, userId: userId})
}
}
function changeParent(card){
card.newParent = depot
card.state = "depot"
}
function hasEffect(){
if (current.variationType === "skip" ||
current.variationType === "draw2" ||
current.variationType === "wild4"){
return true
}else{
return false
}
}
function validCard(cardId){
for (var i = 0; i < playerHands.children.length; i++) {
if (playerHands.children[i].player === multiplayer.activePlayer){
if (!playerHands.children[i].inHand(cardId)) return false
}
}
var card = entityManager.getEntityById(cardId)
if (effect && current.variationType === "draw2" && card.variationType !== "draw2") return false
if (effect && current.variationType === "wild4" && card.variationType !== "wild4") return false
if (card.cardColor === current.cardColor) return true
if (card.variationType === current.variationType) return true
if (card.cardColor === "black") return true
if (current.cardColor === "black") return true
}
function cardEffect(){
if (effect){
if (current && current.variationType === "skip") {
skip()
}
} else {
skipped = false
depot.drawAmount = 1
var userId = multiplayer.activePlayer ? multiplayer.activePlayer.userId : 0
multiplayer.sendMessage(gameLogic.messageSetDrawAmount, {amount: 1, userId: userId})
}
}
function skip(){
skipSound.play()
effect = false
var userId = multiplayer.activePlayer ? multiplayer.activePlayer.userId : 0
multiplayer.sendMessage(gameLogic.messageSetEffect, {effect: false, userId: userId})
skipped = true
if (multiplayer.activePlayer && multiplayer.activePlayer.connected){
multiplayer.leaderCode(function() {
effectTimer.start()
})
}
}
function reverse(){
reverseSound.play()
clockwise ^= true
var userId = multiplayer.activePlayer ? multiplayer.activePlayer.userId : 0
multiplayer.sendMessage(gameLogic.messageSetReverse, {clockwise: clockwise, userId: userId})
}
function draw(amount){
if (drawAmount == 1) {
drawAmount = amount
} else {
drawAmount += amount
}
var userId = multiplayer.activePlayer ? multiplayer.activePlayer.userId : 0
multiplayer.sendMessage(gameLogic.messageSetDrawAmount, {amount: depot.drawAmount, userId: userId})
}
function reset(){
skipped = false
clockwise = true
drawAmount = 1
effect = false
effectTimer.stop()
}
function syncDepot(depotCardIDs, currentId, currentCardColor, skipped, clockwise, effect, drawAmount){
for (var i = 0; i < depotCardIDs.length; i++){
depositCard(depotCardIDs[i])
deck.cardsInStack --
}
depositCard(currentId)
current.cardColor = currentCardColor
depot.skipped = skipped
depot.clockwise = clockwise
depot.effect = effect