diff --git a/Assets/Prefabs/Tower.prefab b/Assets/Prefabs/Tower.prefab index 5259d7240926da3d21236281a919bee39aef71c5..489fa2c9bf37123df46eef60a4915714ff890f62 100644 --- a/Assets/Prefabs/Tower.prefab +++ b/Assets/Prefabs/Tower.prefab @@ -27,9 +27,10 @@ Transform: m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: - - {fileID: 7691419396082110330} + - {fileID: 4358043365550456146} + - {fileID: 8417445742414849243} m_Father: {fileID: 2849581468949299191} - m_RootOrder: 0 + m_RootOrder: 1 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &1091641560809420229 GameObject: @@ -61,8 +62,7 @@ Transform: m_LocalScale: {x: 1, y: 1, z: 1} m_Children: - {fileID: 2158363716064708819} - - {fileID: 4358043365550456146} - - {fileID: 8417445742414849243} + - {fileID: 2249357176531528900} m_Father: {fileID: 0} m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} @@ -78,9 +78,11 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 67aafe08abf8467fb869765ad1410d15, type: 3} m_Name: m_EditorClassIdentifier: + rotatingTurret: {fileID: 2249357176531528900} backgroundSprite: {fileID: 4990586827348949875} constantSprite: {fileID: 6408071058950479439} coloredSprite: {fileID: 1886016283497806366} + turretRotationSpeed: 300 --- !u!120 &4195717166071304880 LineRenderer: m_ObjectHideFlags: 0 @@ -203,12 +205,12 @@ Transform: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 3468452580434664440} - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + 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: 2849581468949299191} - m_RootOrder: 2 + m_Father: {fileID: 2249357176531528900} + m_RootOrder: 1 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!212 &6408071058950479439 SpriteRenderer: @@ -285,12 +287,12 @@ Transform: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 5860039055144437433} - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + 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: 2849581468949299191} - m_RootOrder: 1 + m_Father: {fileID: 2249357176531528900} + m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!212 &1886016283497806366 SpriteRenderer: diff --git a/Assets/Scenes/Simulation.unity b/Assets/Scenes/Simulation.unity index 964aeb647d7e1ca98a915fe1d2b1d95ddb9050da..5f9cda4b30d81fbef4b23ffc86dcd04d9833a17c 100644 --- a/Assets/Scenes/Simulation.unity +++ b/Assets/Scenes/Simulation.unity @@ -306,6 +306,7 @@ MonoBehaviour: - {fileID: 11400000, guid: 14cfef037d7b7084e850217a60295501, type: 2} - {fileID: 11400000, guid: a2978ddc11d13fd43bdf581244985ea0, type: 2} - {fileID: 11400000, guid: f299e01e73a41d5498e4b3ebc22c4afe, type: 2} + - {fileID: 11400000, guid: bc8ca3c42ec58f44bb985de36537d877, type: 2} --- !u!114 &240370942 MonoBehaviour: m_ObjectHideFlags: 0 @@ -412,8 +413,6 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: cf7c5a8235d14edca54533665a0d3ee8, type: 3} m_Name: m_EditorClassIdentifier: - worldWidth: 15 - worldHeight: 15 overviewConfig: {fileID: 11400000, guid: 7c8cbf053badd4b41b80657b3ac3f9b0, type: 2} economyConfig: {fileID: 11400000, guid: 81bfec33eeaebc34587067be5f4845a9, type: 2} worldConfig: {fileID: 11400000, guid: 4295ad0f99658094795e3a4c95b62f83, type: 2} diff --git a/Assets/Scripts/Logic/Data/World/Tower.cs b/Assets/Scripts/Logic/Data/World/Tower.cs index 2814a11c8aa3a7aa8be499d18ad1106efea2aceb..617f5105afdf3cdc5f3f2ab3981f43da07ad8538 100644 --- a/Assets/Scripts/Logic/Data/World/Tower.cs +++ b/Assets/Scripts/Logic/Data/World/Tower.cs @@ -11,6 +11,23 @@ public class Tower : Building { //TODO we need a CanBeNull annotation or some documentation here public Unit Target { get; private set; } + public Unit ClosestEnemy { + get { + Unit closest = null; + float closestDistance = float.PositiveInfinity; + Vector2 turretPosition = Position.ToVectorCentered(); + foreach (Unit unit in World.Units) { + float distance = unit.Position.Distance2(turretPosition); + if (closestDistance > distance) { + closestDistance = distance; + closest = unit; + } + } + + return closest; + } + } + public float RemainingCooldownTime { get; private set; } public bool IsOnCooldown => RemainingCooldownTime > 0; diff --git a/Assets/Scripts/Presentation/World/Tower.cs b/Assets/Scripts/Presentation/World/Tower.cs index 669378aebe5acea0283978824da64d5ec580af2d..43a8e382c24923e00a00a8674558da4fce103937 100644 --- a/Assets/Scripts/Presentation/World/Tower.cs +++ b/Assets/Scripts/Presentation/World/Tower.cs @@ -17,14 +17,20 @@ public class Tower : Structure { [SerializeField] private SpriteRenderer coloredSprite; + [SerializeField] + private float turretRotationSpeed; + private World _world; private void FixedUpdate() { - if (_data.Target != null) { - Vector3 target = _world.LogicToPresentation(_data.Target).transform.position; + Logic.Data.World.Unit logicTarget = _data.Target ?? _data.ClosestEnemy; + if (logicTarget != null && logicTarget.IsAlive) { + Vector3 target = _world.LogicToPresentation(logicTarget).transform.position; Vector3 direction = (target - rotatingTurret.position).normalized; float angle = Mathf.Rad2Deg * Mathf.Atan2(direction.y, direction.x); - rotatingTurret.rotation = Quaternion.AngleAxis(angle - 90, Vector3.forward); + Quaternion desired = Quaternion.AngleAxis(angle - 90, Vector3.forward); + rotatingTurret.rotation = Quaternion.RotateTowards(rotatingTurret.rotation, desired, + Time.fixedDeltaTime * turretRotationSpeed); } }