#card sorting in hands
1 messages · Page 1 of 1 (latest)
Ok so here is the code to which I am adding comments. Then you can ask questions... ```lua
function function sortCardsInHand()
local handTable = assignedPlayer_[1].getHandObjects()
table.sort(handTable, function(card1, card2) return cardTable[card1.getGMNotes()].val <
cardTable[card2.getGMNotes()].val end
local pht = assignedPlayer_[1].getHandTransform() -- the handTransform is how Unity hence TTS describes a player hand in the 3D world. Details are in the API doc, so I will encourage you to look there.
local loc = pht.position -- loc is the center of the hand.
local rt = pht.right -- rt is the offset vector to step one unity unit to toward the right hand end of the hand.
local offSet = pht.scale.x / ( 2 * #handTable)
-- scale is the size vector, and hence scale.x is the width of the hand from the hand point of view
-- I divide by 2 to get the distance from the center to the right end, and by the number of cards to get
-- the "slot size" for each card. (again, from the hand point of view)
for i, c in ipairs(handTable) do -- now loop through the list of cards,
c.setPosition(loc) -- and set the position of the current card to the current slot starting at the center of the hand
loc.x = loc.x + offSet * rt.x -- adjust the slot position in global position space
loc.z = loc.z + offSet * rt.z -- that is move loc toward the right of the hand one slot
end -- at this point all the cards have been sorted and placed in order into the right half of the hand.
-- finally, TTS gets control of things, and automatically spreads the cards out to fill the hand,
-- resulting in the visual sorting effect.
end
Please let me know if this is actually helpful!
Right, this is helpful indeed, although I could figure out generally what things do just by looking at the code (I have fiddled with Unity scripts before), but with the extra comment/explanation it certainly helps get the idea of it! is there a particular reason as to why it has to start by placing the first card from the middle?
Nope, you can just calculate loc from position-scale/2 *rt and don't divide offset by two. I was just too lazy to work it out when I wrote it late in my day. I do like the way it looks though; I think visually the larger spread wouldn't look as good. All three are vectors, and the arithmetic should work. There MIGHT be some edge issues though.
I see, well the game is played with 13 cards in the hand and by 6 players minimum so it should be alright
card sorting in hands
nice!
I noticed that my change also changed your request to change it. Looks weird now 🙂