Pārlūkot izejas kodu

Movement 'done'

Axel Nordh 4 gadi atpakaļ
vecāks
revīzija
0512cb1e8a

+ 2 - 1
.editorconfig

@@ -3,4 +3,5 @@ root=true
 [*.cs]
 dotnet_diagnostic.IDE0051.severity = none
 dotnet_diagnostic.IDE0055.severity = none
-dotnet_diagnostic.IDE0044.severity = none
+dotnet_diagnostic.IDE0044.severity = none
+dotnet_diagnostic.IDE0090.severity = none

+ 16 - 4
Assets/Images/TileSets/GrondPalette.prefab

@@ -2311,6 +2311,16 @@ Tilemap:
       m_TileObjectToInstantiateIndex: 65535
       dummyAlignment: 0
       m_AllTileFlags: 1073741826
+  - first: {x: 5, y: 10, z: 0}
+    second:
+      serializedVersion: 2
+      m_TileIndex: 227
+      m_TileSpriteIndex: 152
+      m_TileMatrixIndex: 0
+      m_TileColorIndex: 0
+      m_TileObjectToInstantiateIndex: 65535
+      dummyAlignment: 0
+      m_AllTileFlags: 1073741826
   m_AnimatedTiles: {}
   m_TileAssetArray:
   - m_RefCount: 1
@@ -2767,6 +2777,8 @@ Tilemap:
     m_Data: {fileID: 11400000, guid: 8eabe9e0b1df24d4e9574e20ecdfe9e5, type: 2}
   - m_RefCount: 1
     m_Data: {fileID: 11400000, guid: 826d3f114a9eefb41b22e835ae8815b4, type: 2}
+  - m_RefCount: 1
+    m_Data: {fileID: 11400000, guid: b0bc2941cbd86ab4a9a8452a2fa4651e, type: 2}
   m_TileSpriteArray:
   - m_RefCount: 1
     m_Data: {fileID: 8008677291861298350, guid: 19571b43769a9484a87ec04e37c8dff9, type: 3}
@@ -3072,7 +3084,7 @@ Tilemap:
     m_Data: {fileID: 4518298084181905382, guid: 19571b43769a9484a87ec04e37c8dff9, type: 3}
   - m_RefCount: 1
     m_Data: {fileID: -19418003413628167, guid: 19571b43769a9484a87ec04e37c8dff9, type: 3}
-  - m_RefCount: 1
+  - m_RefCount: 2
     m_Data: {fileID: 8791563513433778591, guid: 19571b43769a9484a87ec04e37c8dff9, type: 3}
   - m_RefCount: 1
     m_Data: {fileID: -8277790623780734182, guid: 19571b43769a9484a87ec04e37c8dff9, type: 3}
@@ -3219,7 +3231,7 @@ Tilemap:
   - m_RefCount: 1
     m_Data: {fileID: 847677547326338812, guid: 19571b43769a9484a87ec04e37c8dff9, type: 3}
   m_TileMatrixArray:
-  - m_RefCount: 227
+  - m_RefCount: 228
     m_Data:
       e00: 1
       e01: 0
@@ -3238,7 +3250,7 @@ Tilemap:
       e32: 0
       e33: 1
   m_TileColorArray:
-  - m_RefCount: 227
+  - m_RefCount: 228
     m_Data: {r: 1, g: 1, b: 1, a: 1}
   m_TileObjectToInstantiateArray: []
   m_AnimationFrameRate: 1
@@ -3356,7 +3368,7 @@ Grid:
   m_CellGap: {x: 0, y: 0, z: 0}
   m_CellLayout: 0
   m_CellSwizzle: 0
---- !u!114 &7999016489336992188
+--- !u!114 &8951282761748356691
 MonoBehaviour:
   m_ObjectHideFlags: 0
   m_CorrespondingSourceObject: {fileID: 0}

+ 113 - 0
Assets/Images/TileSets/GroundSandRuleTile.asset

@@ -0,0 +1,113 @@
+%YAML 1.1
+%TAG !u! tag:unity3d.com,2011:
+--- !u!114 &11400000
+MonoBehaviour:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 0}
+  m_Enabled: 1
+  m_EditorHideFlags: 0
+  m_Script: {fileID: 11500000, guid: 9d1514134bc4fbd41bb739b1b9a49231, type: 3}
+  m_Name: GroundSandRuleTile
+  m_EditorClassIdentifier: 
+  m_DefaultSprite: {fileID: 8791563513433778591, guid: 19571b43769a9484a87ec04e37c8dff9, type: 3}
+  m_DefaultGameObject: {fileID: 0}
+  m_DefaultColliderType: 1
+  m_TilingRules:
+  - m_Id: 0
+    m_Sprites:
+    - {fileID: -7203716348486262060, guid: 19571b43769a9484a87ec04e37c8dff9, type: 3}
+    m_GameObject: {fileID: 0}
+    m_MinAnimationSpeed: 1
+    m_MaxAnimationSpeed: 1
+    m_PerlinScale: 0.5
+    m_Output: 0
+    m_ColliderType: 1
+    m_RandomTransform: 0
+    m_Neighbors: 02000000010000000100000001000000
+    m_NeighborPositions:
+    - {x: 0, y: 1, z: 0}
+    - {x: 0, y: -1, z: 0}
+    - {x: 1, y: 0, z: 0}
+    - {x: -1, y: 0, z: 0}
+    m_RuleTransform: 3
+  - m_Id: 1
+    m_Sprites:
+    - {fileID: 5867815065556567473, guid: 19571b43769a9484a87ec04e37c8dff9, type: 3}
+    m_GameObject: {fileID: 0}
+    m_MinAnimationSpeed: 1
+    m_MaxAnimationSpeed: 1
+    m_PerlinScale: 0.5
+    m_Output: 0
+    m_ColliderType: 1
+    m_RandomTransform: 0
+    m_Neighbors: 020000000200000002000000010000000100000001000000
+    m_NeighborPositions:
+    - {x: 0, y: 1, z: 0}
+    - {x: -1, y: 1, z: 0}
+    - {x: -1, y: 0, z: 0}
+    - {x: 0, y: -1, z: 0}
+    - {x: 1, y: -1, z: 0}
+    - {x: 1, y: 0, z: 0}
+    m_RuleTransform: 1
+  - m_Id: 2
+    m_Sprites:
+    - {fileID: -19418003413628167, guid: 19571b43769a9484a87ec04e37c8dff9, type: 3}
+    m_GameObject: {fileID: 0}
+    m_MinAnimationSpeed: 1
+    m_MaxAnimationSpeed: 1
+    m_PerlinScale: 0.5
+    m_Output: 0
+    m_ColliderType: 1
+    m_RandomTransform: 0
+    m_Neighbors: 02000000010000000100000001000000
+    m_NeighborPositions:
+    - {x: -1, y: 0, z: 0}
+    - {x: 1, y: 0, z: 0}
+    - {x: 0, y: 1, z: 0}
+    - {x: 0, y: -1, z: 0}
+    m_RuleTransform: 2
+  - m_Id: 3
+    m_Sprites:
+    - {fileID: -5624113679489621503, guid: 19571b43769a9484a87ec04e37c8dff9, type: 3}
+    m_GameObject: {fileID: 0}
+    m_MinAnimationSpeed: 1
+    m_MaxAnimationSpeed: 1
+    m_PerlinScale: 0.5
+    m_Output: 0
+    m_ColliderType: 1
+    m_RandomTransform: 0
+    m_Neighbors: 0100000001000000010000000100000001000000010000000200000001000000
+    m_NeighborPositions:
+    - {x: 0, y: 1, z: 0}
+    - {x: 1, y: 1, z: 0}
+    - {x: 1, y: 0, z: 0}
+    - {x: 0, y: -1, z: 0}
+    - {x: -1, y: -1, z: 0}
+    - {x: -1, y: 0, z: 0}
+    - {x: -1, y: 1, z: 0}
+    - {x: 1, y: -1, z: 0}
+    m_RuleTransform: 1
+  - m_Id: 4
+    m_Sprites:
+    - {fileID: 560172194065194345, guid: 19571b43769a9484a87ec04e37c8dff9, type: 3}
+    m_GameObject: {fileID: 0}
+    m_MinAnimationSpeed: 1
+    m_MaxAnimationSpeed: 1
+    m_PerlinScale: 0.5
+    m_Output: 0
+    m_ColliderType: 1
+    m_RandomTransform: 0
+    m_Neighbors: 0200000002000000010000000100000001000000010000000100000001000000
+    m_NeighborPositions:
+    - {x: -1, y: 1, z: 0}
+    - {x: 1, y: -1, z: 0}
+    - {x: -1, y: -1, z: 0}
+    - {x: 1, y: 1, z: 0}
+    - {x: -1, y: 0, z: 0}
+    - {x: 0, y: -1, z: 0}
+    - {x: 1, y: 0, z: 0}
+    - {x: 0, y: 1, z: 0}
+    m_RuleTransform: 1

+ 8 - 0
Assets/Images/TileSets/GroundSandRuleTile.asset.meta

@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: b0bc2941cbd86ab4a9a8452a2fa4651e
+NativeFormatImporter:
+  externalObjects: {}
+  mainObjectFileID: 11400000
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 154 - 1
Assets/Prefabs/Panels/InitCreaturePanel.prefab

@@ -1,5 +1,156 @@
 %YAML 1.1
 %TAG !u! tag:unity3d.com,2011:
+--- !u!1 &1818213540100709849
+GameObject:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  serializedVersion: 6
+  m_Component:
+  - component: {fileID: 6701367499241878274}
+  - component: {fileID: 8136391629926949381}
+  - component: {fileID: 7659883248638460307}
+  m_Layer: 5
+  m_Name: HealthBar
+  m_TagString: Untagged
+  m_Icon: {fileID: 0}
+  m_NavMeshLayer: 0
+  m_StaticEditorFlags: 0
+  m_IsActive: 1
+--- !u!224 &6701367499241878274
+RectTransform:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 1818213540100709849}
+  m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
+  m_LocalPosition: {x: 0, y: 0, z: 0}
+  m_LocalScale: {x: 1, y: 1, z: 1}
+  m_Children: []
+  m_Father: {fileID: 1745241803252418439}
+  m_RootOrder: 0
+  m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
+  m_AnchorMin: {x: 0, y: 0}
+  m_AnchorMax: {x: 1, y: 1}
+  m_AnchoredPosition: {x: 0, y: 0}
+  m_SizeDelta: {x: -2, y: -2}
+  m_Pivot: {x: 0.5, y: 0.5}
+--- !u!222 &8136391629926949381
+CanvasRenderer:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 1818213540100709849}
+  m_CullTransparentMesh: 1
+--- !u!114 &7659883248638460307
+MonoBehaviour:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 1818213540100709849}
+  m_Enabled: 1
+  m_EditorHideFlags: 0
+  m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
+  m_Name: 
+  m_EditorClassIdentifier: 
+  m_Material: {fileID: 0}
+  m_Color: {r: 0, g: 0.5647059, b: 0.0681768, a: 0.6862745}
+  m_RaycastTarget: 1
+  m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
+  m_Maskable: 1
+  m_OnCullStateChanged:
+    m_PersistentCalls:
+      m_Calls: []
+  m_Sprite: {fileID: 0}
+  m_Type: 0
+  m_PreserveAspect: 0
+  m_FillCenter: 1
+  m_FillMethod: 4
+  m_FillAmount: 1
+  m_FillClockwise: 1
+  m_FillOrigin: 0
+  m_UseSpriteMesh: 0
+  m_PixelsPerUnitMultiplier: 1
+--- !u!1 &5069443264419159292
+GameObject:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  serializedVersion: 6
+  m_Component:
+  - component: {fileID: 1745241803252418439}
+  - component: {fileID: 8535931094124682900}
+  - component: {fileID: 1017959822812074668}
+  m_Layer: 5
+  m_Name: Image
+  m_TagString: Untagged
+  m_Icon: {fileID: 0}
+  m_NavMeshLayer: 0
+  m_StaticEditorFlags: 0
+  m_IsActive: 1
+--- !u!224 &1745241803252418439
+RectTransform:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 5069443264419159292}
+  m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
+  m_LocalPosition: {x: 0, y: 0, z: 0}
+  m_LocalScale: {x: 1, y: 1, z: 1}
+  m_Children:
+  - {fileID: 6701367499241878274}
+  m_Father: {fileID: 8976694485909430296}
+  m_RootOrder: 0
+  m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
+  m_AnchorMin: {x: 1, y: 0}
+  m_AnchorMax: {x: 1, y: 1}
+  m_AnchoredPosition: {x: -2, y: 0}
+  m_SizeDelta: {x: 10, y: -4}
+  m_Pivot: {x: 1, y: 0.5}
+--- !u!222 &8535931094124682900
+CanvasRenderer:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 5069443264419159292}
+  m_CullTransparentMesh: 1
+--- !u!114 &1017959822812074668
+MonoBehaviour:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 5069443264419159292}
+  m_Enabled: 1
+  m_EditorHideFlags: 0
+  m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
+  m_Name: 
+  m_EditorClassIdentifier: 
+  m_Material: {fileID: 0}
+  m_Color: {r: 0, g: 0, b: 0, a: 0}
+  m_RaycastTarget: 0
+  m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
+  m_Maskable: 1
+  m_OnCullStateChanged:
+    m_PersistentCalls:
+      m_Calls: []
+  m_Sprite: {fileID: 0}
+  m_Type: 0
+  m_PreserveAspect: 0
+  m_FillCenter: 1
+  m_FillMethod: 4
+  m_FillAmount: 1
+  m_FillClockwise: 1
+  m_FillOrigin: 0
+  m_UseSpriteMesh: 0
+  m_PixelsPerUnitMultiplier: 1
 --- !u!1 &7294442285524864391
 GameObject:
   m_ObjectHideFlags: 0
@@ -29,7 +180,8 @@ RectTransform:
   m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
   m_LocalPosition: {x: 0, y: 0, z: 0}
   m_LocalScale: {x: 1, y: 1, z: 1}
-  m_Children: []
+  m_Children:
+  - {fileID: 1745241803252418439}
   m_Father: {fileID: 0}
   m_RootOrder: 0
   m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
@@ -88,3 +240,4 @@ MonoBehaviour:
   m_Script: {fileID: 11500000, guid: 8ed3d61a29a3e1c4384a6ab0ba5c4169, type: 3}
   m_Name: 
   m_EditorClassIdentifier: 
+  healthBar: {fileID: 7659883248638460307}

Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 370 - 193
Assets/Scenes/SampleScene.unity


+ 0 - 0
Assets/CameraManager.cs → Assets/Scripts/CameraManager.cs


+ 0 - 0
Assets/CameraManager.cs.meta → Assets/Scripts/CameraManager.cs.meta


+ 6 - 1
Assets/Scripts/Creatures/Actions/AttackAction.cs

@@ -2,10 +2,15 @@ using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
-public class AttackAction : Action {
+public class AttackAction : IAction {
     Creature target;
 
     public AttackAction(Creature target) {
         this.target = target;
     }
+
+    public IEnumerator PerformAction() {
+        yield return null;
+        //throw new System.NotImplementedException();
+    }
 }

+ 3 - 2
Assets/Scripts/Creatures/Actions/Action.cs → Assets/Scripts/Creatures/Actions/IAction.cs

@@ -2,6 +2,7 @@ using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
-public class Action
-{
+public interface IAction {
+
+    public IEnumerator PerformAction();
 }

+ 1 - 1
Assets/Scripts/Creatures/Actions/Action.cs.meta → Assets/Scripts/Creatures/Actions/IAction.cs.meta

@@ -1,5 +1,5 @@
 fileFormatVersion: 2
-guid: 3e10b63d37d11a84994786c194868fc5
+guid: b5244ce4a1c2633448eaa22dee8225ca
 MonoImporter:
   externalObjects: {}
   serializedVersion: 2

+ 155 - 7
Assets/Scripts/Creatures/Actions/MoveAction.cs

@@ -1,16 +1,164 @@
+using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
+using UnityEngine.Tilemaps;
 
-public class MoveAction : Action {
-    Sprite startPosition;
-    Sprite targetPosition;
+public class MoveAction : IAction {
+    Vector3 startPosition;
+    Vector3 targetPosition;
+    private Creature toMove;
+    private Creature targetCreature;
+    private bool shouldMove = false;
+    private bool moveToPosition = false;
 
-    public void SetStartPosition(Sprite startSprite) {
-        startPosition = startSprite;
+    private int movedSquares = 0;
+
+    public void SetStartPosition(Vector3 startPos) {
+        startPosition = startPos;
+    }
+
+    public void SetTargetPosition(Vector3 targetPos) {
+        targetPosition = targetPos;
+        moveToPosition = true;
     }
 
-    public void SetTargetPosition(Sprite targetSprite) {
-        targetPosition = targetSprite;
+    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;
+            }
+        } */
 }

+ 8 - 6
Assets/Scripts/Creatures/Actions/SpellAction.cs

@@ -2,21 +2,23 @@ using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
-public class SpellAction : Action
-{
+public class SpellAction : IAction {
 
     private GameObject target;
+
+    public IEnumerator PerformAction() {
+        throw new System.NotImplementedException();
+    }
+
     // private GameObject spell;
 
     // Start is called before the first frame update
-    void Start()
-    {
+    void Start() {
 
     }
 
     // Update is called once per frame
-    void Update()
-    {
+    void Update() {
 
     }
 }

+ 61 - 12
Assets/Scripts/Creatures/Creature.cs

@@ -1,5 +1,6 @@
 using System.Collections;
 using System.Collections.Generic;
+using System.Linq;
 using UnityEngine;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
@@ -11,20 +12,37 @@ public class Creature : MonoBehaviour {
 
     private int init;
     private int dex = 0;
-    private List<Action> actions = new List<Action>();
+    private int creatureHealth;
+    private List<IAction> actions = new List<IAction>();
+
+    internal int CurrentHealth { get; set; }
+    internal int MaxHealth { get; set; }
+
 
     Weapon weapon;
     int health;
     private CanvasGroup canvasGroup;
     private RectTransform rectTransform;
     private Vector3Int currentPos;
-    private Action firstAction;
-    private Action secondAction;
+
+    internal IAction GetFirstAction() {
+        return firstAction;
+    }
+
+    private IAction firstAction;
+    private IAction secondAction;
+    public GameObject InitPanelGameObject { set; get; }
+
+    internal int movementRate;
 
     public int Dex { get => dex; set => dex = value; }
 
     public int Init { get => init; set => init = value; }
 
+    public int CreatureHealth { get => creatureHealth; set => creatureHealth = value; }
+    public bool IsCreatureAlive { get; set; }
+    public Vector3Int CurrentPos { get => currentPos; set => currentPos = value; }
+
     private void Start() {
         canvasGroup = GetComponent<CanvasGroup>();
         rectTransform = GetComponent<RectTransform>();
@@ -34,6 +52,10 @@ public class Creature : MonoBehaviour {
         return Init;
     }
 
+    internal bool HasSecondAction() {
+        return secondAction != null;
+    }
+
     public void RollInit() {
         Init = Random.Range(0, 100 - dex);
     }
@@ -46,18 +68,18 @@ public class Creature : MonoBehaviour {
         return humanControlled;
     }
 
-    public void SetFirstAction(Action action) {
+    public void SetFirstAction(IAction action) {
         firstAction = action;
     }
 
-    public void SetSecondAction(Action action) {
+    public void SetSecondAction(IAction action) {
         secondAction = action;
     }
 
     internal List<Creature> EnemiesInSquareNextToCreature(Creature creature, List<Creature> combatants) {
-        List<Creature> enemiesClose = new();
+        List<Creature> enemiesClose = new List<Creature>();
         foreach (Creature c in combatants) {
-            Vector3 distance = c.GetCurrentPosition() - creature.GetCurrentPosition();
+            Vector3 distance = c.CurrentPos - creature.CurrentPos;
             if (c.name != creature.name && distance.x <= 1 && distance.x >= -1 && distance.y <= 1 && distance.y >= -1) {
                 enemiesClose.Add(c);
             }
@@ -66,14 +88,41 @@ public class Creature : MonoBehaviour {
     }
 
     internal void SetPositionInGrid(Vector3Int vector3Int) {
-        currentPos = vector3Int;
-    }
-
-    public Vector3Int GetCurrentPosition() {
-        return currentPos;
+        CurrentPos = vector3Int;
     }
 
     public bool HasFirstAction() {
         return firstAction != null;
     }
+
+    internal Creature FindClosestEnemy(Creature creature, List<Creature> combatants) {
+        Creature closestEnemy = null;
+        float minDist = Mathf.Infinity;
+        List<Creature> enemies = combatants.FindAll(p => p.IsHumanControlled() != creature.IsHumanControlled());
+
+        foreach (Creature e in enemies) {
+            float dist = Vector3.Distance(e.CurrentPos, creature.CurrentPos);
+            if (dist < minDist) {
+                closestEnemy = e;
+                minDist = dist;
+            }
+        }
+        Debug.Log("Closest enemy is " + closestEnemy.name);
+        return closestEnemy;
+    }
+
+    public void Death() {
+        IsCreatureAlive = false;
+    }
+
+
+    public void TakeDamage(int damage) {
+        CurrentHealth -= damage;
+        if (CurrentHealth <= 0) {
+            Death();
+        }
+
+        InitPanelGameObject.GetComponent<InitCreaturePanelScript>().UpdateHealthBar(MaxHealth / CurrentHealth);
+    }
+
 }

+ 21 - 0
Assets/Scripts/Creatures/Human.cs

@@ -0,0 +1,21 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class Human : Creature {
+
+    // Start is called before the first frame update
+    void Start() {
+        Dex = Random.Range(0, 100);
+        movementRate = 30;
+        CreatureHealth = Random.Range(20, 30);
+        CurrentHealth = CreatureHealth;
+        MaxHealth = CreatureHealth;
+        IsCreatureAlive = true;
+    }
+
+    // Update is called once per frame
+    void Update() {
+
+    }
+}

+ 1 - 1
Assets/Scripts/Human.cs.meta → Assets/Scripts/Creatures/Human.cs.meta

@@ -1,5 +1,5 @@
 fileFormatVersion: 2
-guid: fd532b0ff07920b4d87211144c91529f
+guid: 67f84b489b3bb5f48b388c97399eace9
 MonoImporter:
   externalObjects: {}
   serializedVersion: 2

+ 42 - 11
Assets/Scripts/Creatures/Skeleton.cs

@@ -4,27 +4,58 @@ using UnityEngine.EventSystems;
 
 namespace RPG_Fight_Test.Assets.Scripts.Creatures {
     public class Skeleton : Creature {
+        private List<Creature> combatants;
+
         private void Start() {
             Dex = Random.Range(0, 20);
+            movementRate = 20;
+            CreatureHealth = Random.Range(10, 20);
+            CurrentHealth = CreatureHealth;
+            MaxHealth = CreatureHealth;
+            IsCreatureAlive = true;
         }
 
-        public void DecideActions(List<Creature> combatants) {
+        private void DecideFirstAction() {
+            List<Creature> closeEnemies = EnemiesInSquareNextToCreature(this, combatants);
+
+            if (closeEnemies.Count > 0) {
+                SetFirstAction(new AttackAction(closeEnemies[0]));
+            } else {
+                // Move towards closest enemy
+                Creature closestEnemy = FindClosestEnemy(this, combatants);
+                MoveAction action = new MoveAction();
+                action.SetStartPosition(CurrentPos);
+                action.SetTargetCreature(closestEnemy);
+                action.SetCreatureToMove(this);
+                SetFirstAction(action);
+            }
+        }
+
+        // Just a copy of first action for now, should have its own logic
+        private void DecideSecondAction() {
             List<Creature> closeEnemies = EnemiesInSquareNextToCreature(this, combatants);
 
-            Debug.Log("Number of close enemies " + closeEnemies.Count);
             if (closeEnemies.Count > 0) {
-                if (!HasFirstAction()) {
-                    SetFirstAction(new AttackAction(closeEnemies[0]));
-                } else {
-                    SetSecondAction(new AttackAction(closeEnemies[0]));
-                }
+                SetSecondAction(new AttackAction(closeEnemies[0]));
             } else {
-                /*if (shouldMove())
-                {
-                    setFirstAction(new MoveAction());
-                }*/
+                // Move towards closest enemy
+                Creature closestEnemy = FindClosestEnemy(this, combatants);
+                MoveAction action = new MoveAction();
+                action.SetStartPosition(CurrentPos);
+                action.SetTargetCreature(closestEnemy);
+                action.SetCreatureToMove(this);
+                SetSecondAction(action);
             }
         }
+        public void DecideActions(List<Creature> combatants) {
+            this.combatants = combatants;
+            List<Creature> closeEnemies = EnemiesInSquareNextToCreature(this, combatants);
+
+            DecideFirstAction();
+            DecideSecondAction();
+            Debug.Log("Number of close enemies " + closeEnemies.Count);
+        }
+
 
         public void ShowInfo() {
 

+ 1 - 1
Assets/Scripts/Die.cs

@@ -3,7 +3,7 @@ using System;
 internal class Die {
 
     int sides;
-    Random rnd = new();
+    Random rnd = new Random();
     Die(int sides) {
         this.sides = sides;
     }

+ 25 - 13
Assets/Scripts/GameManagerScript.cs

@@ -15,15 +15,20 @@ public class GameManagerScript : MonoBehaviour {
 
     [SerializeField] List<GameObject> enemies;
     [SerializeField] List<GameObject> humans;
-    [SerializeField] GameObject initPanel;
-    [SerializeField] GameObject initCreaturePanel;
+    [SerializeField] GameObject initiativePanel;
+    [SerializeField] GameObject initiativeCreaturePanel;
+    [SerializeField] GameObject roundActionsPanel;
 
     [SerializeField] Grid grid;
+
     [SerializeField] Tilemap tileMap;
+    public Tilemap TileMap { get => tileMap; set => tileMap = value; }
+    public Grid Grid { get => grid; set => grid = value; }
 
-    List<Creature> combatants = new();
+    List<Creature> combatants = new List<Creature>();
 
     static GameManagerScript instance;
+
     public static GameManagerScript GetInstance() {
         return instance;
     }
@@ -58,31 +63,38 @@ public class GameManagerScript : MonoBehaviour {
         combatants.Sort((x, y) => x.GetInit().CompareTo(y.GetInit()));
         ClearInitPanel();
         BuildInitPanel();
+        ShowRoundActionsPanel(true);
         roundManager.StartFirstRound(combatants);
     }
 
+    private void ShowRoundActionsPanel(bool show) {
+        roundActionsPanel.SetActive(show);
+    }
+
     private void ClearInitPanel() {
-        int children = initPanel.transform.childCount;
+        int children = initiativePanel.transform.childCount;
         for (int i = children - 1; i >= 0; i--) {
-            GameObject.Destroy(initPanel.transform.GetChild(i).gameObject);
+            GameObject.Destroy(initiativePanel.transform.GetChild(i).gameObject);
         }
     }
 
     public void ShowInitPanel(Boolean show) {
-        initPanel.SetActive(show);
+        initiativePanel.SetActive(show);
     }
 
     internal void BuildInitPanel() {
-        initPanel.SetActive(true);
+        initiativePanel.SetActive(true);
         combatants.ForEach(c => {
-            GameObject creturePanel = GameObject.Instantiate(initCreaturePanel, Vector3.zero, Quaternion.identity);
-            creturePanel.transform.localScale = new Vector3(1, 1, 1);
-            creturePanel.transform.SetParent(initPanel.transform);
-            creturePanel.GetComponent<InitCreaturePanelScript>().SetImage(c.GetComponent<Creature>().GetSprite());
+            GameObject creaturePanel = GameObject.Instantiate(initiativeCreaturePanel, Vector3.zero, Quaternion.identity);
+            creaturePanel.transform.localScale = new Vector3(1, 1, 1);
+            creaturePanel.transform.SetParent(initiativePanel.transform);
+            creaturePanel.GetComponent<InitCreaturePanelScript>().SetImage(c.GetComponent<Creature>().GetSprite());
 
-            Debug.Log(c.name + " init: " + c.GetComponent<Creature>().GetInit() + " Dex " + c.GetComponent<Creature>().Dex + " pos: " + grid.WorldToCell(c.transform.localPosition));
+            Debug.Log(c.name + " init: " + c.GetComponent<Creature>().GetInit() + " Dex " + c.GetComponent<Creature>().Dex +
+                " pos: " + Grid.WorldToCell(c.transform.localPosition));
 
-            c.SetPositionInGrid(grid.WorldToCell(c.transform.localPosition));
+            c.InitPanelGameObject = creaturePanel;
+            c.SetPositionInGrid(Grid.WorldToCell(c.transform.localPosition));
         });
 
     }

+ 0 - 18
Assets/Scripts/Human.cs

@@ -1,18 +0,0 @@
-using System.Collections;
-using System.Collections.Generic;
-using UnityEngine;
-
-public class Human : Creature
-{
-    // Start is called before the first frame update
-    void Start()
-    {
-        Dex = Random.Range(0, 100);
-    }
-
-    // Update is called once per frame
-    void Update()
-    {
-
-    }
-}

+ 5 - 0
Assets/Scripts/Panels/InitPanel/InitCreaturePanelScript.cs

@@ -5,6 +5,7 @@ using UnityEngine.UI;
 
 public class InitCreaturePanelScript : MonoBehaviour {
     private Image creatureImage;
+    [SerializeField] Image healthBar;
 
     private void Awake() {
         creatureImage = GetComponent<Image>();
@@ -13,4 +14,8 @@ public class InitCreaturePanelScript : MonoBehaviour {
     public void SetImage(Sprite sprite) {
         creatureImage.sprite = sprite;
     }
+
+    public void UpdateHealthBar(float fillValue) {
+        healthBar.fillAmount = fillValue;
+    }
 }

+ 53 - 1
Assets/Scripts/RoundManager.cs

@@ -4,25 +4,77 @@ using System.Collections.Generic;
 using System.Linq;
 using RPG_Fight_Test.Assets.Scripts.Creatures;
 using UnityEngine;
+using UnityEngine.UI;
 
 public class RoundManager : MonoBehaviour {
+
+    [SerializeField] Button executeRoundButton;
     private List<Creature> combatants;
     int round;
+
+    private bool executing = false;
+
+    private void Start() {
+        executeRoundButton.onClick.AddListener(ExecuteRound);
+    }
+
+    private void ExecuteRound() {
+        executing = true;
+        executeRoundButton.interactable = false;
+        foreach (Creature c in combatants) {
+            StartCoroutine(c.GetFirstAction().PerformAction());
+        }
+        round++;
+    }
+
+    private void FixedUpdate() {
+        if (executing) {
+            foreach (Creature c in combatants) {
+                c.CurrentPos = Vector3Int.RoundToInt(c.gameObject.transform.position);
+            }
+        }
+    }
+
     internal void StartFirstRound(List<Creature> combatants) {
         this.combatants = combatants;
         round = 1;
         MakeDecisions();
     }
 
+    internal void SetupNextRound() {
+        this.combatants = combatants.FindAll(c => c.IsCreatureAlive);
+        MakeDecisions();
+    }
+
     private void MakeDecisions() {
         foreach (Creature creature in combatants) {
             if (creature.IsHumanControlled()) {
                 CameraManager.GetInstance().FocusOnGameObject(creature.gameObject);
+
+                // TODO REMOVE testing
+                if (creature.name.Equals("Human2")) {
+                    MoveAction ma = new MoveAction();
+                    ma.SetTargetPosition(new Vector3(2.5f, -27.5f, 0f));
+                    ma.SetStartPosition(creature.CurrentPos);
+                    ma.SetCreatureToMove(creature);
+                    creature.SetFirstAction(ma);
+                } else {
+                    creature.SetFirstAction(new AttackAction(combatants[0]));
+                }
+                creature.SetSecondAction(new AttackAction(combatants[0])); // TODO REMOVE testing
                 // Wait for human decision
             } else {
-                ((Skeleton)creature).DecideActions(combatants);
+                ((Skeleton)creature).DecideActions(combatants); // FIXME Skeleton won't work.. need to be more general
                 // AI make decision
             }
         }
+
+        if (CheckIfAllCombatantsHaveTheirActions()) {
+            executeRoundButton.interactable = true;
+        }
+    }
+
+    private bool CheckIfAllCombatantsHaveTheirActions() {
+        return combatants.All(c => c.HasFirstAction() && c.HasSecondAction());
     }
 }

Daži faili netika attēloti, jo izmaiņu fails ir pārāk liels