Skip to content
This repository has been archived by the owner on Jul 3, 2024. It is now read-only.

Commit

Permalink
Unit factory build queue
Browse files Browse the repository at this point in the history
  • Loading branch information
Amatsugu committed Sep 1, 2020
1 parent a38ca50 commit 9c2d5cc
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 60 deletions.
2 changes: 1 addition & 1 deletion Assets/Editor/UI/UnitDatabaseUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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})");
}
}

Expand Down
4 changes: 2 additions & 2 deletions Assets/Editor/UI/UnitIdentifierDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<UnitDatabase>(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");
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scenes/Main.unity
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions Assets/Scripts/Data Store/Databases/UnitDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ 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<int, UnitDefination>();
entityIds = new Dictionary<MobileUnitEntity, int>();
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];
}
Expand Down Expand Up @@ -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)
Expand All @@ -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);
Expand Down
120 changes: 70 additions & 50 deletions Assets/Scripts/Game Logic/Systems/BuildQueueSystem.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,31 @@
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
{
private Dictionary<int, BuildOrder> _pendingBuildOrders;
private List<int> _readyToBuildOrders;
private List<ConstructionOrder> _constructionOrders;
private List<int> _removal;
private int _curOrderID = 0;
private Dictionary<HexCoords, bool> _factoryReady;

private static BuildQueueSystem _INST;
private bool _isReady;

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()
Expand All @@ -55,7 +43,7 @@ protected override void OnUpdate()
{
if (!_isReady)
return;
PlaceReadyBuildings();
BuildReadyOrders();
ProcessConstructionOrders();
}

Expand All @@ -64,63 +52,76 @@ protected override void OnUpdate()
/// </summary>
/// <param name="building">The tile info of the building to build</param>
/// <param name="dst">The tile on which the building will be placed</param>
/// <param name="dropPod">The mesh entity that will be used as a drop pod</param>
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);

/// <summary>
/// Add a building to the build queue
/// </summary>
/// <param name="tile">The tile on which the building will be placed</param>
/// <param name="building">The tile info of the building to build</param>
/// <param name="dropPod">The mesh entity that will be used as a drop pod</param>
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);
}

/// <summary>
/// Place all buildings that have been marked as ready to place
/// </summary>
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
});
}

/// <summary>
/// Places a building on the map
/// </summary>
Expand All @@ -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
});
}

Expand All @@ -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);
}
}
Expand All @@ -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;
}
2 changes: 1 addition & 1 deletion Assets/Scripts/Game Logic/UI/GameUI/UIBuildPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Map/SerializedMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{

}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

using System.Collections;
using System.Collections.Generic;

using Unity.Mathematics;

using UnityEngine;

namespace Amatsugu.Phos.TileEntities
Expand Down
1 change: 1 addition & 0 deletions Assets/Scripts/Map/Units/MobileUnitEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ComponentType> GetComponents()
{
Expand Down

0 comments on commit 9c2d5cc

Please sign in to comment.