120 lines
3.4 KiB
Java
120 lines
3.4 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 jumpBoostDuration = -1;
|
|
int jumpBoostStrength = 5;
|
|
|
|
|
|
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();
|
|
|
|
//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() - 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 boolean collision(){
|
|
if (getIntersectingObjects(Projectile.class).size()>0)
|
|
{
|
|
Greenfoot.setWorld(new GameOverWorld());
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public void handlePowerUps(){
|
|
if (jumpBoostDuration >= 0){
|
|
jumpBoostDuration -= 1;
|
|
} else if (jumpBoostDuration <= -1){
|
|
currentJumpStrength = defaultJumpStrength;
|
|
}
|
|
}
|
|
|
|
public void reveivePowerUp(int duration, String type){
|
|
switch(type){
|
|
case "dummy":
|
|
break;
|
|
case "JumpBoost":
|
|
currentJumpStrength = defaultJumpStrength;
|
|
currentJumpStrength += jumpBoostStrength;
|
|
jumpBoostDuration = duration;
|
|
}
|
|
}
|
|
}
|