package com.sertaogames.terremoto.factory;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont.HAlignment;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.sertaogames.cactus2d.Cactus2DApplication;
import com.sertaogames.cactus2d.GameObject;
import com.sertaogames.cactus2d.components.AudioComponent;
import com.sertaogames.cactus2d.components.BoxCollider;
import com.sertaogames.cactus2d.components.ButtonComponent;
import com.sertaogames.cactus2d.components.LabelComponent;
import com.sertaogames.cactus2d.components.LevelLoader;
import com.sertaogames.cactus2d.components.PhysicsComponent;
import com.sertaogames.cactus2d.components.SpriteRendererComponent;
import com.sertaogames.terremoto.GameState;
import com.sertaogames.terremoto.TerremotoApplication;
import com.sertaogames.terremoto.Utils;
import com.sertaogames.terremoto.component.DragAndDropComponent;
import com.sertaogames.terremoto.component.GameControllerComponent;
import com.sertaogames.terremoto.component.MyHomeComponent;
import com.sertaogames.terremoto.component.NextstateComponent;
import com.sertaogames.terremoto.component.PauseComponent;
import com.sertaogames.terremoto.level.MainLevel;
import com.sertaogames.terremoto.level.MenuLevel;
/**
* Eu tenho uma fabrica de game objects que ser� respons�vel de produzir
* todos os elementos de tela e fisica do jogo.
* @author Jader
*
*/
public class GameObjectsFactory {
public static GameObject createObject(int width, int heigth, int j, int i, Vector2 initialPosition){
GameObject gameObject = new GameObject("Objeto");
Texture tx = Cactus2DApplication.loadTexture("data/textures/allTextures.png", true);
SpriteRendererComponent sr = SpriteRendererComponent.getTextureRegion(tx,width, heigth, j, i);
gameObject.AddComponent(sr);
Utils.offsetCorrection(initialPosition);
DragAndDropComponent dd = new DragAndDropComponent();
gameObject.transform.getLocalPosition().set(new Vector2(initialPosition).sub(new Vector2(sr.spriteRegion.getRegionWidth()/2
,sr.spriteRegion.getRegionHeight()/2)));
gameObject.AddComponent(dd);
gameObject.AddComponent(new AudioComponent("data/sound/drag.wav"));
gameObject.AddComponent(new MyHomeComponent());
addDinamicPhysicBySprite(gameObject);
return gameObject;
}
private static void addDinamicPhysicBySprite(GameObject gameObject) {
PhysicsComponent p = new PhysicsComponent(1, BodyType.KinematicBody, false);
gameObject.AddComponent(p);
Vector2 offset = new Vector2(
gameObject.spriteRenderer.spriteRegion.getRegionWidth()/2,
gameObject.spriteRenderer.spriteRegion.getRegionHeight()/2);
BoxCollider coll = new BoxCollider(
new Vector2(gameObject.spriteRenderer.spriteRegion.getRegionWidth()/2, gameObject.spriteRenderer.spriteRegion.getRegionHeight()/2)
, offset);
gameObject.AddComponent(coll);
}
public static GameObject createPhysicsBox() {
GameObject gameObject = new GameObject("physycsbox");
float offSetHeight = 1024;
float offSetWidth = 600;
int offSetSide = 200;
gameObject.transform.getLocalPosition().sub(offSetSide/2, offSetSide/2);
addStaticPolignPhysic(gameObject, new Vector2(offSetWidth, offSetSide), new Vector2(offSetSide/2, 0));
addStaticPolignPhysic(gameObject, new Vector2(offSetWidth, offSetSide), Utils.offsetCorrection( new Vector2( offSetSide/2, offSetHeight + offSetSide/2 )));
addStaticPolignPhysic(gameObject, new Vector2(offSetSide, offSetHeight), new Vector2( - offSetSide/2, offSetSide/2));
addStaticPolignPhysic(gameObject, new Vector2(offSetSide, offSetHeight), new Vector2(offSetWidth + offSetSide/2, offSetSide/2));
return gameObject;
}
public static GameObject createBackground() {
GameObject gameObject = new GameObject("background");
Texture tx = Cactus2DApplication.loadTexture("data/textures/allTextures.png", true);
SpriteRendererComponent sr = SpriteRendererComponent.getTextureRegion(tx,600, 1024, 0, 0);
gameObject.AddComponent(sr);
Utils.offsetCorrection(gameObject.transform.getLocalPosition());
return gameObject;
}
private static void addStaticPolignPhysic(GameObject go, Vector2 dimensions, Vector2 location) {
GameObject side = new GameObject("side");
PhysicsComponent p = new PhysicsComponent(1, false, false);
side.AddComponent(p);
side.transform.getLocalPosition().set(location);
Vector2 offset = new Vector2(dimensions).mul(0.5f);
BoxCollider coll = new BoxCollider(dimensions, offset);
side.AddComponent(coll);
go.addGameObject(side);
}
public static GameObject createController() {
GameObject controller = new GameObject("Controller");
controller.AddComponent(new GameControllerComponent());
controller.transform.getLocalPosition().set(10, 600);
Utils.offsetCorrection(controller.transform.getLocalPosition());
return controller;
}
public static GameObject createTip(LabelComponent l, GameState newState) {
GameObject tip = new GameObject("Tip");
int height = 512;
int width = 512;
Texture tx = Cactus2DApplication.loadTexture("data/textures/gui.png", true);
SpriteRendererComponent sr = SpriteRendererComponent.getTextureRegion(tx ,width, height, 1, 1);
sr.layer = 2;
sr.gui = true;
tip.AddComponent(sr);
float halfWidth = width * Cactus2DApplication.invCameraZoom / 2;
tip.transform.getPosition().set(Gdx.graphics.getWidth() / 2 - halfWidth, Gdx.graphics.getHeight() / 2 - height * Cactus2DApplication.invCameraZoom / 2);
GameObject label = new GameObject("Message");
label.transform.getPosition().set(halfWidth, 400 * Cactus2DApplication.invCameraZoom);
tip.addGameObject(label);
GameObject subGameObject = new GameObject("Continue");
subGameObject.transform.getLocalPosition().add(width * 0.7f* Cactus2DApplication.invCameraZoom, 0);
subGameObject.AddComponent(new SpriteRendererComponent(null));
subGameObject.spriteRenderer.gui = true;
TextureRegion[][] tmp = TextureRegion.split(tx, 128, 128);
subGameObject.AddComponent(new ButtonComponent(tmp[0][0], tmp[0][1]));
subGameObject.AddComponent(new AudioComponent("data/sound/click.wav"));
subGameObject.AddComponent(new NextstateComponent(newState));
if(GameControllerComponent.state != GameState.WON && GameControllerComponent.state != GameState.LOST){
tip.addGameObject(subGameObject);
}
subGameObject = new GameObject("Reload");
subGameObject.transform.getLocalPosition().add(10, 0);
subGameObject.AddComponent(new SpriteRendererComponent(null));
subGameObject.spriteRenderer.gui = true;
subGameObject.AddComponent(new ButtonComponent(tmp[1][0], tmp[1][1]));
subGameObject.AddComponent(new AudioComponent("data/sound/click.wav"));
subGameObject.AddComponent(new LevelLoader(MainLevel.class));
tip.addGameObject(subGameObject);
subGameObject = new GameObject("Menu");
subGameObject.transform.getLocalPosition().add(width * 0.35f * Cactus2DApplication.invCameraZoom, 0);
subGameObject.AddComponent(new SpriteRendererComponent(null));
subGameObject.spriteRenderer.gui = true;
subGameObject.AddComponent(new ButtonComponent(tmp[0][6], tmp[0][7]));
subGameObject.AddComponent(new AudioComponent("data/sound/click.wav"));
subGameObject.AddComponent(new LevelLoader(MenuLevel.class));
tip.addGameObject(subGameObject);
l.alignment = HAlignment.CENTER;
label.AddComponent(l);
return tip;
}
public static GameObject createGUI() {
Texture tx = Cactus2DApplication.loadTexture("data/textures/gui.png", true);
GameObject gui = new GameObject("Gui");
int width = 640;
int height = 128;
SpriteRendererComponent sprite = SpriteRendererComponent.getTextureRegion(tx, width, height, 3, 0);
gui.AddComponent(sprite);
gui.spriteRenderer.gui = true;
gui.transform.getLocalPosition().sub(20 * Cactus2DApplication.invCameraZoom, 15 * Cactus2DApplication.invCameraZoom);
GameObject subGameObject = new GameObject("Pause");
height = 128;
width = 128;
subGameObject.transform.getLocalPosition().add(465 * Cactus2DApplication.invCameraZoom, 0);
subGameObject.AddComponent(new SpriteRendererComponent(null));
TextureRegion[][] tmp = TextureRegion.split(tx, width, height);
tmp = TextureRegion.split(tx, width, height);
subGameObject.spriteRenderer.gui = true;
subGameObject.AddComponent(new ButtonComponent(tmp[0][2], tmp[0][3]));
subGameObject.AddComponent(new AudioComponent("data/sound/click.wav"));
subGameObject.AddComponent(new PauseComponent());
gui.addGameObject(subGameObject);
LabelComponent label = null;
GameObject time = new GameObject("Time");
label = new LabelComponent(" ");
time.transform.getLocalPosition().add(100* Cactus2DApplication.invCameraZoom, 80* Cactus2DApplication.invCameraZoom);
label.setFontName(TerremotoApplication.fontName);
time.AddComponent(label);
time.AddComponent(new SpriteRendererComponent(null));
time.spriteRenderer.gui = true;
gui.addGameObject(time);
GameObject points = new GameObject("Points");
label = new LabelComponent(" ");
points.transform.getLocalPosition().add(300* Cactus2DApplication.invCameraZoom, 80* Cactus2DApplication.invCameraZoom);
label.setFontName(TerremotoApplication.fontName);
points.AddComponent(label);
points.AddComponent(new SpriteRendererComponent(null));
points.spriteRenderer.gui = true;
gui.addGameObject(points);
subGameObject = new GameObject("Clock");
sprite = SpriteRendererComponent.getTextureRegion(tx, width, height, 6, 3);
subGameObject.transform.getLocalPosition().add(8* Cactus2DApplication.invCameraZoom, 8* Cactus2DApplication.invCameraZoom);
subGameObject.AddComponent(sprite);
subGameObject.spriteRenderer.gui = true;
gui.addGameObject(subGameObject);
return gui;
}
}