void FixedUpdate() { float distance = Vector2.Distance(transform.position, targetPos); if (distance > CrewMember.MOVEMENT_ERROR_RANGE) { transform.position = Vector3.Lerp(transform.position, targetPos, speed / distance * Time.fixedDeltaTime); } else { if (hit) { if (destination.damagedTile) { targetShip.damagedTiles.Remove(destination.damagedTile); Destroy(destination.damagedTile.gameObject); } DamagedPosition dp = Instantiate(damagedTile, destination.transform.position, Quaternion.identity).GetComponent <DamagedPosition> (); destination.damagedTile = dp; dp.tile = destination; dp.isAssignAbleTo = !shotByPlayer; dp.transform.parent = destination.transform.parent.parent; targetShip.damagedTiles.Add(dp); targetShip.TakeDamage(damage); } Destroy(gameObject); } }
public DamagedPosition GetClosestDamagedTile(MovementTile tile) { DamagedPosition dp = null; int shortestPathCount = int.MaxValue; for (int i = 0; i < damagedTiles.Count; i++) { if (damagedTiles[i].IsAvailable()) { int pathCount = AStar.FindPath(tile, damagedTiles[i].tile, true).Count; if (shortestPathCount > pathCount) { shortestPathCount = pathCount; dp = damagedTiles[i]; } } } return(dp); }
void FixedUpdate() { if (printe) { PrintMethod(); printe = false; Debug.Break(); } if (battleManager != null) { if (role == Role.CANNONEER) { CrewMember priorityTarget = isPlayerCrew ? battleManager.GetNextOtherPriority() : battleManager.GetNextPlayerPriority(); if (target != priorityTarget) { target = priorityTarget; if (target) { BeginPath(AStar.FindPath(current, target.current, true)); couldFindPath = false; } } } } else { target = null; } Vector3 tilePos = current.transform.position; float distance = Vector2.Distance(transform.position, tilePos); bool onTilePrev = onTile; if (distance > MOVEMENT_ERROR_RANGE) { Vector3 pos = Vector3.Lerp(transform.position, tilePos, speed / distance * Time.fixedDeltaTime); pos.z = Z_VAL; transform.position = pos; onTile = false; } else { onTile = true; } if (target && path.Count > 0 && target.current != path.last.data) { BeginPath(AStar.FindPath(current, target.current, true)); couldFindPath = false; } if (path.Count > 0 && onTile) { path.RemoveFirst(); if (path.Count > 0) { if (path.first.data.crewMem) { if (path.first.data.crewMem == target) { return; } //Debug.Log (personName + " blocked"); bool cfpTemp; MyLinkedList <MovementTile> p = AStar.FindPath(current, target ? target.current : path.last.data, false, out cfpTemp); if (!target && !cfpTemp) { //... there's nothing here } else { BeginPath(p); couldFindPath = cfpTemp; if (path.Count > 1 && !path.first.next.data.crewMem) { path.RemoveFirst(); } } } if (path.Count > 0) { if (path.first.data.teleportLayerChange && current.teleportLayerChange && path.first.data.layer != current.layer) { Vector3 pos = path.first.data.transform.position; pos.z = Z_VAL; transform.position = pos; } ChangeCurrentTile(path.first.data); } } } if (path.Count == 0 && !target && ((shipPos && current == shipPos.tile) || (role == Role.BAILER && couldFindPath))) { currentAction = Action.IDLE; if (onTile && !onTilePrev) { timeOfLastAction = Time.time; } } if (target && path.Count == 0 && couldFindPath) { currentAction = Action.ATTACKING; } if (target && onTile && !couldFindPath && currentAction != Action.ATTACKING) { BeginPath(AStar.FindPath(current, target.current, false, out couldFindPath)); } if (shipPos && current != shipPos.tile && path.last.data != shipPos.tile && (!target || (role != Role.CANNONEER && target))) { BeginPath(AStar.FindPath(current, shipPos.tile, true)); if (target) { target = null; } } switch (currentAction) { case Action.IDLE: if (role == Role.BAILER) { //TODO: make dump face correct direction when picked up next to edge anim.SetInteger("Idle Action", hasWater ? 3 : 2); if (Time.time - timeOfLastAction > .5f) { if (!hasWater) { BeginPath(ship.GetShortestPathToEdgeTile(current)); current.floodedTile.Bail(); } else { ShipPosition bailPos = ship.GetClosestFloodedTile(current); ship.Assign(this, bailPos ? bailPos : ship.GetVacantPositionByPriority()); } timeOfLastAction = Time.time; hasWater = !hasWater; anim.SetBool("Has Water", hasWater); } } else if (role == Role.CARPENTER) { if (current.damagedTile) { anim.SetInteger("Idle Action", 1); if (Time.time - timeOfLastAction > repairSpeed) { timeOfLastAction = Time.time; if (current.damagedTile.Repair(repairAmount)) { DamagedPosition dp = ship.GetClosestDamagedTile(current); if (dp) { ship.Assign(this, dp); } else if (ship.carpentersQuaters && !ship.carpentersQuaters.isManned) { ship.Assign(this, ship.carpentersQuaters); } else { ship.Assign(this, ship.GetVacantPositionByPriority()); } } } } else { DamagedPosition dp = ship.GetClosestDamagedTile(current); if (dp) { ship.Assign(this, dp); } else { anim.SetInteger("Idle Action", 0); } } } else if (current.damagedTile) { anim.SetInteger("Idle Action", 1); //TODO: if repairing current position and job is changed or attack started for //cannoneer, reset damaged tile ismanned if (Time.time - timeOfLastAction > repairSpeed) { timeOfLastAction = Time.time; current.damagedTile.Repair(repairAmount); } current.damagedTile.isManned = true; } else { anim.SetInteger("Idle Action", 0); if (battleManager && role == Role.CANNONEER && current == shipPos.tile && Time.time - timeOfLastAction > cannonFireSpeed) { float rand = Random.Range(0f, 100f); ((CannonPosition)shipPos).Fire(battleManager, cannonHitChance > rand); timeOfLastAction = Time.time; } } break; case Action.WALKING: break; case Action.ATTACKING: //hopefully this only triggers on isalnd dwellers during an attack if (!target && !shipPos) { currentAction = Action.IDLE; } anim.SetInteger("FacingDirection", GetDirectionOfTile(target.current)); if (!target.target) { target.target = this; } if (Time.time - timeOfLastAction > attackSpeed) { timeOfLastAction = Time.time; target.TakeDamage(damage); } break; } anim.SetInteger("Action", (int)currentAction); Debug.DrawLine(transform.position, current.transform.position, Color.red); }