Package org.freerealm.player

Source Code of org.freerealm.player.FreeRealmPlayer

package org.freerealm.player;

import java.awt.Color;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import java.util.Vector;
import org.freerealm.map.Coordinate;
import org.freerealm.Realm;
import org.freerealm.nation.Nation;
import org.freerealm.unit.Unit;
import org.freerealm.player.mission.Mission;
import org.freerealm.settlement.Settlement;
import org.freerealm.unit.DefaultUnitGroupDefinition;
import org.freerealm.unit.UnitGroupDefinition;

/**
*
* @author Deniz ARIKAN
*/
public class FreeRealmPlayer implements Player, Comparable<FreeRealmPlayer> {

    private Realm realm;
    private int id;
    private int status;
    private String name;
    private Color primaryColor;
    private Color secondaryColor;
    private boolean turnEnded;
    private int wealth;
    private int taxRate;
    private Nation nation;
    private SettlementManager settlementManager;
    private UnitManager unitManager;
    private Properties properties;
    private Vector<Coordinate> exploredCoordinates;
    private Vector<Mission> missions;
    private MessageManager messageManager;
    private HashMap<Integer, Integer> builtImprovementsCount;

    public FreeRealmPlayer(Realm realm) {
        this.realm = realm;
        setTaxRate(0);
        settlementManager = new SettlementManager(this);
        unitManager = new UnitManager(this);
        if (realm.getProperty("starting_wealth") != null) {
            setWealth(Integer.parseInt(realm.getProperty("starting_wealth")));
        }
        exploredCoordinates = new Vector<Coordinate>();
        missions = new Vector<Mission>();
        messageManager = new MessageManager();
        builtImprovementsCount = new HashMap<Integer, Integer>();
    }

    @Override
    public String toString() {
        return getName();
    }

    public int compareTo(FreeRealmPlayer player) {
        if (getId() < player.getId()) {
            return -1;
        } else if (getId() > player.getId()) {
            return 1;
        } else {
            return 0;
        }
    }

    public Realm getRealm() {
        return realm;
    }

    public UnitGroupDefinition getUnitGroupDefinition() {
        DefaultUnitGroupDefinition unitGroupDefinition = new DefaultUnitGroupDefinition();
        Iterator<Unit> iterator = getUnitsIterator();
        while (iterator.hasNext()) {
            Unit unit = iterator.next();
            unitGroupDefinition.setQuantityForUnitType(unit.getType(), unitGroupDefinition.getQuantityForUnitType(unit.getType()) + 1);
        }
        return unitGroupDefinition;
    }

    public int getTotalIncome() {
        int totalIncome = 0;
        Iterator<Settlement> settlementIterator = getSettlementManager().getSettlementsIterator();
        while (settlementIterator.hasNext()) {
            totalIncome = totalIncome + settlementIterator.next().getWealthCollectedByTax();
        }
        /*
        Iterator<Unit> unitIterator = getUnitsIterator();
        while (unitIterator.hasNext()) {
        unitIterator.next();
        }
         */
        return totalIncome;
    }

    public int getTotalIncomeIf(int tax) {
        int totalIncome = 0;
        Iterator<Settlement> settlementIterator = getSettlementManager().getSettlementsIterator();
        while (settlementIterator.hasNext()) {
            totalIncome = totalIncome + settlementIterator.next().getWealthCollectedByTaxIf(tax);
        }
        /*
        Iterator<Unit> unitIterator = getUnitsIterator();
        while (unitIterator.hasNext()) {
        unitIterator.next();
        }
         */
        return totalIncome;
    }

    public int getTotalExpenses() {
        int totalExpenses = 0;
        totalExpenses = totalExpenses + getSettlementManager().getSettlementUpkeep();
        totalExpenses = totalExpenses + getUnitManager().getUnitUpkeep();
        return totalExpenses;
    }

    public boolean isMissionAssigned(int missionId) {
        Iterator<Mission> iterator = getMissionsIterator();
        while (iterator.hasNext()) {
            Mission mission = iterator.next();
            if (mission.getId() == missionId) {
                return true;
            }
        }
        return false;
    }

    // <editor-fold defaultstate="collapsed" desc="Getters & setters">
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public int getPopulation() {
        int population = 0;
        Iterator<Settlement> settlementIterator = getSettlementManager().getSettlementsIterator();
        while (settlementIterator.hasNext()) {
            population = population + settlementIterator.next().getPopulation();
        }
        return population;
    }

    public boolean isTurnEnded() {
        return turnEnded;
    }

    public void setTurnEnded(boolean turnEnded) {
        this.turnEnded = turnEnded;
    }

    public Color getPrimaryColor() {
        return primaryColor;
    }

    public void setPrimaryColor(Color primaryColor) {
        this.primaryColor = primaryColor;
    }

    public Color getSecondaryColor() {
        return secondaryColor;
    }

    public void setSecondaryColor(Color secondaryColor) {
        this.secondaryColor = secondaryColor;
    }

    public int getWealth() {
        return wealth;
    }

    public void setWealth(int wealth) {
        this.wealth = wealth;
    }

    public int getTaxRate() {
        return taxRate;
    }

    public void setTaxRate(int taxRate) {
        this.taxRate = taxRate;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Nation getNation() {
        return nation;
    }

    public void setNation(Nation nation) {
        this.nation = nation;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public String getProperty(String key) {
        return getProperties().getProperty(key);
    }

    public String getNextSettlementName() {
        String settlementName = "";
        Iterator<String> iterator = getNation().getSettlementNamesIterator();
        while (iterator.hasNext()) {
            boolean settlementNameExists = false;
            String trysettlementName = iterator.next();
            Iterator<Settlement> settlementsIterator = getSettlementManager().getSettlementsIterator();
            while (settlementsIterator.hasNext()) {
                Settlement settlement = settlementsIterator.next();
                if (settlement.getName().equals(trysettlementName)) {
                    settlementNameExists = true;
                }
            }
            if (!settlementNameExists) {
                return trysettlementName;
            }
        }
        return settlementName;
    }

    public UnitManager getUnitManager() {
        return unitManager;
    }

    public int getUnitCount() {
        return unitManager.getUnitCount();
    }

    public Iterator<Unit> getUnitsIterator() {
        return getUnitManager().getUnitsIterator();
    }

    public Unit getUnit(int unitId) {
        return getUnitManager().getUnit(unitId);
    }

    public Unit getActiveUnit() {
        return getUnitManager().getActiveUnit();
    }

    public void setActiveUnit(Unit unit) {
        getUnitManager().setActiveUnit(unit);
    }

    public Unit getNextUnit(Unit unit) {
        return getUnitManager().getNextUnit(unit);
    }

    public boolean hasUnit(Unit unit) {
        return getUnitManager().getUnits().containsValue(unit);
    }

    public void addUnit(Unit unit) {
        getUnitManager().addUnit(unit);
    }

    public void removeUnit(Unit unit) {
        getUnitManager().removeUnit(unit);
    }

    public SettlementManager getSettlementManager() {
        return settlementManager;
    }

    public void setSettlementManager(SettlementManager settlementManager) {
        this.settlementManager = settlementManager;
    }

    public Iterator<Settlement> getSettlementsIterator() {
        return getSettlementManager().getSettlementsIterator();
    }

    public int getSettlementCount() {
        return settlementManager.getSettlementCount();
    }

    public void addSettlement(Settlement settlement) {
        getSettlementManager().addSettlement(settlement);
    }

    public void removeSettlement(Settlement settlement) {
        getSettlementManager().removeSettlement(settlement);
    }

    public Properties getProperties() {
        return properties;
    }

    public void addExploredCoordinate(Coordinate coordinate) {
        if (coordinate != null && (!getExploredCoordinates().contains(coordinate))) {
            getExploredCoordinates().add(coordinate);
        }
    }

    public void addExploredCoordinates(Vector<Coordinate> coordinates) {
        Iterator<Coordinate> iterator = coordinates.iterator();
        while (iterator.hasNext()) {
            Coordinate coordinate = iterator.next();
            addExploredCoordinate(coordinate);
        }
    }

    public boolean isCoordinateExplored(Coordinate coordinate) {
        return getExploredCoordinates().contains(coordinate);
    }

    public Vector<Coordinate> getExploredCoordinates() {
        return exploredCoordinates;
    }

    public int getMapExplorationPercent() {
        int mapExplorationPercent = 0;
        mapExplorationPercent = (getExploredCoordinates().size() * 100) / (realm.getMapHeight() * realm.getMapWidth());
        return mapExplorationPercent;
    }

    public void clearMessages() {
        messageManager.clear();
    }

    public void addMessage(Message message) {
        messageManager.addMessage(message);
    }

    public int getUnreadMessageCount() {
        return messageManager.getUnreadMessageCount();
    }

    public Iterator<Message> getUnreadMessagesIterator() {
        return messageManager.getUnreadMessagesIterator();
    }

    public Iterator<Message> getMessagesIterator() {
        return messageManager.getMessagesIterator();
    }

    public void addMission(Mission mission) {
        missions.add(mission);
    }

    public void removeMission(Mission mission) {
        missions.remove(mission);
    }

    public void clearMissions() {
        missions.clear();
    }

    public Iterator<Mission> getMissionsIterator() {
        return missions.iterator();
    }

    public void clearBuiltTileImprovementCount() {
        builtImprovementsCount.clear();
    }

    public Iterator<Integer> getBuiltTileImprovementCountIterator() {
        return builtImprovementsCount.keySet().iterator();
    }

    public int getBuiltTileImprovementCount(int typeId) {
        if (builtImprovementsCount.get(typeId) != null) {
            return builtImprovementsCount.get(typeId);
        } else {
            return 0;
        }
    }

    public void setBuiltTileImprovementCount(int typeId, int count) {
        builtImprovementsCount.put(typeId, count);
    }
    // </editor-fold>
}
TOP

Related Classes of org.freerealm.player.FreeRealmPlayer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.