我的问题是:
为什么在游戏中添加实体(entityManager.addEntity(new Tree(handler, 19 * 64, 7 * 64));
)一次添加2个实体?
我添加了8棵新树,得到了12棵,所以我很困惑
最重要的是,我有一个world.txt文件,里面有我的全世界的瓷砖
世界级在这里:
public class World {
private Handler handler;
private int width, height;
private int spawnX, spawnY;
private int[][] tiles;
//Entities
private EntityManager entityManager;
public World(Handler handler, String path){
this.handler = handler;
entityManager = new EntityManager(handler, new Player(handler, 100, 100));
entityManager.addEntity(new Tree(handler, 16 * 64, 7 * 64));
entityManager.addEntity(new Tree(handler, 17 * 64, 7 * 64));
entityManager.addEntity(new Tree(handler, 18 * 64, 7 * 64));
entityManager.addEntity(new Tree(handler, 19 * 64, 7 * 64));
entityManager.addEntity(new Tree(handler, 16 * 64, 6 * 64));
entityManager.addEntity(new Tree(handler, 17 * 64, 6 * 64));
entityManager.addEntity(new Tree(handler, 18 * 64, 6 * 64));
entityManager.addEntity(new Tree(handler, 19 * 64, 6 * 64));
entityManager.addEntity(new Rock(handler, 100, 450));
loadWorld(path);
entityManager.getPlayer().setX(spawnX);
entityManager.getPlayer().setY(spawnY);
}
public void tick(){
entityManager.tick();
}
public void render(Graphics g){
int xStart = (int) Math.max(0, handler.getGameCamera().getxOffset() / Tile.TILEWIDTH);
int xEnd = (int) Math.min(width, (handler.getGameCamera().getxOffset() + handler.getWidth()) / Tile.TILEWIDTH + 1);
int yStart = (int) Math.max(0, handler.getGameCamera().getyOffset() / Tile.TILEHEIGHT);
int yEnd = (int) Math.min(height, (handler.getGameCamera().getyOffset() + handler.getHeight()) / Tile.TILEHEIGHT + 1);
for(int y = yStart;y < yEnd;y++){
for(int x = xStart;x < xEnd;x++){
getTile(x, y).render(g, (int) (x * Tile.TILEWIDTH - handler.getGameCamera().getxOffset()),
(int) (y * Tile.TILEHEIGHT - handler.getGameCamera().getyOffset()));
}
}
//Entities
entityManager.render(g);
}
public Tile getTile(int x, int y){
if(x < 0 || y < 0 || x >= width || y >= height)
return Tile.grassTile;
Tile t = Tile.tiles[tiles[x][y]];
if(t == null)
return Tile.dirtTile;
return t;
}
private void loadWorld(String path){
String file = Utils.loadFileAsString(path);
String[] tokens = file.split("\\s+");
width = Utils.parseInt(tokens[0]);
height = Utils.parseInt(tokens[1]);
spawnX = Utils.parseInt(tokens[2]);
spawnY = Utils.parseInt(tokens[3]);
tiles = new int[width][height];
for(int y = 0;y < height;y++){
for(int x = 0;x < width;x++){
tiles[x][y] = Utils.parseInt(tokens[(x + y * width) + 4]);
}
}
}
public int getWidth(){
return width;
}
public int getHeight(){
return height;
}
public EntityManager getEntityManager() {
return entityManager;
}
}
我的EntityManager类在这里:
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Comparator;
import dev.blaze.tilegame.Handler;
import dev.blaze.tilegame.entity.creatures.*;
public class EntityManager {
private Handler handler;
private Player player;
private ArrayList<Entity> entities;
private Comparator<Entity> renderSorter = new Comparator<Entity>(){
@Override
public int compare(Entity a, Entity b) {
if(a.getY() + a.getHeight() < b.getY() + b.getHeight())
return -1;
return 1;
}
};
public EntityManager(Handler handler, Player player){
this.handler = handler;
this.player = player;
entities = new ArrayList<Entity>();
addEntity(player);
}
public void tick(){
for(int i = 0;i < entities.size();i++){
Entity e = entities.get(i);
e.tick();
if(!e.isActive())
entities.remove(e);
}
entities.sort(renderSorter);
}
public void render(Graphics g){
for(Entity e : entities){
e.render(g);
}
}
public void addEntity(Entity e){
entities.add(e);
}
//GETTERS SETTERS
public Handler getHandler() {
return handler;
}
public void setHandler(Handler handler) {
this.handler = handler;
}
public Player getPlayer() {
return player;
}
public void setPlayer(Player player) {
this.player = player;
}
public ArrayList<Entity> getEntities() {
return entities;
}
public void setEntities(ArrayList<Entity> entities) {
this.entities = entities;
}
}
我不认为错误发生在EntityManager类中,它可能是我创建Tree类的地方,或者只是eclipse的错误。
树类在这里:
public class Tree extends StaticEntity {
public Tree(Handler handler, float x, float y) {
super(handler, x, y, Tile.TILEWIDTH, Tile.TILEHEIGHT * 2);
bounds.x = 0;
bounds.y = 0;
bounds.width = width;
bounds.height = height;
}
@Override
public void tick() {
}
@Override
public void die(){
}
@Override
public void render(Graphics g) {
g.drawImage(Assets.log, (int) (x - handler.getGameCamera().getxOffset()), (int) (y - handler.getGameCamera().getyOffset()), width, height, null);
}
}
如果您需要了解其他课程,请在评论中告诉我。
答案 0 :(得分:0)
在树类中,我没有放宽度和高度,而是将其替换为64并且只获得了一个树/日志而不是两个。
而不是:
g.drawImage(Assets.log, (int) (x - handler.getGameCamera().getxOffset()), (int) (y - handler.getGameCamera().getyOffset()), width, height, null);
它是:
g.drawImage(Assets.log, (int) (x - handler.getGameCamera().getxOffset()), (int) (y - handler.getGameCamera().getyOffset()), 64, 64, null);