From 0ac1402616222fc678ef69819907f8977cae29ec Mon Sep 17 00:00:00 2001 From: Sveske Juice Date: Sat, 20 Apr 2024 22:42:06 +0200 Subject: [PATCH] select unselect tower --- Assets/Prefabs/Towers/Turret.prefab | 9 +- Assets/Scenes/TowerTest.unity | 92 ++++++++++++++++++- Assets/Scripts/Manager/GameManager.cs | 12 ++- .../PlacementSystem/CameraSlotClickDetect.cs | 7 +- Assets/Scripts/Tower/AimTower.cs | 21 +++-- Assets/Scripts/Tower/GravityTower.cs | 20 ++++ Assets/Scripts/Tower/Tower.cs | 19 +--- ProjectSettings/TagManager.asset | 4 +- 8 files changed, 146 insertions(+), 38 deletions(-) diff --git a/Assets/Prefabs/Towers/Turret.prefab b/Assets/Prefabs/Towers/Turret.prefab index a0696c2..77987d4 100644 --- a/Assets/Prefabs/Towers/Turret.prefab +++ b/Assets/Prefabs/Towers/Turret.prefab @@ -102,7 +102,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 1 + m_IsActive: 0 --- !u!4 &3688593234855359137 Transform: m_ObjectHideFlags: 0 @@ -624,7 +624,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 1 + m_IsActive: 0 --- !u!4 &4508182228569071021 Transform: m_ObjectHideFlags: 0 @@ -777,7 +777,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 1 + m_IsActive: 0 --- !u!4 &2141594701022449211 Transform: m_ObjectHideFlags: 0 @@ -1076,6 +1076,9 @@ MonoBehaviour: type: 3} trajectoryLine: {fileID: 3987931586699850031} trajectoryObjectPoolSize: 25 + wallMask: + serializedVersion: 2 + m_Bits: 257 attackSecondsDelay: 1 projectileSequence: - Arc: 0 diff --git a/Assets/Scenes/TowerTest.unity b/Assets/Scenes/TowerTest.unity index 6db570d..f4854fd 100644 --- a/Assets/Scenes/TowerTest.unity +++ b/Assets/Scenes/TowerTest.unity @@ -929,6 +929,51 @@ Transform: m_Children: [] m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1 &1395991985 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1395991987} + - component: {fileID: 1395991986} + m_Layer: 0 + m_Name: gm + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &1395991986 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1395991985} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 00e1f7ea5be7c564faf42d035aec7f53, type: 3} + m_Name: + m_EditorClassIdentifier: + IsBuildMode: 0 +--- !u!4 &1395991987 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1395991985} + serializedVersion: 2 + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 1.1557862, y: 2.294486, z: 1.574348} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &1621186629 GameObject: m_ObjectHideFlags: 0 @@ -1185,6 +1230,11 @@ PrefabInstance: propertyPath: m_LocalPosition.z value: 11.533513 objectReference: {fileID: 0} + - target: {fileID: 3837216061230793568, guid: fd9f9b61c0ebb324ebc9f929e26706bc, + type: 3} + propertyPath: stopAfterTime + value: 5 + objectReference: {fileID: 0} - target: {fileID: 5753661378891917460, guid: fd9f9b61c0ebb324ebc9f929e26706bc, type: 3} propertyPath: m_LocalRotation.w @@ -1243,7 +1293,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 +1308,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} @@ -1313,7 +1363,11 @@ PrefabInstance: m_RemovedComponents: [] m_RemovedGameObjects: [] m_AddedGameObjects: [] - m_AddedComponents: [] + m_AddedComponents: + - targetCorrespondingSourceObject: {fileID: 6163557566655415017, guid: fd9f9b61c0ebb324ebc9f929e26706bc, + type: 3} + insertIndex: -1 + addedObject: {fileID: 1970787077} m_SourcePrefab: {fileID: 100100000, guid: fd9f9b61c0ebb324ebc9f929e26706bc, type: 3} --- !u!1 &1797347348 GameObject: @@ -1488,6 +1542,37 @@ PrefabInstance: m_AddedGameObjects: [] m_AddedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: 48ecac7381f19af3bab6fef54e6dbadc, type: 3} +--- !u!1 &1970787075 stripped +GameObject: + m_CorrespondingSourceObject: {fileID: 6163557566655415017, guid: fd9f9b61c0ebb324ebc9f929e26706bc, + type: 3} + m_PrefabInstance: {fileID: 1732242001} + m_PrefabAsset: {fileID: 0} +--- !u!114 &1970787077 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1970787075} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: c2d4e884ed2ba0944bfe55454353bc2d, type: 3} + m_Name: + m_EditorClassIdentifier: + mainCamera: {fileID: 1970787082} + layerMask: + serializedVersion: 2 + m_Bits: 1024 + selectLayer: + serializedVersion: 2 + m_Bits: 256 +--- !u!20 &1970787082 stripped +Camera: + m_CorrespondingSourceObject: {fileID: 3255403189825384754, guid: fd9f9b61c0ebb324ebc9f929e26706bc, + type: 3} + m_PrefabInstance: {fileID: 1732242001} + m_PrefabAsset: {fileID: 0} --- !u!1001 &667212637924654954 PrefabInstance: m_ObjectHideFlags: 0 @@ -1575,3 +1660,4 @@ SceneRoots: - {fileID: 1671091699} - {fileID: 770231297} - {fileID: 504816165} + - {fileID: 1395991987} diff --git a/Assets/Scripts/Manager/GameManager.cs b/Assets/Scripts/Manager/GameManager.cs index e3c5bf7..5a33a26 100644 --- a/Assets/Scripts/Manager/GameManager.cs +++ b/Assets/Scripts/Manager/GameManager.cs @@ -1,5 +1,3 @@ -using System.Collections; -using System.Collections.Generic; using UnityEngine; public class GameManager : MonoBehaviour @@ -7,7 +5,15 @@ public class GameManager : MonoBehaviour public static GameManager Instance; public bool IsBuildMode = false; - public Tower SelectedTower; + private Tower selectedTower; + public Tower SelectedTower { get { return selectedTower; } set { + if (selectedTower != null) + selectedTower.TowerSelected(false); + + selectedTower = value; + selectedTower.TowerSelected(true); + Debug.Log($"Selected: {selectedTower}"); + }} private void Awake() { diff --git a/Assets/Scripts/PlacementSystem/CameraSlotClickDetect.cs b/Assets/Scripts/PlacementSystem/CameraSlotClickDetect.cs index ae1e7cc..ecd51ac 100644 --- a/Assets/Scripts/PlacementSystem/CameraSlotClickDetect.cs +++ b/Assets/Scripts/PlacementSystem/CameraSlotClickDetect.cs @@ -30,12 +30,13 @@ public class CameraSlotClickDetect : MonoBehaviour { var slotInfo = hit.collider.gameObject.GetComponentInParent(); slotInfo.OnClick(); + GameManager.Instance.SelectedTower = null; } RaycastHit selectHit; - if (Physics.Raycast(ray, out selectHit, Mathf.Infinity, selectLayer)) + if (Physics.Raycast(ray, out selectHit, Mathf.Infinity, selectLayer) && !GameManager.Instance.IsBuildMode) { - var obj = selectHit.collider.gameObject.GetComponentInChildren(); - obj.TowerSelected.Value = true; + var tower = selectHit.collider.gameObject.GetComponentInChildren(); + GameManager.Instance.SelectedTower = tower; } } diff --git a/Assets/Scripts/Tower/AimTower.cs b/Assets/Scripts/Tower/AimTower.cs index fb1eb81..7bcd357 100644 --- a/Assets/Scripts/Tower/AimTower.cs +++ b/Assets/Scripts/Tower/AimTower.cs @@ -17,6 +17,7 @@ public class AimTower : Tower [SerializeField] private GameObject ghostPrefab; [SerializeField] private LineRenderer trajectoryLine; [SerializeField] private int trajectoryObjectPoolSize = 25; + [SerializeField] private LayerMask wallMask; private int poolIdx = 0; public Vector3 AimDirection => barrel.transform.forward; @@ -35,18 +36,18 @@ public class AimTower : Tower horizontalArc.Value.AddListener(SnapVerticalToHorizontal); - TowerSelected.AddListener(OnTowerSelected); - UpdateBarrelRotation(); UpdateTrajectory(); - OnTowerSelected(TowerSelected); } - private void OnTowerSelected(bool selected) + public override void TowerSelected(bool selected) { - horizontalArc.enabled = selected; - verticalArc.enabled = selected; - trajectoryLine.enabled = selected; + base.TowerSelected(selected); + + horizontalArc.gameObject.SetActive(selected); + verticalArc.gameObject.SetActive(selected); + trajectoryLine.gameObject.SetActive(selected); + UpdateTrajectory(); } protected override void OnDestroy() @@ -77,9 +78,9 @@ public class AimTower : Tower { ghost.SetActive(false); } + if (!this.selected) return; - if (!TowerSelected.Value) return; - + Debug.Log("UPAD"); Vector3 origin = barrel.Tip.position; Vector3 dir = barrel.transform.forward; List pointsInTrajectory = new(); @@ -88,7 +89,7 @@ public class AimTower : Tower { Debug.DrawRay(origin, dir.normalized * k_trajectory_maxdist, Color.red, 5f); RaycastHit hit; - if (!Physics.Raycast(origin, dir, out hit, k_trajectory_maxdist)) + if (!Physics.Raycast(origin, dir, out hit, k_trajectory_maxdist, wallMask)) break; pointsInTrajectory.Add(hit.point); diff --git a/Assets/Scripts/Tower/GravityTower.cs b/Assets/Scripts/Tower/GravityTower.cs index 49b34f9..0f4573c 100644 --- a/Assets/Scripts/Tower/GravityTower.cs +++ b/Assets/Scripts/Tower/GravityTower.cs @@ -14,6 +14,16 @@ public class GravityTower : Tower [SerializeField] private AnimationCurve distToAcceleration; + public override void TowerSelected(bool selected) + { + + } + + protected override void Awake() + { + base.Awake(); + } + protected override void FixedUpdate() { Collider[] projectilesInRadius = Physics.OverlapSphere(orb.position, radius, affectedBodies); @@ -25,4 +35,14 @@ public class GravityTower : Tower projectileBody.AddForce(projToOrb.normalized * distToAcceleration.Evaluate(dist)); } } + + protected override void OnDestroy() + { + base.OnDestroy(); + } + + protected override void Update() + { + base.Update(); + } } diff --git a/Assets/Scripts/Tower/Tower.cs b/Assets/Scripts/Tower/Tower.cs index 60fd100..3ddee8e 100644 --- a/Assets/Scripts/Tower/Tower.cs +++ b/Assets/Scripts/Tower/Tower.cs @@ -4,29 +4,20 @@ using UnityEngine.Assertions; public abstract class Tower : MonoBehaviour { - public Observer TowerSelected { get; set; } = new(false); - + protected bool selected = false; protected HealthComponent healthComp; // Getters + public virtual void TowerSelected(bool selected) + { + this.selected = selected; + } protected virtual void Awake() { healthComp = GetComponent(); Assert.IsNotNull(healthComp); - TowerSelected.AddListener(DeselectTowers); - } - - public static void DeselectTowers(bool selected) - { - if (selected) - { - foreach (var tower in GameObject.FindObjectsByType(FindObjectsSortMode.None)) - { - tower.TowerSelected.Value = false; - } - } } protected virtual void OnDestroy() diff --git a/ProjectSettings/TagManager.asset b/ProjectSettings/TagManager.asset index 317644e..8d75b91 100644 --- a/ProjectSettings/TagManager.asset +++ b/ProjectSettings/TagManager.asset @@ -14,9 +14,9 @@ TagManager: - UI - Projectile - + - Tower - - - - - + - Cell - - -