/// <summary> /// Readies battlefield between hero and neutral units /// </summary> /// <param name="width">Width of battlefield</param> /// <param name="height">Height of battlefield</param> /// <param name="attacker">Attacking hero</param> /// <param name="defender">Defending units</param> public void beginCombat(int width, int height, Hero attacker, UnitTree defender, bool ai) { Width = width; Height = height; InCombat = true; Canwalk = new int[width, height]; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { Canwalk[x, y] = MapGenerator.MapMaker.CANWALK; } } BattleField = new BattleField(width, height, attacker, defender, Canwalk); populateField(); populateInitative(attacker, defender); possibleMovement = new PossibleMovement(field, unitsOnField, canwalk, width, height); flipReachableAndAttackable(); this.ai = ai; if (ai) { combatAi = new CombatAi(this, field, unitsOnField, width, height); } }
/// <summary> /// Executes the action, swapping unit in fromSlot with unit in toSlot. /// </summary> public override void execute() { HeroMeetReact hmr1; if (Gm.Reactions[fromHero.x, fromHero.y].HasPreReact()) { hmr1 = (HeroMeetReact)Gm.Reactions[fromHero.x, fromHero.y].PreReaction; } else { hmr1 = (HeroMeetReact)Gm.Reactions[fromHero.x, fromHero.y]; } HeroMeetReact hmr2; if (Gm.Reactions[toHero.x, toHero.y].HasPreReact()) { hmr2 = (HeroMeetReact)Gm.Reactions[toHero.x, toHero.y].PreReaction; } else { hmr2 = (HeroMeetReact)Gm.Reactions[toHero.x, toHero.y]; } UnitTree utFrom = hmr1.Hero.Units; UnitTree utTo = hmr2.Hero.Units; Unit tmp = utTo.GetUnits()[toSlot]; int tmpAmount = utTo.getUnitAmount(toSlot); utTo.setUnit(utFrom.GetUnits()[fromSlot], utFrom.getUnitAmount(fromSlot), toSlot); utFrom.setUnit(tmp, tmpAmount, fromSlot); }
/// <summary> /// Default constructor used when the game launches and the Hero doesn't belong to a player /// </summary> public Mantooth() : base(LOCAL_SPRITE_ID, PORTRAIT_ID, NAME, DESCRIPTION, new Cost(GOLD_COST, WOOD_COST, ORE_COST, CRYSTAL_COST, GEM_COST)) { unitTree = new UnitTree(); unitTree.addUnit(new StoneTroll(), UnityEngine.Random.Range(2, 4)); SetUnits(unitTree); }
/// <summary> /// Constructor when Hero belongs to a player /// </summary> /// <param name="player">The player this hero belongs to</param> /// <param name="position">Position to spawn the player in</param> public Gork(Player player, Point position) : base(player, position, LOCAL_SPRITE_ID, PORTRAIT_ID, NAME, DESCRIPTION, new Cost(GOLD_COST, WOOD_COST, ORE_COST, CRYSTAL_COST, GEM_COST)) { unitTree = new UnitTree(); unitTree.addUnit(new StoneTroll(), UnityEngine.Random.Range(2, 4)); SetUnits(unitTree); }
/// <summary> /// Constructor for Battlefield for battle with heroless units /// </summary> /// <param name="width">Width of battlefield</param> /// <param name="height">Height of battlefield</param> /// <param name="attacker">The attacking hero</param> /// <param name="defender">The defending units</param> /// <param name="canWalk">Canwalk</param> public BattleField(int width, int height, Hero attacker, UnitTree defender, int[,] canWalk) { Width = width; Height = height; Attacker = attacker; CanWalk = canWalk; aStar = new AStarAlgo(canWalk, width, height, true); attackingUnits = attacker.Units; defendingUnits = defender; Unit[] aUnits = attacker.Units.GetUnits(); Unit[] dUnits = defender.GetUnits(); int increment = height / UnitTree.TREESIZE; int place = 0; unitsPos = new UnitAndAmount[width, height]; for (int i = 0; i < UnitTree.TREESIZE; i++) { if (aUnits[i] != null) { UnitAndAmount atroop = new UnitAndAmount(attacker.Units, i); unitsPos[0, place] = atroop; } if (dUnits[i] != null) { UnitAndAmount dtroop = new UnitAndAmount(defender, i); unitsPos[width - 1, place] = dtroop; } place += increment; } }
/// <summary> /// Constructor /// </summary> /// <param name="ut">UnitTree</param> /// <param name="i">Index of unit in UnitTree</param> public UnitAndAmount(UnitTree ut, int i) { UnitTree = ut; posInUnitTree = i; Unit = ut.GetUnits()[i]; Amount = ut.getUnitAmount(i); }
public UnitReaction(UnitTree units, Point pos) { Units = units; Pos = pos; GameObject go = GameObject.Find("GameManager"); gm = go.GetComponent <GameManager>(); }
/// <summary> /// Consturctor for towns /// </summary> /// <param name="owner">The player that owns the town, NULL if not owned</param> /// <param name="localSpriteID">The spriteID for the given town</param> /// <param name="position">The map position for the town</param> public Town(Player owner, int localSpriteID, Point position) : base(localSpriteID, category) { Position = position; StationedUnits = new UnitTree(); RelatedDwellings = new List <DwellingBuilding>(); Owner = owner; StationedUnits = new UnitTree(); VisitingUnits = new UnitTree(); }
/// <summary> /// Merges input army2 into army1 /// </summary> /// <param name="units2">Second input army</param> public void Merge(UnitTree units2) { // Merge duplicates units2 into units1 for (int i = 0; i < GetUnits().Length; i++) { if (GetUnits()[i] != null) { for (int j = 0; j < GetUnits().Length; j++) { if (units2.GetUnits()[j] != null && units2.GetUnits()[j].equals(GetUnits()[i])) { // Duplicate found in a bar, put one of them into the other one, and add amount unitAmount[i] += units2.unitAmount[j]; units2.GetUnits()[j] = null; } } } } // Merge duplicates in same bar for (int i = 0; i < GetUnits().Length; i++) { if (GetUnits()[i] != null) { for (int j = i + 1; j < GetUnits().Length; j++) { if (GetUnits()[j] != null && GetUnits()[i].equals(GetUnits()[j])) { // Duplicate found in a bar, put one of them into the other one, and add amount unitAmount[i] += unitAmount[j]; units[j] = null; } } } } // Merge units2 into units1 for (int i = 0; i < GetUnits().Length; i++) { // If unit is found in units2 if (units2.GetUnits()[i] != null) { // Check the units1 row to find next open spot to put it for (int j = 0; j < GetUnits().Length; j++) { if (GetUnits()[j] == null) { GetUnits()[j] = units2.GetUnits()[i]; unitAmount[j] = units2.getUnitAmount(i); units2.GetUnits()[i] = null; } } } } }
/// <summary> /// Called upon when a hero tries to enter a stationedTown /// </summary> /// <param name="units1">first army to check into</param> /// <param name="units2">the other army</param> /// <returns></returns> public bool CanMerge(UnitTree units2) { if (units != null || units2 == null || units2.units != null) { return(true); } // Check if merge can be done int count = 0; for (int i = 0; i < TREESIZE; i++) { if (units[i] != null) { count++; for (int j = 0; j < TREESIZE; j++) { if (units[i] != null && units[i].equals(units2.units[j])) { count--; } } } if (units2.units[i] != null) { count++; } } if (count <= TREESIZE) { return(true); } return(false); /* * * UnitTree tmp1 = new UnitTree(this); * UnitTree tmp2 = new UnitTree(units2); * // Perform testMerge of tmp2 into tmp1 * tmp1.Merge(tmp2); * * // Can merge if count in the second unittree is less or same as first unittree's openspots * if (tmp1.OpenSpots() >= tmp2.CountUnits()) * { * return true; * } * return false; */ }
/// <summary> /// Executes the action, unit is switched. /// </summary> public override void execute() { HeroMeetReact hmr; if (Gm.Reactions[pos.x, pos.y].HasPreReact()) { hmr = (HeroMeetReact)Gm.Reactions[pos.x, pos.y].PreReaction; } else { hmr = (HeroMeetReact)Gm.Reactions[pos.x, pos.y]; } UnitTree ut = hmr.Hero.Units; ut.swapUnits(from, to); }
// When in town window, activated by clicking on first and then second hero public void swapHeroes() { // Only swap if there is a hero in one of the spots if (visitingHero != null || stationedHero != null) { // TODO check merge, and merge - if there's not an hero in stationedarmy if (stationedHero == null) { if (VisitingHero.Units.CanMerge(stationedUnits)) { VisitingHero.Units.Merge(stationedUnits); } } // Swap heroes Hero tmpHero = visitingHero; visitingHero = stationedHero; stationedHero = tmpHero; // Swap town's armies to the new heroes armies if (visitingHero == null) { visitingUnits = new UnitTree(); } else { visitingUnits = visitingHero.Units; } if (StationedHero == null) { stationedUnits = new UnitTree(); } else { stationedUnits = stationedHero.Units; } } }
/// <summary> /// Readies initative and populates 2d array for units /// </summary> /// <param name="attacker">Attacking hero</param> /// <param name="defender">Defending units</param> public void populateInitative(Hero attacker, UnitTree defender) { unitsOnField = new GameObject[width, height]; Initative = new UnitGameObject[UnitTree.TREESIZE * 2]; int logPos = 0; int increment = Height / UnitTree.TREESIZE; int place = 0; livingAttackers = livingDefenders = 0; // Adds attacking units UnitTree units = attacker.Units; for (int i = 0; i < UnitTree.TREESIZE; i++) { if (units.GetUnits()[i] != null) { GameObject go = Instantiate(unit, parent.transform); go.name = "a" + i; SpriteRenderer sr = go.GetComponent <SpriteRenderer>(); sr.sprite = troll; sr.sortingLayerName = "CombatUnits"; TextMesh tm = go.GetComponentInChildren <TextMesh>(); tm.text = "" + units.getUnitAmount(i); tm.GetComponent <Renderer>().sortingLayerName = "CombatUnits"; UnitGameObject ugo = go.GetComponent <UnitGameObject>(); ugo.UnitTree = units; ugo.PosInUnitTree = i; ugo.GraphicalBattlefield = this; ugo.AttackingSide = true; ugo.Initative = units.GetUnits()[i].Unitstats.Initative; ugo.LogicalPos = new Point(0, place); Initative[logPos++] = ugo; //set correct graphical pos go.transform.localPosition = field[0, place].transform.localPosition; UnitsOnField[0, place] = go; field[0, place].GetComponent <GroundGameObject>().IsOccupied = true; livingAttackers++; } place += increment; } //Adds defending units units = defender; place = 0; for (int i = 0; i < UnitTree.TREESIZE; i++) { if (units.GetUnits()[i] != null) { GameObject go = Instantiate(unit, parent.transform); go.name = "d" + i; SpriteRenderer sr = go.GetComponent <SpriteRenderer>(); sr.flipX = true; sr.sprite = troll; sr.sortingLayerName = "CombatUnits"; TextMesh tm = go.GetComponentInChildren <TextMesh>(); tm.text = "" + units.getUnitAmount(i); tm.GetComponent <Renderer>().sortingLayerName = "CombatUnits"; UnitGameObject ugo = go.GetComponent <UnitGameObject>(); ugo.UnitTree = units; ugo.PosInUnitTree = i; ugo.GraphicalBattlefield = this; ugo.AttackingSide = false; ugo.Initative = units.GetUnits()[i].Unitstats.Initative; ugo.LogicalPos = new Point(width - 1, place); Initative[logPos++] = ugo; //set correct graphical pos go.transform.localPosition = field[width - 1, place].transform.localPosition; UnitsOnField[Width - 1, place] = go; field[width - 1, place].GetComponent <GroundGameObject>().IsOccupied = true; livingDefenders++; } place += increment; } Array.Sort(initative); Array.Reverse(initative); WhoseTurn = 0; initative[whoseTurn].ItsTurn = true; }
public UnitTree(UnitTree unitTree) { this.units = unitTree.units; this.unitAmount = unitTree.unitAmount; }
protected void SetUnits(UnitTree unitTree) { Units = unitTree; }