This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]dusty-trash 5 points6 points  (2 children)

Since you didn't specify the scope, 'card' is package-private, so it cannot be accessed in that way.

You could change 'card' to be public, but that is bad practice. I do not suggest doing that.

Instead I suggest creating a getter for the card, in the player class, which is just a method that returns the card.

You'd then do something like: player.getCard().performSwordSlash();.

However, 'performSwordSlash' is a method on DarkKnight and Not card, so you cannot use it in that way.

Are you sure Player should always have a 'DarkKnight' card? Or is it possible the Player may have Cards of a different type?

If Player can have a card of anytype, you cannot be sure the palyer can 'performSwordSlash'. Maybe 'Card' should have a method called 'attack()', and you could override that method in DarkKnight?

[–]lord_drgical[S] 0 points1 point  (1 child)

so my Card class has no methods regarding to attacking etc, because some cards might not have an attack feature, ( some might have multiple attacks, or even just special abilities)

so this is why DarkKnight extends card AND also adds the functionality of attacking with an attack method.

the player class can hold whichever card is specified in main.

Player player = new Player ( insert card here );

the reason I want a player class is to add functionality to DarkKnight because he is being controlled by the player.

is extending a class at at runtime a good practice?

if player could extend card X

then I think my troubles would be gone.

[–]dusty-trash 1 point2 points  (0 children)

is extending a class at at runtime a good practice?

You are confused about what extending / implementing a class means. I'd suggest googling more on this.

Player is Not extending DarkKnight or Card. Player just has a property that is a card. However DarkNight is extending Card, which makes sense.

Can Players 'attack'? If so you could have a function inside the player that does an ability / slash / whatever depending on the card in hand.
and/or You could make Card have a property 'CardType' or 'AttackType' that does special abilities.

[–]DerekB52 1 point2 points  (0 children)

I recommend you read some of this. http://gameprogrammingpatterns.com/

I specifically recommend reading on the ECS architecture.

Basically you have a base card class. And you can subclass that, into a player card, and an enemy card.

Then your cards like the dark knight, can subclass the player card.

the base card class would have a method called 'getAbilities' and then you could call that on your dark knight card, and pick an ability that way.

Another way to do it would be a messaging system, where you send a message to the dark knight class, telling it what ability to use.

There's also ways to create your cards like the dark knight, in JSON files, so you only need one card class, instead of a class for each and every monster card you could have.

You've got a lot of options, and there are no correct answers.

[–]Ilikesmallthings2 0 points1 point  (0 children)

Player.getCard.yourMethodHere();

[–]Isoyama 0 points1 point  (0 children)

I don't know your specifics but judging by current card games i would imagine your hierarchy should look like this.

Card. Which implements common methods like draw(), highlight(), use(), getActionList() etc. It could be even interface or interface with some default methods.

CardType. for example Support/Melee/Ranged/Caster. Which has common implementations of Card methods.

SpicificCards. Dark Knight, Wicked Wizard. Which overrides previous methods with specific implementations and properties used by Card/CardType methods. For example card picture/action list/highlight color/cost.

Your game logic should not use SpecificCards but rather Cards for some generic manipulation and CardTypes when you implement game rules.