From 74dd3727f62603f64958790f9e588d47c8f000ff Mon Sep 17 00:00:00 2001
From: miepzerino <o.skotnik@gmail.com>
Date: Sat, 23 Dec 2023 03:14:14 +0000
Subject: [PATCH] Added player movement while drilling

---
 Assets/Characters/Player/Player.prefab   |    8 +-
 Assets/Scripts/Managers/GameManager.cs   |   17 ++++-
 Assets/Scripts/PlayerController.cs       |   64 ++++++++++++++++----
 TODOs                                    |    1 
 Assets/Scenes/GameplayScene.unity        |   50 ++++------------
 Assets/Scripts/Events/CharacterEvents.cs |    1 
 6 files changed, 82 insertions(+), 59 deletions(-)

diff --git a/Assets/Characters/Player/Player.prefab b/Assets/Characters/Player/Player.prefab
index 2c4a01b..5a84793 100644
--- a/Assets/Characters/Player/Player.prefab
+++ b/Assets/Characters/Player/Player.prefab
@@ -223,7 +223,7 @@
     m_Bits: 0
   m_Interpolate: 0
   m_SleepingMode: 1
-  m_CollisionDetection: 0
+  m_CollisionDetection: 1
   m_Constraints: 4
 --- !u!114 &1512638649171213079
 MonoBehaviour:
@@ -386,7 +386,7 @@
     adaptiveTiling: 0
   m_AutoTiling: 0
   serializedVersion: 2
-  m_Size: {x: 0.7205616, y: 0.7178567}
+  m_Size: {x: 0.7, y: 0.7}
   m_EdgeRadius: 0
 --- !u!95 &2451348752795735853
 Animator:
@@ -435,8 +435,8 @@
     maxDepth: 0
     minNormalAngle: 0
     maxNormalAngle: 0
-  groundDistance: 0.05
-  wallDistance: 0.2
+  groundDistance: 0.02
+  wallDistance: 0.02
   ceilingDistance: 0.05
   rotorGO: {fileID: 1519978218308964847}
   _isGrounded: 1
diff --git a/Assets/Scenes/GameplayScene.unity b/Assets/Scenes/GameplayScene.unity
index c2d7705..1c11e86 100644
--- a/Assets/Scenes/GameplayScene.unity
+++ b/Assets/Scenes/GameplayScene.unity
@@ -3188,7 +3188,7 @@
   m_Color: {r: 1, g: 1, b: 1, a: 1}
   m_Origin: {x: 0, y: -1, z: 0}
   m_Size: {x: 14, y: 7, z: 1}
-  m_TileAnchor: {x: 0.5, y: 0.5, z: 0}
+  m_TileAnchor: {x: 0, y: 0, z: 0}
   m_TileOrientation: 0
   m_TileOrientationMatrix:
     e00: 1
@@ -4323,25 +4323,25 @@
       e33: 0
   - m_RefCount: 0
     m_Data:
-      e00: 0
+      e00: 5.0497e-41
       e01: 0
       e02: 5.5000076
       e03: 5.5000076
       e10: 0
       e11: 0
-      e12: 6.95e-43
-      e13: 6.95e-43
-      e20: 1.1215021e-15
-      e21: 14.638138
-      e22: -1.3211928e-28
-      e23: -1.093541e-19
-      e30: 4.5905e-41
-      e31: 6.98e-43
-      e32: 6.95e-43
-      e33: 6.98e-43
+      e12: 5.55e-43
+      e13: 5.55e-43
+      e20: 0.000116169685
+      e21: 8.914042e-18
+      e22: -8.137887e-29
+      e23: -1.1375036e-19
+      e30: 4.5904e-41
+      e31: 5.58e-43
+      e32: 5.55e-43
+      e33: 5.58e-43
   m_TileColorArray:
   - m_RefCount: 0
-    m_Data: {r: 4.4012e-41, g: 4.4012e-41, b: 4.4012e-41, a: 4.4012e-41}
+    m_Data: {r: 4.2084e-41, g: 4.2084e-41, b: 4.2084e-41, a: 4.2084e-41}
   m_TileObjectToInstantiateArray: []
   m_AnimationFrameRate: 1
   m_Color: {r: 1, g: 1, b: 1, a: 1}
@@ -4990,30 +4990,6 @@
     - target: {fileID: 3345854317100013954, guid: c220ec455fce341408d66d880b464cad, type: 3}
       propertyPath: m_Name
       value: Player
-      objectReference: {fileID: 0}
-    - target: {fileID: 3884295854780712968, guid: c220ec455fce341408d66d880b464cad, type: 3}
-      propertyPath: m_Size.x
-      value: 0.7
-      objectReference: {fileID: 0}
-    - target: {fileID: 3884295854780712968, guid: c220ec455fce341408d66d880b464cad, type: 3}
-      propertyPath: m_Size.y
-      value: 0.7
-      objectReference: {fileID: 0}
-    - target: {fileID: 3884295854780712968, guid: c220ec455fce341408d66d880b464cad, type: 3}
-      propertyPath: m_EdgeRadius
-      value: 0
-      objectReference: {fileID: 0}
-    - target: {fileID: 4941032555966952991, guid: c220ec455fce341408d66d880b464cad, type: 3}
-      propertyPath: m_CollisionDetection
-      value: 1
-      objectReference: {fileID: 0}
-    - target: {fileID: 5338311196462064651, guid: c220ec455fce341408d66d880b464cad, type: 3}
-      propertyPath: wallDistance
-      value: 0.02
-      objectReference: {fileID: 0}
-    - target: {fileID: 5338311196462064651, guid: c220ec455fce341408d66d880b464cad, type: 3}
-      propertyPath: groundDistance
-      value: 0.02
       objectReference: {fileID: 0}
     m_RemovedComponents: []
     m_RemovedGameObjects: []
diff --git a/Assets/Scripts/Events/CharacterEvents.cs b/Assets/Scripts/Events/CharacterEvents.cs
index 5d4e7f6..51c31e3 100644
--- a/Assets/Scripts/Events/CharacterEvents.cs
+++ b/Assets/Scripts/Events/CharacterEvents.cs
@@ -9,4 +9,5 @@
     public static UnityAction<GameObject, int> characterDamaged;
     public static UnityAction<GameObject, int> characterHealed;
     public static UnityAction<ContactPoint2D, DrillDirection> characterDrill;
+    public static UnityAction<Vector3> characterDrillingToPosition;
 }
diff --git a/Assets/Scripts/Managers/GameManager.cs b/Assets/Scripts/Managers/GameManager.cs
index c7925d3..d4b9e44 100644
--- a/Assets/Scripts/Managers/GameManager.cs
+++ b/Assets/Scripts/Managers/GameManager.cs
@@ -59,8 +59,9 @@
     }
     public void CharacterDrill(ContactPoint2D contact, DrillDirection drillDirection)
     {
-        Vector3Int cellCoord = tilemap.transform.GetComponentInParent<GridLayout>().WorldToCell(contact.point);
-        switch(drillDirection)
+        GridLayout grid = tilemap.transform.GetComponentInParent<GridLayout>();
+        Vector3Int cellCoord = grid.WorldToCell(contact.point);
+        switch (drillDirection)
         {
             case DrillDirection.Left:
                 cellCoord.x = cellCoord.x - 1;
@@ -72,7 +73,17 @@
                 cellCoord.y = cellCoord.y - 1;
                 break;
         }
-        tilemap.SetTile(cellCoord, null);
+
+        //Debug.Log("cellCoord: " + grid.CellToWorld(cellCoord));
+        //Debug.Log(tilemap.HasTile(cellCoord));
+        if (tilemap.HasTile(cellCoord))
+        {
+            tilemap.SetTile(cellCoord, null);
+            Vector3 moveToPosition = grid.CellToWorld(cellCoord);
+            moveToPosition.x += 0.5f;
+            moveToPosition.y += 0.5f;
+            CharacterEvents.characterDrillingToPosition.Invoke(moveToPosition);
+        }
     }
     public void GameLoaded()
     {
diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs
index ed9f0d7..5f0246c 100644
--- a/Assets/Scripts/PlayerController.cs
+++ b/Assets/Scripts/PlayerController.cs
@@ -19,17 +19,22 @@
 
     [NonSerialized]
     public Rigidbody2D rb;
+    [NonSerialized]
+    public Damageable health;
+
+    BoxCollider2D boxCollider;
     Animator animator;
     Animator animator_rotor;
     public GameObject rotorGO;
     TouchingDirections touchingDirections;
-    [NonSerialized]
-    public Damageable health;
 
     // Time it takes to drill
     public float drillingTime = 1f;
     private float timeSinceDrill = 0f;
-
+    private bool drillToPosition;
+    private Vector3 drillTilePosition;
+    private Vector3 drillOriginalPosition;
+    
     [SerializeField]
     private bool _isMoving;
     public bool IsMoving
@@ -89,7 +94,15 @@
         }
     }
 
+    private void OnEnable()
+    {
+        CharacterEvents.characterDrillingToPosition += (DrillingToPosition);
+    }
 
+    private void OnDisable()
+    {
+        CharacterEvents.characterDrillingToPosition -= (DrillingToPosition);
+    }
 
     private void Awake()
     {
@@ -98,6 +111,7 @@
         animator_rotor = rotorGO.GetComponent<Animator>();
         touchingDirections = GetComponent<TouchingDirections>();
         health = GetComponent<Damageable>();
+        boxCollider = GetComponent<BoxCollider2D>();
         if (SaveSystem.isGameLoaded)
         {
             GameObject uiManager = GameObject.Find("GameManager");
@@ -181,14 +195,22 @@
         if (IsDrilling)
         {
             rb.velocity = Vector2.zero;
-            if (timeSinceDrill > drillingTime)
+            if (drillToPosition)
             {
-                IsDrilling = false;
-                timeSinceDrill = 0;
-            }
-            else
-            {
-                timeSinceDrill += Time.deltaTime;
+                if (timeSinceDrill < drillingTime)
+                {
+                    gameObject.transform.position = Vector3.Lerp(drillOriginalPosition, drillTilePosition, timeSinceDrill / drillingTime);
+                    //Debug.Log("drillTilePosition: " + drillTilePosition);
+                    //Debug.Log("timeSinceDrill: " + timeSinceDrill);
+                    timeSinceDrill += Time.deltaTime;
+                }
+                else
+                {
+                    boxCollider.enabled = true;
+                    IsDrilling = false;
+                    drillToPosition = false;
+                    timeSinceDrill = 0;
+                }
             }
         }
     }
@@ -207,15 +229,12 @@
             IsMoving = false;
             IsFlying = false;
             //moveInput = Vector2.zero;
-
         }
     }
 
     private void Drill(DrillDirection drillDirection)
     {
-        IsDrilling = true;
         ContactPoint2D[] contactPoints = new ContactPoint2D[1];
-        Debug.Log("drillDirection: " + drillDirection.ToString());
         //rb.GetContacts(contactPoints);
         switch (drillDirection)
         {
@@ -229,7 +248,24 @@
                 touchingDirections.groundHits[0].collider.GetContacts(contactPoints);
                 break;
         }
-        CharacterEvents.characterDrill.Invoke(contactPoints[0], drillDirection);
+        //Debug.Log(contactPoints[0].otherRigidbody?.gameObject.name);
+        if (contactPoints[0].otherRigidbody?.name == "Tilemap")
+        {
+            boxCollider.enabled = false;
+            IsDrilling = true;
+            CharacterEvents.characterDrill.Invoke(contactPoints[0], drillDirection);
+        }
+    }
+
+    private void DrillingToPosition(Vector3 tilePosition)
+    {
+        //Debug.Log("DrillingToPosition() called");
+        //Debug.Log(gameObject.transform.position);
+        drillToPosition = true;
+        drillTilePosition = tilePosition;
+        //Debug.Log("drillTilePosition: " + drillTilePosition);
+        drillOriginalPosition = gameObject.transform.position;
+        //Debug.Log("drillOriginalPosition: " + drillOriginalPosition);
     }
 
     //private void DrillLeftOrRight()
diff --git a/TODOs b/TODOs
index 203ae72..b99e873 100644
--- a/TODOs
+++ b/TODOs
@@ -12,7 +12,6 @@
 
 
  NEXT
-  Move player to drill tile
   Animate drilling
   Add borders to map
   Add background to map

--
Gitblit v1.9.3