World.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package me.hammerle.snuviengine.game;
  2. import java.io.IOException;
  3. import me.hammerle.snuviengine.api.Chunk;
  4. import me.hammerle.snuviengine.api.Shader;
  5. import me.hammerle.snuviengine.api.Texture;
  6. import me.hammerle.snuviengine.util.WrappedInputStream;
  7. import me.hammerle.snuviengine.util.WrappedOutputStream;
  8. public class World
  9. {
  10. private int width;
  11. private int height;
  12. private Chunk[][] chunks;
  13. private final Texture tiles = new Texture("images/tileset-blackvolution.png");
  14. private final Hero h;
  15. private float cameraX = 0.0f;
  16. private float cameraY = 0.0f;
  17. private float lastCameraX = 0.0f;
  18. private float lastCameraY = 0.0f;
  19. public World(Hero h)
  20. {
  21. this.h = h;
  22. }
  23. public void read(WrappedInputStream in) throws IOException
  24. {
  25. width = in.readInt();
  26. height = in.readInt();
  27. if((width % Chunk.CHUNK_SIZE) != 0 || (height % Chunk.CHUNK_SIZE) != 0)
  28. {
  29. throw new IllegalArgumentException("world width and height must be a multiply of " + Chunk.CHUNK_SIZE);
  30. }
  31. chunks = new Chunk[width / Chunk.CHUNK_SIZE][height / Chunk.CHUNK_SIZE];
  32. for(int x = 0; x < chunks.length; x++)
  33. {
  34. for(int y = 0; y < chunks[x].length; y++)
  35. {
  36. chunks[x][y] = new Chunk(x, y);
  37. chunks[x][y].read(in);
  38. }
  39. }
  40. }
  41. public void write(WrappedOutputStream out) throws IOException
  42. {
  43. out.writeInt(width);
  44. out.writeInt(height);
  45. for(Chunk[] chunk : chunks)
  46. {
  47. for(Chunk c : chunk)
  48. {
  49. c.write(out);
  50. }
  51. }
  52. }
  53. public void tick()
  54. {
  55. h.tick();
  56. lastCameraX = cameraX;
  57. lastCameraY = cameraY;
  58. cameraX = -getViewX((float) h.xPos);
  59. cameraY = -getViewY((float) h.yPos);
  60. }
  61. public void renderTick(float lag)
  62. {
  63. Shader.translateTo(lastCameraX + (cameraX - lastCameraX) * lag, lastCameraY + (cameraY - lastCameraY) * lag);
  64. Shader.setLightLocation(0, 100 + lastCameraX + (cameraX - lastCameraX) * lag, 100 + lastCameraY + (cameraY - lastCameraY) * lag);
  65. Shader.setColorEnabled(false);
  66. Shader.setTextureEnabled(true);
  67. tiles.bind();
  68. for(Chunk[] chunk : chunks)
  69. {
  70. for(Chunk c : chunk)
  71. {
  72. c.drawBackground();
  73. }
  74. }
  75. Shader.updateMatrix();
  76. Shader.setColorEnabled(true);
  77. Shader.setTextureEnabled(false);
  78. h.renderTick(lag);
  79. Shader.setColorEnabled(false);
  80. Shader.setTextureEnabled(true);
  81. tiles.bind();
  82. for(Chunk[] chunk : chunks)
  83. {
  84. for(Chunk c : chunk)
  85. {
  86. c.drawForeground();
  87. }
  88. }
  89. }
  90. private float getViewX(float x)
  91. {
  92. x -= Shader.getViewWidth() >> 1;
  93. if(x < 0)
  94. {
  95. return 0;
  96. }
  97. float max = width * Chunk.TILE_SIZE - Shader.getViewWidth();
  98. if(x > max)
  99. {
  100. return max;
  101. }
  102. return x;
  103. }
  104. private float getViewY(float y)
  105. {
  106. y -= Shader.getViewHeight() >> 1;
  107. if(y < 0)
  108. {
  109. return 0;
  110. }
  111. float max = height * Chunk.TILE_SIZE - Shader.getViewHeight();
  112. if(y > max)
  113. {
  114. return max;
  115. }
  116. return y;
  117. }
  118. }