Advertisement
dermetfan

LevelGenerator version that creates Bodies

Mar 14th, 2014
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.13 KB | None | 0 0
  1. package net.dermetfan.blackpoint2;
  2.  
  3. import com.badlogic.gdx.math.MathUtils;
  4. import com.badlogic.gdx.math.Vector2;
  5. import com.badlogic.gdx.physics.box2d.Body;
  6. import com.badlogic.gdx.physics.box2d.BodyDef;
  7. import com.badlogic.gdx.physics.box2d.Fixture;
  8. import com.badlogic.gdx.physics.box2d.PolygonShape;
  9. import com.badlogic.gdx.physics.box2d.World;
  10. import com.badlogic.gdx.utils.Disposable;
  11.  
  12. public class LevelGenerator implements Disposable {
  13.  
  14.     private World world;
  15.     private BodyDef bodyDef;
  16.     private float leftEdge, rightEdge, minGap, maxGap, minWidth, maxWidth, height, angle, y;
  17.     private PolygonShape shape = new PolygonShape();
  18.  
  19.     public LevelGenerator(World world, BodyDef bodyDef, float leftEdge, float rightEdge, float minGap, float maxGap, float minWidth, float maxWidth, float height, float angle) {
  20.         this.bodyDef = bodyDef;
  21.         this.leftEdge = leftEdge;
  22.         this.rightEdge = rightEdge;
  23.         this.minGap = minGap;
  24.         this.maxGap = maxGap;
  25.         this.minWidth = minWidth;
  26.         this.maxWidth = maxWidth;
  27.         this.height = height;
  28.         this.angle = angle;
  29.     }
  30.  
  31.     public void generate(float topEdge) {
  32.         if(y + MathUtils.random(minGap, maxGap) > topEdge)
  33.             return;
  34.  
  35.         y = topEdge;
  36.         float width = MathUtils.random(minWidth, maxWidth);
  37.         float x = MathUtils.random(leftEdge, rightEdge - width);
  38.  
  39.         shape.setAsBox(width / 2, height / 2);
  40.  
  41.         bodyDef.position.set(x + width / 2, y + height / 2);
  42.         bodyDef.angle = MathUtils.random(-angle / 2, angle / 2);
  43.         Body body = world.createBody(bodyDef);
  44.         Fixture fixture = body.createFixture(shape, 0);
  45.     }
  46.  
  47.     /** @return the {@link #leftEdge} */
  48.     public float getLeftEdge() {
  49.         return leftEdge;
  50.     }
  51.  
  52.     /** @param leftEdge the {@link #leftEdge} to set */
  53.     public void setLeftEdge(float leftEdge) {
  54.         this.leftEdge = leftEdge;
  55.     }
  56.  
  57.     /** @return the {@link #rightEdge} */
  58.     public float getRightEdge() {
  59.         return rightEdge;
  60.     }
  61.  
  62.     /** @param rightEdge the {@link #rightEdge} to set */
  63.     public void setRightEdge(float rightEdge) {
  64.         this.rightEdge = rightEdge;
  65.     }
  66.  
  67.     /** @return the {@link #minGap} */
  68.     public float getMinGap() {
  69.         return minGap;
  70.     }
  71.  
  72.     /** @param minGap the {@link #minGap} to set */
  73.     public void setMinGap(float minGap) {
  74.         this.minGap = minGap;
  75.     }
  76.  
  77.     /** @return the {@link #maxGap} */
  78.     public float getMaxGap() {
  79.         return maxGap;
  80.     }
  81.  
  82.     /** @param maxGap the {@link #maxGap} to set */
  83.     public void setMaxGap(float maxGap) {
  84.         this.maxGap = maxGap;
  85.     }
  86.  
  87.     /** @return the {@link #minWidth} */
  88.     public float getMinWidth() {
  89.         return minWidth;
  90.     }
  91.  
  92.     /** @param minWidth the {@link #minWidth} to set */
  93.     public void setMinWidth(float minWidth) {
  94.         this.minWidth = minWidth;
  95.     }
  96.  
  97.     /** @return the {@link #maxWidth} */
  98.     public float getMaxWidth() {
  99.         return maxWidth;
  100.     }
  101.  
  102.     /** @param maxWidth the {@link #maxWidth} to set */
  103.     public void setMaxWidth(float maxWidth) {
  104.         this.maxWidth = maxWidth;
  105.     }
  106.  
  107.     /** @return the {@link #height} */
  108.     public float getHeight() {
  109.         return height;
  110.     }
  111.  
  112.     /** @param height the {@link #height} to set */
  113.     public void setHeight(float height) {
  114.         this.height = height;
  115.     }
  116.  
  117.     @Override
  118.     public void dispose() {
  119.         shape.dispose();
  120.     }
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement