数组列表映射不起作用

时间:2018-04-17 18:08:16

标签: java arrays eclipse list procedural

我正在创建一个程序生成的Roguelike,类似于Isaac的Binding。我把我的地图作为数组列表。我已经让程序记住了最后一个房间的门,但它不记得房间本身。我穿过西门,东门还在打开,但是如果我穿过那扇门,是的,西门在原来的房间里仍然打开,但是其他的门是随机的。

这是我的代码 -

游戏状态:

public class GameState extends JFrame implements KeyListener {
Container contentPane=this.getContentPane();
Graphics bufferGraphics;

int characterX=463;
int characterY=486;
int oldCharacterX=463;
int oldCharacterY=486;
int xAxis;
int yAxis;
Image characterNorth = CustomImages.createImageIcon("Images/characterNorth.jpg").getImage();
Image characterEast = CustomImages.createImageIcon("Images/characterEast.jpg").getImage();
Image characterSouth = CustomImages.createImageIcon("Images/characterSouth.jpg").getImage();
Image characterWest = CustomImages.createImageIcon("Images/characterWest.jpg").getImage();
Image brickWall = CustomImages.createImageIcon("Images/brickWall.jpg").getImage();
Image brickFloor = CustomImages.createImageIcon("Images/brickFloor.jpg").getImage();
Image character=characterNorth;
boolean pressed=false;

ArrayList<RoomState> map = new ArrayList<RoomState>();
RoomState currentRoom = new RoomState();
RoomState currentRoomState=new RoomState();

GameState() {
    this.setBounds(0, 0, 1680, 1050);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    addKeyListener(this);
    setFocusable(true);
    requestFocusInWindow();
}

public void move(int x, int y) {
    characterX+=x;
    characterY+=y;

    //Check move
    currentRoomState=currentRoomState.MoveToNextRoom(true, false, false, false);
    currentRoomState=currentRoomState.MoveToNextRoom(false, true, false, false);
    currentRoomState=currentRoomState.MoveToNextRoom(false, false, true, false);
    currentRoomState=currentRoomState.MoveToNextRoom(false, false, false, true);
}

public void paint(Graphics g) {
    for(xAxis=58;xAxis<=858;xAxis=xAxis+50) {
        for(yAxis=81;yAxis<=881;yAxis=yAxis+50) {
            g.drawImage(brickFloor,xAxis,yAxis,null);
        }
        yAxis=31;
    }
    for(xAxis=8;xAxis<958;xAxis=xAxis+50) {
        g.drawImage(brickWall,xAxis,yAxis,null);
    }
    yAxis=931;
    for(xAxis=8;xAxis<=908;xAxis=xAxis+50) {
        g.drawImage(brickWall,xAxis,yAxis,null);
    }
    xAxis=8;
    for(yAxis=81;yAxis<=881;yAxis=yAxis+50) {
        g.drawImage(brickWall,xAxis,yAxis,null);
    }
    xAxis=908;
    for(yAxis=81;yAxis<=881;yAxis=yAxis+50) {
        g.drawImage(brickWall,xAxis,yAxis,null);
    }

    if(currentRoom.northDoor) {
        g.drawImage(brickFloor,458,31,null);
    }
    if(currentRoom.eastDoor) {
        g.drawImage(brickFloor,908,481,null);
    }
    if(currentRoom.southDoor) {
        g.drawImage(brickFloor,458,931,null);
    }
    if(currentRoom.westDoor) {
        g.drawImage(brickFloor,8,481,null);
    }

    g.drawImage(character,characterX,characterY,null);
}

@Override
public void keyPressed(KeyEvent arg0) { //Character rotation and movement.
    if(pressed==false) {
        pressed=true;
        if(arg0.getKeyCode() == KeyEvent.VK_W){
            if(character==characterNorth) {
                if(characterY>86 && characterX>13 && characterX<913) {
                    characterY=characterY-50;
                }else if(currentRoom.northDoor && characterX==463) {
                    oldCharacterY=characterY;
                    characterY=characterY-50;
                    if(characterY==-14) {
                        RoomState nextRoom = new RoomState();
                        nextRoom.southDoor = true;
                        nextRoom.rs_SouthDoor = currentRoom;
                        currentRoom.rs_NorthDoor = nextRoom;
                        map.add(nextRoom);
                        currentRoom = nextRoom;
                        nextRoom = null;
                        characterX=463;
                        characterY=936;
                        repaint();
                    }
                }
            }else {
                character=characterNorth;
            }
        }
        if(arg0.getKeyCode() == KeyEvent.VK_A){
            if(character==characterWest && characterY>36 && characterY<926) {
                if(characterX>63) {
                    oldCharacterX=characterX;
                    characterX=characterX-50;
                }else if(currentRoom.westDoor && characterY==486) {
                    oldCharacterX=characterX;
                    characterX=characterX-50;
                    if(characterX==-37) {
                        RoomState nextRoom = new RoomState();
                        nextRoom.eastDoor = true;
                        nextRoom.rs_EastDoor = currentRoom;
                        currentRoom.rs_WestDoor = nextRoom;
                        map.add(nextRoom);
                        currentRoom = nextRoom;
                        nextRoom = null;
                        characterX=913;
                        characterY=486;
                        repaint();
                    }
                }
            }else {
                character=characterWest;
            }
        }
        if(arg0.getKeyCode() == KeyEvent.VK_S){
            if(character==characterSouth) {
                if(characterY<871 && characterX>13 && characterX<913) {
                    oldCharacterY=characterY;
                    characterY=characterY+50;
                }else if(currentRoom.southDoor && characterX==463) {
                    oldCharacterY=characterY;
                    characterY=characterY+50;
                    if(characterY==986) {
                        RoomState nextRoom = new RoomState();
                        nextRoom.northDoor = true;
                        nextRoom.rs_NorthDoor = currentRoom;
                        currentRoom.rs_SouthDoor = nextRoom;
                        map.add(nextRoom);
                        currentRoom = nextRoom;
                        nextRoom = null;
                        characterX=463;
                        characterY=36;
                        repaint();
                    }
                }
            }else {
                character=characterSouth;
            }
        }
        if(arg0.getKeyCode() == KeyEvent.VK_D){
            if(character==characterEast && characterY>36 && characterY<926) {
                if(characterX<848) {
                    oldCharacterX=characterX;
                    characterX=characterX+50;
                }else if(currentRoom.eastDoor && characterY==486) {
                    oldCharacterX=characterX;
                    characterX=characterX+50;
                    if(characterX==963) {
                        RoomState nextRoom = new RoomState();
                        nextRoom.westDoor = true;
                        nextRoom.rs_WestDoor = currentRoom;
                        currentRoom.rs_EastDoor = nextRoom;
                        map.add(nextRoom);
                        currentRoom = nextRoom;
                        nextRoom = null;
                        characterX=13;
                        characterY=486;
                        repaint();
                    }
                }
            }else {
                 character=characterEast;
            }
        }
        repaint(oldCharacterX,oldCharacterY,40,40);
        repaint(characterX,characterY,40,40);
        }
    }

@Override
public void keyReleased(KeyEvent arg0) { //Prevents keys from being held down.
    if(arg0.getKeyCode() == KeyEvent.VK_W){
        pressed=false;
    }
    if(arg0.getKeyCode() == KeyEvent.VK_A){
        pressed=false;
    }
    if(arg0.getKeyCode() == KeyEvent.VK_S){
        pressed=false;
    }
    if(arg0.getKeyCode() == KeyEvent.VK_D){
        pressed=false;
    }
}

@Override
public void keyTyped(KeyEvent arg0) {
}

}

RoomState:

public class RoomState {

boolean northDoor;
boolean eastDoor;
boolean southDoor;
boolean westDoor;
boolean doorsOpen;
int northRoll;
int eastRoll;
int southRoll;
int westRoll;
Random r=new Random();

//Reference to the adjacent rooms
RoomState rs_NorthDoor=null;
RoomState rs_EastDoor=null;
RoomState rs_SouthDoor=null;
RoomState rs_WestDoor=null;

//Initial
RoomState() {
    while(!doorsOpen) {
        northRoll=r.nextInt(4)+1;
        eastRoll=r.nextInt(4)+1;
        southRoll=r.nextInt(4)+1;
        westRoll=r.nextInt(4)+1;
        if(northRoll==1) {
            northDoor=true;
        }else {
            northDoor=false;
        }
        if(eastRoll==1) {
            eastDoor=true;
        }else {
            eastDoor=false;
        }
        if(southRoll==1) {
            southDoor=true;
        }else {
            southDoor=false;
        }
        if(westRoll==1) {
            westDoor=true;
        }else {
            westDoor=false;
        }
        if(northDoor==false && eastDoor==false && southDoor==false && westDoor==false) {
            doorsOpen=false;
        }else {
            doorsOpen=true;
        }
    }
}

RoomState(RoomState previousState, boolean north, boolean east, boolean south, boolean west) {
    this();

    if(north) {
        rs_NorthDoor=previousState;
        northDoor = true;
    }else if(east) {
        rs_EastDoor=previousState;
        eastDoor=true;
    }else if(south) {
        rs_SouthDoor=previousState;
        southDoor=true;
    }else if(west) {
        rs_WestDoor=previousState;
        westDoor=true;
    }
}

public RoomState MoveToNextRoom(boolean north, boolean east, boolean south, boolean west) {
    if(north) {
        if(rs_NorthDoor==null) {
            rs_NorthDoor=new RoomState(this,north,east,south,west);
        }
        return rs_NorthDoor;
    }
    if(east) {
        if(rs_EastDoor==null) {
            rs_EastDoor=new RoomState(this,north,east,south,west);
        }
        return rs_EastDoor;
    }
    if(south) {
        if(rs_SouthDoor==null) {
            rs_SouthDoor=new RoomState(this,north,east,south,west);
        }
        return rs_SouthDoor;
    }
    if(west) {
        if(rs_WestDoor==null) {
            rs_WestDoor=new RoomState(this,north,east,south,west);
        }
        return rs_WestDoor;
    }
    return null;
}

}

0 个答案:

没有答案