using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Tilemaps; public class MoveAction : IAction { Vector3 startPosition; Vector3 targetPosition; private Creature toMove; private Creature targetCreature; private bool moveToPosition = false; List highlightedTiles; private int movedSquares = 0; private bool actionPerformed; private LineRenderer LR; public bool ActionPerformed { get => actionPerformed; set => actionPerformed = value; } bool hilightEnbled = true; public bool HilightEnabled { get => hilightEnbled; set => hilightEnbled = value; } public void SetStartPosition(Vector3 startPos) { startPosition = startPos; } public void SetTargetPosition(Vector3 targetPos) { targetPosition = targetPos; moveToPosition = true; } public void SetTargetCreature(Creature target) { targetCreature = target; moveToPosition = false; } public void SetCreatureToMove(Creature toMove) { this.toMove = toMove; } /* * If someone is within 5 feet, should you stop and attack? If not, attack of opportunity? * Try not to move within 5 feet of non target, and still closing in to target (prio) * När toMove är innom 5 foot av target, avbryt move och attakera direkt? eller flytta efter till end of movement, och då attackera, även om target fortsätter att flytta sig. */ public IEnumerator PerformAction() { Tilemap tileMap = GameManagerScript.GetInstance().TileMap; if (moveToPosition) { Vector3 distance = tileMap.WorldToCell(targetPosition - startPosition); int safety = 0; while (!distance.Equals(Vector3.zero)) { safety++; if (distance.x != 0 || distance.y != 0) { Vector3 movement = toMove.transform.position; if (distance.x > 0) { movement.x += 1; distance.x -= 1; } else if (distance.x < 0) { movement.x -= 1; distance.x += 1; } if (distance.y > 0) { movement.y += 1; distance.y -= 1; } else if (distance.y < 0) { movement.y -= 1; distance.y += 1; } Creature tileBlockedBy = TileOccupiedBy(movement); if (tileBlockedBy != null) { Debug.Log("Tile blocked by: " + tileBlockedBy.name); IAction actionWhenBlocked = toMove.ActionWhenBlocked(tileBlockedBy); if (actionWhenBlocked != null) { movedSquares += 2; // consume two move, and set and use second action to decision toMove.SetSecondAction(actionWhenBlocked); yield return actionWhenBlocked.PerformAction(); } } else { toMove.transform.position = movement; movedSquares++; } if (toMove.movementRate <= movedSquares * 5) { // this one have moved all that they can for one action Debug.Log(toMove.name + " moved there entire " + toMove.movementRate + " distance, stopping"); ActionPerformed = true; break; } yield return new WaitForSeconds(1); } if (safety > 120) { throw new System.Exception("LOOPED 120 times"); } } } else { // move to creature Vector3 distance = tileMap.WorldToCell(targetCreature.transform.position - startPosition); int safety = 0; while (!distance.Equals(Vector3.zero)) { safety++; if (distance.x != 0 || distance.y != 0) { // Move diagonally Vector3 movement = toMove.transform.position; if (distance.x > 0) { movement.x += 1; distance.x -= 1; } else if (distance.x < 0) { movement.x -= 1; distance.x += 1; } if (distance.y > 0) { movement.y += 1; distance.y -= 1; } else if (distance.y < 0) { movement.y -= 1; distance.y += 1; } Creature tileBlockedBy = TileOccupiedBy(movement); if (tileBlockedBy != null) { Debug.Log(toMove.name + "s Tile blocked by: " + tileBlockedBy.name); IAction action = toMove.ActionWhenBlocked(tileBlockedBy); toMove.SetSecondAction(action); movedSquares += 2; // consume two move, and set and use second action to decision yield return toMove.GetSecondAction().PerformAction(); } else { toMove.transform.position = movement; movedSquares++; } if (toMove.movementRate <= movedSquares * 5) { // this one have moved all that they can for one action Debug.Log(toMove.name + " moved there entire " + toMove.movementRate + " distance, stopping"); break; } yield return new WaitForSeconds(1); distance = tileMap.WorldToCell(targetCreature.transform.position - toMove.transform.position); Debug.Log(toMove.name + " move to " + targetPosition + "(" + tileMap.WorldToCell(targetPosition) + ") from " + startPosition + "(" + tileMap.WorldToCell(startPosition) + " by moving " + distance); } if (safety > 1000) { throw new System.Exception("LOOPED 1000 times"); } } } actionPerformed = true; yield return null; } private Creature TileOccupiedBy(Vector3 nextSprite) { List creatures = GameManagerScript.GetInstance().GetCombatants(); Tilemap tileMap = GameManagerScript.GetInstance().TileMap; Vector3Int cellToCheck = tileMap.WorldToCell(nextSprite); Creature returnValue = null; foreach (Creature creature in creatures) { if (cellToCheck.Equals(tileMap.WorldToCell(creature.transform.position))) { returnValue = creature; } } return returnValue; } }