本文共 2398 字,大约阅读时间需要 7 分钟。
So I'm trying to make a CCG type of game using javafx (similar to Hearthstone), and I was wondering if it's a very bad idea to have one class for each card, because I've done some research and most fan made card games of Hearthstone use JSON to get the card objects. For example if the game only had 4 cards with the names:
DragonMaster
Vulcan
Pirate
Chuck Norris
I'd have 4 classes, with the names respectively. Now I though this was necessary since every card would have its own behaviour, meaning one card might do something when summoned, and in this case I have a method that gets called for that action, while others might have other methods or none at all.
Now as you can probably tell, if you have 100 cards in the game, the number of classes also gets big (and that's just for the cards package, since I'm using the MVC model, the project got really big without me realizing it).
Some other information:
All concrete card classes (e.g. Vulcan) extend an abstract Card class that contains fields such as name and cost.
Inside the cards package in the model, I have two other packages before having the actual classes, one for minions and another one for spells.
Also, If I don't have a class for each card, how can I instantiate a new specific card to add to the player's hand? Is there a different solution to this "class abundance" problem, and does this actually affect performance when the game is running or even space in the jar file?
If someone could anwser my question or any kind of help I'd be very appreciated.
解决方案
Having thousands of classes will not impact performance in any noticeable way. You can even pre-compile them ahead of time or cache them.
That said, many game engines are using a entity-component approach to represent game entities and keep things like effects as different functions.
This way you can combine different effects cards have in ways that can't be done with single-inheritance.
Below is a link that describes the approach.
You could adapt this gradually.
For example, your cards could be just one type of entity.
class Card {
int mana;
String name;
Effect[] effects;
Trigger[] triggers;
}
Effects could be pieces of code that act on the playfield and the current card.
interface Effect {
void affect(Playfield pf);
}
Triggers could be event listeners relevant to the card.
I.e. there could be a trigger that fires when the opponent plays a card.
etc.
转载地址:https://blog.csdn.net/weixin_34236334/article/details/118845673 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!