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(); } // 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); Actor ground = getOneIntersectingObject(Plattform.class); if (ground != null && verticalSpeed >= 0) { int groundTop = ground.getY() - ground.getImage().getHeight() / 2; int characterBottom = getY() + getImage().getHeight() / 2; // Nur landen, wenn der Spieler vorher über dem Boden war int oldCharacterBottom = oldY + getImage().getHeight() / 2; if (oldCharacterBottom <= groundTop && characterBottom >= groundTop) { setLocation( getX(), groundTop - getImage().getHeight() / 2 ); verticalSpeed = 0; onGround = true; } } else { onGround = false; } } 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; } } }