Coverage Summary for Class: PhysicsManager (com.mygdx.game.Managers)

Class Class, % Method, % Line, %
PhysicsManager 100% (1/1) 84.6% (11/13) 78% (46/59)


1 package com.mygdx.game.Managers; 2  3 import com.badlogic.gdx.maps.MapLayer; 4 import com.badlogic.gdx.maps.MapLayers; 5 import com.badlogic.gdx.maps.MapObject; 6 import com.badlogic.gdx.maps.MapObjects; 7 import com.badlogic.gdx.maps.objects.RectangleMapObject; 8 import com.badlogic.gdx.math.Rectangle; 9 import com.badlogic.gdx.math.Vector2; 10 import com.badlogic.gdx.physics.box2d.*; 11 import com.mygdx.game.Components.TileMap; 12  13 import java.util.ArrayList; 14 import java.util.Objects; 15  16 import static com.mygdx.utils.Constants.PHYSICS_TIME_STEP; 17  18 /** 19  * Manages the box2D world and bodies for the collision detection and physics 20  */ 21 public final class PhysicsManager { 22  private static final float TILE_SIZE_INV = 1.0f; 23  public static boolean initialized = false; 24  public static World box2DWorld; 25  private static ArrayList<Body> box2DBodies; 26  private static Box2DDebugRenderer debug; 27  private static ArrayList<Integer> toRemove; 28  public static void Initialize() { 29  Initialize(false); 30  } 31  32  /** 33  * Draw the box2D world with debug borders shown. 34  * 35  * @param drawDebug true to show debug borders 36  */ 37  public static void Initialize(boolean drawDebug) { 38  if (initialized) { 39  return; 40  } 41  initialized = true; 42  box2DWorld = new World(new Vector2(0, 0), true); 43  box2DBodies = new ArrayList<>(); 44  box2DWorld.setContactListener(new CollisionManager()); 45  if (drawDebug) { 46  debug = new Box2DDebugRenderer(true, false, true, true, false, true); 47  } 48  toRemove = new ArrayList<Integer>(); 49  } 50  51  public static int createBody(BodyDef bDef, FixtureDef fDef, Object userData) { 52  tryInit(); 53  bDef.fixedRotation = true; 54  Body b = box2DWorld.createBody(bDef); 55  b.createFixture(fDef); 56  b.setUserData(userData); 57  box2DBodies.add(b); 58  return box2DBodies.size(); 59  } 60  public static void deleteBody(int index){ 61  toRemove.add(index); 62  } 63  64  private static Shape tile_getShape(Rectangle rectangle) { 65  PolygonShape polygonShape = new PolygonShape(); 66  polygonShape.setAsBox( 67  rectangle.width * 0.5f * TILE_SIZE_INV, 68  rectangle.height * 0.5f * TILE_SIZE_INV); 69  return polygonShape; 70  } 71  72  private static Vector2 tile_getCenter(Rectangle rectangle) { 73  Vector2 center = new Vector2(); 74  rectangle.getCenter(center); 75  return center.scl(TILE_SIZE_INV); 76  } 77  78  /** 79  * Populates the map with box2D bodies necessary for collisions to happen. 80  * 81  * @param map tilemap we are operating on 82  */ 83  public static void createMapCollision(TileMap map) { 84  MapLayers layers = map.getTileMap().getLayers(); 85  MapObjects objects = null; 86  for (MapLayer layer : layers) { 87  if (Objects.equals(layer.getName(), "Objects")) { 88  objects = layer.getObjects(); 89  break; 90  } 91  } 92  if (objects == null) { 93  return; 94  } 95  96  for (MapObject object : objects) { 97  Rectangle rectangle = ((RectangleMapObject) object).getRectangle(); 98  99  //create a dynamic within the world body (also can be KinematicBody or StaticBody 100  BodyDef bodyDef = new BodyDef(); 101  bodyDef.type = BodyDef.BodyType.StaticBody; 102  Body body = box2DWorld.createBody(bodyDef); 103  104  //create a fixture for each body from the shape 105  Fixture fixture = body.createFixture(tile_getShape(rectangle), 1); 106  fixture.setFriction(0.1f); 107  108  //setting the position of the body's origin. In this case with zero rotation 109  body.setTransform(tile_getCenter(rectangle), 0); 110  } 111  } 112  113  public static Body getBody(int id) { 114  return box2DBodies.get(id - 1); 115  } 116  117  private static void tryInit() { 118  if (!initialized) { 119  Initialize(); 120  } 121  } 122  123  public static void update() { 124  tryInit(); 125  box2DWorld.step(PHYSICS_TIME_STEP, 6, 2); 126  if(!toRemove.isEmpty()){ 127  for (Integer i : toRemove) { 128  Body x = getBody(i); 129  x.setActive(false); 130  131  } 132  toRemove.clear(); 133  } 134  135  if (debug != null) { 136  debug.render(box2DWorld, RenderingManager.getCamera().combined); 137  } 138  } 139  140  public static void cleanUp() { 141  tryInit(); 142  box2DWorld.dispose(); 143  if (debug != null) { 144  debug.dispose(); 145  } 146  } 147  148  149 }