Card game services.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3133 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Andrzej Kapolka
2004-10-13 02:03:26 +00:00
parent af6409e742
commit 82cf7b0a47
15 changed files with 1534 additions and 0 deletions
@@ -0,0 +1,70 @@
//
// $Id: CardGameController.java,v 1.1 2004/10/13 02:03:26 andrzej Exp $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.parlor.card.client;
import com.threerings.crowd.data.PlaceConfig;
import com.threerings.crowd.util.CrowdContext;
import com.threerings.parlor.card.Log;
import com.threerings.parlor.card.data.CardCodes;
import com.threerings.parlor.card.data.Hand;
import com.threerings.parlor.game.GameController;
import com.threerings.presents.dobj.MessageEvent;
import com.threerings.presents.dobj.MessageListener;
/**
* A controller class for card games. Handles common functions like
* accepting dealt hands.
*/
public abstract class CardGameController extends GameController
implements MessageListener,
CardCodes
{
// Documentation inhertied
public void init(CrowdContext context, PlaceConfig config)
{
super.init(context, config);
_ctx.getClient().getClientObject().addListener(this);
}
// Documentation inherited
public void messageReceived(MessageEvent event)
{
if(event.getName().equals(TAKE_HAND))
{
handDealt((Hand)event.getArgs()[0]);
}
}
/**
* Called when the server deals the client a new hand of cards.
*
* @param hand the hand dealt to the user
*/
protected void handDealt(Hand hand)
{}
}
@@ -0,0 +1,213 @@
//
// $Id: CardPanel.java,v 1.1 2004/10/13 02:03:26 andrzej Exp $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.parlor.card.client;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import com.samskivert.util.ObserverList;
import com.threerings.media.FrameManager;
import com.threerings.media.VirtualMediaPanel;
import com.threerings.media.image.Mirage;
import com.threerings.media.sprite.Sprite;
import com.threerings.parlor.card.Log;
import com.threerings.parlor.card.data.Card;
import com.threerings.parlor.card.data.CardCodes;
import com.threerings.parlor.card.data.Deck;
import com.threerings.parlor.card.data.Hand;
/**
* Extends VirtualMediaPanel to provide services specific to rendering
* and manipulating playing cards.
*/
public abstract class CardPanel extends VirtualMediaPanel
implements CardCodes
{
/** Calls CardSpriteObserver.cardSpriteClicked. */
protected static class CardSpriteClickedOp implements ObserverList.ObserverOp
{
protected CardSprite sprite;
protected MouseEvent me;
public CardSpriteClickedOp(CardSprite sprite, MouseEvent me)
{
this.sprite = sprite;
this.me = me;
}
public boolean apply(Object observer)
{
((CardSpriteObserver)observer).cardSpriteClicked(sprite, me);
return true;
}
}
/** Calls CardSpriteObserver.cardSpriteDragged. */
protected static class CardSpriteDraggedOp implements ObserverList.ObserverOp
{
protected CardSprite sprite;
protected MouseEvent me;
public CardSpriteDraggedOp(CardSprite sprite, MouseEvent me)
{
this.sprite = sprite;
this.me = me;
}
public boolean apply(Object observer)
{
((CardSpriteObserver)observer).cardSpriteDragged(sprite, me);
return true;
}
}
/**
* Constructor.
*
* @param frameManager the frame manager
*/
public CardPanel(FrameManager frameManager)
{
super(frameManager);
addMouseListener(
new MouseAdapter()
{
public void mousePressed(MouseEvent me)
{
ArrayList al = new ArrayList();
_spritemgr.getHitSprites(al, me.getX(), me.getY());
if(al.size() > 0)
{
Iterator it = al.iterator();
int highestLayer = Integer.MIN_VALUE;
CardSprite highestSprite = null;
while(it.hasNext())
{
Sprite sprite = (Sprite)it.next();
if(sprite instanceof CardSprite)
{
CardSprite cs = (CardSprite)sprite;
if(cs.getRenderOrder() > highestLayer)
{
highestLayer = cs.getRenderOrder();
highestSprite = cs;
}
}
}
activeCardSprite = highestSprite;
if(activeCardSprite != null)
{
handleX = activeCardSprite.getX() - me.getX();
handleY = activeCardSprite.getY() - me.getY();
hasBeenDragged = false;
}
}
else
{
activeCardSprite = null;
}
}
public void mouseReleased(MouseEvent me)
{
if(activeCardSprite != null && hasBeenDragged)
{
activeCardSprite.queueNotification(
new CardSpriteDraggedOp(activeCardSprite, me)
);
}
}
public void mouseClicked(MouseEvent me)
{
if(activeCardSprite != null)
{
activeCardSprite.queueNotification(
new CardSpriteClickedOp(activeCardSprite, me)
);
}
}
}
);
addMouseMotionListener(
new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent me)
{
if(activeCardSprite != null &&
activeCardSprite.isDraggable())
{
activeCardSprite.setLocation(
me.getX() + handleX,
me.getY() + handleY
);
hasBeenDragged = true;
}
}
}
);
}
/**
* Returns the image for the back of a playing card.
*
* @return the card back image
*/
public abstract Mirage getCardBackImage();
/**
* Returns the image for the front of the specified card.
*
* @param card the desired card
* @return the card front image
*/
public abstract Mirage getCardImage(Card card);
/** The last card sprite pressed. */
private CardSprite activeCardSprite;
/** The location of the cursor in the active sprite. */
private int handleX, handleY;
/** Whether or not the active sprite has been dragged. */
private boolean hasBeenDragged;
}
@@ -0,0 +1,150 @@
//
// $Id: CardSprite.java,v 1.1 2004/10/13 02:03:26 andrzej Exp $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.parlor.card.client;
import com.threerings.media.image.Mirage;
import com.threerings.media.sprite.OrientableImageSprite;
import com.threerings.parlor.card.data.Card;
/**
* A sprite representing a playing card.
*/
public class CardSprite extends OrientableImageSprite
{
/** The panel responsible for the sprite. */
protected CardPanel _panel;
/** The depicted card. */
protected Card _card;
/** Whether or not the card is facing up. */
protected boolean _facingUp;
/** Whether or not the user can drag the card around the board. */
protected boolean _draggable;
/**
* Creates a new upward-facing card sprite.
*
* @param panel the panel responsible for the sprite
* @param card the card to depict
*/
public CardSprite(CardPanel panel, Card card)
{
_panel = panel;
_card = card;
_facingUp = true;
updateMirage();
}
/**
* Creates a new card sprite.
*
* @param panel the panel responsible for the sprite
* @param card the card to depict
* @param facingUp whether or not the card should be facing up
*/
public CardSprite(CardPanel panel, Card card, boolean facingUp)
{
_panel = panel;
_card = card;
_facingUp = facingUp;
updateMirage();
}
/**
* Sets the card to depict.
*
* @param card the new card
*/
public void setCard(Card card)
{
_card = card;
updateMirage();
}
/**
* Returns the card being depicted.
*
* @return the current card
*/
public Card getCard()
{
return _card;
}
/**
* Turns this card up or down.
*
* @param facingUp whether or not the card should be facing up
*/
public void setFacingUp(boolean facingUp)
{
_facingUp = facingUp;
updateMirage();
}
/**
* Checks whether this card is facing up or down.
*
* @return true if the card is facing up, false if facing down
*/
public boolean isFacingUp()
{
return _facingUp;
}
/**
* Sets whether or not the user can drag this card around the board.
*
* @param draggable whether or not the user can drag the card
*/
public void setDraggable(boolean draggable)
{
_draggable = draggable;
}
/**
* Checks whether or not the user can drag this card.
*
* @return true if the user can drag the card, false if not
*/
public boolean isDraggable()
{
return _draggable;
}
/**
* Updates the mirage according to the current state.
*/
private void updateMirage()
{
setMirage(_facingUp ? _panel.getCardImage(_card) : _panel.getCardBackImage());
}
}
@@ -0,0 +1,47 @@
//
// $Id: CardSpriteObserver.java,v 1.1 2004/10/13 02:03:26 andrzej Exp $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.parlor.card.client;
import java.awt.event.*;
/**
* Observer interface for (draggable) card sprites.
*/
public interface CardSpriteObserver
{
/**
* Notifies the observer that the user clicked a card sprite.
*
* @param sprite the dragged sprite
* @param me the mouse event associated with the drag
*/
public void cardSpriteClicked(CardSprite sprite, MouseEvent me);
/**
* Notifies the observer that the user dragged a card sprite to a new
* location.
*
* @param sprite the dragged sprite
* @param me the mouse event associated with the drag
*/
public void cardSpriteDragged(CardSprite sprite, MouseEvent me);
}