java - Updating only items near the player -
since hit out of memory error, have been trying improve code items closest player update , not @ once, overloading system.
firstly world[][]
multi-dimensional array holding random integer values inside, being: 0 (nothing), 1(star), 2(planet).
next take world[][]
, individually create each planet , star own values in bodymanager class
that , until started adding features caused overload in memory. changed code nearby area scanned , when star found (since planets form around stars solar system) bodymanager class called , creates planets around star star itself.
i running types of issues trying method (eg. stars , planets not render, out of bounds errors world[][]
etc.). seeking best method stop memory overloads loading map @ manageable pace. if think current approach on right track having problems loop in update()
method running , not updating surroundings properly.
finally, there better way? if yes, please point me in right direction.
otherwise how can fix issues loop , getting items render?
please notify me if more information needed!
update , render methods in world class (where bodymanager called from)
// bodymanager constructor called earlier public void update() { playerx = (int) entitymanager.getplayer().getx(); playery = (int) entitymanager.getplayer().gety(); (int x=(playerx - 30) / bodymanager.tile_width; x<(playerx + 31) / bodymanager.tile_width; x++) { (int y=(playery - 30) / bodymanager.tile_height; y<(playery + 31) / bodymanager.tile_height; y++) { system.out.println(x); if (playerx < 0 || playerx > width || playery < 0 || playery > height) break; else if (world[x][y] == 1) { bodymanager.initbody(x, y); } } } entitymanager.update(); } public void render(graphics g) { int xstart = (int) math.max(0, handler.getgamecamera().getxoffset() / bodymanager.tile_width); int xend = (int) math.min(width, (handler.getgamecamera().getxoffset() + handler.getwidth()) / bodymanager.tile_width + 1); int ystart = (int) math.max(0, handler.getgamecamera().getyoffset() / bodymanager.tile_height); int yend = (int) math.min(height, (handler.getgamecamera().getyoffset() + handler.getheight()) / bodymanager.tile_height + 1); (int y=ystart; y<yend; y++) { (int x=xstart; x<xend; x++) { bodymanager.render(g); } } entitymanager.render(g); }
bodymanager class
public class bodymanager { //static public final static int tile_width = 20, tile_height = 20; //class private handler handler; private int[][] world; private map<integer, arraylist<bodies>> map; public bodymanager(handler handler, int[][] world) { this.handler = handler; this.world = world; map = new hashmap<>(); map.put(1, new arraylist<>()); //star map.put(2, new arraylist<>()); //planet } public void initbody(int starx, int stary) { system.out.println("solar sytem initialized"); map.get(1).add(new star( handler, starx, stary, randint(512, 1024) )); (int x=(starx-6); x<(starx+7); x++) { (int y=(stary - 6); y<(stary+7); y++) { if (world[x][y] == 2) { map.get(2).add(new planet( handler, x, y, randint(256, 512), randfloat(0, 4), randint(0, 360) )); } } } } public void update() { (arraylist<bodies> bodies : map.values()) { (int = 0; i<bodies.size(); i++) { bodies b = bodies.get(i); b.update(); } } } public void render(graphics g) { (arraylist<bodies> bodies : map.values()) { (bodies b : bodies) { b.render(g); } } } public float randfloat(int min, int max) { random rand = new random(); float randfloat = min + (max - min) * rand.nextfloat(); return randfloat; } public int randint (int min, int max) { random rand = new random(); int randint = rand.nextint((max - min) + 1) + min; return randint; } }
Comments
Post a Comment