/**
* Write a description of class Pxl2 here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Pxl2 extends Actor
{
private int vSpeed = 0;
private int acceleration = 1;
private int jumpHeight= - 8;
Ball2 ball2 = new Ball2();
Wall wall = new Wall();
/**
* Act - do whatever the Pxl2 wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
movement();
checkFall();
images();
fireball();
fireWall();
dead();
}
/**
* This method lets the character move
*/
public void movement()
{
int y = getY();
int x = getX();
if(Greenfoot.isKeyDown("left"))
{
move(-5);
}
if(Greenfoot.isKeyDown("right"))
{
move(5);
}
if(Greenfoot.isKeyDown("up"))
{
jump();
}
}
/**
* changes the images for the fighting
*/
public void images()
{
if(Greenfoot.isKeyDown("/"))
{
setImage("Rival-punch.png");
}
else
{
setImage("Rival-1.png");
}
if(Greenfoot.isKeyDown("."))
{
setImage("Rival-kick.png");
}
if(Greenfoot.isKeyDown("left"))
{
setImage("Rival-move-forward.png");
}
if(Greenfoot.isKeyDown("right"))
{
setImage("Rival-move.png");
}
}
/**
* makes the character jump
*/public void jump()
{
vSpeed= -5;
gravity();
}
/**
* makes the character fall
*/
public void checkFall()
{
if(onGround())
{
vSpeed = 0;
}
else
{
gravity();
}
}
/**
* makes the fall look real
*/
private void gravity()
{
setLocation(getX(), getY() +vSpeed);
vSpeed = vSpeed + acceleration;
}
/**
* makes the character land on the ground
*/
public boolean onGround()
{
Actor under = getOneObjectAtOffset(0,getImage().getHeight()/2,Ground.class);
return under != null;
}
/**
* makes the character shoot the fireball
*/
public void fireball()
{
if (Greenfoot.isKeyDown("'"))
{
setImage("Rival-punch.png");
World MyWorld = getWorld();
MyWorld.addObject(ball2, 0, 0);
ball2.setLocation(getX(), getY());
ball2.setRotation(getRotation());
}
}
/**
* special ability activate. makes a fire wall
*/
public void fireWall()
{
if (Greenfoot.isKeyDown("0"))
{
setImage("Rival-punch.png");
World MyWorld = getWorld();
MyWorld.addObject(wall, 0, 0);
wall.setLocation(getX(), getY());
wall.setRotation(getRotation());
}
}
public void dead()
{
if(isTouching(Giant.class))
{
removeTouching(Giant.class);
}
}
public void hitByFire()
{
Actor Fire2 = getOneObjectAtOffset(10, 10, Fire.class);
if(Fire.class != null)
{
World world = getWorld();
MyWorld myWorld = (MyWorld)world;
healthBar healthBar = MyWorld.getHealthBar();
healthBar.loseHealth();
}
}
}