So I have a scenario I'm working on where there are two Dragons made from a Dragon class, one red, one blue. Each Dragon can shoot fireballs up to a maximum of 3 fireballs per Dragon. This is tracked in the Dragon class with the int numFireballs and each time a Fireball is created it adds to the numFireballs.
Inside the Fireball class there is a timer for the Fireball's lifespan. When the timer runs out the Fireball is removed. I wrote a method in the Dragon class to be used to lower the numFireballs but I am unsure how to access the specific redDragon or blueDragon's variable. If it was just one Dragon that would be fine but as I need it to manage specific Dragon's variables I am a little lost. Help please!
Just in case the world code is needed...
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* A Dragon that can be Red or Blue and breathe fire.
*
* @author (Mitch Belmer)
* @version (.01)
*/
public class Dragon extends Actor
{
private String image;
private int timer;
private int numFireballs;
private final int MAX_FIREBALLS = 3;
/**
* Dragon Constructor.
*/
public Dragon(String drgImg)
{
image = drgImg;
setImage(image);
numFireballs = 0;
timer = 0;
int firstX = getX();
int firstY = getY();
}//end Dragon constructor
/**
* Act - do whatever the Dragon wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
tankControls();
breatheFire();
fireCooldown();
}//end act
/**
* Give Dragon tank control movement.
*/
public void tankControls()
{
if (Greenfoot.isKeyDown("left"))
{
turn(-2);
}//end if
if (Greenfoot.isKeyDown("right"))
{
turn(2);
}//end if
if (Greenfoot.isKeyDown("up"))
{
int originalX = getX();
int originalY = getY();
move(2);
if (getOneIntersectingObject(Wall.class) != null)
{
setLocation(originalX, originalY);
}//end if
}//end if
if (Greenfoot.isKeyDown("down"))
{
int originalX = getX();
int originalY = getY();
move(-2);
if (getOneIntersectingObject(Wall.class) != null)
{
setLocation(originalX, originalY);
}//end if
}//end if
}//end tankControls
/**
* Use Spacebar to shoot fireball.
*/
public void breatheFire()
{
if (Greenfoot.isKeyDown("space") && timer == 0 && numFireballs < 3)
{
Fireball fireball = new Fireball(this);
getWorld().addObject(fireball, getX(), getY());
fireball.setRotation(getRotation());
timer = 30;
numFireballs++;
}//end if
}//end breatheFire
/**
* Use timer for fireball cooldown.
*/
public void fireCooldown()
{
if (timer > 0)
{
timer = timer - 1;
}//end if
}//end fireCooldown
/**
* Use this method to increase amount of Fireballs allowed
*/
public void fireAdd()
{
numFireballs--;
}//end fireAdd
/**
* Reset position to starting position
*/
public void posReset()
{
setLocation( firstX, firstY);
}//end posReset
}//end Dragonimport greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Fireballs are weapons for the Dragons.
*
* @author (Mitch Belmer)
* @version (.01)
*/
public class Fireball extends Actor
{
public int life = 200;
private Dragon creator;
/**
* Constructor - allows fireball to know which dragon object created it
*/
public Fireball(Dragon creator)
{
this.creator = creator;
}//end Fireball Constructor
/**
* Act - do whatever the Fireball wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
fireHit();
bounce();
lifespan();
}//end act
/**
* Make Fireball bounce when hitting a wall.
*/
public void bounce()
{
int originalX = getX();
int originalY = getY();
move(8);
if (getOneIntersectingObject(Wall.class) != null)
{
setLocation(originalX, originalY);
setRotation(getRotation() - 45);
}//end if
}//end bounce
/**
* Countdown lifespan of Fireball and remove.
*/
public void lifespan()
{
life = life - 1;
if (life == 0)
{
getWorld().removeObject(this);
}//end if
}//end lifespan
/**
* Detect Fireball Collision and reset players & Adjust Score
*/
public void fireHit()
{
Dragon dragon = (Dragon)getOneIntersectingObject(Dragon.class);
if (dragon != null && life < 180)
{
((DragonArena)getWorld()).roundOver(dragon);
}//end if
}//end fireHit
/**
* getLife method returns the current life value (int)
*/
public int getLife()
{
return life;
}//end getLife
/**
* getCreator provides the reference to the dragon that created this fireball object
*/
public Dragon getCreator()
{
return creator;
}//end getCreator
}//end Fireball Actorimport greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* DragonArena is a two player game of fire-breathing dragons.
*
* @author (Mitch Belmer)
* @version (.01)
*/
public class DragonArena extends World
{
private int redScore;
private int blueScore;
private Dragon redDragon, blueDragon;
/**
* Constructor for objects of class DragonArena.
*
*/
public DragonArena()
{
// Create a new world with 768x768 cells with a cell size of 1x1 pixels.
super(768, 768, 1);
prepare();
redScore = 0;
blueScore = 0;
showScore();
}//end DragonArena
/**
* Prepare the world for the start of the program.
* That is: create the initial objects and add them to the world.
*/
private void prepare()
{
Wall wall = new Wall();
addObject(wall,221,366);
Wall wall2 = new Wall();
addObject(wall2, 571, 366);
redDragon = new Dragon("RedDragon.png");
redDragon.turn(-90);
addObject(redDragon,404,596);
blueDragon = new Dragon("BlueDragon.png");
blueDragon.turn(90);
addObject(blueDragon, 404, 180);
}//end prepare
/**
* Show Red and Blue Player score
*/
public void showScore()
{
showText("Red Player: " + redScore, 80, 25);
showText("Blue Player: " + blueScore, 675,25);
}//end showScore
/**
* Accessor methods for each of the player objects
*/
public Dragon getRedDragon() { return redDragon; }
public Dragon getBlueDragon() { return blueDragon; }
/**
* roundOver method allows for the world to start a new round
*/
public void roundOver(Dragon dragon)
{
//adjust the score
if (dragon == redDragon) {
//redDragon was hit so increase blue dragon score
blueScore++;
}//end if
else {
redScore++;
}//end else
}//end roundOver
}//end MyWorld
