Coverage Summary for Class: Renderable (com.mygdx.game.Components)

Class Class, % Method, % Line, %
Renderable 100% (1/1) 30.8% (4/13) 26.7% (16/60)


1 package com.mygdx.game.Components; 2  3 import com.badlogic.gdx.graphics.Texture; 4 import com.badlogic.gdx.graphics.g2d.Sprite; 5 import com.badlogic.gdx.math.MathUtils; 6 import com.badlogic.gdx.math.Vector2; 7 import com.mygdx.game.Managers.RenderLayer; 8 import com.mygdx.game.Managers.RenderingManager; 9 import com.mygdx.game.Managers.ResourceManager; 10  11 /** 12  * Add the ability for the object to be shown 13  */ 14 public class Renderable extends Component { 15  protected Sprite sprite; 16  private boolean isVisible; 17  18  /** 19  * Called in other constructors, loads no textures by itself. 20  */ 21  public Renderable() { 22  super(); 23  isVisible = true; 24  type = ComponentType.Renderable; 25  sprite = new Sprite(); 26  RenderingManager.addItem(this, RenderLayer.Transparent); 27  } 28  29  /** 30  * Associates Renderable with the given texture sprite and layer. 31  * 32  * @param texId the id of the texture the sprite will take on 33  * @param layer the rendering layer 34  */ 35  public Renderable(int texId, RenderLayer layer) { 36  this(); 37  sprite = new Sprite(ResourceManager.getTexture(texId)); 38  RenderingManager.addItem(this, layer); 39  } 40  41  /** 42  * Associates Renderable with the given sprite from a texture atlas and a layer. 43  * 44  * @param atlasId the id of the texture atlas containing the sprite 45  * @param texName the name of the texture the sprite will take on 46  * @param layer the rendering layer 47  */ 48  public Renderable(int atlasId, String texName, RenderLayer layer) { 49  this(); 50  sprite = new Sprite(ResourceManager.getSprite(atlasId, texName)); 51  RenderingManager.addItem(this, layer); 52  } 53  54  /**NEW 55  * 56  * @param img image path to apply 57  * @param layer the rendering layer 58  */ 59  public Renderable(String img, RenderLayer layer) { 60  this(); 61  Texture texture = new Texture(img); 62  sprite = new Sprite(texture); 63  RenderingManager.addItem(this, layer); 64  } 65  66  67  /** 68  * Locates the sprite at the position of the parent's Transform component. 69  */ 70  @Override 71  public void update() { 72  super.update(); 73  if (sprite == null) { 74  return; 75  } 76  Transform c = parent.getComponent(Transform.class); 77  if (c == null) { 78  return; 79  } 80  Vector2 p = c.getPosition(); 81  Vector2 s = c.getScale(); 82  83  sprite.setPosition(p.x, p.y); 84  sprite.setRotation(MathUtils.radiansToDegrees * c.getRotation()); 85  sprite.setScale(s.x, s.y); 86  } 87  88  @Override 89  public void render() { 90  super.render(); 91  if (sprite == null || !isVisible) { 92  return; 93  } 94  sprite.draw(RenderingManager.getBatch()); 95  } 96  97  @Override 98  public void cleanUp() { 99  super.cleanUp(); 100  } 101  102  public Sprite getSprite() { 103  return sprite; 104  } 105  106  /** 107  * Asignes a new texture compatible with textures sourced from atlas 108  * 109  * @param s the sprite contain the texture 110  */ 111  public void setTexture(Sprite s) { 112  Sprite a = getSprite(); 113  a.setTexture(s.getTexture()); 114  a.setU(s.getU()); 115  a.setV(s.getV()); 116  a.setU2(s.getU2()); 117  a.setV2(s.getV2()); 118  } 119  120  public boolean isVisible() { 121  return isVisible; 122  } 123  124  public void show() { 125  isVisible = true; 126  } 127  128  public void hide() { 129  isVisible = false; 130  } 131  132  public void toggleVisibility() { 133  isVisible = !isVisible; 134  } 135  136 }