One Card! - Multiplayer Card Game
import QtQuick 2.0
import Felgo 3.0
Item {
id: playerHand
width: 400
height: 134
property double zoom: 1.0
property int originalWidth: 400
property int originalHeight: 134
property int start: 7
property var hand: []
property var player: MultiplayerUser{}
property bool onu: false
property int score: 0
property double offset: width/10
SoundEffect {
volume: 0.5
id: drawSound
source: "../../assets/snd/draw.wav"
}
SoundEffect {
volume: 0.5
id: depositSound
source: "../../assets/snd/deposit.wav"
}
SoundEffect {
volume: 0.5
id: winSound
source: "../../assets/snd/win.wav"
}
Image {
id: playerHandImage
source: multiplayer.activePlayer === player && !gameLogic.acted? "../../assets/img/PlayerHand2.png" : "../../assets/img/PlayerHand1.png"
width: parent.width / 400 * 560
height: parent.height / 134 * 260
anchors.horizontalCenter: parent.horizontalCenter
anchors.bottom: parent.bottom
anchors.bottomMargin: parent.height * (-0.5)
z: 0
smooth: true
onSourceChanged: {
z = 0
neatHand()
}
}
Image {
anchors.bottom: parent.bottom
anchors.horizontalCenter: parent.horizontalCenter
source: "../../assets/img/Blocked.png"
width: 170
height: width
z: 100
visible: depot.skipped && multiplayer.activePlayer == player
smooth: true
}
Image {
anchors.top: parent.top
anchors.right: parent.right
source: "../../assets/img/Bubble.png"
rotation: parent.rotation * (-1)
width: 60
height: width
z: 100
visible: onu
smooth: true
}
function startHand(){
pickUpCards(start)
}
function reset(){
while(hand.length) {
hand.pop()
}
onu = false
scaleHand(1.0)
}
function neatHand(){
hand.sort(function(a, b) {
return a.order - b.order
})
offset = originalWidth * zoom / 10
if (hand.length > 7){
offset = playerHand.originalWidth * zoom / hand.length / 1.5
}
for (var i = 0; i < hand.length; i ++){
var card = hand[i]
var handAngle = 40
var cardAngle = handAngle / hand.length * (i + 0.5) - handAngle / 2
var handWidth = offset * (hand.length - 1) + card.originalWidth * zoom
var cardX = (playerHand.originalWidth * zoom - handWidth) / 2 + (i * offset)
card.rotation = cardAngle
card.y = Math.abs(cardAngle) * 1.5
card.x = cardX
card.z = i + 50 + playerHandImage.z
}
}
function pickUpCards(amount){
onuButton.button.enabled = false
var pickUp = deck.handOutCards(amount)
for (var i = 0; i < pickUp.length; i ++){
hand.push(pickUp[i])
changeParent(pickUp[i])
if (multiplayer.localPlayer == player){
pickUp[i].hidden = false
}
drawSound.play()
}
neatHand()
}
function syncHand(cardIDs) {
hand = []
for (var i = 0; i < cardIDs.length; i++){
var tmpCard = entityManager.getEntityById(cardIDs[i])
hand.push(tmpCard)
changeParent(tmpCard)
deck.cardsInStack --
if (multiplayer.localPlayer == player){
tmpCard.hidden = false
}
drawSound.play()
}
neatHand()
}
function changeParent(card){
card.newParent = playerHand
card.state = "player"
}
function inHand(cardId){
for (var i = 0; i < hand.length; i ++){
if(hand[i].entityId === cardId){
return true
}
}
return false
}
function removeFromHand(cardId){
for (var i = 0; i < hand.length; i ++){
if(hand[i].entityId === cardId){
hand[i].width = hand[i].originalWidth
hand[i].height = hand[i].originalHeight
hand.splice(i, 1)
depositSound.play()
neatHand()
return
}
}
}
function markValid(){
if (!depot.skipped && !gameLogic.gameOver && !colorPicker.chosingColor){
for (var i = 0; i < hand.length; i ++){
if (depot.validCard(hand[i].entityId)){
hand[i].glowImage.visible = true
hand[i].updateCardImage()
}else{
hand[i].glowImage.visible = false
hand[i].saturation = -0.5
hand[i].lightness = 0.5
}
}
var validId = randomValidId()
if(validId == null){
deck.markStack()
}
}
}
function unmark(){
for (var i = 0; i < hand.length; i ++){
hand[i].glowImage.visible = false
hand[i].updateCardImage()
}
}
function scaleHand(scale){
zoom = scale
playerHand.height = playerHand.originalHeight * zoom
playerHand.width = playerHand.originalWidth * zoom
for (var i = 0; i < hand.length; i ++){
hand[i].width = hand[i].originalWidth * zoom
hand[i].height = hand[i].originalHeight * zoom
}
neatHand()
}
function randomValidId(){
var valids = getValidCards()
if (valids.length > 0){
var randomIndex = Math.floor(Math.random() * (valids.length))
return valids[randomIndex].entityId
}else{
return null
}
}
function getValidCards(){
var valids = []
for (var i = 0; i < hand.length; i ++){
if (depot.validCard(hand[i].entityId)){
valids.push(entityManager.getEntityById(hand[i].entityId))
}
}
return valids
}
function missedOnu(){
if (hand.length === 0 && !onu){
if (multiplayer.myTurn) onuButton.button.enabled = false
return true
} else {
return false
}
}
function closeToWin(){
if (hand.length == 2){
var userDisconnected = (!multiplayer.activePlayer || !multiplayer.activePlayer.connected)
if ((!onuButton.visible || userDisconnected) && !depot.skipped && !onu && !gameLogic.gameOver){
var userId = multiplayer.activePlayer ? multiplayer.activePlayer.userId : 0
onuButton.onu(userId)
}
var valids = getValidCards()
if (multiplayer.myTurn && !depot.skipped && !gameLogic.gameOver && !onu && valids.length > 0){
onuButton.button.enabled = true
} else if (multiplayer.myTurn){
onuButton.button.enabled = false
}
return true
}else if (multiplayer.myTurn){
onuButton.button.enabled = false
return false
}
}
function checkWin(){
if (hand.length == 0 && onu){
winSound.play()
return true
}else{
return false
}
}
function points(){
var points = 0
for (var i = 0; i < hand.length; i++) {
points += hand[i].points
}
return points
}
Behavior on width {
NumberAnimation { easing.type: Easing.InOutQuad; duration: 400 }
}
Behavior on height {