samedi 18 mars 2017

Implementing cards and effects in a trading card game using Java reflection

I am trying to develop a computer card game (something like Hearthstone but less complex). I have a table in a database with all the cards that I want in the game. The table has a column cardType that stores the card type, which should be the corresponding constructor for a java object. I want to instantiate an object of that type based on the table field.

For example: Let's say I have in my table a card with cardType = BasicMinion(7,7).

Then in Java I have a class:

BasicMinion extends Card { 
public BasicMinion(int health, int attack) {
// initialize
}
public void play(){
//do something
}
}

I can use reflection to create an object using the string I get from the database doing something like this: Java how to instantiate a class from string

However, my cards extend the class Card which has some basic events to which it can react. Will reflection allow me to call the methods from the base class Card and also the methods that the derived class has?

Basically I want to have for example an ArrayList<Card> where I can put all the card types I got from the database so I can call their methods when an event occurs.

I seem to be missing something. The question is pretty vague, but how should I do this?





Aucun commentaire:

Enregistrer un commentaire