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

Class Class, % Method, % Line, %
Renderable 100% (1/1) 81.8% (9/11) 64.7% (22/34)


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