/// <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> /// 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); }
/// <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> /// 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; }