Я создаю пошаговую 2D-игру «Звездные войны», в которой игрок должен переходить от плитки к плитке в сетке, избегая при этом врагов (я знаю, захватывающие вещи, верно !!).Сетка - это двумерный массив плиток, которые являются объектами, которые я создал с помощью Slick и Light-Weight Java Game Library (lwjgl), чтобы я мог добавить к ним текстуру.Я довольно новичок во всем этом, и я пытаюсь добавить прослушиватели кликов к своим плиткам (или TileGrid?), Чтобы при нажатии на плитку игрок перемещался туда, но пока мне не повезло,Если кто-нибудь сможет помочь в этом вопросе, он получит мою сердечную благодарность и 50% прав на игру, когда я продам игру LucasArts!
Геттеры и сеттеры опущены
Класс плитки
public class Tile {
private float x, y, width, height;
private Texture texture;
private TileType type;
public Tile(float x, float y, float width, float height, TileType type) {
setX(x);
setY(y);
setWidth(width);
setHeight(height);
setType(type);
setTexture(quickLoad(type.textureName));
}
public void draw() {
drawQuadTex(texture, x, y, width, height);
}
}
Класс TileGrid
public class TileGrid extends JFrame {
public Tile[][] map;
public TileGrid() {
int width = Artist.getGridWidth();
int height = Artist.getGridHeight();
map = new Tile[width][height];
for(int i = 0; i < map.length; i++) {
for(int j = 0; j < map[i].length; j++) {
map[i][j] = new Tile(i * 64, j * 64, 64, 64, TileType.Space1);
}
}
}
public static int[][] randomGridGenerator() {
Random random = new Random();
int width = Artist.getGridWidth();
int height = Artist.getGridHeight();
int[][] map = new int[width][height];
for(int i = 0; i < map.length; i++) {
for(int j = 0; j < map[i].length; j++) {
int randInt = random.nextInt(20) + 1;
map[i][j] = randInt;
}
}
return map;
}
public TileGrid(int[][] newMap) {
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
System.out.println(x + ", " + y);
}
});
int width = Artist.getGridWidth();
int height = Artist.getGridHeight();
map = new Tile[width][height];
for(int i = 0; i < map.length; i++) {
for(int j = 0; j < map[i].length; j++) {
switch (newMap[i][j]) {
case 1:
map[i][j] = new Tile(i * 64, j * 64, 64, 64, TileType.Space1);
break;
case 2:
map[i][j] = new Tile(i * 64, j * 64, 64, 64, TileType.Space2);
break;
}
}
}
}
public void setTile(int xCoord, int yCoord, TileType type) {
map[xCoord][yCoord] = new Tile(xCoord * 64, yCoord * 64, 64, 64, type);
}
public Tile getRandomTile() {
Random rand = new Random();
int width = Artist.getGridWidth();
int height = Artist.getGridHeight();
int x = rand.nextInt(width) ;
int y = rand.nextInt(height);
return map[x][y];
}
public Tile getTile(int xCoord, int yCoord) {
return map[xCoord][yCoord];
}
public void draw() {
for(int i = 0; i < map.length; i++) {
for(int j = 0; j < map[i].length; j++) {
Tile t = map[i][j];
drawQuadTex(t.getTexture(), t.getX(), t.getY(), t.getWidth(), t.getHeight());
}
}
}
Класс TileType
public enum TileType {
Space1("space64-1"), Space2("space64-2"), Space3("space64-3"), Space4("space64-4"), Space5("space64-5"),
Space6("space64-6"), Space7("space64-7"), Space8("space64-8"), Space9("space64-9"), Space10("space64-10"),
Space11("space64-11"), Space12("space64-12"), Space13("space64-13"), Space14("space64-14"), Space15("space64-15"),
Space16("space64-16"), Space17("space64-17"), Space18("space64-18"), Space19("space64-19"), Space20("space64-20");
String textureName;
TileType(String textureName) {
this.textureName = textureName;
}
}
Класс игрока
public class Player extends SpaceShip {
private TileGrid grid;
private int width, height, health;
float x, y;
private Texture texture;
private Tile locationTile;
/*
public Player(Texture texture, Tile startTile, int width, int height) {
setTexture(texture);
setX(startTile.getX());
setY(startTile.getY());
setWidth(width);
setHeight(height);
}
*/
public Player(Texture texture, TileGrid grid, int width, int height) {
setTexture(texture);
Tile tile = grid.getRandomTile();
setLocationTile(tile);
setX(tile.getX());
setY(tile.getY());
setWidth(width);
setHeight(height);
setGrid(grid);
}
public void movePlayer() {
int height = Artist.getHeight();
Tile newLocation = grid.getTile((int) Math.floor(Mouse.getX() / 64), (int) Math.floor((height - Mouse.getY() - 1)/ 64));
setLocationTile(newLocation);
setX(newLocation.getX());
setY(newLocation.getY());
}
public void draw() {
drawQuadTex(texture, x, y, width, height);
}
Точка входа
public class Boot {
public Boot() {
beginSession();
int[][] map = randomGridGenerator();
TileGrid grid = new TileGrid(map);
// Player p = new Player(quickLoad("x-wing"), grid.getTile(10, 10), 64, 64);
Player p = new Player(quickLoad("x-wing"), grid, 64, 64);
while(!Display.isCloseRequested()) {
grid.draw();
p.draw();
//p.movePlayer();
Display.update();
Display.sync(60);
}
Display.destroy();
}
public static void main(String[] args) {
new Boot();
}
}
}
Artist Class (для рендеринга и т. Д.)
public class Artist {
public static final int WIDTH = 1280, HEIGHT = 960;
public static final int GRID_WIDTH = 20, GRID_HEIGHT = 15;
public static void beginSession() {
Display.setTitle("Star Wars");
try {
Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, WIDTH, HEIGHT, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
public static void drawQuad(float x, float y, float width, float height) {
glBegin(GL_QUADS);
glVertex2f(x, y);
glVertex2f(x + width, y);
glVertex2f(x + width, y + height);
glVertex2f(x, y + height);
glEnd();
}
public static void drawQuadTex(Texture tex, float x, float y, float width, float height) {
tex.bind();
glTranslatef(x, y, 0);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(0, 0);
glTexCoord2f(1, 0);
glVertex2f(width, 0);
glTexCoord2f(1, 1);
glVertex2f(width, height);
glTexCoord2f(0, 1);
glVertex2f(0, height);
glEnd();
glLoadIdentity();
}
public static Texture loadTexture(String path, String fileType) {
Texture tex = null;
InputStream in = ResourceLoader.getResourceAsStream(path);
try {
tex = TextureLoader.getTexture(fileType, in);
} catch (IOException e) {
e.printStackTrace();
}
return tex;
}
public static Texture quickLoad(String name) {
Texture tex = null;
tex = loadTexture("resources/" + name + ".png", "PNG");
return tex;
}
В моей попытке выше событие должно просто возвращать координаты, когдаЯ нажимаю на это.Я намерен завершить логику перемещения игрока, как только я настрою прослушиватель событий.Вся помощь приветствуется.