From 0db4c4956820b140c9f18a64a6882745205cdcd9 Mon Sep 17 00:00:00 2001 From: Sveske Juice Date: Sat, 20 Apr 2024 20:48:38 +0200 Subject: [PATCH] projectile damaging + robots can die:) --- Assets/Prefabs/Projectiles/Bullet.prefab | 16 ++++++++ Assets/Prefabs/Towers/BaseTower.prefab | 39 ++++++++++++++++++- Assets/Prefabs/Towers/BlackHole.prefab | 30 ++++++++++++++ Assets/Prefabs/Towers/Turret.prefab | 32 ++++++++++++++- Assets/Scenes/TowerTest.unity | 4 +- Assets/Scripts/Tower/AimTower.cs | 1 - Assets/Scripts/Tower/Projectile.cs | 27 +++++++++++++ Assets/Scripts/Tower/ProjectileTower.cs | 2 +- Assets/Scripts/Utilities/HealthComponent.cs | 5 +++ Assets/Scripts/Utilities/ProjectileSpawner.cs | 1 + 10 files changed, 151 insertions(+), 6 deletions(-) diff --git a/Assets/Prefabs/Projectiles/Bullet.prefab b/Assets/Prefabs/Projectiles/Bullet.prefab index 36a7388..a1c57b3 100644 --- a/Assets/Prefabs/Projectiles/Bullet.prefab +++ b/Assets/Prefabs/Projectiles/Bullet.prefab @@ -13,6 +13,7 @@ GameObject: - component: {fileID: 5408793041970514069} - component: {fileID: 58437666630366278} - component: {fileID: 4851113807957284959} + - component: {fileID: 8700343489547611766} m_Layer: 6 m_Name: Bullet m_TagString: Untagged @@ -133,3 +134,18 @@ Rigidbody: m_Interpolate: 0 m_Constraints: 0 m_CollisionDetection: 0 +--- !u!114 &8700343489547611766 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2962360034595352958} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: a98521a4b559621438fc196adc605f3b, type: 3} + m_Name: + m_EditorClassIdentifier: + bounciness: 0.516 + damage: 10 + wallRebounces: 4 diff --git a/Assets/Prefabs/Towers/BaseTower.prefab b/Assets/Prefabs/Towers/BaseTower.prefab index 724ce7b..f614500 100644 --- a/Assets/Prefabs/Towers/BaseTower.prefab +++ b/Assets/Prefabs/Towers/BaseTower.prefab @@ -73,6 +73,10 @@ PrefabInstance: type: 3} insertIndex: -1 addedObject: {fileID: 7883233016828513649} + - targetCorrespondingSourceObject: {fileID: 919132149155446097, guid: eb5e13df78999ef878da1db5c63224e2, + type: 3} + insertIndex: -1 + addedObject: {fileID: 5565472806559733162} m_SourcePrefab: {fileID: 100100000, guid: eb5e13df78999ef878da1db5c63224e2, type: 3} --- !u!1 &872127579833310669 stripped GameObject: @@ -95,7 +99,40 @@ MonoBehaviour: startHealth: 100 OnHealthZero: m_PersistentCalls: - m_Calls: [] + m_Calls: + - m_Target: {fileID: 7883233016828513649} + m_TargetAssemblyTypeName: HealthComponent, Assembly-CSharp + m_MethodName: SimpleKill + m_Mode: 1 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 0 + m_CallState: 2 OnHealthChange: m_PersistentCalls: m_Calls: [] +--- !u!65 &5565472806559733162 +BoxCollider: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 872127579833310669} + m_Material: {fileID: 0} + m_IncludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_ExcludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_LayerOverridePriority: 0 + m_IsTrigger: 0 + m_ProvidesContacts: 0 + m_Enabled: 1 + serializedVersion: 3 + m_Size: {x: 1, y: 1, z: 1} + m_Center: {x: 0, y: 0, z: 0} diff --git a/Assets/Prefabs/Towers/BlackHole.prefab b/Assets/Prefabs/Towers/BlackHole.prefab index 8974ea4..1485b82 100644 --- a/Assets/Prefabs/Towers/BlackHole.prefab +++ b/Assets/Prefabs/Towers/BlackHole.prefab @@ -231,6 +231,36 @@ PrefabInstance: propertyPath: m_Name value: BlackHole objectReference: {fileID: 0} + - target: {fileID: 5565472806559733162, guid: f54a14318952c90c5b47a300aedb0d15, + type: 3} + propertyPath: m_Size.x + value: 0.5101528 + objectReference: {fileID: 0} + - target: {fileID: 5565472806559733162, guid: f54a14318952c90c5b47a300aedb0d15, + type: 3} + propertyPath: m_Size.y + value: 1.8364654 + objectReference: {fileID: 0} + - target: {fileID: 5565472806559733162, guid: f54a14318952c90c5b47a300aedb0d15, + type: 3} + propertyPath: m_Size.z + value: 0.5177386 + objectReference: {fileID: 0} + - target: {fileID: 5565472806559733162, guid: f54a14318952c90c5b47a300aedb0d15, + type: 3} + propertyPath: m_Center.x + value: -0.022192955 + objectReference: {fileID: 0} + - target: {fileID: 5565472806559733162, guid: f54a14318952c90c5b47a300aedb0d15, + type: 3} + propertyPath: m_Center.y + value: -0.0720216 + objectReference: {fileID: 0} + - target: {fileID: 5565472806559733162, guid: f54a14318952c90c5b47a300aedb0d15, + type: 3} + propertyPath: m_Center.z + value: -0.008693576 + objectReference: {fileID: 0} m_RemovedComponents: [] m_RemovedGameObjects: [] m_AddedGameObjects: diff --git a/Assets/Prefabs/Towers/Turret.prefab b/Assets/Prefabs/Towers/Turret.prefab index 24d00ea..d2c26b9 100644 --- a/Assets/Prefabs/Towers/Turret.prefab +++ b/Assets/Prefabs/Towers/Turret.prefab @@ -383,7 +383,7 @@ Transform: m_GameObject: {fileID: 2676689439087408815} serializedVersion: 2 m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0, y: 0, z: 0.00723} + m_LocalPosition: {x: 0, y: 0, z: 0.00846} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: [] @@ -994,6 +994,36 @@ PrefabInstance: propertyPath: m_Name value: Turret objectReference: {fileID: 0} + - target: {fileID: 5565472806559733162, guid: f54a14318952c90c5b47a300aedb0d15, + type: 3} + propertyPath: m_Size.x + value: 0.5204178 + objectReference: {fileID: 0} + - target: {fileID: 5565472806559733162, guid: f54a14318952c90c5b47a300aedb0d15, + type: 3} + propertyPath: m_Size.y + value: 1.2033536 + objectReference: {fileID: 0} + - target: {fileID: 5565472806559733162, guid: f54a14318952c90c5b47a300aedb0d15, + type: 3} + propertyPath: m_Size.z + value: 0.6565219 + objectReference: {fileID: 0} + - target: {fileID: 5565472806559733162, guid: f54a14318952c90c5b47a300aedb0d15, + type: 3} + propertyPath: m_Center.x + value: -0.02208674 + objectReference: {fileID: 0} + - target: {fileID: 5565472806559733162, guid: f54a14318952c90c5b47a300aedb0d15, + type: 3} + propertyPath: m_Center.y + value: -0.07302019 + objectReference: {fileID: 0} + - target: {fileID: 5565472806559733162, guid: f54a14318952c90c5b47a300aedb0d15, + type: 3} + propertyPath: m_Center.z + value: -0.05310279 + objectReference: {fileID: 0} - target: {fileID: 8365322460670124409, guid: f54a14318952c90c5b47a300aedb0d15, type: 3} propertyPath: m_Name diff --git a/Assets/Scenes/TowerTest.unity b/Assets/Scenes/TowerTest.unity index af54993..1520f0f 100644 --- a/Assets/Scenes/TowerTest.unity +++ b/Assets/Scenes/TowerTest.unity @@ -1243,7 +1243,7 @@ PrefabInstance: - target: {fileID: 7348612162646443967, guid: fd9f9b61c0ebb324ebc9f929e26706bc, type: 3} propertyPath: m_LocalRotation.w - value: -0.1380528 + value: -0.13805284 objectReference: {fileID: 0} - target: {fileID: 7348612162646443967, guid: fd9f9b61c0ebb324ebc9f929e26706bc, type: 3} @@ -1258,7 +1258,7 @@ PrefabInstance: - target: {fileID: 7348612162646443967, guid: fd9f9b61c0ebb324ebc9f929e26706bc, type: 3} propertyPath: m_LocalRotation.z - value: -0.13815574 + value: -0.13815573 objectReference: {fileID: 0} - target: {fileID: 8882344377078016156, guid: fd9f9b61c0ebb324ebc9f929e26706bc, type: 3} diff --git a/Assets/Scripts/Tower/AimTower.cs b/Assets/Scripts/Tower/AimTower.cs index f717840..dac4169 100644 --- a/Assets/Scripts/Tower/AimTower.cs +++ b/Assets/Scripts/Tower/AimTower.cs @@ -106,7 +106,6 @@ public class AimTower : Tower ghosts[poolIdx].transform.position = ghostPos; ghosts[poolIdx].SetActive(true); poolIdx = (poolIdx + 1) % trajectoryObjectPoolSize; - Debug.Log($"pool idx: {poolIdx}"); } else { diff --git a/Assets/Scripts/Tower/Projectile.cs b/Assets/Scripts/Tower/Projectile.cs index a82b6f8..ea9a131 100644 --- a/Assets/Scripts/Tower/Projectile.cs +++ b/Assets/Scripts/Tower/Projectile.cs @@ -9,6 +9,15 @@ public class Projectile : MonoBehaviour private Collider projCol; + [SerializeField, Range(0, 50)] + private int damage = 10; + + [SerializeField] + private int wallRebounces = 4; + private int hitWalls = 0; + + public HealthComponent comingFrom; + private void Awake() { projCol = GetComponent(); @@ -19,4 +28,22 @@ public class Projectile : MonoBehaviour projCol.material = pMat; } + + private void OnCollisionEnter(Collision collision) + { + HealthComponent hitHealthComp = collision.gameObject.GetComponent(); + if (hitHealthComp == comingFrom) return; + if (hitHealthComp) + { + hitHealthComp.TakeDamage(damage); + Destroy(gameObject); + } + else + { + if (++hitWalls == wallRebounces) + { + Destroy(gameObject); + } + } + } } diff --git a/Assets/Scripts/Tower/ProjectileTower.cs b/Assets/Scripts/Tower/ProjectileTower.cs index bc6a932..9cdb5ea 100644 --- a/Assets/Scripts/Tower/ProjectileTower.cs +++ b/Assets/Scripts/Tower/ProjectileTower.cs @@ -29,7 +29,7 @@ public class ProjectileTower : AimTower Debug.DrawRay(transform.position, horizontalArc.ToKnobVector, Color.red, attackSecondsDelay); Debug.DrawRay(transform.position, verticalArc.ToKnobVector, Color.green, attackSecondsDelay); Debug.DrawRay(transform.position, AimDirection, Color.yellow, attackSecondsDelay); - projectileSpawner.RunBulletSequence(barrel.transform.position, transform.up, AimDirection, projectileSequence); + projectileSpawner.RunBulletSequence(barrel.Tip.position, transform.up, AimDirection, projectileSequence); } while (true); } } diff --git a/Assets/Scripts/Utilities/HealthComponent.cs b/Assets/Scripts/Utilities/HealthComponent.cs index ce52ae9..74cefa2 100644 --- a/Assets/Scripts/Utilities/HealthComponent.cs +++ b/Assets/Scripts/Utilities/HealthComponent.cs @@ -28,5 +28,10 @@ public class HealthComponent : MonoBehaviour if (currentHealth == 0) OnHealthZero?.Invoke(); } + + public void SimpleKill() + { + Destroy(gameObject); + } } diff --git a/Assets/Scripts/Utilities/ProjectileSpawner.cs b/Assets/Scripts/Utilities/ProjectileSpawner.cs index 59b0381..6926abf 100644 --- a/Assets/Scripts/Utilities/ProjectileSpawner.cs +++ b/Assets/Scripts/Utilities/ProjectileSpawner.cs @@ -45,6 +45,7 @@ public class ProjectileSpawner : MonoBehaviour projectile.transform.up = bulletDir; projectile.GetComponent().AddForce(bulletDir * bulletPattern.Speed); + projectile.GetComponent().comingFrom = GetComponent(); if (bulletPattern.Burst) yield return new WaitForSeconds(bulletPattern.BurstDelay);