package generators;
import org.newdawn.slick.geom.Vector2f;
import squaresgame.ColourManager;
import squaresgame.LineComponent;
import framework.collision.CircularCollisionMask;
import framework.collision.CollisionComponent;
import framework.collision.CollisionComponent.CollisionEvent;
import framework.component.Component;
import framework.component.ComponentSystem;
import framework.component.ParentComponent;
import framework.component.filter.ComponentPathFilter;
import framework.component.path.AbsoluteComponentPath;
import framework.component.path.ComponentPath;
import framework.component.util.MouseFollowerComponent;
import framework.event.Event;
import framework.event.EventListener;
import framework.event.EventSender;
import framework.event.EventSystem;
import framework.event.filter.EventSenderComponentFilter;
import framework.rendering.SpriteComponent;
import framework.spacial.PositionComponent;
public class CursorGenerator {
public static void setUpCursor(){
ComponentSystem.getInstance().getRoot().addComponent(generateCursor());
}
private static ParentComponent generateCursor(){
ParentComponent cursorParent = new ParentComponent("Cursor");
SpriteComponent spriteComp = new SpriteComponent(ColourManager.getCursorForPlayer(0), new Vector2f(-10, -10), 20, 20);
spriteComp.setAlwaysOnTop();
cursorParent.addComponent(spriteComp);
CollisionComponent collis = new CollisionComponent(new CircularCollisionMask(10));
cursorParent.addComponent(collis);
setUpEventListener(collis);
cursorParent.addComponent(new PositionComponent(50,50));
cursorParent.addComponent(new MouseFollowerComponent());
return cursorParent;
}
private static void setUpEventListener(Component collis) {
ComponentPath path = new AbsoluteComponentPath(collis);
EventSenderComponentFilter filter = new EventSenderComponentFilter(new ComponentPathFilter(path));
EventListener listener = generateCollisionListener();
EventSystem.getInstance().registerEventListener(listener, "CollisionStarted", filter);
EventSystem.getInstance().registerEventListener(listener, "CollisionStopped", filter);
EventSystem.getInstance().registerEventListener(listener, "MouseReleased", null);
}
private static EventListener generateCollisionListener() {
return new EventListener() {
@Override
public void onEvent(Event e) {
if(e != null){
if(e.getType().equals("CollisionStarted")){
onCollisionStarted((CollisionEvent)e);
}else if(e.getType().equals("CollisionStopped")){
onCollisionStopped((CollisionEvent)e);
}else if(e.getType().equals("MouseReleased")){
onMouseReleased();
}
}
}
@Override
public int getUniqueID() {
return 0;
}
@Override
public String getType() {
return "CursorListener";
}
};
}
private static void onCollisionStarted(CollisionEvent e){
CollisionComponent inContactWith = e.getCollidedWith();
//Deselect all other lines
CollisionComponent cursorCollisComp = (CollisionComponent) ComponentSystem.getInstance().getComponentWithTypeAndLabel("Cursor", CollisionComponent.class);
if(cursorCollisComp != null){
for(CollisionComponent collis: cursorCollisComp.getAllComponentsInContact()){
deselectLine(collis);
}
}
//Change the image
if(!isSiblingLinePlaced(inContactWith)){
SpriteComponent spriteComp = (SpriteComponent) inContactWith.getSiblingByType(SpriteComponent.class.getName());
if(spriteComp != null){
spriteComp.setImageFilename(ColourManager.getHighlightedLine(), true);
}
}
}
private static void onCollisionStopped(CollisionEvent e){
deselectLine(e.getCollidedWith());
}
private static void deselectLine(CollisionComponent c){
if(c != null && !isSiblingLinePlaced(c)){
SpriteComponent spriteComp = (SpriteComponent) c.getSiblingByType(SpriteComponent.class.getName());
if(spriteComp != null){
spriteComp.setImageFilename(ColourManager.getUnplacedLine(), true);
}
}
}
private static void onMouseReleased(){
CollisionComponent collis = (CollisionComponent) ComponentSystem.getInstance().getComponentWithTypeAndLabel("Cursor", CollisionComponent.class.getName());
//Make it the last one it was in contact with
CollisionComponent inContactWith = null;
for(CollisionComponent c:collis.getAllComponentsInContact()){
inContactWith = c;
}
if(inContactWith != null){
LineComponent lineComp = (LineComponent) inContactWith.getSiblingByType(LineComponent.class.getName());
EventSystem.getInstance().pushEvent(new PlayerClickedOnLineEvent(lineComp));
}
}
private static boolean isSiblingLinePlaced(Component c){
if(c != null){
LineComponent lineComp = (LineComponent) c.getSiblingByType(LineComponent.class.getName());
if(lineComp != null && lineComp.getLine() != null){
return lineComp.getLine().isPlaced();
}
}
return false;
}
public static class PlayerClickedOnLineEvent extends Event{
public PlayerClickedOnLineEvent(EventSender sender) {
super(sender);
}
@Override
protected String getEventTypeID() {
return "PlayerClickedOnLine";
}
public squaresgame.Line getLine(){
return ((LineComponent) getSender()).getLine();
}
}
}