4.3 WALL is STOP
? stop不同于gameClosure, 因为stop的对象有可能变化,因此碰撞具有stop属性的物体,应该交由gamePeople,为了简化,我们先假定WALL IS STOP.
private void peopleMove(KeyEvent e) {
String stop = PeopleLocation.WALL;
List<People> peopleList = gamePeople.getPeopleList();
for (int i = 0; i < peopleList.size(); i++) {
People people = peopleList.get(i);
if(PeopleLocation.BABA.equalsIgnoreCase(people.getName())) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
gamePeople.move(1,people,stop);
break;
case KeyEvent.VK_DOWN:
gamePeople.move(-1,people,stop);
break;
case KeyEvent.VK_RIGHT:
gamePeople.move(2,people,stop);
break;
case KeyEvent.VK_LEFT:
gamePeople.move(-2,people,stop);
break;
}
}
}
}
? 我们在GamePeople中的move方法,遍历找出who is stop,当然,此时,使用List,遍历操作有些麻烦,于是,我们使用了数据结构Map.(当然,初始化的工作也增加了)
private List<People> peopleList;
private Map<String,List<People>> peopleMap;
public GamePeople() {
this.peopleList = new ArrayList<>();
this.peopleMap = new HashMap<>();
this.peopleMap.put(PeopleLocation.BABA, new ArrayList<People>());
this.peopleMap.put(PeopleLocation.ROCK, new ArrayList<People>());
this.peopleMap.put(PeopleLocation.WALL, new ArrayList<People>());
this.peopleMap.put(PeopleLocation.FLAG, new ArrayList<People>());
People baba = new People(5 * 24, 9 * 24, PeopleLocation.BABA);
this.peopleList.add(baba);
this.peopleMap.get(PeopleLocation.BABA).add(baba);
People flag = new People(14 * 24, 9 * 24, PeopleLocation.FLAG);
this.peopleList.add(flag);
this.peopleMap.get(PeopleLocation.FLAG).add(flag);
People rock0 = new People(9 * 24, 8 * 24, PeopleLocation.ROCK);
People rock1 = new People(9 * 24, 9 * 24, PeopleLocation.ROCK);
People rock2 = new People(9 * 24, 10 * 24, PeopleLocation.ROCK);
this.peopleList.add(rock0);this.peopleMap.get(PeopleLocation.ROCK).add(rock0);
this.peopleList.add(rock1);this.peopleMap.get(PeopleLocation.ROCK).add(rock1);
this.peopleList.add(rock2);this.peopleMap.get(PeopleLocation.ROCK).add(rock2);
for (int i = 4; i < 15; i++) {
People wall0 = new People(i * 24, 7 * 24, PeopleLocation.WALL);
People wall1 = new People(i * 24, 11 * 24, PeopleLocation.WALL);
this.peopleList.add(wall0);this.peopleMap.get(PeopleLocation.WALL).add(wall0);
this.peopleList.add(wall1);;this.peopleMap.get(PeopleLocation.WALL).add(wall1);
}
}
修改GamePeople种的move方法:(这里物体先移动,后碰撞,反弹)
public void move(int direction, People you, String stop) {
switch (direction) {
case 1:
you.up = true;
you.down = you.right = you.left = false;
you.setY(you.getY() - 24);
break;
case -1:
you.down = true;
you.up = you.right = you.left = false;
you.setY(you.getY() + 24);
break;
case 2:
you.right = true;
you.up = you.down = you.left = false;
you.setX(you.getX() + 24);
break;
case -2:
you.left = true;
you.up = you.right = you.down = false;
you.setX(you.getX() - 24);
break;
}
List<People> stopList = this.peopleMap.get(stop);
for (int i = 0; i < stopList.size(); i++) {
People stopPeople = stopList.get(i);
if(stopPeople.getX() == you.getX() && stopPeople.getY() == you.getY()) {
if (you.up) {
you.setY(you.getY() + 24);
} else if (you.down) {
you.setY(you.getY() - 24);
} else if (you.right) {
you.setX(you.getX() - 24);
} else if (you.left) {
you.setX(you.getX() + 24);
}
}
}
}
? 此时,我们控制的角色便无法穿墙和WALL.之后,我们会进行游戏的核心逻辑ROCK IS PUSH.目前,大家操作的角色在穿越rock时,显示在rock后,这是由于我们绘制时先绘制baba,后绘制rock,因此导致rock覆盖了baba.因此,大家可以根据自己需求,调整draw()的先后顺序。
|