private void stopSelected() { for (GameSprite sprite : selected) { if (sprite instanceof Unit) { Unit unit = (Unit) sprite; unit.stop(); } } }
public void newGame() { this.party = new Unit[8]; // 6 party members, 2 extra space in case 1 in party and 5 in "storage" this.party[0] = Unit.characterFromID(Character.BRIAN, 1, 0); this.party[0].equip[Unit.WEAPON] = new Stick(1); for (int i = 1; i < this.party.length; i++) // TODO: consider having a "shell" character class w/ character info and rebuild // character from that (so things always get instantiated/refreshed as expected) { this.party[i] = new None(i); } this.mapStates = new ArrayList<Integer>(); for (int i = 0; i < Map.numMaps; i++) { for (int j = 0; j < Map.mapStates[i]; j++) { this.mapStates.add(0); } } this.inventory = new ArrayList<Item>(); this.dataExists = true; this.curMap = 0; this.curX = 14; this.curY = 58; this.respawnMap = 0; this.respawnX = 14; this.respawnY = 58; this.money = 0; this.gameTime = 0; }
@Override public void think() { // place your code here. // here is a very basic sample of what you can do: Random rand = new Random(); // spawn some random units, if you have enough supplies for (Building.HeadQuarters hq : getSelfAgents().getHQsList()) { if (new Random().nextBoolean()) { if (getSuppliesAmount() >= Config.Unit.Ranged.creationCost) { hq.spawnUnit(UnitType.RANGED); } } else { if (getSuppliesAmount() >= Config.Unit.Melee.creationCost) { hq.spawnUnit(UnitType.MELEE); } } } // list all friendly units ArrayList<Unit> units = getSelfAgents().getAllUnits(); // list friendly ranged units: ArrayList<Unit> rangedUnits = getSelfAgents().getRangedList(); // list friendly melee units: ArrayList<Unit> meleeUnits = getSelfAgents().getMeleeList(); // list all supplies' positions ArrayList<Point> supplies = new ArrayList<Point>(); for (int i = 0; i < getGameField().getWidth(); i++) { for (int j = 0; j < getGameField().getHeight(); j++) { if (getGameField().isSuppliesAt(i, j)) { supplies.add((new Point(i, j))); } } } // gather nearby supplies, or move randomly if there is none for (Unit unit : units) { if (getGameField().isSuppliesAt(unit.getX() + 1, unit.getY())) { unit.move(Direction.EAST); } else if (getGameField().isSuppliesAt(unit.getX() - 1, unit.getY())) { unit.move(Direction.WEST); } else if (getGameField().isSuppliesAt(unit.getX(), unit.getY() + 1)) { unit.move(Direction.SOUTH); } else if (getGameField().isSuppliesAt(unit.getX(), unit.getY() - 1)) { unit.move(Direction.NORTH); } else { // pick a random direction and make sure it is walkable(there is no // wall there) and no friendly unit is already there Direction dir; dir = Direction.getDirByNum(rand.nextInt()); // check walkable if (getGameField().isWalkable(unit.getX() + dir.deltaX, unit.getY() + dir.deltaY)) { // check if there is no unit if (getSelfAgents().getUnitAt(unit.getX() + dir.deltaX, unit.getY() + dir.deltaY) == null) { unit.move(dir); } } } } // attack everything you can see!! // (note that each agent will only run the LAST command given to it, // meaning they will ignore the move command and run the attack command // if possible) ArrayList<Unit> enemyUnits = getEnemyAgents().getAllUnits(); ArrayList<Building.HeadQuarters> enemyBuildings = getEnemyAgents().getHQsList(); for (Unit unit : units) { for (Unit enemyUnit : enemyUnits) { if (unit.isInAttackRange(enemyUnit)) { unit.attack(enemyUnit); } } for (Building enemyBuilding : enemyBuildings) { if (unit.isInAttackRange(enemyBuilding)) { unit.attack(enemyBuilding); } } } }