You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
3.4 KiB

  1. package roadtrip.model;
  2. import com.jme3.bullet.collision.shapes.ConeCollisionShape;
  3. import com.jme3.bullet.control.RigidBodyControl;
  4. import com.jme3.math.Vector2f;
  5. import com.jme3.math.Vector3f;
  6. import com.jme3.scene.Spatial;
  7. import com.jme3.terrain.geomipmap.TerrainQuad;
  8. import java.util.Collections;
  9. import java.util.LinkedList;
  10. import java.util.List;
  11. import java.util.Random;
  12. /**
  13. * Created by dejvino on 21.01.2017.
  14. */
  15. public class ProceduralMapQuadBlock extends AbstractProceduralBlock
  16. {
  17. private List<MapObjectInstance> mapObjects;
  18. public ProceduralMapQuadBlock(long seed)
  19. {
  20. super(seed);
  21. }
  22. public void initialize(TerrainQuad terrainQuad)
  23. {
  24. float cellSize = terrainQuad.getPatchSize() * 8f * terrainQuad.getLocalScale().x * 2f;
  25. mapObjects = new LinkedList<>();
  26. Random quadRand = getBlockRandom();
  27. Vector2f prevPos = null;
  28. Vector2f quadPos = new Vector2f(terrainQuad.getWorldTranslation().x, terrainQuad.getWorldTranslation().z);
  29. for (int i = 0; i < quadRand.nextInt(terrainQuad.getPatchSize() * terrainQuad.getPatchSize()); i++) {
  30. Vector2f pos;
  31. if (prevPos == null || quadRand.nextFloat() < 0.2f) {
  32. pos = new Vector2f((quadRand.nextFloat() - 0.5f) * cellSize, (quadRand.nextFloat() - 0.5f) * cellSize)
  33. .addLocal(quadPos);
  34. } else {
  35. pos = new Vector2f((quadRand.nextFloat() - 0.5f) * (cellSize / 10f), (quadRand.nextFloat() - 0.5f) * (cellSize / 10f))
  36. .addLocal(prevPos);
  37. }
  38. prevPos = pos;
  39. float height = terrainQuad.getHeight(pos);
  40. String type;
  41. if (quadRand.nextInt(10) == 0) {
  42. type = "rock";
  43. } else {
  44. type = "tree";
  45. }
  46. Vector3f location = new Vector3f(pos.x, height, pos.y);
  47. mapObjects.add(new MapObjectInstance(type, location));
  48. }
  49. // walls
  50. if (quadRand.nextInt(3) == 0) {
  51. int dir = quadRand.nextInt(4);
  52. for (int i = 0; i < quadRand.nextInt(terrainQuad.getPatchSize() * (10 + quadRand.nextInt(100))); i++) {
  53. Vector2f pos;
  54. if (prevPos == null || quadRand.nextFloat() < 0.1f) {
  55. pos = new Vector2f((quadRand.nextFloat() - 0.5f) * cellSize, (quadRand.nextFloat() - 0.5f) * cellSize)
  56. .addLocal(quadPos);
  57. } else {
  58. if (quadRand.nextInt(10) == 0) {
  59. dir = (dir + quadRand.nextInt(2) * 2 - 1) % 4;
  60. }
  61. pos = new Vector2f(dir == 1 ? 1 : (dir == 3 ? -1 : 0), dir == 0 ? 1 : (dir == 2 ? -1 : 0)).addLocal(prevPos);
  62. }
  63. prevPos = pos;
  64. float height = terrainQuad.getHeight(pos);
  65. String type = "wall";
  66. Vector3f location = new Vector3f(pos.x, height, pos.y);
  67. mapObjects.add(new MapObjectInstance(type, location));
  68. }
  69. }
  70. }
  71. public Iterable<? extends MapObjectInstance> getMapObjects()
  72. {
  73. if (mapObjects == null) throw new RuntimeException("Call to getMapObjects on an uninitialized block.");
  74. return mapObjects;
  75. }
  76. }