4

I am developing a game in Java, and I am trying to create enemies that can shoot projectiles. I am defining the base class for all enemy types, and I have already defined a projectile class. What I would like to do is define a class type variable in the base enemy class, and a shootProjectile() method that will create a new instance of the defined projectile class when called. Pseudo code:

public class EnemyClass extends GameCharacter {
        protected Class projectileType;
        protected int a;
        protected int b;

        // Constructor and other stuff here

        public void shootProjectile() {
            projectileType newProjectile = new projectileType(a, b);
        }
}

public class MyEnemySubClass extends EnemyClass {
        public MyEnemySubClass() {
            super();
            projectileType = MySuperProjectile;
        }
        // Rest of class stuff here
}

public class MySuperProjectile extends Projectile {
        public MySuperProjectile(parameter a, parameter b) {
              // Constructor stuff using a and b
        }
        // Other projectile-related stuff
}

The idea of course is that in the enemy sub classes all I need to do is define what class of projectile they shoot, and the shootProjectile() method in the base class will do the rest.

I'm not sure how to achieve this. I have looked at defining generics but not sure how to make this work for my purposes and I'm not sure I fully understand how they work. Is something like this even possible in Java, or would I need to override shootProjectile for each subclass of EnemyClass to create their own projectile while attacking?

Thanks in advance!

3
  • use Class.newInstance(). Also, use Class<? extends Projectile> so you can put the newInstance in a reference of Projectile Commented Mar 12, 2014 at 21:45
  • it would also be easier to add setter methods to your Projectile class in order to pass the values for a and b, as you don't know what constructor will be available. Commented Mar 12, 2014 at 21:46
  • Thank you for all the responses guys, plenty of different approaches to consider which is always nice to have! I'll try each one and see which one I prefer. Thanks again! Commented Mar 13, 2014 at 10:14

3 Answers 3

2

If I understand you correctly, you do not need to use generics.

If you want different implementations of Enemy shoot different implementation of Projectile then just define abstract method Projectile shootProjectile() in Enemy abstract class and implement it as you see fit in each Enemy subclass.

But if you for some reason insist on using them. Them add parameter T to Enemy class. And define the method as T shootProjectile(). Example:

abstract class Enemy<T extends Projectile> {
     abstract T shootProjectile();
}

And again. Do implementation in subclasses like:

class Sniper extends Enemy<SniperRifleBullet>{

    @Override
    SniperRifleBullet shootProjectile() {
        return new SniperRifleBullet();
    }
}

class Projectile{}

class SniperRifleBullet extends Projectile{}
Sign up to request clarification or add additional context in comments.

Comments

1

You have Class.forName(), getClass().newInstance(), the prototype pattern, or functional objects as choices. In the latter case, you would supply an object whose sole purpose was to create an instance of the required class (that would be your projectileType variable).

Comments

0

A better approach is to use a "projectile factory".... Something like this: - Create a interface ProjectileFactory with one single method like createProjectil. - Change the BaseEnemyClass to have a instance of ProjectileFactory instead of have a Class.

Now you can construct your Enemies with a factory and can have enemies that shoot the same projectile class with especific parameters.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.