| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- 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;
- }
- } */
- }
|