Making Chunks load?

Started by
1 comment, last by Exessuz 11 years, 8 months ago
Hello.
I've been coding for some time now, and just can't seem to find a way to load chunks?
I'm making a game, which has a big world.
The map is loaded using a scanner, from a text file, the map is pre-made.
A map example:
ggggwgg
ggggwgg
wgggggg
bbbgggg


g, w, and b is tiles.
The map is really big, and it's gonna begin lagging for people if I don't make chunk loading.
I think I can figure out for myself how to make the player stay in the middle of the screen while loading chunks.

[spoiler]import java.awt.Graphics; import java.awt.Image; import javax.swing.*; public class Screen extends JPanel implements Runnable { public static Thread gameLoop; public static int worldWidth = Frame.sizer.getFrameWidth(); public static int worldHeight = Frame.sizer.getFrameHeight(); public static int blockSize = 64; Image grass; Map map; public Screen() { map = new Map(); ImageIcon img = new ImageIcon("Res/g.png"); grass = img.getImage(); gameLoop = new Thread(this); gameLoop.start(); } public void paintComponent(Graphics g) { for (int x = 0; x < worldWidth; x++) { for (int y = 0; y < worldHeight; y++) { if (map.getMap(x, y).equals("g")) { g.drawImage(grass, x * blockSize, y * blockSize, blockSize, blockSize, null); } } } } public void run() { while (true) { repaint(); try { Thread.sleep(25); } catch (Exception e) { } } } } [/spoiler]

[spoiler]import java.io.File; import java.util.Scanner; public class Map { public static String[] World = new String[Screen.worldHeight]; public Map() { openFile(); readFile(); closeFile(); } Scanner scan; public String getMap(int x, int y) { String index = World[y].substring(x, x + 1); return index; } public void openFile() { try { scan = new Scanner(new File("Levels/Test")); } catch (Exception e) { } } public void readFile() { while (scan.hasNext()) { for (int i = 0; i < Screen.worldHeight; i++) { World = scan.next(); } } } public void closeFile() { scan.close(); } } [/spoiler]
Advertisement

  1. Please use source tags (hit the button on the second row, far right side).
  2. Have you actually ascertained that this is a problem?
  3. If it is a problem, why not chunk load using threads? There are a myriad of transition schemes - such as keeping the world (nxm tiles) in memory with an additional +1 tile offset in each direction so the player can "move" and give you time to chuck the area they moved away from and preload whatever [non-visible] map they might be near. Even if you did this in a single thread I don't see it being an overly large problem [for you].
http://vterrain.org/LOD/Implementations/

this site has many implementations of LODs algorithms

This topic is closed to new replies.

Advertisement