// USAGE: returns a crude randomly-generated direction // NOTE: use this in the case there are no enemy units to head towards public Unit.Direction randomDir() { Array dVals = Enum.GetValues(typeof(Unit.Direction)); Unit.Direction rDir = (Unit.Direction)dVals.GetValue(UnityEngine.Random.Range(0, dVals.Length)); return(rDir); }
// add a new unit to the field public void addUnit(List <string> actions, bool isSelectable, Cell startCell, string unitName, Unit.Faction faction, Unit.Direction facing, int id, List <Unit.Faction> enemyFactions) { if (allUnits == null) { allUnits = new List <Unit>(); } // the unit will set its proper name later GameObject newUnit = new GameObject("newUnit"); // adds appropriate tag in reference to the unit's faction newUnit.tag = faction.ToString(); // BoxCollider is needed to make this unit clickable if (isSelectable) { newUnit.AddComponent <BoxCollider2D>(); } // For now, all units get a basic ai controller except // units directly controlled by the player if (faction == Unit.Faction.Player) { newUnit.AddComponent <ManualController>(); } else { // newUnit.AddComponent<RandomAIController>(); newUnit.AddComponent <SimpleAIController>(); /// MODIFED THIS HERE FOR TESTING !!! /// } // add the appropriate IntfActionModule impls to the unit foreach (string action in actions) { switch (action) { case "singlepanelbasicattack": newUnit.AddComponent <SinglePanelBasicAttack>(); break; case "smacknearby": newUnit.AddComponent <SmackNearby>(); break; case "jumptest": newUnit.AddComponent <JumpTestAction>(); break; } } Unit unitComp = newUnit.AddComponent <Unit>(); unitComp.setUnit(startCell, facing, Unit.Faction.Player, unitName, id, enemyFactions); startCell.setUnit(unitComp); allUnits.Add(unitComp); }
// returns a list of cells in range public HashSet <Cell> findTargetCells(Cell position, Unit.Direction facing) { HashSet <Cell> retCells = new HashSet <Cell>(); Cell nextCell = GameObject.Find("grid").GetComponent <Grid>().nextCell(position, facing, 1); if (nextCell != position) { retCells.Add(nextCell); } return(retCells); }
// USAGE: execute the action public void executeAction(Cell position, Unit.Direction facing) { HashSet <Unit> units = findTargetUnits(position, facing); if (units.Count != 0) { foreach (Unit unit in units) { unit.takeDamage(attackDamage); } gameObject.GetComponent <Unit> ().canAct = false; } }
// returns a list of cells in range // NOTE: the returned list is either EMPTY or contains ONE cell public HashSet <Cell> findTargetCells(Cell position, Unit.Direction facing) { HashSet <Cell> targetCells = new HashSet <Cell> (); Cell jumpToCell = GameObject.Find("grid").GetComponent <Grid> (). nextCell(position, facing, numJumpSpaces); if (jumpToCell != null) { targetCells.Add(jumpToCell); } return(targetCells); }
public void PlayRotateAnimation(Unit.Direction toDirection, Action onFinished) { if (toDirection == Unit.Direction.Left) { var sprite = GetComponent <SpriteRenderer>(); sprite.flipX = true; } else if (toDirection == Unit.Direction.Right) { var sprite = GetComponent <SpriteRenderer>(); sprite.flipX = false; } onFinished(); }
// returns a list of cells in range public HashSet <Cell> findTargetCells(Cell position, Unit.Direction facing) { HashSet <Cell> retCells = new HashSet <Cell>(); Grid grid = GameObject.Find("grid").GetComponent <Grid>(); Cell lleftCell = grid.nextCell(position, Unit.Direction.LLeft, 1); if (lleftCell != position) { retCells.Add(lleftCell); retCells.Add(grid.nextCell(lleftCell, Unit.Direction.ULeft, 1)); retCells.Add(grid.nextCell(lleftCell, Unit.Direction.LRight, 1)); } Cell uleftCell = grid.nextCell(position, Unit.Direction.ULeft, 1); if (uleftCell != position) { retCells.Add(uleftCell); retCells.Add(grid.nextCell(uleftCell, Unit.Direction.LLeft, 1)); retCells.Add(grid.nextCell(uleftCell, Unit.Direction.URight, 1)); } Cell urightCell = grid.nextCell(position, Unit.Direction.URight, 1); if (urightCell != position) { retCells.Add(urightCell); retCells.Add(grid.nextCell(urightCell, Unit.Direction.ULeft, 1)); retCells.Add(grid.nextCell(urightCell, Unit.Direction.LRight, 1)); } Cell lrightCell = grid.nextCell(position, Unit.Direction.LRight, 1); if (lrightCell != position) { retCells.Add(lrightCell); retCells.Add(grid.nextCell(lrightCell, Unit.Direction.LLeft, 1)); retCells.Add(grid.nextCell(lrightCell, Unit.Direction.URight, 1)); } //Cell nextCell = GameObject.Find("grid").GetComponent<Grid>().nextCell(position, facing, 1); //if (nextCell != position) // retCells.Add(nextCell); //retCells.RemoveWhere(x => x == position); return(retCells); }
// USAGE: attempts to "jump" the current unit into desired spot, // if said spot is open; do nothing otherwise // WARNING: allTargets.Single will work ONLY if there is exactly // one item in the hashset !!! (ok for this case) public void executeAction(Cell position, Unit.Direction facing) { Unit thisUnit = gameObject.GetComponent <Unit> (); HashSet <Cell> allTargets = findTargetCells(position, facing); if (allTargets.Count != 0) { Cell newCell = allTargets.Single(); if (!newCell.getOccupied()) { thisUnit.moveUnit(numJumpSpaces); thisUnit.canAct = false; } } }
void OnTriggerEnter(Collider col) { if (col.gameObject.tag.Equals("Player")) { Unit player = col.transform.parent.gameObject.GetComponent <Unit>(); direction = player.lastDirection; switch (direction) { case Unit.Direction.North: grassFall = Vector3.right * 30; break; case Unit.Direction.South: grassFall = Vector3.left * 30; break; case Unit.Direction.East: grassFall = Vector3.forward * 30; break; case Unit.Direction.West: grassFall = Vector3.back * 30; break; case Unit.Direction.NE: grassFall = new Vector3(1, 0, -1); break; case Unit.Direction.NW: grassFall = new Vector3(1, 0, 1); break; case Unit.Direction.SE: grassFall = new Vector3(-1, 0, -1); break; case Unit.Direction.SW: grassFall = new Vector3(-1, 0, 1); break; default: break; } coll = true; } }
// returns a list of units in range (may be empty) of position public HashSet <Unit> findTargetUnits(Cell position, Unit.Direction facing) { HashSet <Unit> retUnits = new HashSet <Unit>(); HashSet <Cell> targetCells = findTargetCells(position, facing); foreach (Cell cell in targetCells) { if (cell.getOccupied()) { retUnits.Add(cell.getUnit()); } } // returns empty set if no units in rate return(retUnits); }
// returns a list of units in range (may be empty) of position public HashSet <Unit> findTargetUnits(Cell position, Unit.Direction facing) { HashSet <Unit> retUnits = new HashSet <Unit>(); // for this attack, we only check the one cell Cell nextCell = GameObject.Find("grid").GetComponent <Grid>().nextCell(position, facing, 1); if (nextCell != position) { if (nextCell.getOccupied()) { retUnits.Add(nextCell.getUnit()); } } // returns empty set if no units in rate return(retUnits); }
// USAGE: returns the n-th cell from current cell in the specified direction // NOTE: currently just used by the Unit class for movement process public Cell nextCell(Cell currentCell, Unit.Direction dir, int n) { int row = currentCell.getRow(); int col = currentCell.getCol(); Cell nthCell = gridLayout[row, col]; // returns currentCell if all else fails if (n == 0) { return(nthCell); } int val = 0; switch (dir) { case Unit.Direction.LLeft: val = Mathf.Min(gridLayout.GetLength(0) - 1, row + n); nthCell = gridLayout [val, col]; break; case Unit.Direction.URight: val = Mathf.Max(0, row - n); nthCell = gridLayout [val, col]; break; case Unit.Direction.ULeft: val = Mathf.Max(0, col - n); nthCell = gridLayout [row, val]; break; case Unit.Direction.LRight: val = Mathf.Min(gridLayout.GetLength(1) - 1, col + n); nthCell = gridLayout [row, val]; break; } return(nthCell); }
public GameRotateResultAction(int unitId, Unit.Direction direction) : base(unitId) { this.direction = direction; }
// returns the states of target units after executing the action // this assumes every action as a target, is that fair? // like, you can attack units, heal units, maybe cast buffs // btw this is used to determine the utility of an action // we use it for the value iteration routine public List <Unit> resultOfAction(Cell position, Unit.Direction facing, List <Unit> targets) { return(null); }
// returns a list of units in range (may be empty) of position // WARNING: null is returned since this action does not affect ANY units public HashSet <Unit> findTargetUnits(Cell position, Unit.Direction facing) { return(new HashSet <Unit>()); }