package squaresgame;
import squaresgame.GameController.SquarePlacedEvent;
import framework.component.Component;
import framework.component.ComponentSystem;
import framework.event.Event;
import framework.event.EventListener;
import framework.event.EventSystem;
import framework.rendering.SpriteComponent;
import generators.SquareGenerator;
public class SquareComponent extends Component implements EventListener{
private final Square square;
public SquareComponent(Square square) {
super();
this.square = square;
EventSystem.getInstance().registerEventListener(this, "SquarePlaced", null);
}
public Square getSquare() {
return square;
}
@Override
public boolean allowSameTypedSiblings() {
return false;
}
@Override
protected byte getDataFormatVersion() {
return 0;
}
@Override
public void onEvent(Event e) {
if(e != null && e.getType().equals("SquarePlaced")){
SquarePlacedEvent lineEvent = (SquarePlacedEvent) e;
if(lineEvent.getSquare() != null && lineEvent.getSquare().equals(this.square)){
SpriteComponent squareSprite = (SpriteComponent) ComponentSystem.getInstance().getComponentWithTypeAndLabel(SquareGenerator.getSquareLabel(square), SpriteComponent.class);
if(squareSprite != null){
squareSprite.setImageFilename(ColourManager.getSquareForPlayer(square.getOwningPlayer().getId()), true);
}
}
}
}
}