Hello,
I tried to make a control for my Player(s). I figured out, how to make turning from right to left or left to right look better by mirroring vertically. Sadly i just canĀ“t figure out how to let the Player look to the right (or front? Matter of perspective... Just the normal rotation when placed in World).
This is why i decided to ask for help here. Player is abstract and has two Subclasses namely PlayerOne and PlayerTwo which extend from Player. Heres the Code:
public abstract class Player extends Actor
{ protected static final int UP = 270;
protected static final int RIGHT = 0;
protected static final int DOWN = 90;
protected static final int LEFT = 180;
boolean isTrueLeft;
boolean isTrueRight;
protected String upKey;
protected String leftKey;
protected String downKey;
protected String rightKey;
protected int localRotation;
protected int newLocalRotation;
/**
* Act - do whatever the Player wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
performMovement();
}
public boolean canMove() {
return canMove(1);
}
public boolean canMove(int distance) {
World myWorld = getWorld();
int x = getNextX(distance);
int y = getNextY(distance);
if (x >= myWorld.getWidth() || y >= myWorld.getHeight() - 1) {
return false;
} else if (x < 0 || y < 0) {
return false;
}
List<Rock> steine = myWorld.getObjectsAt(x, y, Rock.class);
if (steine.isEmpty()) {
return true;
}
return false;
}
public int getNextX(int distance) {
int x = getX();
switch (localRotation) {
case RIGHT:
x = x + distance;
break;
case LEFT:
x = x - distance;
break;
}
return x;
}
public int getNextY(int distance) {
int y = getY();
switch (localRotation) {
case DOWN:
y = y + distance;
break;
case UP:
y = y - distance;
break;
}
return y;
}
public void turnLeft() {
switch (localRotation) {
case DOWN:
setImageRotation(RIGHT);
break;
case RIGHT:
setImageRotation(UP);
break;
case UP:
setImageRotation(LEFT);
break;
case LEFT:
setImageRotation(DOWN);
break;
}
}
public void turnRight() {
switch (localRotation) {
case DOWN:
setImageRotation(LEFT);
break;
case RIGHT:
setImageRotation(DOWN);
break;
case UP:
setImageRotation(RIGHT);
break;
case LEFT:
setImageRotation(UP);
break;
}
}
public void setImageRotation(int richtung) {
this.localRotation = richtung;
switch (richtung) {
case DOWN:
setRotation(90);
break;
case RIGHT:
setRotation(90);
break;
case UP:
setRotation(-90);
break;
case LEFT:
setRotation(180);
break;
default:
break;
}
}
public void move(int steps){
if(canMove(steps)){
super.move(steps);
}
}
public void performMovement(){
if(Greenfoot.isKeyDown(upKey)) {
if(canMove()){
newLocalRotation = UP;
setLocation(getX(), getY()-1);
localRotation = newLocalRotation;
}
} else if (Greenfoot.isKeyDown(leftKey)) {
if(canMove()){
newLocalRotation = LEFT;
checkTurn();
setRotation(LEFT);
localRotation = newLocalRotation;
move(1);
}
} else if (Greenfoot.isKeyDown(downKey)) {
if(canMove()){
newLocalRotation = DOWN;
setLocation(getX(), getY()+1);
localRotation = newLocalRotation;
}
} else if (Greenfoot.isKeyDown(rightKey)) {
if(canMove()){
newLocalRotation = RIGHT;
isTrueRight = true;
checkTurn();
setRotation(RIGHT);
localRotation = newLocalRotation;
move(1);
}
}
System.out.println(getRotation());
}
public void checkTurn() {
if (localRotation == 0 && newLocalRotation == 180 && isTrueLeft|| localRotation == 180 && newLocalRotation == 0 && isTrueRight) {
getImage().mirrorVertically();
}
}
}
Thanks for any advice given!