JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
start on deck designer
[peach-cgt.git] / client.coffee
index 95e1306..967917d 100644 (file)
@@ -44,10 +44,18 @@ transform_y = (y) ->
        return y unless state.agent is 'p2'
        return flip_y y
 
-next_card_z = -> return top_card_z += 1
+next_card_z = ->
+       top_card_z += 1
+       # p1 gets even numbers, p2 gets odd numbers
+       if state.agent is 'p1'
+               top_card_z += top_card_z % 2
+       else
+               top_card_z += 1 - (top_card_z % 2)
+
+       show_message "new z: #{top_card_z}"
+
+       return top_card_z
 
-bring_card_to_front = (card) ->
-       card.view.css "z-index": next_card_z()
 
 new_blank_card = (x, y, css_class) ->
        view = $ $ "<div class=\"blank_card #{css_class}\" style=\"left: #{transform_x x}px; top: #{transform_y y}px; z-index: 0\"></div>"
@@ -61,6 +69,9 @@ find_pile = (x, y) ->
                        return pile
        return null
 
+in_your_hand = (card) ->
+       return (not (card.pile?)) and ((transform_y card.y) < (card_height * 0.8))
+
 uninstantiate_card = (card) ->
        show_message "uninstantiate card #{card.number}"
        card.view.remove()
@@ -77,6 +88,9 @@ instantiate_card = (card) ->
        else
                card_class = 'your_card'
 
+       if in_your_hand card
+               card_class = "#{card_class} your_hand"
+
        if card.z > top_card_z
                top_card_z = card.z
 
@@ -177,7 +191,6 @@ initialize_cards = () ->
        # instantiate cards in play
        for card in state.cards
                delete card.view
-               instantiate_card card unless card.pile?
 
        unless piles?
                piles = [ # global
@@ -196,6 +209,7 @@ initialize_cards = () ->
 
        update_pile_views()
 
+# also makes sure all non-piled cards are instantiated
 update_pile_views = ->
        ps = {}
        for card in state.cards
@@ -211,6 +225,9 @@ update_pile_views = ->
                                        uninstantiate_card card
                        else
                                ps[card.pile] = { total: 1, top_card: card, top_z: card.z }
+               else
+                       # not in a pile
+                       instantiate_card card unless card.view?
 
        for pile in piles
                # where should the caption be?
@@ -244,19 +261,27 @@ init = ->
 
        state = window.game_model.new me
        state.on 'move', (agent, card, x, y, z, pile) ->
-               update_pile_views()
+               if z > top_card_z
+                       top_card_z = z
+               update_pile_views() # ensures instantiation of all visible cards
+               if @cards[card].view? # the card is visible
+                       # show it face down if it's in the other player's hand
+                       @cards[card].view.toggleClass 'your_hand', in_your_hand @cards[card]
+
                if agent is me
                        tell_server ['move', agent, card, x, y, z, pile]
                else
-                       # FIXME use the z from the server
-                       bring_card_to_front state.cards[card]
-                       state.cards[card].view.animate { left: "#{transform_x x}px", top: "#{transform_y y}px"}, 800
+                       if @cards[card].view?
+                               @cards[card].view.css "z-index": z
+                               @cards[card].view.animate { left: "#{transform_x x}px", top: "#{transform_y y}px"}, 800
        state.on 'mark', (agent, card, state) ->
-               @cards[card].view.toggleClass 'marked', state
+               if @cards[card].view?
+                       @cards[card].view.toggleClass 'marked', state
                if agent is me
                        tell_server ['mark', agent, card, state]
        state.on 'flip', (agent, card, state) ->
-               @cards[card].view.toggleClass 'flipped', state
+               if @cards[card].view?
+                       @cards[card].view.toggleClass 'flipped', state
                if agent is me
                        tell_server ['flip', agent, card, state]
        state.on 'set_cards', (agent, cards) ->