From 77db237e9ebc8c79a5a660c5ebe1e1031f8c731b Mon Sep 17 00:00:00 2001 From: Stedd Date: Sun, 3 Sep 2023 20:39:15 +0200 Subject: [PATCH] Give MoveCommand to selected units only --- Assets/Prefabs/Unit.prefab | 1 + Assets/Scenes/MainScene.unity | 23 ++- Assets/Scripts/Units/Unit.cs | 3 + Assets/Scripts/Units/UnitCommandGiver.cs | 35 +++++ Assets/Scripts/Units/UnitCommandGiver.cs.meta | 11 ++ Assets/Scripts/Units/UnitMovement.cs | 26 +--- Assets/Scripts/Units/UnitSelectionHandler.cs | 10 +- .../com.unity.probuilder/Settings.json | 135 ++++++++++++++++++ 8 files changed, 211 insertions(+), 33 deletions(-) create mode 100644 Assets/Scripts/Units/UnitCommandGiver.cs create mode 100644 Assets/Scripts/Units/UnitCommandGiver.cs.meta diff --git a/Assets/Prefabs/Unit.prefab b/Assets/Prefabs/Unit.prefab index f424c6d..fc583c3 100644 --- a/Assets/Prefabs/Unit.prefab +++ b/Assets/Prefabs/Unit.prefab @@ -144,6 +144,7 @@ MonoBehaviour: syncDirection: 0 syncMode: 0 syncInterval: 0 + _unitMovement: {fileID: 8681090027785358193} _onSelect: m_PersistentCalls: m_Calls: diff --git a/Assets/Scenes/MainScene.unity b/Assets/Scenes/MainScene.unity index e1b7ecb..e8056b2 100644 --- a/Assets/Scenes/MainScene.unity +++ b/Assets/Scenes/MainScene.unity @@ -803,8 +803,9 @@ GameObject: m_Component: - component: {fileID: 1784456514} - component: {fileID: 1784456513} + - component: {fileID: 1784456515} m_Layer: 0 - m_Name: UnitSelectionHandler + m_Name: UnitHandlers m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 @@ -824,7 +825,7 @@ MonoBehaviour: m_EditorClassIdentifier: _layerMask: serializedVersion: 2 - m_Bits: 4294967295 + m_Bits: 1 --- !u!4 &1784456514 Transform: m_ObjectHideFlags: 0 @@ -840,6 +841,22 @@ Transform: m_Children: [] m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &1784456515 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1784456512} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 74e28a0cf1a767d498b804650f3b1ea1, type: 3} + m_Name: + m_EditorClassIdentifier: + _unitSelectionHandler: {fileID: 1784456513} + _layerMask: + serializedVersion: 2 + m_Bits: 1 --- !u!1 &1896718083 GameObject: m_ObjectHideFlags: 0 @@ -1152,6 +1169,6 @@ SceneRoots: - {fileID: 1954283568} - {fileID: 9051835} - {fileID: 1904145807} + - {fileID: 1784456514} - {fileID: 168675456} - {fileID: 578241694} - - {fileID: 1784456514} diff --git a/Assets/Scripts/Units/Unit.cs b/Assets/Scripts/Units/Unit.cs index 8779478..0c1bfe8 100644 --- a/Assets/Scripts/Units/Unit.cs +++ b/Assets/Scripts/Units/Unit.cs @@ -7,9 +7,12 @@ using UnityEngine.Serialization; public class Unit : NetworkBehaviour { + [SerializeField] private UnitMovement _unitMovement; [SerializeField] private UnityEvent _onSelect; [SerializeField] private UnityEvent _onDeSelect; + public UnitMovement UnitMovement => _unitMovement; + #region Client [Client] diff --git a/Assets/Scripts/Units/UnitCommandGiver.cs b/Assets/Scripts/Units/UnitCommandGiver.cs new file mode 100644 index 0000000..fd2559b --- /dev/null +++ b/Assets/Scripts/Units/UnitCommandGiver.cs @@ -0,0 +1,35 @@ +using System; +using UnityEngine; +using UnityEngine.InputSystem; + +public class UnitCommandGiver : MonoBehaviour +{ + [SerializeField] private UnitSelectionHandler _unitSelectionHandler; + [SerializeField] private LayerMask _layerMask; + + private Camera _camera; + + private void Start() + { + _camera = Camera.main; + } + + private void Update() + { + if (!Mouse.current.rightButton.wasPressedThisFrame) return; + + Ray ray = _camera.ScreenPointToRay(Mouse.current.position.ReadValue()); + + if (!Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, _layerMask)) return; + + TryMove(hit.point); + } + + private void TryMove(Vector3 destination) + { + foreach (var selectedUnit in _unitSelectionHandler.SelectedUnits) + { + selectedUnit.UnitMovement.CmdMove(destination); + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Units/UnitCommandGiver.cs.meta b/Assets/Scripts/Units/UnitCommandGiver.cs.meta new file mode 100644 index 0000000..e7442c6 --- /dev/null +++ b/Assets/Scripts/Units/UnitCommandGiver.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 74e28a0cf1a767d498b804650f3b1ea1 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Units/UnitMovement.cs b/Assets/Scripts/Units/UnitMovement.cs index 60daac1..d137657 100644 --- a/Assets/Scripts/Units/UnitMovement.cs +++ b/Assets/Scripts/Units/UnitMovement.cs @@ -12,7 +12,7 @@ public class UnitMovement : NetworkBehaviour #region Server - [Command] private void CmdMove(Vector3 destination) + [Command] public void CmdMove(Vector3 destination) { if (NavMesh.SamplePosition(destination, out NavMeshHit hit, 1f, NavMesh.AllAreas)) { @@ -21,28 +21,4 @@ public class UnitMovement : NetworkBehaviour } #endregion - - #region Client - - public override void OnStartAuthority() - { - base.OnStartAuthority(); - _camera = Camera.main; - _mouse = Mouse.current; - } - - [ClientCallback] - private void Update() - { - if (!isOwned) return; - if (!_mouse.rightButton.wasPressedThisFrame) return; - Ray ray = _camera.ScreenPointToRay(_mouse.position.ReadValue()); - - if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity)) - { - CmdMove(hit.point); - } - } - - #endregion } \ No newline at end of file diff --git a/Assets/Scripts/Units/UnitSelectionHandler.cs b/Assets/Scripts/Units/UnitSelectionHandler.cs index fd66ff2..d9cf851 100644 --- a/Assets/Scripts/Units/UnitSelectionHandler.cs +++ b/Assets/Scripts/Units/UnitSelectionHandler.cs @@ -10,7 +10,7 @@ public class UnitSelectionHandler : MonoBehaviour [SerializeField] private LayerMask _layerMask; private Camera _mainCamera; - private readonly List _selectedUnits = new(); + public List SelectedUnits { get; } = new(); private void Start() { @@ -32,12 +32,12 @@ public class UnitSelectionHandler : MonoBehaviour private void ClearSelectedUnits() { - foreach (Unit selectedUnit in _selectedUnits) + foreach (Unit selectedUnit in SelectedUnits) { selectedUnit.DeSelect(); } - _selectedUnits.Clear(); + SelectedUnits.Clear(); } private void ClearSelectionArea() @@ -50,9 +50,9 @@ public class UnitSelectionHandler : MonoBehaviour if (!unit.isOwned) return; - _selectedUnits.Add(unit); + SelectedUnits.Add(unit); - foreach (Unit selectedUnit in _selectedUnits) + foreach (Unit selectedUnit in SelectedUnits) { selectedUnit.Select(); } diff --git a/ProjectSettings/Packages/com.unity.probuilder/Settings.json b/ProjectSettings/Packages/com.unity.probuilder/Settings.json index e6ccc4a..1163310 100644 --- a/ProjectSettings/Packages/com.unity.probuilder/Settings.json +++ b/ProjectSettings/Packages/com.unity.probuilder/Settings.json @@ -6,6 +6,61 @@ "key": "lightmapping.autoUnwrapLightmapUV", "value": "{\"m_Value\":true}" }, + { + "type": "System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", + "key": "mesh.newShapesSnapToGrid", + "value": "{\"m_Value\":true}" + }, + { + "type": "System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", + "key": "mesh.meshColliderIsConvex", + "value": "{\"m_Value\":false}" + }, + { + "type": "System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", + "key": "editor.autoRecalculateCollisions", + "value": "{\"m_Value\":false}" + }, + { + "type": "System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", + "key": "experimental.enabled", + "value": "{\"m_Value\":false}" + }, + { + "type": "System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", + "key": "UnityEngine.ProBuilder.ProBuilderEditor-isUtilityWindow", + "value": "{\"m_Value\":false}" + }, + { + "type": "System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", + "key": "editor.toolbarIconGUI", + "value": "{\"m_Value\":false}" + }, + { + "type": "System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", + "key": "editor.backFaceSelectEnabled", + "value": "{\"m_Value\":false}" + }, + { + "type": "System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", + "key": "editor.showSceneInfo", + "value": "{\"m_Value\":false}" + }, + { + "type": "System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", + "key": "ShapeComponent.ResetSettings", + "value": "{\"m_Value\":false}" + }, + { + "type": "System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", + "key": "editor.showEditorNotifications", + "value": "{\"m_Value\":false}" + }, + { + "type": "System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", + "key": "ShapeComponent.SettingsEnabled", + "value": "{\"m_Value\":false}" + }, { "type": "UnityEngine.ProBuilder.LogLevel, Unity.ProBuilder, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", "key": "log.level", @@ -30,6 +85,86 @@ "type": "UnityEngine.ProBuilder.SemVer, Unity.ProBuilder, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", "key": "preferences.version", "value": "{\"m_Value\":{\"m_Major\":5,\"m_Minor\":1,\"m_Patch\":1,\"m_Build\":-1,\"m_Type\":\"\",\"m_Metadata\":\"\",\"m_Date\":\"\"}}" + }, + { + "type": "UnityEngine.ProBuilder.PivotLocation, Unity.ProBuilder, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", + "key": "mesh.newShapePivotLocation", + "value": "{\"m_Value\":1}" + }, + { + "type": "UnityEngine.Rendering.ShadowCastingMode, UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", + "key": "mesh.shadowCastingMode", + "value": "{\"m_Value\":1}" + }, + { + "type": "UnityEngine.Material, UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", + "key": "mesh.userMaterial", + "value": "{\"m_Value\":{\"instanceID\":0}}" + }, + { + "type": "UnityEditor.StaticEditorFlags, UnityEditor.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", + "key": "mesh.defaultStaticEditorFlags", + "value": "{\"m_Value\":0}" + }, + { + "type": "UnityEngine.ProBuilder.ColliderType, Unity.ProBuilder, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", + "key": "mesh.newShapeColliderType", + "value": "{\"m_Value\":2}" + }, + { + "type": "UnityEngine.ProBuilder.UnwrapParameters, Unity.ProBuilder, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", + "key": "lightmapping.defaultLightmapUnwrapParameters", + "value": "{\"m_Value\":{\"m_HardAngle\":88.0,\"m_PackMargin\":20.0,\"m_AngleError\":8.0,\"m_AreaError\":15.0}}" + }, + { + "type": "UnityEngine.ProBuilder.RectSelectMode, Unity.ProBuilder, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", + "key": "editor.dragSelectRectMode", + "value": "{\"m_Value\":0}" + }, + { + "type": "UnityEngine.ProBuilder.SelectionModifierBehavior, Unity.ProBuilder, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", + "key": "editor.rectSelectModifier", + "value": "{\"m_Value\":2}" + }, + { + "type": "UnityEngine.ProBuilder.SelectMode, Unity.ProBuilder, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", + "key": "editor.selectMode", + "value": "{\"m_Value\":1}" + }, + { + "type": "UnityEngine.ProBuilder.SelectMode, Unity.ProBuilder, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", + "key": "editor.lastMeshSelectMode", + "value": "{\"m_Value\":2}" + }, + { + "type": "System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", + "key": "ShapeBuilder.ActiveShapeIndex", + "value": "{\"m_Value\":0}" + }, + { + "type": "System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", + "key": "ShapeBuilder.LastPivotLocation", + "value": "{\"m_Value\":1}" + }, + { + "type": "UnityEngine.Vector3, UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", + "key": "ShapeBuilder.LastPivotPosition", + "value": "{\"m_Value\":{\"x\":0.0,\"y\":0.0,\"z\":0.0}}" + }, + { + "type": "UnityEngine.Vector3, UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", + "key": "ShapeBuilder.LastSize", + "value": "{\"m_Value\":{\"x\":1.2949085235595704,\"y\":0.0,\"z\":1.3470239639282227}}" + }, + { + "type": "UnityEngine.Quaternion, UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", + "key": "ShapeBuilder.LastRotation", + "value": "{\"m_Value\":{\"x\":0.0,\"y\":0.0,\"z\":0.0,\"w\":1.0}}" + }, + { + "type": "UnityEngine.ProBuilder.Shapes.Shape, Unity.ProBuilder, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", + "key": "ShapeBuilder.Sprite", + "value": "{}" } ] }