Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.util.ArrayList;
- import info.gridworld.actor.Actor;
- import info.gridworld.actor.Critter;
- import info.gridworld.grid.Location;
- public class GenomeBase extends Critter
- {
- private static int DEFAULT_ATK = 1, DEFAULT_DEF = 1, DEFAULT_CON = 1;
- private int dAtk, dDef, dCon;
- public int Atk, Def, Con;
- private byte DefRegenStep;
- private boolean canAttack;
- /**
- * Default constructor of GenomeBase, all stats default to 1
- */
- public GenomeBase()
- {
- dAtk = Atk = DEFAULT_ATK;
- dDef = Def = DEFAULT_DEF;
- dCon = Con = DEFAULT_CON;
- canAttack = true;
- setColor(Color.white);
- }
- public GenomeBase(int Atk, int Def, int Con)
- {
- // Set all default and current stats
- this.dAtk = this.Atk = Atk;
- this.dDef = this.Def = Def;
- this.dCon = this.Con = Con;
- canAttack = true;
- setColor(Color.white); // Default GenomeBase color is white
- }
- /**
- * Attack another GenomeBase
- * @param toAttack, the GenomeBase to attack
- */
- public void attack(GenomeBase toAttack)
- {
- if(canAttack)
- {
- canAttack = false;
- toAttack.attacked(this);
- }
- }
- /**
- * Being attacked by another GenomBase
- * This is private to avoid confusion, attack another GenomeBase through attack()
- * @param attacker the GenomeBase who is attacking
- */
- private void attacked(GenomeBase attacker)
- {
- if(attacker.Atk > 0)
- {
- int DmgIncoming = attacker.Atk;
- if(Def > 0)
- {
- DmgIncoming -= Def;
- Def -= DmgIncoming;
- if(Def < 0)
- Def = 0;
- if(DmgIncoming > 0)
- {
- Con -= DmgIncoming;
- if(Con <= 0)
- {
- removeSelfFromGrid();
- }
- }
- }
- }
- // Repoiste, if still alive
- attack(attacker);
- }
- /**
- * GenomeBase regeneration, gains Con and Def with conditions.
- */
- private void regen()
- {
- if(Con < dCon)
- Con++;
- if(DefRegenStep == 2)
- {
- DefRegenStep = 0;
- Def++;
- }
- else
- DefRegenStep++;
- }
- // Override Methods
- @Override
- public void act()
- {
- if (getGrid() == null)
- return;
- ArrayList<Actor> actors = getActors();
- processActors(actors);
- if(getLocation() == null) return; // Apparently stuff goes wrong when you remove an actor from the grid in the middle of stuff?
- ArrayList<Location> moveLocs = getMoveLocations();
- Location loc = selectMoveLocation(moveLocs);
- makeMove(loc);
- }
- @Override
- public ArrayList<Actor> getActors()
- {
- return getGrid().getNeighbors(getLocation());
- }
- @Override
- public void processActors(ArrayList<Actor> actors)
- {
- canAttack = true;
- if(Def == dDef && Con == dCon)
- {
- // We're ready, let's attack if we can
- ArrayList<GenomeBase> genomeList = new ArrayList<GenomeBase>();
- for(Actor a : actors)
- {
- if(a instanceof GenomeBase) // Get all other genomes around so we can attack them
- {
- genomeList.add((GenomeBase) a);
- }
- }
- int num = genomeList.size();
- if(num > 0) // There are other actors around us, let's attack
- {
- GenomeBase toAttack = genomeList.get(((int) Math.random() * num));
- attack(toAttack);
- }
- }
- else
- {
- regen(); // Spend a turn regenerating Con and Def
- }
- }
- @Override
- public ArrayList<Location> getMoveLocations()
- {
- return getGrid().getEmptyAdjacentLocations(getLocation());
- }
- @Override
- public Location selectMoveLocation(ArrayList<Location> locs)
- {
- int n = locs.size();
- if (n == 0)
- return getLocation();
- int r = (int) (Math.random() * n);
- return locs.get(r);
- }
- @Override
- public void makeMove(Location loc)
- {
- setDirection(getLocation().getDirectionToward(loc));
- super.makeMove(loc);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement