SpriteBatch batch;// OrthographicCamera camera;// Texture texture;// ( png - )
batch = new SpriteBatch(); batch.disableBlending(); camera = new OrthographicCamera(FIELD_SIZE, FIELD_SIZE);
public static final int FIELD_SIZE = 51;// ( ) public static float UPDATE_TIME = 0.001f;// ""
public abstract class Cell { public Color color; Sprite sprite; public Cell(Texture texture, Color color){ this.color = color; sprite = new Sprite(texture); sprite.setColor(color); sprite.setSize(1, 1); } public abstract void update(Cell[][] map, int x, int y, Texture texture); public void setColor(Color color){ this.color = color; sprite.setColor(color); } public void draw(SpriteBatch batch,int x, int y){ sprite.setPosition(x-Main.FIELD_SIZE/2-sprite.getWidth()/2, y-Main.FIELD_SIZE/2-sprite.getHeight()/2); sprite.draw(batch); } }
public class Wall extends Cell { public Wall(Texture texture) { super(texture, new Color(0f, 0f, 0f, 1)); } @Override public void update(Cell[][] map, int x, int y, Texture texture) { } } public class Empty extends Cell { public Empty(Texture texture) { super(texture, new Color(1, 1, 1, 1)); } @Override public void update(Cell[][] map, int x, int y, Texture texture) { } }
Cell[][] map;
map = new Cell[FIELD_SIZE][FIELD_SIZE]; texture = new Texture(Gdx.files.internal("tile.png"));// char[][] bmap = (new MazeGenerator()).getMaze(FIELD_SIZE - 1); for (int i = 0; i < FIELD_SIZE; i++) for (int j = 0; j < FIELD_SIZE; j++) { if (bmap[i][j] == 0) map[i][j] = new Empty(texture); if (bmap[i][j] == 1) map[i][j] = new Wall(texture); }
public class Unit extends Cell { Cell[][] my_map = new Cell[3][3];// , float update_time = Main.UPDATE_TIME;// int mapX = 1, mapY = 1;// Vector<Action> queue = new Vector<Action>();// enum Action { left, right, up, down// } public Unit(Texture texture, Cell[][] map, int x, int y) { super(texture, new Color(1f, 0, 0, 1)); for (int i = x - 1; i <= x + 1; i++) for (int j = y - 1; j <= y + 1; j++) my_map[i - x + 1][j - y + 1] = map[i][Main.FIELD_SIZE - j - 1]; my_map[1][1] = this; homeX = 1; homeY = 1; } private int goRight(Cell[][] map, int x, int y, Texture texture) {...}//map - , , x,y - private int goLeft(Cell[][] map, int x, int y, Texture texture) {...} private int goUp(Cell[][] map, int x, int y, Texture texture) {...} private int goDown(Cell[][] map, int x, int y, Texture texture) {...}
private int goRight(Cell[][] map, int x, int y, Texture texture) {...}//map - , private int goLeft(Cell[][] map, int x, int y, Texture texture) {...}//x,y - private int goUp(Cell[][] map, int x, int y, Texture texture) {...} private int goDown(Cell[][] map, int x, int y, Texture texture) {...}
public static Vector<Action> getPath(Cell[][] my_map, int x, int y, int nx,int ny){...}
@Override public void render() { this.update();// Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); batch.setProjectionMatrix(camera.combined); batch.begin(); for (int i = 0; i < FIELD_SIZE; i++) for (int j = 0; j < FIELD_SIZE; j++) if(!(map[i][j] instanceof Wall))// map[i][j].draw(batch, i, j); batch.end(); } public void update() { Input input = Gdx.input; for (int i = 0; i < FIELD_SIZE; i++) for (int j = 0; j < FIELD_SIZE; j++) map[i][j].update(map, i, j, texture);// if(input.isKeyPressed(Input.Keys.W))// , , , camera.zoom-=Gdx.graphics.getDeltaTime(); if(input.isKeyPressed(Input.Keys.S)) camera.zoom+=Gdx.graphics.getDeltaTime(); if(input.isKeyPressed(Input.Keys.Q)) camera.rotate(Gdx.graphics.getDeltaTime()*90); if(input.isKeyPressed(Input.Keys.E)) camera.rotate(-Gdx.graphics.getDeltaTime()*90); if(input.isKeyPressed(Input.Keys.CONTROL_LEFT)) UPDATE_TIME+=Gdx.graphics.getDeltaTime(); if(input.isKeyPressed(Input.Keys.SHIFT_LEFT)) UPDATE_TIME-=Gdx.graphics.getDeltaTime(); if(input.isKeyPressed(Input.Keys.LEFT)) camera.translate(new Vector2(-Gdx.graphics.getDeltaTime()*50,0)); if(input.isKeyPressed(Input.Keys.RIGHT)) camera.translate(new Vector2(Gdx.graphics.getDeltaTime()*50,0)); if(input.isKeyPressed(Input.Keys.UP)) camera.translate(new Vector2(0,Gdx.graphics.getDeltaTime()*50)); if(input.isKeyPressed(Input.Keys.DOWN)) camera.translate(new Vector2(0,-Gdx.graphics.getDeltaTime()*50)); if(input.isKeyPressed(Input.Keys.SPACE)){// UPDATE_TIME = 1f; camera = new OrthographicCamera(FIELD_SIZE, FIELD_SIZE); } camera.update(); if (input.isTouched()) {// float stepX = Gdx.graphics.getWidth() / FIELD_SIZE; float stepY = Gdx.graphics.getHeight() / FIELD_SIZE; float x = input.getX(); float y = input.getY(); for (int i = 0; i < FIELD_SIZE; i++) for (int j = 0; j < FIELD_SIZE; j++) { if (x >= stepX * i && x <= stepX * (i + 1) && y >= stepY * j && y <= stepY * (j + 1)) if (map[i][FIELD_SIZE - j - 1] instanceof Empty) map[i][FIELD_SIZE - j - 1] = new Unit(texture, map, i, j); } } }
Source: https://habr.com/ru/post/224525/
All Articles