private bool targetIsInRange() { if (target.getEntityType() == Entity.EntityType.Unit) { Unit tUnit = (Unit)target; double dis = Math.Sqrt(Math.Pow((tUnit.x - unit.x), 2) + Math.Pow(tUnit.y - unit.y, 2)); return(dis <= unit.stats.attackRange); } else { StaticEntity se = (StaticEntity)(target); float xC = se.orginCell.Xcoord; float yC = se.orginCell.Ycoord; short width = se.width; short height = se.height; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { if (EntityLocController.findDistance(unit.x, unit.y, xC + i, yC + j) <= unit.stats.attackRange) { return(true); } } } } return(false); }
/// <summary> /// Performs the GuardAttack action. /// </summary> /// <returns>false if the action is not complete, true if the action is complete.</returns> public override bool work() { State targetState = target.getState(); // Check if target's state is DEAD or REMOVE if (!targetDead && targetState.getPrimaryState() == State.PrimaryState.Dead || targetState.getPrimaryState() == State.PrimaryState.Remove) { targetDead = true; } // unit cannot attack, return true. if (!unit.stats.canAttack) { return(true); } // Distance between unit and the Cell it is guarding. float dis = EntityLocController.findDistance(unit.x, unit.y, unit.getGuardCell().Xcoord, unit.getGuardCell().Ycoord); // Is target dead or out of range? if (targetDead || dis > MAX_DIST) { // Create a new MoveAction if it is needed. if (moveAction == null) { moveAction = new MoveAction(unit.getGuardCell().Xcoord, unit.getGuardCell().Ycoord, gw, unit); } // Move back to original position return(moveAction.work()); } // Attack the enemy. else { // Create a new AttackAction if it is needed. if (attackAction == null) { attackAction = new AttackAction(unit, target, gw); } // Chase or attack the target. attackAction.work(); } return(false); }
/// <summary> /// This function will perform a building cycle if the number of ticks since the last cycle is equal to TICKS_PER_CYCLE. /// </summary> /// <returns>true if the building is complete and the action is finished, false otherwise.</returns> public override bool work() { // Building is complete, finish action. if (building.health == building.stats.maxHealth) { return(true); } // Unit cannot build, disregard action. if (!unit.stats.canBuild) { return(true); } if (curTicks % TICKS_PER_CYCLE == 0) { // Check if unit is adjacent to building. if (isUnitNextToBuilding()) { unit.getState().setPrimaryState(State.PrimaryState.Building); if (building.stats.maxHealth - building.health <= unit.stats.buildSpeed) { // Finish the building. building.health = building.stats.maxHealth; building.isCompleted = true; return(true); } else { // Continue building the building. building.health += unit.stats.buildSpeed; } } else { // Move towards the building. Insert a move action into the Unit's action queue. Cell targetCell = EntityLocController.findClosestCell(unit, building, gw); MoveAction moveAction = new MoveAction(targetCell.Xcoord, targetCell.Ycoord, gw, unit); ActionController.insertIntoActionQueue(unit, moveAction); } } curTicks++; return(false); }
/// <summary> /// This function checks if the unit is occupying a Cell next to the building. /// </summary> /// <returns>true if the unit is next to the building, false otherwise.</returns> private bool isUnitNextToBuilding() { float xC = building.orginCell.Xcoord; float yC = building.orginCell.Ycoord; short width = building.width; short height = building.height; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { if (EntityLocController.findDistance(unit.x, unit.y, xC + i, yC + j) <= 1.99) { return(true); } } } return(false); }