I am working on my final project for my game design and development class. I have a method that creates both a class object for a character and a spatial geometry to represent an enemy character.
The enemy character is created but I cannot access the object created in the method just the geometry.
Here is the method that creates the enemy:
/*
Method to create an enemy model.
*/
protected Spatial makeEnemy(String t, float x, float y, float z) {
Character emy = new Character(t);
// load a character from jme3test-test-data
Spatial enemy = assetManager.loadModel(emy.getLocation());
if (t=="ogre") {
enemy.scale(4.0f);
} else {
enemy.scale(1.0f);
}
enemy.setLocalTranslation(x, y, z);
//add physics
enemy_phy = new RigidBodyControl(2f);
enemy.addControl(enemy_phy);
bulletAppState.getPhysicsSpace().add(enemy_phy);
//add a light to make the model visible
DirectionalLight sun = new DirectionalLight();
sun.setDirection(new Vector3f(-0.1f, -0.7f, 1.0f));
enemy.addLight(sun);
enemy.rotate(0.0f, 3.0f, 0.0f);
return enemy;
}//end makeEnemy
The character class is as follows:
public class Character {
private String modelLocation; //the location of the model
private int hitPoints;
private int atk;
private int score;
Character() {
modelLocation = "Models/Ninja/Ninja.mesh.xml";//makes the ninja the default character
hitPoints = 10;
atk = 1;
score = 0;
}//end no argument constructor
/*
This will be the constructor used primarily for
creating enemy characters.
*/
Character(String s){
if(s=="ogre"){
modelLocation = "Models/Sinbad.j3o";
hitPoints = 5;
atk = 1;
score= 0;
} else {
modelLocation = "Models/Oto/Oto.mesh.xml";
hitPoints = 10;
atk = 1;
score = 0;
}
}//end constructor
/*
This constructor will be used for
creating the player character.
*/
Character(String s, int h, int a){
modelLocation = s;
hitPoints = h;
atk = a;
score= 0;//score always starts at zero
}//end constructor
/*
Getter Methods
*/
public String getLocation() {
return modelLocation;
}//end getLocation
public int getHitPoints() {
return hitPoints;
}//end getHitPoints
public int getAtk() {
return atk;
}//end getAtk
public int getScore() {
return score;
}//end getScore
/*
Setter methods
*/
public void setHitPoints(int n) {
hitPoints = n;
}//end setHitPoints
public void setAtk(int n) {
atk = n;
}//end setAtk
public void setScore(int n) {
score = n;
}//end setScore
//method to deal damage to character
public void takeDamage(int a) {
hitPoints = hitPoints - a;
}//end takeDamage
//mehtod to add to character score
public void addScore() {
if(modelLocation == "Models/Sinbad.j3o") {
score = score + 1; // 1 point for a ogre
} else {
score = score + 2; //2 points for golems
}//end if else
}//end addScore
}//end character
In another method I am able to access the spatial created in this method and make changes to it, but not the emy
character object created. I just need to know how to access its hitPoints
variable and such from outside the method.