I have a "poison trap" that when an animal class comes into contact with it, it should blow up, have a poison cloud explosion occur, and then deal damage over time to any animal that came in contact with the cloud of poison. How would i implement something like this?
Poison Trap code:
Poison Cloud code:
Animal code:
Additionally Id like my other type of trap, a landmine, to deal a set amount of damage to animals when it comes into contact with the explosion
Landmine code:
Explosion code:
All my traps are also under a trap superclass:
Trap superclass code:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.ArrayList;
/**
* Write a description of class poisonTrap here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class poisonTrap extends Traps
{
/**
* Act - do whatever the poisonTrap wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public poisonTrap()
{
GreenfootImage image = getImage();
image.scale(image.getWidth() - 160, image.getHeight() - 210);
setImage(image);
}
public void checkHitAnimal()
{
ArrayList<Hunter> h = (ArrayList<Hunter>)getObjectsAtOffset(0,0, Hunter.class);
if (h.size() > 0)
{
getWorld().addObject (new Poison(), this.getX(), this.getY());
getWorld().removeObject(this);
}
}
public void act()
{
checkHitAnimal();
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class poison here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Poison extends Effects
{
/**
* Act - do whatever the poison wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
GreenfootImage[] currAnim;
int animTimer;
private static final int frameDuration = 3;
public Poison()
{
GreenfootImage[] poisonAni = new GreenfootImage[9];
for (int i=0; i<9; i++)
{
poisonAni[i] = new GreenfootImage("ptile" + i + ".png");
poisonAni[i].scale (100, 100);
}
setAnimation(poisonAni);
}
public void act()
{
//this.getImage().setTransparency(getImage().getTransparency() - 3); //slowly becomes transparent
removeTouching(Hunter.class);
setImage();
/*if (hunter != null)
{
getWorld().removeObject(hunter);
}
if (this.getImage().getTransparency() < 5)
{
getWorld().removeObject(this);
}*/
}
private void setAnimation(GreenfootImage[] anim)
{
currAnim = anim;
animTimer = -1;
setImage();
}
private void setImage()
{
animTimer = (animTimer+1)%(frameDuration*currAnim.length);
if (animTimer%frameDuration == 0) {
if (currAnim[currAnim.length-1] == getImage()) getWorld().removeObject(this);
setImage(currAnim[animTimer/frameDuration]);
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* The abstract class for all animals in the game
*
* @author Jonathan
* @version 0.1
*/
public abstract class Animal extends Actor
{
private boolean isScared;
protected int speed;
protected int expDrop;
protected int health;
protected BetterGreenfootSound spawnSound;
protected BetterGreenfootSound deathSound;
/**
* Act - do whatever the Animal wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
move();
}
public void addedToWorld(World world)
{
SoundManager.getInstance().playSound(spawnSound, SoundChannel.SFX);
// Move to a random location within the bounds of the world
setLocation(world.getWidth(), Greenfoot.getRandomNumber(69)); //TODO: Get proper bounds
}
/**
* Moves the animal
*/
protected void move()
{
// TODO: Default implementation of animal movement
if (isScared)
{
// Move towards right of the screen and away from the hunters
}
else
{
// TODO
}
}
/**
* Scares the animal away from the hunter
*/
public void scare()
{
isScared = true;
}
/**
* Damages the animal by the amount specified
* @param hunter The hunter that is damaging the animal
* @param amount The amount of damage to deal
*/
public void damage(Hunter hunter, int amount)
{
health -= amount;
if (health <= 0)
{
die(hunter);
}
}
/**
* Kills the animal and rewards its killer
* @param killer The animal's killer
*/
public void die(Hunter killer)
{
killer.increaseExp(expDrop);
SoundManager.getInstance().playSound(deathSound, SoundChannel.SFX);
getWorld().removeObject(this);
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.ArrayList;
/**
* Write a description of class Landmine here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Landmine extends Traps
{
/**
* Act - do whatever the Landmine wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public Landmine()
{
GreenfootImage image = getImage();
image.scale(image.getWidth() - 150, image.getHeight() - 120);
setImage(image);
}
public void checkHitAnimal()
{
ArrayList<Hunter> h = (ArrayList<Hunter>)getObjectsAtOffset(0,0, Hunter.class);
if (h.size() > 0)
{
getWorld().addObject (new Explosion(), this.getX(), this.getY());
getWorld().removeObject(this);
}
}
public void act()
{
checkHitAnimal();
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Explosion here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Explosion extends Effects
{
GreenfootImage[] currAnim;
int animTimer;
private static final int frameDuration = 3;
public Explosion()
{
GreenfootImage[] explosionAni = new GreenfootImage[10];
for (int i=0; i<10; i++)
{
explosionAni[i] = new GreenfootImage("tile" + i + ".png");
explosionAni[i].scale (100, 100);
}
setAnimation(explosionAni);
}
public void act()
{
//this.getImage().setTransparency(getImage().getTransparency() - 3); //slowly becomes transparent
removeTouching(Hunter.class);
setImage();
/*if (hunter != null)
{
getWorld().removeObject(hunter);
}
if (this.getImage().getTransparency() < 5)
{
getWorld().removeObject(this);
}*/
}
private void setAnimation(GreenfootImage[] anim)
{
currAnim = anim;
animTimer = -1;
setImage();
}
private void setImage()
{
animTimer = (animTimer+1)%(frameDuration*currAnim.length);
if (animTimer%frameDuration == 0) {
if (currAnim[currAnim.length-1] == getImage()) getWorld().removeObject(this);
setImage(currAnim[animTimer/frameDuration]);
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Traps here.
*
* @author (your name)
* @version (a version number or a date)
*/
public abstract class Traps extends Actor
{
/**
* Act - do whatever the Traps wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
protected double damage;
protected double durability;
protected abstract void checkHitAnimal ();
public void act()
{
}
}
