package squaresgame;
import main.L;
import squaresgame.GameController.SquarePlacedEvent;
import squaresgame.GameController.TurnChangedEvent;
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 framework.rendering.TextRenderComponent;
public class TurnCounterComponent extends Component implements EventListener{
Player currentPlayer = null;
public TurnCounterComponent() {
super();
EventSystem.getInstance().registerEventListener(this, "TurnChanged", null);
}
@Override
public boolean allowSameTypedSiblings() {
return true;
}
@Override
protected byte getDataFormatVersion() {
return 0;
}
@Override
public void onEvent(Event e) {
if(e != null && e.getType().equals("TurnChanged")){
this.currentPlayer = ((TurnChangedEvent) e).getCurrentPlayer();
updateTurnCount();
}
}
@Override
public void onSiblingAdded(Component sibling) {
super.onSiblingAdded(sibling);
updateTurnCount();
}
private void updateTurnCount(){
// TextRenderComponent textComp = (TextRenderComponent) getSiblingByType(TextRenderComponent.class.getName());
// if(textComp != null && currentPlayer != null){
// textComp.setText("Player "+currentPlayer.getName()+"'s move");
//
// changeCursorColour();
// }
SpriteComponent spriteComp = (SpriteComponent) getSiblingByType(SpriteComponent.class.getName());
if(spriteComp != null && currentPlayer != null){
spriteComp.setImageFilename(ColourManager.getSquareForPlayer(currentPlayer.getId()), true);
changeCursorColour();
}
}
private void changeCursorColour() {
SpriteComponent cursorSprite = (SpriteComponent) ComponentSystem.getInstance().getComponentWithTypeAndLabel("Cursor", SpriteComponent.class);
if(cursorSprite != null){
cursorSprite.setImageFilename(ColourManager.getCursorForPlayer(currentPlayer.getId()), true);
}
}
}