Game.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package eu.tankernn.game;
  2. import static eu.tankernn.game.Settings.DUDV_MAP;
  3. import static eu.tankernn.game.Settings.NIGHT_TEXTURE_FILES;
  4. import static eu.tankernn.game.Settings.NORMAL_MAP;
  5. import static eu.tankernn.game.Settings.TEXTURE_FILES;
  6. import java.io.FileNotFoundException;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import org.lwjgl.util.vector.Vector3f;
  10. import org.lwjgl.util.vector.Vector4f;
  11. import eu.tankernn.gameEngine.TankernnGame;
  12. import eu.tankernn.gameEngine.entities.Entity;
  13. import eu.tankernn.gameEngine.entities.Light;
  14. import eu.tankernn.gameEngine.entities.Player;
  15. import eu.tankernn.gameEngine.entities.PlayerCamera;
  16. import eu.tankernn.gameEngine.loader.textures.TerrainTexturePack;
  17. import eu.tankernn.gameEngine.loader.textures.Texture;
  18. import eu.tankernn.gameEngine.renderEngine.DisplayManager;
  19. import eu.tankernn.gameEngine.renderEngine.Scene;
  20. import eu.tankernn.gameEngine.renderEngine.gui.GuiTexture;
  21. import eu.tankernn.gameEngine.renderEngine.skybox.Skybox;
  22. import eu.tankernn.gameEngine.renderEngine.water.WaterTile;
  23. import eu.tankernn.gameEngine.terrains.TerrainPack;
  24. import eu.tankernn.gameEngine.util.DistanceSorter;
  25. import eu.tankernn.gameEngine.util.InternalFile;
  26. import eu.tankernn.gameEngine.util.MousePicker;
  27. public class Game extends TankernnGame {
  28. MousePicker picker;
  29. List<Entity> entities;
  30. List<Light> lights;
  31. List<GuiTexture> guis;
  32. Light sun;
  33. TerrainPack terrainPack;
  34. Player player;
  35. public Game() {
  36. super(new Skybox(Texture.newCubeMap(InternalFile.fromFilenames("skybox", TEXTURE_FILES, "png"), 200), Texture.newCubeMap(InternalFile.fromFilenames("skybox", NIGHT_TEXTURE_FILES, "png"), 200), 400), DUDV_MAP, NORMAL_MAP);
  37. entities = new ArrayList<Entity>();
  38. lights = new ArrayList<Light>();
  39. sun = new Light(new Vector3f(1000, 1000, 0), new Vector3f(1f, 1f, 1f));
  40. lights.add(sun);
  41. try {
  42. setupTerrain();
  43. } catch (FileNotFoundException e) {
  44. e.printStackTrace();
  45. }
  46. setupWater();
  47. setupGuis();
  48. player = new Player(0, new Vector3f(0, 0, 0), new Vector3f(0, 0, 0), 1,
  49. loader.getModel(0).getRawModel().getBoundingBox(), terrainPack);
  50. camera = new PlayerCamera(player, terrainPack);
  51. entities.add(player);
  52. entities.add(new Entity(2, new Vector3f(0, 0, 0), new Vector3f(0, 0, 0), 1,
  53. loader.getModel(2).getRawModel().getBoundingBox()));
  54. lights.add(new Light(new Vector3f(0, 1000, 0), new Vector3f(1f, 1f, 1f)));
  55. picker = new MousePicker(camera, camera.getProjectionMatrix(), terrainPack, entities, guis);
  56. }
  57. private void setupTerrain() throws FileNotFoundException {
  58. Texture backgroundTexture = loader.loadTexture("grassy.png");
  59. Texture rTexture = loader.loadTexture("dirt.png");
  60. Texture gTexture = loader.loadTexture("pinkFlowers.png");
  61. Texture bTexture = loader.loadTexture("path.png");
  62. TerrainTexturePack texturePack = new TerrainTexturePack(backgroundTexture, rTexture, gTexture, bTexture);
  63. Texture blendMap = loader.loadTexture("blendMap.png");
  64. terrainPack = new TerrainPack(loader, texturePack, blendMap, 1337);
  65. }
  66. private void setupWater() {
  67. WaterTile water = new WaterTile(50, 50, 0, 60);
  68. waterMaster.addWaterTile(water);
  69. }
  70. private void setupGuis() {
  71. guis = new ArrayList<GuiTexture>();
  72. }
  73. public void update() {
  74. player.move();
  75. camera.update();
  76. picker.update();
  77. terrainPack.update(player);
  78. if (picker.getCurrentTerrainPoint() != null) {
  79. entities.get(1).setPosition(picker.getCurrentTerrainPoint());
  80. }
  81. }
  82. public void render() {
  83. // Sort list of lights
  84. DistanceSorter.sort(lights, camera);
  85. renderer.renderShadowMap(entities, sun);
  86. Scene scene = new Scene(entities, terrainPack, lights, camera, sky);
  87. waterMaster.renderBuffers(renderer, scene);
  88. renderer.renderScene(scene, new Vector4f(0, 1, 0, Float.MAX_VALUE));
  89. waterMaster.renderWater(camera, lights);
  90. DisplayManager.updateDisplay();
  91. }
  92. }