Making an Card Object
by Danny Mejia · in Torque Game Builder · 09/12/2006 (9:05 am) · 3 replies
I want to make a card game. But I don't know how I should make the object in torque script. I have make the card game has text base game in C++. Know am read to make the 2D game.
This is what I have do in C++:
A. I have these classes
1.Card.
2. Hand
3. Deck
5. Player
6. House
With torque script I know that I should have fields for the for Suits and Value.
But should I make a datablock for all the cards to use or should I just set up each card by it self?
This is what I have do in C++:
A. I have these classes
1.Card.
2. Hand
3. Deck
5. Player
6. House
With torque script I know that I should have fields for the for Suits and Value.
But should I make a datablock for all the cards to use or should I just set up each card by it self?
About the author
#2
t2dScenceObjectDataBlock(Card)
{
Face = "";
Suit = "";
};
%card_2_heart =new t2dStaticSprite()
{
config = Card;
imageMap = card_2_heart;
type = 2_heart;
Face = "2";
Suit = "heart";
};
09/18/2006 (3:21 pm)
I was think about making a datablock like this but I don't know if its going to work itt2dScenceObjectDataBlock(Card)
{
Face = "";
Suit = "";
};
%card_2_heart =new t2dStaticSprite()
{
config = Card;
imageMap = card_2_heart;
type = 2_heart;
Face = "2";
Suit = "heart";
};
#3
The down side of this obviously is that you can't really use those static sprites in the designer, but I'm not sure how important that is. Like I said, I'd hold off on creating the image until it was necessary. Also, you're probably going to want to make some pretty extensive use of mounting which isn't covered here so that they are positioned where you want. And most likely you'll want to call setUseMouseEvents(true) for the static sprites since I'm pretty sure you'll want them to react to mouse events, but I'll let you figure out when and where you want to throw that in. If you don't really care, then put it right in when you create the static sprite.
Good luck!
-Andrew Douglas
theoreticalgames.com
09/18/2006 (3:47 pm)
Hmm.. that really depends on how much you want to use the designer versus just coding the cards up in script. If it were me, I'd create 4 images, 1 for each suit. You could probably get away with throwing them all into 1 image but just watch how big you get that one image. It also makes it a bit trickier to code around as you have to find the right frame number. I'd go ahead and create the backImage in the designer and give it a name in the designer like "backImage" that you can clone for any cards that need to display face down. Anyway, assuming 4 images that are already configured in your managed datablocks.cs - this is just off the top of my head so your mileage may vary://note: you would probably have a bunch of other states/layers, but that's not the point of this here code
$Card::State::FaceDown = 0;
$Card::State::FaceUp = 1;
$Layers::DefaultFaceDownLayer = 10;
$Layers::DefaultFaceUpLayer = 9;
function Card::init(%this, %face, %suit)
{
%this.face = %face;
%this.suit = %suit;
%this.state = $Card::State::FaceDown;
// Other pieces of info would include stack and stackOrder, for tracking things like if it's discarded
// and where it was in the pile.
// You could also track which player held the card if it was a multiplayer game.
// Anyway.. that's out the scope of this.
}
function Card::showImage(%this, %position)
{
if (%this.state == $Card::State::FaceUp)
{
if (! isObject(%this.frontImage))
{
%this.frontImage = new t2dStaticSprite("card_" @ %this.suit @ %this.face) {
imageMap = "card_" @ %this.suit @ ImageMap"; //assumes that you called it that in the designer.
frame = %this.face; //assumes that the face values match up to the frames in your image
size = "80.000 100.000";
position = %position;
sceneGraph = MySceneGraph; //this is whatever you've defined for the sceneGraph variable.
layer = $Layers::DefaultFaceUpLayer;
};
}
return %this.frontImage;
}
else
{
if (! isObject(%this.backImage))
{
%this.backImage = backImage.clone(true);
%this.backImage.setPosition(%position);
%this.backImage.setLayer($Layers::DefaultFaceDownLayer);
}
return %this.backImage;
}
}The down side of this obviously is that you can't really use those static sprites in the designer, but I'm not sure how important that is. Like I said, I'd hold off on creating the image until it was necessary. Also, you're probably going to want to make some pretty extensive use of mounting which isn't covered here so that they are positioned where you want. And most likely you'll want to call setUseMouseEvents(true) for the static sprites since I'm pretty sure you'll want them to react to mouse events, but I'll let you figure out when and where you want to throw that in. If you don't really care, then put it right in when you create the static sprite.
Good luck!
-Andrew Douglas
theoreticalgames.com
Torque Owner Charles "monkeyboy" Gibson
I created an imagemap of all 52 cards and dragged them into TGB. For each card I gave them two dynamic fields called "face" and "suit". "face" represented the card's value and "suit" represented the card type (i.e. hearts, clubs, spades, and diamonds).
Again, to me that smells because I had to create dynamic fields for each of the cards I pulled into TGB. The more I thought about it, I would have to do that in code as well. I am interested in hearing from someone else of an easier way.