问题
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.
回答1:
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.
https://en.wikipedia.org/wiki/Entity%E2%80%93component%E2%80%93system
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.
回答2:
I am no expert in this field, but I think while your approach would work, it could make maintainability and scaling more difficult.
Another alternative you can try is to store the characteristics of your cards in a database (or some kind of properties file). In this case, you just need a common Card
class, and it loads its values from database.
class Card {
int id;
String name;
int cost;
int speed;
OnSummonEffect summonEffect;
}
interface OnSummonEffect { ... }
abstract class StatBoostEffect implements OnSummonEffect { ... }
class AttackBoostEffect extends StatBoostEffect { ... }
Card Table
CardID Name Cost Speed SummonEffect
1 DragonMaster 5 10 com.foo.bar.AttackBoostEffect
2 Vulcan 3 12 com.foo.bar.SomeSummonEffect
Although saving the class name may not sound elegant (and it probably has its own side effects), but this approach makes it easy to add new things.
Adding a new card type is simply adding a new entry to the database table, and potentially adding new "effect" class to the project. If you decide to change some attributes (or add new attributes) to the card, you just need to add/change column in database table and then that single Card
class. If you made one class for each card type, you would have to change the base card, then change every single card class.
来源:https://stackoverflow.com/questions/47047039/does-having-many-classes-worsens-performance-in-java