181 lines
5.7 KiB
Java
181 lines
5.7 KiB
Java
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
|
|
|
|
/**
|
|
* Write a description of class Player here.
|
|
*
|
|
* @author (your name)
|
|
* @version (a version number or a date)
|
|
*/
|
|
public class Player extends Actor
|
|
{
|
|
int MAX_HEALTH = 3;
|
|
int health = MAX_HEALTH;
|
|
|
|
int jumpBoostDuration = -1;
|
|
int jumpBoostStrength = 5;
|
|
|
|
boolean projectileReady = false;
|
|
int projectileTimer;
|
|
|
|
private int speed = 6;
|
|
private int currentJumpStrength;
|
|
private int defaultJumpStrength = 15;
|
|
private int gravity = 1;
|
|
|
|
private int verticalSpeed = 0;
|
|
private boolean onGround = false;
|
|
|
|
public void act()
|
|
{
|
|
collision();
|
|
moveLeftAndRight();
|
|
jump();
|
|
fall();
|
|
handlePowerUps();
|
|
shootProjectile();
|
|
checkHealth();
|
|
|
|
//Check ob der Spieler am unteren Fensterrand ist
|
|
if (getY()+1 >= getWorld().getHeight()){
|
|
Greenfoot.setWorld(new GameOverWorld());
|
|
}
|
|
//Check ob der Spieler an der Seite vom Fenster ist
|
|
if (getX() <= 0){
|
|
setLocation(getWorld().getWidth(), getY());
|
|
} else if (getX() >= getWorld().getWidth()-1){
|
|
setLocation(1, getY());
|
|
}
|
|
}
|
|
|
|
// Bewegung nach links und rechts
|
|
private void moveLeftAndRight()
|
|
{
|
|
if (Greenfoot.isKeyDown("a"))
|
|
{
|
|
setLocation(getX() - speed, getY());
|
|
}
|
|
|
|
if (Greenfoot.isKeyDown("d"))
|
|
{
|
|
setLocation(getX() + speed, getY());
|
|
}
|
|
}
|
|
|
|
// Springen
|
|
private void jump()
|
|
{
|
|
if (Greenfoot.isKeyDown("space") && onGround)
|
|
{
|
|
verticalSpeed = -currentJumpStrength;
|
|
onGround = false;
|
|
}
|
|
}
|
|
|
|
// Schwerkraft
|
|
private void fall() {
|
|
int oldY = getY();
|
|
verticalSpeed += gravity;
|
|
int newY = getY() + verticalSpeed;
|
|
setLocation(getX(), newY);
|
|
// Alle Plattformen überprüfen, die der Spieler berührt
|
|
java.util.List<Plattform> platforms = getIntersectingObjects(Plattform.class);
|
|
boolean landed = false;
|
|
for (Plattform platform : platforms) {
|
|
int groundTop = platform.getY()+5 - platform.getImage().getHeight() / 2;
|
|
int characterBottom = getY() + getImage().getHeight() / 2;
|
|
int oldCharacterBottom = oldY + getImage().getHeight() / 2;
|
|
// Prüfen, ob der Spieler von oben auf diese Plattform fällt
|
|
if (verticalSpeed >= 0 && oldCharacterBottom <= groundTop && characterBottom >= groundTop) {
|
|
setLocation( getX(), groundTop - getImage().getHeight() / 2 );
|
|
verticalSpeed = 0;
|
|
landed = true;
|
|
break;
|
|
}
|
|
}
|
|
onGround = landed;
|
|
}
|
|
|
|
public void shootProjectile(){
|
|
projectileTimer++;
|
|
MouseInfo mouse = Greenfoot.getMouseInfo();
|
|
if (Greenfoot.mousePressed(null)){
|
|
PlayerProjectile playerProjectile = new PlayerProjectile();
|
|
getWorld().addObject(playerProjectile, getX(), getY());
|
|
playerProjectile.turnTowards(Greenfoot.getMouseInfo().getX(), Greenfoot.getMouseInfo().getY());
|
|
projectileReady = false;
|
|
}
|
|
if (projectileTimer >= 10){
|
|
projectileReady = true;
|
|
projectileTimer = 0;
|
|
}
|
|
}
|
|
|
|
public void collision(){
|
|
if (getIntersectingObjects(Projectile.class).size()>0
|
|
|| getIntersectingObjects(Enemy.class).size()>0
|
|
|| getIntersectingObjects(Spike.class).size()>0)
|
|
{
|
|
health--;
|
|
getWorld().removeObjects(getIntersectingObjects(Projectile.class));
|
|
getWorld().removeObjects(getIntersectingObjects(Enemy.class));
|
|
getWorld().removeObjects(getWorld().getObjects(Spike.class));
|
|
|
|
getWorld().removeObjects(getIntersectingObjects(Spike.class));
|
|
if (getWorld().getObjects(Heart.class) != null){
|
|
if (health == 2){
|
|
getWorld().getObjects(Heart.class).get(2).setImage("a.png");
|
|
} else if (health == 1){
|
|
getWorld().getObjects(Heart.class).get(1).setImage("a.png");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void checkHealth(){
|
|
if (health <= 0){
|
|
Greenfoot.setWorld(new GameOverWorld());
|
|
}
|
|
}
|
|
|
|
public void handlePowerUps(){
|
|
if (jumpBoostDuration >= 0){
|
|
jumpBoostDuration -= 1;
|
|
} else if (jumpBoostDuration <= -1){
|
|
currentJumpStrength = defaultJumpStrength;
|
|
}
|
|
}
|
|
|
|
public void receivePowerUp(int duration, String type){
|
|
switch(type){
|
|
case "dummy":
|
|
break;
|
|
case "JumpBoost":
|
|
currentJumpStrength = defaultJumpStrength;
|
|
currentJumpStrength += jumpBoostStrength;
|
|
jumpBoostDuration = duration;
|
|
break;
|
|
case "Heal":
|
|
if (health < MAX_HEALTH){
|
|
if (health == 2){
|
|
getWorld().getObjects(Heart.class).get(2).setImage("Health.png");
|
|
} else if (health == 1){
|
|
getWorld().getObjects(Heart.class).get(1).setImage("Health.png");
|
|
}
|
|
health++;
|
|
}
|
|
break;
|
|
case "GroundPower":
|
|
World theWorld= this.getWorld();
|
|
if(!theWorld.getObjects(TemporaryGround.class).isEmpty())
|
|
{
|
|
theWorld.getObjects(TemporaryGround.class).get(0).RefreshLifeToDuration(duration);
|
|
}
|
|
else
|
|
{
|
|
theWorld.addObject((new TemporaryGround(duration)), 200, 575);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|