/*
* AQP Project
* http://http://code.google.com/p/aqp-project/
* Alexandre Gomez - Clément Troesch - Fabrice Latterner
*/
package com.aqpproject.worldmodel.game.state;
import com.aqpproject.game.Singleton;
import com.aqpproject.tools.Vector2D;
import com.aqpproject.worldmodel.Control;
import com.aqpproject.worldmodel.GameState;
import com.aqpproject.worldmodel.game.WorldModelGame;
import com.aqpproject.worldmodel.game.entity.WEMenu;
import com.aqpproject.worldmodel.game.entity.WorldEntity;
import java.io.IOException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
/**
*
* @author Alexandre
*/
public class TitleGameState implements GameState {
/**
* Constructor
*/
public TitleGameState() {
m_initialized = false;
}
/**
* @see WorldModel#initialize()
*/
@Override
public void initialize() {
if (!m_initialized) { //If not initialized
m_worldEntities = new HashMap<>();
try {
Singleton.getVisualisation().loadSprites("config/titlemenu.xml");
} catch (Exception ex) {
Logger.getLogger(WorldModelGame.class.getName()).log(Level.SEVERE, null, ex);
}
m_theTimeCreationState = Singleton.getWorldModel().getTime();
createEntities();
//Initialisation
m_initialized = true;
try {
Singleton.getAudioController().loadPlaylist("playlist1");
} catch (ParserConfigurationException | SAXException | IOException ex) {
Logger.getLogger(TitleGameState.class.getName()).log(Level.SEVERE, null, ex);
}
try {
Singleton.getAudioController().playPlaylist("playlist1");
} catch (ParserConfigurationException | SAXException | IOException ex) {
Logger.getLogger(TitleGameState.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
/**
* @see WorldModel#destroy()
*/
@Override
public void destroy() {
//Entities
for (WorldEntity e : m_worldEntities.values()) {
e.destroy();
}
m_worldEntities.clear();
}
/**
* Create the entities
*/
private void createEntities() {
m_worldEntities.put("logo", new WEMenu("logo", "logo", new Vector2D(-256, -128), 0, 1));
Singleton.getVisualisation().setActorAlpha("logo", 0.0f);
m_worldEntities.put("logo_titre_v2", new WEMenu("logo_titre_v2", "logo_titre_v2", new Vector2D(-512, -256), 0, 1));
Singleton.getVisualisation().setActorAlpha("logo_titre_v2", 0.0f);
m_worldEntities.put("start_deg", new WEMenu("start_deg", "start_deg", new Vector2D(-64, -320), 0, 1));
Singleton.getVisualisation().setActorAlpha("start_deg", 0.0f);
}
private void showImage(String actor, long debut, long fin, int nbTransition, long time) {
if (time < fin + m_theTimeCreationState && time > debut + m_theTimeCreationState) {
for (int i = 0; i <= nbTransition; i++) {
if (time > ((fin - debut) / 20) * i + debut + m_theTimeCreationState && time < ((fin - debut) / 20) * (i + 1) + debut + m_theTimeCreationState) {
Singleton.getVisualisation().setActorAlpha(actor, (1.0f / nbTransition) * i);
}
}
}
}
private void hideImage(String actor, long debut, long fin, int nbTransition, long time) {
if (time < fin + m_theTimeCreationState && time > debut + m_theTimeCreationState) {
for (int i = 0; i <= nbTransition; i++) {
if (time > ((fin - debut) / 20) * i + debut + m_theTimeCreationState && time < ((fin - debut) / 20) * (i + 1) + debut + m_theTimeCreationState) {
Singleton.getVisualisation().setActorAlpha(actor, 1.0f - (1.0f / nbTransition) * i);
}
}
}
}
/**
* @see WorldModel#update()
*/
@Override
public void update() {
if (m_worldEntities.isEmpty()) {
return;
}
long time = Singleton.getWorldModel().getTime();
for (WorldEntity e : m_worldEntities.values()) {
e.update(time);
}
//AQP-Project present - show
showImage("logo", 0, 1000, 10, time);
//AQP-Project present - hide
hideImage("logo", 2000, 3000, 10, time);
//Image - nom du jeu - titre show
showImage("logo_titre_v2", 4000, 5000, 10, time);
showImage("start_deg", 4000, 5000, 10, time);
}
/**
* @see WorldModel#controlEntity(java.lang.String,
* com.aqpproject.worldmodel.Control)
*/
@Override
public void control(Control control) {
if (control == Control.ENTER) {
Singleton.getWorldModel().setNextState(new MainMenuGameState());
}
}
/**
* @see WorldModel#updateActorTransform()
*/
@Override
public void updateActorTransform() {
//Camera
Singleton.getVisualisation().setCameraPosition(0, 0);
//Entities
for (WorldEntity e : m_worldEntities.values()) {
e.updateActorTransform();
}
}
/**
* @see WorldModel#saveEntitiesState()
*/
@Override
public void saveEntitiesState() {
for (WorldEntity e : m_worldEntities.values()) {
e.saveTransform();
}
}
@Override
public boolean isInitialized() {
return m_initialized;
}
@Override
public void startContact(String firstEntity, String secondEntity, Vector2D normal, Vector2D point) {
}
@Override
public void stopContact(String firstEntity, String secondEntity, Vector2D normal, Vector2D point) {
}
@Override
public HashMap<String, WorldEntity> getWorldEntities() {
return m_worldEntities;
}
@Override
public void control(Control control, String entity) {
throw new UnsupportedOperationException("Not supported yet.");
}
/////////////////////////////////////////
// Attributes
/////////////////////////////////////////
protected HashMap<String, WorldEntity> m_worldEntities;
protected boolean m_initialized;
protected long m_theTimeCreationState;
}