World.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.setColorEnabled(false);
  65. Shader.setTextureEnabled(true);
  66. tiles.bindTexture();
  67. for(Chunk[] chunk : chunks)
  68. {
  69. for(Chunk c : chunk)
  70. {
  71. c.drawBackground();
  72. }
  73. }
  74. Shader.updateMatrix();
  75. Shader.setColorEnabled(true);
  76. Shader.setTextureEnabled(false);
  77. h.renderTick(lag);
  78. Shader.setColorEnabled(false);
  79. Shader.setTextureEnabled(true);
  80. tiles.bindTexture();
  81. for(Chunk[] chunk : chunks)
  82. {
  83. for(Chunk c : chunk)
  84. {
  85. c.drawForeground();
  86. }
  87. }
  88. }
  89. private float getViewX(float x)
  90. {
  91. x -= Shader.getViewWidth() >> 1;
  92. if(x < 0)
  93. {
  94. return 0;
  95. }
  96. float max = width * Chunk.TILE_SIZE - Shader.getViewWidth();
  97. if(x > max)
  98. {
  99. return max;
  100. }
  101. return x;
  102. }
  103. private float getViewY(float y)
  104. {
  105. y -= Shader.getViewHeight() >> 1;
  106. if(y < 0)
  107. {
  108. return 0;
  109. }
  110. float max = height * Chunk.TILE_SIZE - Shader.getViewHeight();
  111. if(y > max)
  112. {
  113. return max;
  114. }
  115. return y;
  116. }
  117. }