Я делаю Pac-Man. На данный момент у меня есть один контролируемый игроком PacMan и один контролируемый игроком призрак. Они оба распространяют один и тот же абстрактный класс. У меня возникли проблемы с выяснением способа проверить, не сталкиваются ли они. Это абстрактный класс `Как сделать столкновение
package org.entity.mobs;
import java.awt.Point;
import org.Constants;
import org.entity.Entity;
public abstract class Mob extends Entity {
private Point destination;
protected int nextDirection = 4;
protected int direction;
protected int tempX;
public Mob(int x, int y) {
super(x, y);
destination = new Point();
}
public void update() {
if (Constants.walls[destination.y][destination.x] != 1) {
switch (direction) {
case 1:
y -= 2;
break;
case 2:
y += 2;
break;
case 3:
x -= 2;
break;
case 4:
x += 2;
break;
default:
break;
}
}
if (onGrid()) {
direction = nextDirection;
}
}
public void setNextDirection(int key) {
switch (key) {
case Constants.PACMAN_UP:
this.nextDirection = 1;
break;
case Constants.PACMAN_DOWN:
this.nextDirection = 2;
break;
case Constants.PACMAN_LEFT:
this.nextDirection = 3;
break;
case Constants.PACMAN_RIGHT:
this.nextDirection = 4;
break;
}
}
public Point getDestination() {
return destination;
}
public void setDestination() {
Point point = new Point();
switch (direction) {
case 1: // up
point.setLocation(x/20, ((y - 1)/20));
if (x % 20 <= 4 || x % 20 >= 16) {
tempX = (x + 5)/20;
} else {
tempX = x;
}
break;
case 2: // down
point.setLocation(x/20, (y/20) + 1);
if (x % 20 <= 4 || x % 20 >= 16) {
tempX = (x + 5)/20;
} else {
tempX = x;
}
break;
case 3: // left
point.setLocation(((x - 1)/20), y/20);
if (x % 20 <= 4 || x % 20 >= 16) {
tempX = (x + 5)/20;
} else {
tempX = x;
}
break;
case 4: // right
point.setLocation((x/20) + 1, y/20);
if (x % 20 <= 4 || x % 20 >= 16) {
tempX = (x + 5)/20;
} else {
tempX = x;
}
break;
default:
break;
}
destination.setLocation(point);
}
public boolean onGrid() {
return (x % 20 == 0) && (y % 20 == 0);
}
}
` А вот где я собирался поставить метод Collide
package org;
import org.entity.tiles.Wall;
import org.entity.tiles.Pellet;
import java.awt.Graphics;
import java.util.ArrayList;
import org.entity.Entity;
import org.entity.mobs.Mob;
public class Grid {
private Entity[][] grid = new Entity[31][28];
private ArrayList<Mob> entities = new ArrayList<>();
public Grid() {
// Fills grid
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[0].length; col++) {
switch (Constants.walls[row][col]) {
case 0:
grid[row][col] = new Pellet(col, row);
break;
case 1:
grid[row][col] = new Wall(col, row);
break;
case 2:
// TODO middle
break;
case 3:
grid[row][col] = new Pellet(col, row, true);
break;
default:
break;
}
}
}
// TODO: possibly add more
}
public void update() {
// move entities in units that can evenly go into 20 (1,2,4,5,10,20)
for (Mob mob : entities) {
mob.setDestination();
mob.update();
}
}
public void draw(Graphics g) {
// Draw Maze
g.drawImage(ImageLibrary.MAZE.getImage(), 0, 0, null);
// draws Tiles
for (Entity[] row : grid) {
for (Entity tile : row) {
if (tile != null) {
tile.draw(g);
}
}
}
// draws mobs
for (Entity mob : entities) {
mob.draw(g);
}
}
public void add(Mob entity) {
entities.add(entity);
}
}
Оформить заказ: http://www.edu4java.com/ru/game/game6.html – AK47
Вы можете создать метод getBounds() для обоих спрайтов и проверить, пересекаются ли они. Pro tip: просто потому, что ваш спрайт не является прямоугольником, не означает, что обнаружение столкновения не может быть прямоугольником – AK47