/*
* AQP Project
* http://http://code.google.com/p/aqp-project/
* Alexandre Gomez - Clément Troesch - Fabrice Latterner
*/
package com.aqpproject.visualisation.gdx;
import com.aqpproject.tools.Color;
import com.aqpproject.tools.Vector2D;
import com.aqpproject.visualisation.InitializationEvent;
import com.aqpproject.visualisation.InitializationListener;
import com.aqpproject.visualisation.Visualisation;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.scenes.scene2d.Actor;
import java.awt.Rectangle;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import javax.swing.event.EventListenerList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
* VisualisationGDX class
*
* @author Alexandre, Clement, Fabrice
*/
public class VisualisationGDX implements Visualisation {
/**
* Constructor
*/
public VisualisationGDX() {
super();
m_eventListenerList = new EventListenerList();
}
/**
* @see Visualisation#initialize(int, int, boolean)
*/
@Override
public void initialize(int width, int height, boolean fullscreen) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.width = width;
config.height = height;
config.title = "AQP Project - Game";
config.fullscreen = fullscreen;
config.resizable = false;
initialize(config);
}
/**
* Initialize the component
*
* @param config configuration of the visualisation
*/
private void initialize(LwjglApplicationConfiguration config) {
//Attributes
m_sprites = new HashMap<>();
m_actors = new HashMap<>();
m_textActors = new HashMap<>();
m_lineActors = new HashMap<>();
m_particleEffects = new HashMap<>();
m_particleActors = new HashMap<>();
m_deletedParticleActors = new ArrayList<>();
//Start the visualisation
m_gameGDX = new GameGDX(config.fullscreen);
m_application = new LwjglApplication(m_gameGDX, config);
}
/**
* @see Visualisation#destroy()
*/
@Override
public void destroy() {
m_application.exit();
}
/**
* @see Visualisation#createSprite(java.lang.String, java.lang.String,
* java.awt.Rectangle)
*/
@Override
public void createSprite(String name, String path, Rectangle rect, int frame) {
SpriteGDX sprite = new SpriteGDX(path, rect, frame);
m_sprites.put(name, sprite);
}
/**
* @see Visualisation#createActor(java.lang.String, java.lang.String, float,
* float, float)
*/
@Override
public void createActor(String name, String spriteName, float posX, float posY, float rotation, int frame, boolean menu, boolean useRealCenter) {
ActorGDX a = new ActorGDX(name, m_sprites.get(spriteName), posX, posY, rotation, frame, useRealCenter);
m_gameGDX.addActor(a, menu);
m_actors.put(name, a);
}
@Override
public void createTextActor(String name, String text, float size, float posX, float posY, boolean menu) {
TextActorGDX a = new TextActorGDX(name, text, size, posX, posY);
m_gameGDX.addActor(a, menu);
m_textActors.put(name, a);
}
@Override
public void createTextActorPerso(String name, String text, float size, float posX, float posY, String font, boolean menu) {
TextActorGDX a = new TextActorGDX(name, text, size, posX, posY, font);
m_gameGDX.addActor(a, menu);
m_textActors.put(name, a);
}
@Override
public void createLineActor(String name, float x1, float y1, float x2, float y2, boolean menu) {
LineActorGDX a = new LineActorGDX(name, x1, x2, y1, y2, m_gameGDX.getStage());
m_gameGDX.addActor(a, menu);
m_lineActors.put(name, a);
}
/**
* @see Visualisation#updateActor(java.lang.String, float, float, float)
*/
@Override
public void updateActor(String name, float posX, float posY, float rotation) {
Actor actor = m_actors.get(name);
actor.x = posX;
actor.y = posY;
actor.rotation = rotation;
}
@Override
public void updateTextActor(String name, String text) {
TextActorGDX actor = m_textActors.get(name);
actor.setText(text);
}
@Override
public void updateTextActor(String name, String text, float posX, float posY) {
updateTextActor(name, text);
TextActorGDX actor = m_textActors.get(name);
actor.x = posX;
actor.y = posY;
}
@Override
public void updateLineActor(String name, float x1, float y1, float x2, float y2) {
LineActorGDX actor = m_lineActors.get(name);
actor.setLine(x1, y1, x2, y2);
}
@Override
public String getTextActor(String name) {
return m_textActors.get(name).getText();
}
/**
* @see Visualisation#deleteActor(java.lang.String)
*/
@Override
public void deleteActor(String name, boolean menu) {
Actor a = m_actors.get(name);
m_gameGDX.removeActor(a, menu);
m_actors.remove(name);
}
@Override
public void deleteTextActor(String name, boolean menu) {
Actor a = m_textActors.get(name);
m_gameGDX.removeActor(a, menu);
m_textActors.remove(name);
}
@Override
public void deleteLineActor(String name, boolean menu) {
Actor a = m_lineActors.get(name);
m_gameGDX.removeActor(a, menu);
m_lineActors.remove(name);
}
/**
* @see Visualisation#setCameraPosition(float, float)
*/
@Override
public void setCameraPosition(float posX, float posY) {
m_gameGDX.setCameraPosition(posX, posY);
}
@Override
public void setZoom(float zoom) {
m_gameGDX.setZoom(zoom);
}
/**
* @see Visualisation#createMap(java.lang.String)
*/
@Override
public void createMap(String mapPath) {
MapActorGDX map = new MapActorGDX(mapPath, m_gameGDX.getStage());
m_gameGDX.setMap(map);
}
/**
* @see Visualisation#deleteMap()
*/
@Override
public void deleteMap() {
m_gameGDX.removeMap();
}
/**
* @see
* Visualisation#addInitializationListener(com.aqpproject.visualisation.InitializationListener)
*/
@Override
public void addInitializationListener(InitializationListener listener) {
m_eventListenerList.add(InitializationListener.class, listener);
}
/**
* @see Visualisation#notifyInitializationListener()
*/
@Override
public void notifyInitializationListener() {
for (InitializationListener i : m_eventListenerList.getListeners(InitializationListener.class)) {
i.visualisationInitialized(new InitializationEvent(this));
}
}
/**
* @see Visualisation#getSpriteCenter(java.lang.String)
*/
@Override
public Vector2D getDefaultSpriteCenter(String name) {
return m_sprites.get(name).getDefaultCenter();
}
@Override
public Vector2D getRealSpriteCenter(String name) {
return m_sprites.get(name).getRealCenter();
}
@Override
public Vector2D getSpriteSize(String name) {
return new Vector2D(m_sprites.get(name).getWidth(), m_sprites.get(name).getHeight());
}
/**
* @see Visualisation#loadSprites(java.lang.String)
*/
@Override
public void loadSprites(String path) throws ParserConfigurationException, IOException, SAXException {
File file = new File(path);
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(file);
doc.getDocumentElement().normalize();
Element root = doc.getDocumentElement();
NodeList children = root.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node n = children.item(i);
if (n.getNodeType() == Node.ELEMENT_NODE) {
Element e = (Element) n;
String name = e.getAttribute("name");
String entityPath = e.getAttribute("path");
int x = Integer.parseInt(e.getAttribute("x"));
int y = Integer.parseInt(e.getAttribute("y"));
int width = Integer.parseInt(e.getAttribute("width"));
int height = Integer.parseInt(e.getAttribute("height"));
int frame = Integer.parseInt(e.getAttribute("frame"));
createSprite(name, entityPath, new Rectangle(x, y, width, height), frame);
}
}
}
@Override
public void loadParticleEffects(String path) throws ParserConfigurationException, IOException, SAXException {
File file = new File(path);
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(file);
doc.getDocumentElement().normalize();
Element root = doc.getDocumentElement();
NodeList children = root.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node n = children.item(i);
if (n.getNodeType() == Node.ELEMENT_NODE) {
Element e = (Element) n;
String name = e.getAttribute("name");
String effectPath = e.getAttribute("effectPath");
String imagePath = e.getAttribute("imagePath");
createParticleEffect(name, effectPath, imagePath);
}
}
}
@Override
public int getTile(MAP_LAYER layer, Vector2D position) {
return m_gameGDX.getTile(layer, position);
}
@Override
public ArrayList<Vector2D> getTilePositions(MAP_LAYER layer, int code) {
return m_gameGDX.getTilePositions(layer, code);
}
@Override
public int getMapWidth() {
return m_gameGDX.getMapWidth();
}
@Override
public int getMapHeight() {
return m_gameGDX.getMapHeight();
}
@Override
public void updateActorFrame(String actorName, int frame) {
m_actors.get(actorName).setFrame(frame);
}
@Override
public void setActorAlpha(String actorName, float alpha) {
m_actors.get(actorName).setAlpha(alpha);
}
@Override
public void setActorTeint(String actorName, Color teint) {
m_actors.get(actorName).setTeint(teint);
}
@Override
public void setTextActorAlpha(String actorName, float alpha) {
m_textActors.get(actorName).setAlpha(alpha);
}
@Override
public void setActorScale(String actorName, float scale) {
m_actors.get(actorName).setScale(scale);
}
@Override
public void setActorShadowSize(String actorName, int shadowSize) {
m_actors.get(actorName).setShadowSize(shadowSize);
}
@Override
public Vector2D getResolution() {
return new Vector2D(m_application.getGraphics().getWidth(), m_application.getGraphics().getHeight());
}
@Override
public void createParticleEffect(String name, String effectFile, String imageDir) {
m_particleEffects.put(name, new ParticleEffectGDX(effectFile, imageDir));
}
@Override
public void createParticleActor(String name, String effectName, float posX, float posY, boolean activated) {
ParticleActorGDX a = new ParticleActorGDX(m_particleEffects.get(effectName).createEffect(), posX, posY, activated);
m_particleActors.put(name, a);
m_gameGDX.addActor(a, false);
}
@Override
public void updateParticleActor(String name, float posX, float posY, boolean activated) {
ParticleActorGDX a = m_particleActors.get(name);
if (a != null) {
a.x = posX;
a.y = posY;
a.setActive(activated);
}
}
@Override
public void deleteParticleActor(String name, boolean autoremove) {
ParticleActorGDX a = m_particleActors.remove(name);
a.setActive(false);
if (autoremove) {
m_deletedParticleActors.add(a);
} else {
m_gameGDX.removeActor(a, false);
}
}
@Override
public void updateParticles(float delta) {
try {
for (String key : m_particleActors.keySet()) {
m_particleActors.get(key).update(delta);
}
} catch (ConcurrentModificationException e) {
System.out.println("Update Particles error");
}
for (ParticleActorGDX a : m_deletedParticleActors) {
a.update(delta);
}
for (int i = 0; i < m_deletedParticleActors.size(); i++) {
if (m_deletedParticleActors.get(i).canBeRemoved()) {
ParticleActorGDX a = m_deletedParticleActors.remove(i);
i--;
m_gameGDX.removeActor(a, false);
}
}
}
public ActorGDX getActor(String s) {
if (m_actors.containsKey(s)) {
return m_actors.get(s);
} else {
return null;
}
}
/////////////////////////////////////////
// Attributes
/////////////////////////////////////////
private HashMap<String, SpriteGDX> m_sprites;
private HashMap<String, ActorGDX> m_actors;
private HashMap<String, TextActorGDX> m_textActors;
private HashMap<String, LineActorGDX> m_lineActors;
private HashMap<String, ParticleEffectGDX> m_particleEffects;
private HashMap<String, ParticleActorGDX> m_particleActors;
private ArrayList<ParticleActorGDX> m_deletedParticleActors;
private GameGDX m_gameGDX;
private LwjglApplication m_application;
/////////////////////////////////////////
// Listener
/////////////////////////////////////////
private EventListenerList m_eventListenerList;
}