package com.sertaogames.terremoto.component;
import java.util.ArrayList;
import java.util.List;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.sertaogames.cactus2d.Cactus2DApplication;
import com.sertaogames.cactus2d.Component;
import com.sertaogames.cactus2d.GameObject;
import com.sertaogames.cactus2d.components.LabelComponent;
import com.sertaogames.cactus2d.components.PhysicsComponent;
import com.sertaogames.cactus2d.components.TouchComponent;
import com.sertaogames.terremoto.GameState;
import com.sertaogames.terremoto.level.MainLevel;
public class GameControllerComponent extends Component {
public static GameObject tipGameObject = null;
public static GameObject parent = null;
private static List<GameObject> objetos;
public static GameState state = GameState.INFORMATION;
public static GameState nextState = GameState.MEMORIZE;
public static boolean finishState = true;
private float maxTime = 10;
private float currentTime = maxTime;
private String msg = "Observe\na cena.\nVoce tem\n" + (int)maxTime + " segundos.";
LabelComponent timeLabel = null;
private LabelComponent pointsLabel;
private int currentPoints = 0;
private List<MyHomeComponent> verifyOriginalLocation = new ArrayList<MyHomeComponent>();
public GameControllerComponent() {
tipGameObject = null;
parent = null;
state = GameState.INFORMATION;
nextState = GameState.MEMORIZE;
finishState = true;
}
@Override
public void init() {
objetos = gameObject.findAll("Objeto");
GameObject gameObject = this.gameObject.find("Time");
timeLabel = gameObject.getComponent(LabelComponent.class);
gameObject = gameObject.find("Points");
pointsLabel = gameObject.getComponent(LabelComponent.class);
parent = this.parent;
}
@Override
public void update() {
updateControll();
switch (state) {
case INFORMATION:
updateInformation();
break;
case MEMORIZE:
updateMemorize();
break;
case EARTHQUAKE:
updateEarthQuake();
break;
case PLAYING:
updatePlaying();
break;
}
}
private void updateControll() {
if(state != GameState.INFORMATION && state != GameState.PAUSE){
if(!finishState){
currentTime -= Cactus2DApplication.deltaTime;
timeLabel.text = " " + (int)(1 + currentTime);
if(currentTime < 0){
currentTime = 0;
finishState = true;
}
}
}
}
private void updateInformation() {
if(finishState){
if(nextState == GameState.EARTHQUAKE){
Cactus2DApplication.jukeBox.play(1);
} else {
Cactus2DApplication.jukeBox.play(0);
}
gameObject.AddComponent(new TipComponent(true, msg, nextState));
GameControllerComponent.pause();
finishState = false;
}
}
private void updateMemorize() {
if (finishState) {
msg = "Um forte\nTERREMOTO\nse aproxima.";
state = GameState.INFORMATION;
nextState = GameState.EARTHQUAKE;
currentTime = maxTime;
for (GameObject objeto : objetos) {
physics = objeto.getComponent(PhysicsComponent.class);
physics.setBodyType(BodyType.DynamicBody);
}
}
}
PhysicsComponent physics = null;
float accel = 20f;
int facingDirectionX = 1;
int facingDirectionY = 1;
int groundSpeedX = 0;
int groundSpeedY = 0;
float delay = 0f;
double r;
public void updateEarthQuake() {
Cactus2DApplication.jukeBox.play(2);
delay -= Cactus2DApplication.deltaTime;
if(delay < 0){
delay = 1f;
for (GameObject objeto : objetos) {
facingDirectionX *= (Math.random() *10)> 0.5d ? 1 : -1;
physics = objeto.getComponent(PhysicsComponent.class);
physics.rigidbody.applyAngularImpulse(10);
physics.rigidbody.setLinearVelocity(facingDirectionX * accel + groundSpeedX, facingDirectionY * accel + groundSpeedY);
}
}
if(finishState) {
state = GameState.INFORMATION;
nextState = GameState.PLAYING;
maxTime = 90;
currentTime = maxTime;
msg = "Reorganize\nos objetos.\nVoce tem\n" + (int)maxTime + " segundos.";
for (GameObject objeto : objetos) {
objeto.AddComponent(new TouchComponent(new Vector2(100, 100)));
verifyOriginalLocation.add(objeto.getComponent(MyHomeComponent.class));
}
}
}
public void updatePlaying() {
currentPoints = 0;
for (MyHomeComponent I : verifyOriginalLocation) {
if(I.isMyHome()){
currentPoints ++;
}
}
pointsLabel.text = currentPoints + "/" + verifyOriginalLocation.size();
if(currentPoints >= verifyOriginalLocation.size()){
state = GameState.INFORMATION;
nextState = GameState.WON;
msg = "YOU WIN!";
finishState = true;
currentTime = maxTime;
} else {
if(finishState){
msg = "YOU LOST!";
state = GameState.INFORMATION;
nextState = GameState.LOST;
}
}
}
public static void pause() {
GameObject game = parent.find("Gui");
game = game.find("Pause");
if(game != null) {
game.setActive(false);
}
nextState = state;
state = GameState.PAUSE;
MainLevel.pause();
for (GameObject go : objetos) {
go.setPaused(true);
}
}
public static void unPause() {
if(GameControllerComponent.state == GameState.PAUSE){
state = nextState;
}
GameObject game = parent.find("Gui");
game = game.find("Pause");
if(GameControllerComponent.state == GameState.PLAYING && game != null) {
game.setActive(true);
}
MainLevel.unPause();
for (GameObject go : objetos) {
go.setPaused(false);
}
}
}