Reputation: 169
package com.evansgame.newproject.fps;
import java.util.concurrent.locks.ReentrantLock;
import java.util.*;
abstract class Weapon{
int weaponID;//why can't this be static?
DrawMe drawMe;//why can't this be static?
int itemID;
float x;
float y;
float z;
static ReentrantLock weaponsLock = new ReentrantLock();
static LinkedList<Weapon> weapons = new LinkedList<>();
boolean active = true;
Weapon(int itemID, float x, float y, float z){
this.itemID = itemID;
this.x = x;
this.y = y;
this.z = z;
}
static class Bazoooka extends Weapon{
static final int WEAPON_ID = 0;
static final DrawMe bazoookaDrawMe = DrawMe.colorClone(DrawMe.loadModel("bazoooka"),0,.1f,.8f,1,0,.2f);
Bazoooka(int itemID, float x, float y, float z){
super(itemID,x,y,z);
drawMe = bazoookaDrawMe;//same across all Bazoookas
weaponID = 0;//same across all Bazoookas
}
}
}
Variables weaponID and drawMe are to be the same across all Bazoooka instances. When I'm accessing instances of Weapon I need the weaponID and DrawMe for whatever type of weapon it happens to be. It feels like these variables are static why do I have to use instance variables for them?
Upvotes: 1
Views: 75
Reputation: 169
I ended up using an enum
package com.evansgame.newproject.fps;
import java.util.concurrent.locks.ReentrantLock;
import java.util.*;
class Weapon{
static enum WeaponEnum{
NO_WEAPON(0, new DrawMe(),Player.loadAnimation("standing"),Player.loadAnimation("run"))
,BAZOOOKA(1, DrawMe.colorClone(DrawMe.loadModel("bazoooka"),0,.1f,.8f,1,0,.2f),Player.loadAnimation("standWithBazoooka"),Player.loadAnimation("runWithBazoooka"));
final int WEAPON_ID;
final DrawMe DRAW_ME;
final Moment[] STANDING_ANIMATION;
final Moment[] RUNNING_ANIMATION;
WeaponEnum(int id, DrawMe d, Moment[] standingAnimation, Moment[] runningAnimation){
WEAPON_ID = id;
DRAW_ME = d;
STANDING_ANIMATION = standingAnimation;
RUNNING_ANIMATION = runningAnimation;
}
}
static ReentrantLock weaponsLock = new ReentrantLock();
static LinkedList<Weapon> weapons = new LinkedList<>();
WeaponEnum weaponType;
int itemID;
float x;
float y;
float z;
boolean active = true;
Weapon(WeaponEnum weaponType, int itemID, float x, float y, float z){
this.weaponType = weaponType;
this.itemID = itemID;
this.x = x;
this.y = y;
this.z = z;
}
}
Upvotes: 0
Reputation: 33895
You could use getters instead of just fields:
abstract class Weapon {
abstract int getID();
abstract DrawMe getDrawMe();
...
}
Then in the Bazooka class you just override those methods like:
static final int WEAPON_ID = 0;
static final DrawMe bazoookaDrawMe = DrawMe.colorClone(DrawMe.loadModel("bazoooka"),0,.1f,.8f,1,0,.2f);
@Override
int getID() {
return WEAPON_ID;
}
@Override
DrawMe getDrawMe() {
return bazoookaDrawMe;
}
Upvotes: 1
Reputation: 285405
One option: give the abstract class a abstract public int getId();
method declaration. Let each sub-type override this returning its own static field result. But the static field should be declared in the subclass not the abstract parent.
public abstract class Weapon {
public abstract int getId();
public abstract DrawMe getDrawMe();
// .... other fields
}
public class Bazooka extends Weapon {
private static final int BAZOOKA_ID = 200;
private DrawMe drawMe;
@Override
public int getId() {
return BAZOOKA_ID;
}
@Override
public DrawMe getDrawMe() {
if (drawMe == null) {
drawMe = new BazookaDrawMe();
}
return drawMe;
}
}
Upvotes: 0