Jump to content
  • Advertisement
Sign in to follow this  
SirHekmawful

Making Chunks load?

This topic is 2305 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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] Edited by SirHekmawful

Share this post


Link to post
Share on other sites
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].

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!