JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
hide other players hand
[peach-cgt.git] / client.coffee
index f47f0a1..139158d 100644 (file)
@@ -1,6 +1,8 @@
 # globals
 $table = null
+table_width = 0
 table_height = 0
+card_width = 0
 card_height = 0
 state = null
 server_url = null
@@ -33,8 +35,11 @@ new_button = (text) -> $ $ "<div class=\"button\">#{text}</div>"
 
 # transform coordinates from client-side coords to server-side coords (or back)
 # this makes it so p2 view everything upside down (mirrored), but still sends coords rightside up
+flip_x = (x) -> table_width - card_width - x
 flip_y = (y) -> table_height - card_height - y
-transform_x = (x) -> x
+transform_x = (x) ->
+       return x unless state.agent is 'p2'
+       return flip_x x
 transform_y = (y) ->
        return y unless state.agent is 'p2'
        return flip_y y
@@ -44,11 +49,21 @@ next_card_z = -> return top_card_z += 1
 bring_card_to_front = (card) ->
        card.view.css "z-index": next_card_z()
 
-new_blank_card = (x, y) ->
-       view = $ $ "<div class=\"blank_card\" style=\"left: #{transform_x x}px; top: #{transform_y y}px; z-index: 0\"></div>"
+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>"
        $table.append view
        return view
 
+find_pile = (x, y) ->
+       fudge = 40
+       for pile in piles
+               if -fudge < pile.x - x < fudge and -fudge < pile.y - y < fudge
+                       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()
@@ -65,6 +80,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
 
@@ -93,8 +111,15 @@ instantiate_card = (card) ->
                update_pile_views()
        view.bind 'dragstop', (event, ui) ->
                p = view.position()
-               pile = null # FIXME figure out what pile we moved to
-               state.move state.agent, card.number, transform_x(p.left), transform_y(p.top), card.z, pile
+               x = transform_x(p.left)
+               y = transform_y(p.top)
+               pile = find_pile x, y
+               if pile?
+                       x = pile.x
+                       y = pile.y
+                       pile = pile.key
+                       view.css {left: transform_x(x), top: transform_y(y)}
+               state.move state.agent, card.number, x, y, card.z, pile
 
 error_lag = 3
 
@@ -158,21 +183,25 @@ initialize_cards = () ->
        # instantiate cards in play
        for card in state.cards
                delete card.view
-               instantiate_card card unless card.pile?
 
        unless piles?
                piles = [ # global
-                       {key: 'p2_draw', x: transform_x(140), y: transform_y(20), name: "Draw Pile"}
-                       {key: 'p2_discard', x: transform_x(20), y: transform_y(20), name: "Discard Pile"}
-                       {key: 'p1_draw', x: transform_x(140), y: transform_y(flip_y(20)), name: "Draw Pile"}
-                       {key: 'p1_discard', x: transform_x(20), y: transform_y(flip_y(20)), name: "Discard Pile"}
+                       {key: 'p2_draw', x: 140, y: 20, name: "Draw Pile"}
+                       {key: 'p2_discard', x: 20, y: 20, name: "Discard Pile"}
+                       {key: 'p1_draw', x: flip_x(140), y: flip_y(20), name: "Draw Pile"}
+                       {key: 'p1_discard', x: flip_x(20), y: flip_y(20), name: "Discard Pile"}
                ]
                for pile in piles
-                       pile.$blank = new_blank_card pile.x, pile.y
+                       if pile.key.substr(0, 2) is state.agent
+                               css_class = 'my_card'
+                       else
+                               css_class = 'your_card'
+                       pile.$blank = new_blank_card pile.x, pile.y, css_class
                        pile.$caption = $ $ "<div class=\"pile_caption\"><div>#{pile.name}:</div><div class=\"n_cards\">#{n_cards 0}</div></div>"
 
        update_pile_views()
 
+# also makes sure all non-piled cards are instantiated FIXME rename
 update_pile_views = ->
        ps = {}
        for card in state.cards
@@ -184,8 +213,13 @@ update_pile_views = ->
                                                uninstantiate_card ps[card.pile].top_card
                                        ps[card.pile].top_card = card
                                        ps[card.pile].top_z = card.z
+                               else if card.view
+                                       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?
@@ -220,10 +254,14 @@ init = ->
        state = window.game_model.new me
        state.on 'move', (agent, card, x, y, z, pile) ->
                update_pile_views()
+               if state.cards[card].view? # the card is visible
+                       # show it face down if it's in the other player's hand
+                       state.cards[card].view.toggleClass 'your_hand', in_your_hand state.cards[card]
+
                if agent is me
                        tell_server ['move', agent, card, x, y, z, pile]
                else
-                       # FIXME should we use the z from the server? Should p1 use odd numbers and p2 even?
+                       # 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
        state.on 'mark', (agent, card, state) ->
@@ -247,7 +285,9 @@ init = ->
 
 $ ->
        $table = $ '#table'
+       table_width = $table.width()
        table_height = $table.height()
+       card_width = $('#loading-card').outerWidth()
        card_height = $('#loading-card').outerHeight()
 
        init()