News:

11 March 2016 - Forum Rules

Main Menu
Menu

Show posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Show posts Menu

Messages - Lloyd2k4

#1
Disch, I had already tried there and it didn't work, BUT you are correct that it is supposed to go there.  I figured out *why* it wasn't working even though it is supposed to be there.

It was in my render() method for the World:


int xStart = (int) Math.max(0, handler.getGameCamera().getxOffset() / Constants.DEFAULT_WIDTH);
int xEnd = (int) Math.min(width, (handler.getGameCamera().getxOffset() + handler.getWidth()) / Constants.DEFAULT_WIDTH + 1);
int yStart = (int) Math.max(0, handler.getGameCamera().getyOffset() / Constants.DEFAULT_HEIGHT);
int yEnd = (int) Math.min(height, (handler.getGameCamera().getyOffset() + handler.getHeight()) / Constants.DEFAULT_HEIGHT + 1);


Changed to:


int xStart = (int) handler.getGameCamera().getxOffset() / Constants.DEFAULT_WIDTH - 1;
int xEnd = (int) (handler.getGameCamera().getxOffset() + handler.getWidth()) / Constants.DEFAULT_WIDTH + 1;
int yStart = (int) handler.getGameCamera().getyOffset() / Constants.DEFAULT_HEIGHT - 1;
int yEnd = (int) (handler.getGameCamera().getyOffset() + handler.getHeight()) / Constants.DEFAULT_HEIGHT + 1;


The min and max methods were restricting everything because it was never allowing anything beyond the bounds of the map to render.

And here's what I added to getTile() just for reference:


public Tile getTile(int x, int y){
x = (x % width + width) % width;
y = (y % height + height) % height;

Tile t = Tile.tiles[ tiles[x][y] ];
if(t == null)
return Tile.tiles[0];
else
return t;
}


It appears the formula used in that stackoverflow method above works fine.
#2
So, I'm encountering an issue on where exactly I need to place this code.  I've been trying different areas in my World class, and can't figure out if I need to put it in the loading routine, the render routine, or both.  So far, everything I've tried has resulted in no change. :angel:

My guess is I may need to re-engineer the way my World class works in order to get this to work properly.


public class World {

protected Handler handler;
private int width, height;
private int spawnX, spawnY;
private int[][] tiles;

private EntityManager entityManager;

/**
* Generic World constructor
* @param handler
* @param path
*/
public World(Handler handler, String path){
this.handler = handler;
loadWorld(path);
// TODO: need to not hardcode this but rather go based on the top most job of the player party later on, also
// need to make a facing up sprite and a facing down sprite, and eventually an animation walking sprite
BufferedImage[] sprites = new BufferedImage[] {Assets.getTile(7), Assets.getTile(8), Assets.getTile(9), Assets.getTile(10)};
entityManager = new EntityManager(handler,
new Player(handler,spawnX * Constants.DEFAULT_WIDTH,spawnY * Constants.DEFAULT_HEIGHT,Constants.DEFAULT_WIDTH, Constants.DEFAULT_HEIGHT,sprites));
}
/**
* This method ticks all the entities within this World.
*/
public void update(){
entityManager.update();
}
/**
* This method renders the map, but will only render the tiles that are within our camera's field of view
* plus a little extra
* @param g - Graphics object
*/
public void render(Graphics g){
int xStart = (int) Math.max(0, handler.getGameCamera().getxOffset() / Constants.DEFAULT_WIDTH);
int xEnd = (int) Math.min(width, (handler.getGameCamera().getxOffset() + handler.getWidth()) / Constants.DEFAULT_WIDTH + 1);
int yStart = (int) Math.max(0, handler.getGameCamera().getyOffset() / Constants.DEFAULT_HEIGHT);
int yEnd = (int) Math.min(height, (handler.getGameCamera().getyOffset() + handler.getHeight()) / Constants.DEFAULT_HEIGHT + 1);
// render each tile within the calculated camera view
for(int y = yStart; y < yEnd; y++){
for(int x = xStart; x < xEnd; x++){
getTile(x,y).render(g, (int)(x * Constants.DEFAULT_WIDTH - handler.getGameCamera().getxOffset()),
(int)(y * Constants.DEFAULT_HEIGHT - handler.getGameCamera().getyOffset()));
}
}
entityManager.render(g);
}
/**
* This method will return the actual Tile object which contains the image
* and any tile specific data based on the id stored in the tiles[][] 2d array.
* This is both null safe and out of bounds safe; it will return a default tile
* in either case.
* @param x - x tile coordinate
* @param y - y tile coordinate
* @return - Tile object to be rendered at x,y (in tiles, not pixels)
*/
public Tile getTile(int x, int y){
if( x < 0 || y < 0 || x >= width || y >= height )
return Tile.tiles[0];

Tile t = Tile.tiles[ tiles[x][y] ];
if(t == null)
return Tile.tiles[0];
else
return t;

}
/**
* This method will load the world file from a text file and tokenize it
* , then apply the necessary data to this World object.
* @param path - the path to the resource text file.
*/
private void loadWorld(String path){
String file = Utils.loadFile(path);
String[] tokens = file.split("\\s+");
// the first few tokens will always be this data
width = Utils.parseInt(tokens[0]);
height = Utils.parseInt(tokens[1]);
spawnX = Utils.parseInt(tokens[2]);
spawnY = Utils.parseInt(tokens[3]);
// begin allocating the tile id's for each tile of the map
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] );
}
}
}
}


Also, I found this little stackoverflow of the whole modulo operator discussion for the purpose of this:

http://stackoverflow.com/questions/30698661/java-2d-array-making-it-torus-like
#3
Disch, you are a savant. Thank you so much!  I just tested your code in a java for loop trying 2^9 (512) and it properly loops back to zero after hitting 511. Anything that isnt a perfect bit constraint 2^n doesnt work properly and seems to do a looping pattern with the remainder from the closest bit, but anything that is a valid bit constraint 2^n where n is any integer, works.

I had no idea it was this simple.
#4
Hello everyone,

Im looking for anyone who has exerience modding or dealing with any game that has a torus algorithm implemented in order to wrap its 2d tile maps.

An example of a game that does this is the original final fantasy on the NES.

Im trying to develop a similar algorithm to replicate this feature in a stand alone original project, but im not having much luck.

If anyone knows anything about this subject or knows how some old school games implemented it, I would surely appreciate listening to what you have to say!

I apologize for my lack of grammar. Im posting this from my android browser so I cant type as well as I normally would.