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 shouldMove = false; private bool moveToPosition = false; private int movedSquares = 0; 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; } checkIfOccupoied(movement); 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); } 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; } 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"); } } } yield return null; } private void checkIfOccupoied(Vector3 movement) { } /* public IEnumerator PerformAction() { if (startPosition == null || (targetPosition == null && targetCreature == null) || toMove == null) { Debug.Log("Move action not setup correctly, missing startPosition " + startPosition + " or target position " + targetPosition + " or gameObject " + toMove); throw new System.Exception("Failed MoveAction"); } if (targetCreature != null) { targetPosition = targetCreature.CurrentPos; } if (movedSquares >= toMove.movementRate || startPosition.Equals(targetPosition)) { yield break; } UnityEngine.Tilemaps.Tilemap tileMap = GameManagerScript.GetInstance().TileMap; Vector3Int startPositionAdjusted = tileMap.WorldToCell(startPosition); Vector3Int targetPositionAdjusted = tileMap.WorldToCell(targetPosition); Vector3Int distanceToTarget = (startPositionAdjusted - targetPositionAdjusted); Vector3Int finalPos = tileMap.WorldToCell(startPositionAdjusted - distanceToTarget); Debug.Log("Start position " + startPosition + " Adjusted: " + startPositionAdjusted + " final pos " + finalPos + " distance " + (-(startPositionAdjusted - targetPositionAdjusted))); float time = 30 / 6f; float eleapsedTime = 0f; while (eleapsedTime < time) { if (targetCreature != null) { targetPosition = targetCreature.CurrentPos; targetPositionAdjusted = tileMap.WorldToCell(targetPosition); distanceToTarget = (startPositionAdjusted - targetPositionAdjusted); finalPos = tileMap.WorldToCell(startPositionAdjusted - distanceToTarget); } toMove.transform.position = Vector3.Lerp(tileMap.GetCellCenterWorld(startPositionAdjusted), tileMap.GetCellCenterWorld(finalPos), (eleapsedTime / time)); eleapsedTime += Time.deltaTime; yield return null; } } */ }