package org.freerealm.unit;
import java.util.Iterator;
import java.util.Vector;
import org.freerealm.Container;
import org.freerealm.resource.Resource;
import org.freerealm.map.Coordinate;
import org.freerealm.map.MapItem;
import org.freerealm.Realm;
import org.freerealm.property.Fight;
import org.freerealm.property.ContainerProperty;
import org.freerealm.player.Player;
/**
*
* @author Deniz ARIKAN
*/
public class Unit extends MapItem implements Container, Fighter {
public static final int UNIT_ACTIVE = 0;
public static final int UNIT_SUSPENDED = 1;
private UnitType type;
private float movementPoints;
private boolean skippedForCurrentTurn;
private Vector<Order> orders;
private ContainerManager containerManager;
private int status;
public Unit(Realm realm, UnitType type, Coordinate coordinate, Player player) {
super(realm, coordinate, player);
status = UNIT_ACTIVE;
setType(type);
orders = new Vector<Order>();
containerManager = new ContainerManager(realm, this);
setMovementPoints(type.getMovementPoints());
}
public Unit(Realm realm) {
super(realm);
status = UNIT_ACTIVE;
orders = new Vector<Order>();
containerManager = new ContainerManager(realm, this);
}
@Override
public String toString() {
return getType().toString() + " " + getId();
}
// <editor-fold defaultstate="collapsed" desc="Getters & setters">
public UnitType getType() {
return type;
}
public void setType(UnitType type) {
this.type = type;
setMovementPoints(type.getMovementPoints());
}
public float getMovementPoints() {
return movementPoints;
}
public void setMovementPoints(float movementPoints) {
this.movementPoints = movementPoints;
}
public boolean isSkippedForCurrentTurn() {
return skippedForCurrentTurn;
}
public void setSkippedForCurrentTurn(boolean skippedForCurrentTurn) {
this.skippedForCurrentTurn = skippedForCurrentTurn;
}
public Order getNextOrder() {
if (orders.size() > 0) {
return orders.get(0);
} else {
return null;
}
}
public Iterator<Order> getOrdersIterator() {
return orders.iterator();
}
public void addOrder(Order order) {
orders.add(order);
}
public void removeOrder(Order order) {
orders.remove(order);
}
public void clearOrders() {
orders.clear();
}
public int getTotalContainedWeight() {
return containerManager.getTotalCargoWeight();
}
public int getResourceQuantity(Resource resource) {
return containerManager.getResourceQuantity(resource);
}
public void setResourceQuantity(Resource resource, int quantity) {
containerManager.setResourceQuantity(resource, quantity);
}
public int getRemainingCapacity(Resource resource) {
ContainerProperty storeResource = (ContainerProperty) getType().getAbility("ContainerProperty");
if (storeResource == null) {
return 0;
}
if (!storeResource.hasResource(resource)) {
return 0;
}
return getTotalCapacity(resource) - getTotalContainedWeight();
}
public int getTotalCapacity() {
ContainerProperty containerProperty = (ContainerProperty) getType().getAbility("ContainerProperty");
if (containerProperty == null) {
return 0;
}
return containerProperty.getCapacity();
}
public int getTotalCapacity(Resource resource) {
return getTotalCapacity();
}
public int getRemainingCapacity() {
return getTotalCapacity() - getTotalContainedWeight();
}
public ContainerManager getContainerManager() {
return containerManager;
}
public void setContainerManager(ContainerManager containerManager) {
this.containerManager = containerManager;
}
public Iterator<Resource> getStorableResourcesIterator() {
ContainerProperty containerProperty = (ContainerProperty) getType().getAbility("ContainerProperty");
if (containerProperty == null) {
return new Vector<Resource>().iterator();
}
return containerProperty.getResourcesIterator();
}
public Iterator<Resource> getContainedResourcesIterator() {
return containerManager.getContainedResourcesIterator();
}
public boolean isStoringResource(Resource resource) {
ContainerProperty containerProperty = (ContainerProperty) getType().getAbility("ContainerProperty");
if (containerProperty == null) {
return false;
}
return containerProperty.hasResource(resource);
}
public Iterator<Integer> getContainedUnitsIterator() {
return containerManager.getContainedUnitsIterator();
}
public boolean canContainUnit(UnitType unitType) {
ContainerProperty containerProperty = (ContainerProperty) getType().getAbility("ContainerProperty");
if (containerProperty == null) {
return false;
}
return containerProperty.hasUnitType(unitType.getId());
}
public int getContainedPopulation() {
return containerManager.getContainedPopulation();
}
public void setContainedPopulation(int population) {
containerManager.setContainedPopulation(population);
}
public boolean canContainPopulation() {
ContainerProperty containerProperty = (ContainerProperty) getType().getAbility("ContainerProperty");
if (containerProperty == null) {
return false;
}
return containerProperty.isAccomodatingPopulation();
}
public void addUnit(int unitId) {
containerManager.addUnit(unitId);
}
public void removeUnit(int unitId) {
containerManager.removeUnit(unitId);
}
public boolean canAttack() {
Fight fight = (Fight) getType().getAbility("Fight");
if (fight == null) {
return false;
}
if (getMovementPoints() == 0) {
return false;
}
return true;
}
public int getAttackPoints() {
Fight fight = (Fight) getType().getAbility("Fight");
if (fight == null) {
return 0;
}
return fight.getAttackPoints();
}
public int getDefencePoints() {
Fight fight = (Fight) getType().getAbility("Fight");
if (fight == null) {
return 0;
}
return fight.getDefencePoints();
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
// </editor-fold>
}