Oop lab/gdx/pacman rev

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
หน้านี้เป็นส่วนหนึ่งของ oop lab

สร้างคลาสใหม่

public class ReversePacman extends Pacman {
    public ReversePacman(int x, int y, World world) {
        super(x, y, world);
    }
}

Override method

    @Override
    public void setNextDirection(int dir) {
    }
public class ReversePacman extends Pacman {
    private int [] reverseDirMap;
    
    public ReversePacman(int x, int y, World world) {
        super(x, y, world);
        
        reverseDirMap = new int[5];
        reverseDirMap[Pacman.DIRECTION_STILL] = Pacman.DIRECTION_STILL;
        reverseDirMap[Pacman.DIRECTION_LEFT] = Pacman.DIRECTION_RIGHT;
        reverseDirMap[Pacman.DIRECTION_RIGHT] = Pacman.DIRECTION_LEFT;
        reverseDirMap[Pacman.DIRECTION_UP] = Pacman.DIRECTION_DOWN;
        reverseDirMap[Pacman.DIRECTION_DOWN] = Pacman.DIRECTION_UP;
    }

    @Override
    public void setNextDirection(int dir) {
        int revDir = reverseDirMap[dir]; 
        super.setNextDirection(revDir);
    }
}

สร้างใน World

public class World {
    // ...    
    World(PacmanGame pacmanGame) {
        // ...

        //pacman = new Pacman(60,60,this);
        pacman = new ReversePacman(60,60,this);

        // ...
    }
}