package server;
import general.datastructures.Vector2f;
import general.helperclasses.Math;
import general.interfaces.IUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import client.game.Game;
import server.player.Player;
import server.sprite.MovableSprite;
import server.sprite.Sprite;
import server.world.RouteCalculator;
//TODO Vielleicht nicht mehr als thread laufen lassen, da er keine fortlaufenden aktionen durchr�hrt
public class UnitManager{
protected final ThreadGroup sprites_tg;
protected List<Sprite> sprites = new ArrayList<Sprite>();
protected Game game;
protected RouteCalculator routecalculator;
protected boolean isRunning=true;
public UnitManager(Game g)
{
this.game = g;
routecalculator = new RouteCalculator(g.getMap());
routecalculator.start();
sprites_tg = new ThreadGroup("TG Unitmanager");
}
public boolean moveSprite(Sprite s, Vector2f sc_destination)
{
routecalculator.getRouteStorage().requestRoute(Math.worldToMap(s.getPosition(),game.getMap().getTilesize()), Math.screenToMap(sc_destination, game.getViewport(), game.getMap().getTilesize()), IUnit.TYPE_LAND, (int) s.getId());
try{
Vector<Vector2f> wc_waypoints = routecalculator.getRouteStorage().getRoute((int) s.getId());
if(wc_waypoints!=null)
{
((MovableSprite)s).appendWaypoints(wc_waypoints);
return true;
}
else
{
System.out.println("Keine Route erstellt");
return false;
}
}catch (Exception e) {
System.err.println("Could not append waypoints");
return false;
}
}
public void cleanup()
{
this.isRunning = false;
for (Sprite s : sprites)
{
s.kill();
}
routecalculator.kill();
}
public boolean createSprite(String name, String ref,Vector2f sc_position, float speed, Player owner)
{
//if(game.getMap().isInMap(position)&&game.getMap().isPassable(IUnit.TYPE_LAND, position))
{
//TODO playerID anpassen
Sprite s = new MovableSprite(game, name, ref, Math.screenToWorld(sc_position, game.getViewport()), speed, game.getPlayer(0));
//Sprite s = new MovableSprite(game, ref, Math.mapToWorld(new Vector2f(10, 10),game.map.getTilesize()), speed);
synchronized(sprites)
{
this.sprites.add(s);
s.start();
}
return true;
}
//else
//{
// System.out.println("Einheit kann nicht an diese Position gesetzt werden!");
// System.out.println(game.getMap().isInMap(Math.worldToMap(position,game.getMap().getTilesize())));
// System.out.println(game.getMap().isPassable(IUnit.TYPE_LAND, Math.worldToMap(position,game.getMap().getTilesize())));
// return false;
//}
}
public Sprite getSprite(int id)
{
return sprites.get(id);
}
public int getSpriteCount()
{
return sprites.size();
}
public List<Sprite> getSprites()
{
return sprites;
}
public ThreadGroup getThreadGroup()
{
return this.sprites_tg;
}
}