123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- package me.hammerle.snuviengine.game;
- import java.io.IOException;
- import me.hammerle.snuviengine.api.Chunk;
- import me.hammerle.snuviengine.api.Shader;
- import me.hammerle.snuviengine.api.Texture;
- import me.hammerle.snuviengine.util.WrappedInputStream;
- import me.hammerle.snuviengine.util.WrappedOutputStream;
- public class World
- {
- private int width;
- private int height;
- private Chunk[][] chunks;
-
- private final Texture tiles = new Texture("images/tileset-blackvolution.png");
-
- private final Hero h;
-
- private float cameraX = 0.0f;
- private float cameraY = 0.0f;
- private float lastCameraX = 0.0f;
- private float lastCameraY = 0.0f;
-
- public World(Hero h)
- {
- this.h = h;
- }
-
- public void read(WrappedInputStream in) throws IOException
- {
- width = in.readInt();
- height = in.readInt();
- if((width % Chunk.CHUNK_SIZE) != 0 || (height % Chunk.CHUNK_SIZE) != 0)
- {
- throw new IllegalArgumentException("world width and height must be a multiply of " + Chunk.CHUNK_SIZE);
- }
- chunks = new Chunk[width / Chunk.CHUNK_SIZE][height / Chunk.CHUNK_SIZE];
- for(int x = 0; x < chunks.length; x++)
- {
- for(int y = 0; y < chunks[x].length; y++)
- {
- chunks[x][y] = new Chunk(x, y);
- chunks[x][y].read(in);
- }
- }
- }
-
- public void write(WrappedOutputStream out) throws IOException
- {
- out.writeInt(width);
- out.writeInt(height);
- for(Chunk[] chunk : chunks)
- {
- for(Chunk c : chunk)
- {
- c.write(out);
- }
- }
- }
-
- public void tick()
- {
- h.tick();
- lastCameraX = cameraX;
- lastCameraY = cameraY;
- cameraX = -getViewX((float) h.xPos);
- cameraY = -getViewY((float) h.yPos);
- }
-
- public void renderTick(float lag)
- {
- Shader.translateTo(lastCameraX + (cameraX - lastCameraX) * lag, lastCameraY + (cameraY - lastCameraY) * lag);
-
- Shader.setLightLocation(0, 100 + lastCameraX + (cameraX - lastCameraX) * lag, 100 + lastCameraY + (cameraY - lastCameraY) * lag);
-
- Shader.setColorEnabled(false);
- Shader.setTextureEnabled(true);
- tiles.bind();
- for(Chunk[] chunk : chunks)
- {
- for(Chunk c : chunk)
- {
- c.drawBackground();
- }
- }
-
- Shader.updateMatrix();
- Shader.setColorEnabled(true);
- Shader.setTextureEnabled(false);
- h.renderTick(lag);
-
- Shader.setColorEnabled(false);
- Shader.setTextureEnabled(true);
- tiles.bind();
- for(Chunk[] chunk : chunks)
- {
- for(Chunk c : chunk)
- {
- c.drawForeground();
- }
- }
- }
-
- private float getViewX(float x)
- {
- x -= Shader.getViewWidth() >> 1;
- if(x < 0)
- {
- return 0;
- }
- float max = width * Chunk.TILE_SIZE - Shader.getViewWidth();
- if(x > max)
- {
- return max;
- }
- return x;
- }
-
- private float getViewY(float y)
- {
- y -= Shader.getViewHeight() >> 1;
- if(y < 0)
- {
- return 0;
- }
- float max = height * Chunk.TILE_SIZE - Shader.getViewHeight();
- if(y > max)
- {
- return max;
- }
- return y;
- }
- }
|