diff --git a/Assets/Editor/UI/UnitDatabaseUI.cs b/Assets/Editor/UI/UnitDatabaseUI.cs index edd0dc28..613ef7eb 100644 --- a/Assets/Editor/UI/UnitDatabaseUI.cs +++ b/Assets/Editor/UI/UnitDatabaseUI.cs @@ -42,7 +42,7 @@ public override void OnInspectorGUI() GUI.enabled = true; foreach (var unitDef in database.unitEntites.Values) { - EditorGUILayout.LabelField($"[{unitDef.id}]\t {(unitDef.unit == null ? "Null" : unitDef.unit.name)}:\t ({unitDef.unit.unitDomain}|{unitDef.unit.unitClass})"); + EditorGUILayout.LabelField($"[{unitDef.id}]\t {(unitDef.info == null ? "Null" : unitDef.info.name)}:\t ({unitDef.info.unitDomain}|{unitDef.info.unitClass})"); } } diff --git a/Assets/Editor/UI/UnitIdentifierDrawer.cs b/Assets/Editor/UI/UnitIdentifierDrawer.cs index a2ee1a82..52c6db1f 100644 --- a/Assets/Editor/UI/UnitIdentifierDrawer.cs +++ b/Assets/Editor/UI/UnitIdentifierDrawer.cs @@ -15,8 +15,8 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten var asset = AssetDatabase.FindAssets($"t:{nameof(UnitDatabase)}").First(); var assetPath = AssetDatabase.GUIDToAssetPath(asset); var db = AssetDatabase.LoadAssetAtPath(assetPath); - var units = db.unitEntites.Values.Where(b => b.unit != null); - var names = units.Select(b => $" [{b.unit.unitDomain}|{b.unit.unitClass}] T{b.unit.tier} {b.unit.name}\t {b.id}").Prepend("--Select Unit--").ToArray(); + var units = db.unitEntites.Values.Where(b => b.info != null); + var names = units.Select(b => $" [{b.info.unitDomain}|{b.info.unitClass}] T{b.info.tier} {b.info.name}\t {b.id}").Prepend("--Select Unit--").ToArray(); var ids = units.Select(b => b.id + 1).Prepend(0).ToArray(); EditorGUI.BeginProperty(position, label, property); var sProp = property.FindPropertyRelative("id"); diff --git a/Assets/Scenes/Main.unity b/Assets/Scenes/Main.unity index 3049e251..55b814d7 100644 --- a/Assets/Scenes/Main.unity +++ b/Assets/Scenes/Main.unity @@ -6411,7 +6411,7 @@ MonoBehaviour: m_HandleRect: {fileID: 2102994817} m_Direction: 2 m_Value: 0 - m_Size: 0.99999726 + m_Size: 1 m_NumberOfSteps: 0 m_OnValueChanged: m_PersistentCalls: @@ -13245,7 +13245,7 @@ RectTransform: m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0, y: 1} m_AnchorMax: {x: 1, y: 1} - m_AnchoredPosition: {x: 0, y: -442.99997} + m_AnchoredPosition: {x: 0, y: -443.00003} m_SizeDelta: {x: 0, y: 0} m_Pivot: {x: 0, y: 0} --- !u!114 &1427279150 diff --git a/Assets/Scripts/Data Store/Databases/UnitDatabase.cs b/Assets/Scripts/Data Store/Databases/UnitDatabase.cs index baffbe99..79c06659 100644 --- a/Assets/Scripts/Data Store/Databases/UnitDatabase.cs +++ b/Assets/Scripts/Data Store/Databases/UnitDatabase.cs @@ -21,6 +21,9 @@ public class UnitDatabase : ScriptableObject, ISerializationCallbackReceiver private UnitDefination[] _defs; private int nextId = 0; + + public UnitDefination this[UnitIdentifier id] => unitEntites[id.id]; + public void OnAfterDeserialize() { unitEntites = new Dictionary(); @@ -28,7 +31,7 @@ public void OnAfterDeserialize() for (int i = 0; i < _ids.Length; i++) { unitEntites.Add(_ids[i], _defs[i]); - entityIds.Add(_defs[i].unit, _ids[i]); + entityIds.Add(_defs[i].info, _ids[i]); if(_ids[i] > nextId) nextId = _ids[i]; } @@ -58,7 +61,7 @@ public void OnBeforeSerialize() public struct UnitDefination { public int id; - public MobileUnitEntity unit; + public MobileUnitEntity info; } public bool RegisterUnit(MobileUnitEntity unit, out UnitDefination unitDef) @@ -71,7 +74,7 @@ public bool RegisterUnit(MobileUnitEntity unit, out UnitDefination unitDef) unitDef = new UnitDefination { id = nextId++, - unit = unit, + info = unit, }; unitEntites.Add(unitDef.id, unitDef); diff --git a/Assets/Scripts/Game Logic/Systems/BuildQueueSystem.cs b/Assets/Scripts/Game Logic/Systems/BuildQueueSystem.cs index 1fa4826c..03a2d07a 100644 --- a/Assets/Scripts/Game Logic/Systems/BuildQueueSystem.cs +++ b/Assets/Scripts/Game Logic/Systems/BuildQueueSystem.cs @@ -1,15 +1,9 @@ using Amatsugu.Phos.TileEntities; using Amatsugu.Phos.Tiles; -using AnimationSystem.AnimationData; -using AnimationSystem.Animations; - using System.Collections.Generic; using Unity.Entities; -using Unity.Mathematics; - -using Random = UnityEngine.Random; public class BuildQueueSystem : ComponentSystem { @@ -17,6 +11,8 @@ public class BuildQueueSystem : ComponentSystem private List _readyToBuildOrders; private List _constructionOrders; private List _removal; + private int _curOrderID = 0; + private Dictionary _factoryReady; private static BuildQueueSystem _INST; private bool _isReady; @@ -24,20 +20,12 @@ public class BuildQueueSystem : ComponentSystem protected override void OnCreate() { GameEvents.OnMapLoaded += InitBuildQueue; - GameEvents.OnAnimationEvent += OnAnimationEvent; } protected override void OnDestroy() { base.OnDestroy(); GameEvents.OnMapLoaded -= InitBuildQueue; - GameEvents.OnAnimationEvent -= OnAnimationEvent; - } - - private void OnAnimationEvent(int eventId) - { - if (_pendingBuildOrders.ContainsKey(eventId)) - _readyToBuildOrders.Add(eventId); } private void InitBuildQueue() @@ -55,7 +43,7 @@ protected override void OnUpdate() { if (!_isReady) return; - PlaceReadyBuildings(); + BuildReadyOrders(); ProcessConstructionOrders(); } @@ -64,63 +52,76 @@ protected override void OnUpdate() /// /// The tile info of the building to build /// The tile on which the building will be placed - /// The mesh entity that will be used as a drop pod - public static void QueueBuilding(BuildingTileEntity building, Tile dst, MeshEntityRotatable dropPod) => _INST.QueueBuilding(dst, building, dropPod); + public static void QueueBuilding(BuildingTileEntity building, Tile dst) => _INST.QueueBuilding(dst, building); /// /// Add a building to the build queue /// /// The tile on which the building will be placed /// The tile info of the building to build - /// The mesh entity that will be used as a drop pod - private void QueueBuilding(Tile tile, BuildingTileEntity building, MeshEntityRotatable dropPod) + private void QueueBuilding(Tile tile, BuildingTileEntity building) { - var hqMode = building is HQTileEntity; - var orderID = tile.Coords.ToString().GetHashCode(); + var orderID = _curOrderID++; _pendingBuildOrders.Add(orderID, new BuildOrder { building = building, dstTile = tile }); - if (hqMode) - { - var pos = tile.SurfacePoint; - pos.y = Random.Range(90, 100); - var e = dropPod.Instantiate(pos); - EntityManager.AddComponentData(e, new FallAnim - { - startSpeed = new float3(0, Random.Range(-100, -90), 0) - }); - EntityManager.AddComponentData(e, new Floor - { - Value = tile.Height - }); - EntityManager.AddComponentData(e, new HitFloorCallback - { - eventId = orderID - }); - EntityManager.AddComponentData(e, new Gravity { Value = 9.8f }); - } - else + _readyToBuildOrders.Add(orderID); + } + + private void QueueUnit(FactoryBuildingTile factory, UnitIdentifier unit) + { + var orderId = _curOrderID++; + _pendingBuildOrders.Add(orderId, new BuildOrder { - _readyToBuildOrders.Add(orderID); - } + factory = factory, + unit = unit + }); + if (!_factoryReady.ContainsKey(factory.Coords)) + _factoryReady.Add(factory.Coords, true); } + /// /// Place all buildings that have been marked as ready to place /// - private void PlaceReadyBuildings() + private void BuildReadyOrders() { for (int i = 0; i < _readyToBuildOrders.Count; i++) { var orderId = _readyToBuildOrders[i]; - EventManager.RemoveAllEventListeners(orderId); - PlaceBuilding(_pendingBuildOrders[orderId]); - _pendingBuildOrders.Remove(orderId); + var order = _pendingBuildOrders[orderId]; + switch (order.orderType) + { + case OrderType.Building: + PlaceBuilding(order); + _pendingBuildOrders.Remove(orderId); + break; + + case OrderType.Unit: + if (_factoryReady.ContainsKey(order.factory.Coords) && _factoryReady[order.factory.Coords]) //Check if can build + { + StartUnitConstruction(order); + } + break; + } } _readyToBuildOrders.Clear(); } + private void StartUnitConstruction(BuildOrder order) + { + var unit = GameRegistry.UnitDatabase[order.unit]; + _factoryReady[order.factory.Coords] = true; + order.factory.StartConstruction(unit.info); + _constructionOrders.Add(new ConstructionOrder + { + orderType = OrderType.Unit, + targetBuilding = order.factory.Coords, + buildTime = unit.info.buildTime + }); + } + /// /// Places a building on the map /// @@ -134,7 +135,7 @@ private void PlaceBuilding(BuildOrder order) _constructionOrders.Add(new ConstructionOrder { buildTime = GameRegistry.Cheats.INSTANT_BUILD ? Time.ElapsedTime : Time.ElapsedTime + order.building.constructionTime, - building = order.dstTile.Coords + targetBuilding = order.dstTile.Coords }); } @@ -148,7 +149,16 @@ private void ProcessConstructionOrders() var curOrder = _constructionOrders[i]; if (Time.ElapsedTime > curOrder.buildTime) { - (GameRegistry.GameMap[curOrder.building] as BuildingTile).Build(); + switch (curOrder.orderType) + { + case OrderType.Building: + (GameRegistry.GameMap[curOrder.targetBuilding] as BuildingTile).Build(); + break; + + case OrderType.Unit: + (GameRegistry.GameMap[curOrder.targetBuilding] as FactoryBuildingTile).FinishConstruction(); + break; + } _removal.Add(i); } } @@ -163,10 +173,20 @@ public struct BuildOrder { public Tile dstTile; public BuildingTileEntity building; + public FactoryBuildingTile factory; + public UnitIdentifier unit; + public OrderType orderType; +} + +public enum OrderType +{ + Building, + Unit } public struct ConstructionOrder { public double buildTime; - public HexCoords building; + public HexCoords targetBuilding; + public OrderType orderType; } \ No newline at end of file diff --git a/Assets/Scripts/Game Logic/UI/GameUI/UIBuildPanel.cs b/Assets/Scripts/Game Logic/UI/GameUI/UIBuildPanel.cs index aa1b9948..9289baa1 100644 --- a/Assets/Scripts/Game Logic/UI/GameUI/UIBuildPanel.cs +++ b/Assets/Scripts/Game Logic/UI/GameUI/UIBuildPanel.cs @@ -156,7 +156,7 @@ public void Show(FactoryBuildingTile factoryTile) } if (j < units.Length) { - var unit = _unitDatabase.unitEntites[units[j].id].unit; + var unit = _unitDatabase.unitEntites[units[j].id].info; if ( unit.tier == _tier) { _icons[i].SetActive(false); diff --git a/Assets/Scripts/Map/SerializedMap.cs b/Assets/Scripts/Map/SerializedMap.cs index 72dcd276..213086e2 100644 --- a/Assets/Scripts/Map/SerializedMap.cs +++ b/Assets/Scripts/Map/SerializedMap.cs @@ -47,7 +47,7 @@ public Map Deserialize(TileDatabase tileDb, UnitDatabase unitDb) for (int i = 0; i < units.Length; i++) { var sUnit = units[i]; - map.AddUnit(unitDb.unitEntites[sUnit.unitId].unit, map[new HexCoords(sUnit.x, sUnit.y, tileEdgeLength)], sUnit.faction); + map.AddUnit(unitDb.unitEntites[sUnit.unitId].info, map[new HexCoords(sUnit.x, sUnit.y, tileEdgeLength)], sUnit.faction); } return map; } diff --git a/Assets/Scripts/Map/Tiles/MappedTiles/Buildings/FactoryBuildingTile.cs b/Assets/Scripts/Map/Tiles/MappedTiles/Buildings/FactoryBuildingTile.cs index e9b595ae..fc536716 100644 --- a/Assets/Scripts/Map/Tiles/MappedTiles/Buildings/FactoryBuildingTile.cs +++ b/Assets/Scripts/Map/Tiles/MappedTiles/Buildings/FactoryBuildingTile.cs @@ -12,5 +12,15 @@ public FactoryBuildingTile(HexCoords coords, float height, Map map, FactoryTileE factoryInfo = tInfo; } + public virtual void StartConstruction(MobileUnitEntity unitEntity) + { + + } + + public virtual void FinishConstruction() + { + + } + } } \ No newline at end of file diff --git a/Assets/Scripts/Map/Tiles/TileInfo/Buildings/FactoryTileEntity.cs b/Assets/Scripts/Map/Tiles/TileInfo/Buildings/FactoryTileEntity.cs index 78716ca0..d120e291 100644 --- a/Assets/Scripts/Map/Tiles/TileInfo/Buildings/FactoryTileEntity.cs +++ b/Assets/Scripts/Map/Tiles/TileInfo/Buildings/FactoryTileEntity.cs @@ -2,6 +2,9 @@ using System.Collections; using System.Collections.Generic; + +using Unity.Mathematics; + using UnityEngine; namespace Amatsugu.Phos.TileEntities diff --git a/Assets/Scripts/Map/Units/MobileUnitEntity.cs b/Assets/Scripts/Map/Units/MobileUnitEntity.cs index 94dc2673..7afd2a9a 100644 --- a/Assets/Scripts/Map/Units/MobileUnitEntity.cs +++ b/Assets/Scripts/Map/Units/MobileUnitEntity.cs @@ -34,6 +34,7 @@ public class MobileUnitEntity : MeshEntityRotatable [CreateNewAsset("Assets/GameData/MapAssets/Meshes/UI/HealthBar", typeof(HealthBarDefination))] public HealthBarDefination healthBar; public float3 healthBarOffset; + public float buildTime; public override IEnumerable GetComponents() {