diff --git a/Assets/Scripts/Map/Tiles/MappedTiles/Buildings/BuildingTile.cs b/Assets/Scripts/Map/Tiles/MappedTiles/Buildings/BuildingTile.cs index cf5f2d96..62f08f06 100644 --- a/Assets/Scripts/Map/Tiles/MappedTiles/Buildings/BuildingTile.cs +++ b/Assets/Scripts/Map/Tiles/MappedTiles/Buildings/BuildingTile.cs @@ -52,6 +52,10 @@ public BuildingTile(HexCoords coords, float height, Map map, BuildingTileEntity metaTiles = new MetaTile[tInfo.footprint.footprint.Length - 1]; } + /// + /// Gets the Rich Text string representing information about the production of this building + /// + /// String Builder public StringBuilder GetProductionString() { if (_productionData.resourceIds == null) @@ -80,20 +84,24 @@ public StringBuilder GetProductionString() return sb; } + /// + /// The TileEntity that contains information about the underlying tile + /// public override TileEntity MeshEntity => buildingInfo.preserveGroundTile ? originalTile : buildingInfo; + /// + /// Gets the rotation of the building on this tile + /// + /// Quaternion of the building's rotation protected virtual quaternion GetBuildingRotation() { return rotation; } - public virtual void SetBuildingRotation(int rotation) - { - rotationAngle = rotation; - this.rotation = quaternion.RotateY(math.radians(60 * rotation)); - Map.EM.SetComponentData(_building, new Rotation { Value = this.rotation }); - } - + /// + /// Gets the resources that this building will return when deconstructed + /// + /// Array of resource Identifiers public ResourceIndentifier[] GetResourceRefund() { var res = new ResourceIndentifier[buildingInfo.cost.Length]; @@ -104,6 +112,7 @@ public ResourceIndentifier[] GetResourceRefund() return res; } + public override void OnHeightChanged() { base.OnHeightChanged(); @@ -123,8 +132,12 @@ public override void OnPlaced() { base.OnPlaced(); StartConstruction(); + CreateMetaTiles(); } + /// + /// Start the construction animations for this building + /// protected virtual void StartConstruction() { if (buildingInfo.constructionMesh != null) @@ -137,7 +150,6 @@ protected virtual void StartConstruction() } buildingInfo.constructionMesh.Instantiate(SurfacePoint, GetBuildingRotation(), buildingInfo.buildingMesh.height, buildingInfo.buildingMesh, buildingInfo.constructionTime); } - CreateMetaTiles(); } public override Entity Render() @@ -187,6 +199,9 @@ public override void OnShow() Map.EM.RemoveComponent(subMeshes, typeof(DisableRendering)); } + /// + /// Completes the build phase of this building + /// public void Build() { if (isBuilt) @@ -195,14 +210,27 @@ public void Build() OnBuilt(); RenderBuilding(); Start(); + if(buildingInfo.useMetaTiles) + { + for (int i = 0; i < metaTiles.Length; i++) + { + metaTiles[i].Start(); + } + } map.InvokeOnBuilt(Coords); } + /// + /// Callback for when this build phase completes + /// protected virtual void OnBuilt() { NotificationsUI.NotifyWithTarget(NotifType.Info, $"Construction Complete: {buildingInfo.GetNameString()}", Coords); } + /// + /// Instantiates the building entities for this tile + /// public virtual void RenderBuilding() { if (buildingInfo.buildingMesh.mesh == null) @@ -229,15 +257,22 @@ public override void Start() if (!isBuilt) return; ApplyTileProperites(); - ApplyBonuses(); + ApplyAdjacencyBonuses(); ApplyBuffs(); } + /// + /// Instantiate the submesses for this building tile + /// + /// The rotation of the building public virtual void RenderSubMeshes(quaternion rot) { subMeshes = buildingInfo.buildingMesh.InstantiateSubMeshes(rot, _building); } + /// + /// Replace tiles in this building's footprint with meta tiles + /// public virtual void CreateMetaTiles() { if (buildingInfo.useMetaTiles) @@ -248,16 +283,24 @@ public virtual void CreateMetaTiles() if (tiles[i] == Coords) continue; var tgtTile = map[tiles[i]]; - metaTiles[j++] = map.ReplaceTile(tgtTile, new MetaTile(tiles[i], tgtTile.Height, map, tgtTile.originalTile, this)); + var mt = map.ReplaceTile(tgtTile, new MetaTile(tiles[i], tgtTile.Height, map, tgtTile.originalTile, this)); + metaTiles[j++] = mt; } } } + /// + /// Gets the DOTS Entity for the building on this tile + /// + /// Entity protected virtual Entity GetBuildingEntity() { return buildingInfo.buildingMesh.mesh != null ? _building : _tileEntity; } + /// + /// Applies component data to entities on this building + /// protected virtual void ApplyTileProperites() { var entity = GetBuildingEntity(); @@ -314,16 +357,16 @@ public override void TileUpdated(Tile src, TileUpdateType updateType) if (!IsBuilt) return; if (updateType == TileUpdateType.Placed || updateType == TileUpdateType.Removed) - ApplyBonuses(); + ApplyAdjacencyBonuses(); } - protected virtual void ApplyBonuses() + /// + /// Applies adjanceny bonuses to neighboring tiles + /// + protected virtual void ApplyAdjacencyBonuses() { if (!IsBuilt || !_isRendered) return; - var entity = GetBuildingEntity(); - Map.EM.AddComponentData(entity, new ConsumptionMulti { Value = 1 }); - Map.EM.AddComponentData(entity, new ProductionMulti { Value = 1 }); var neighbors = map.GetNeighbors(Coords); if (!_adjacencyConnectors.IsCreated) _adjacencyConnectors = new NativeArray(6 * 3, Allocator.Persistent); @@ -346,6 +389,11 @@ protected virtual void ApplyBonuses() } } + /// + /// Add and apply a buff to this tile + /// + /// The source tile for the buff + /// The buff to apply public virtual void AddBuff(HexCoords src, StatsBuffs buff) { if (buffSources.ContainsKey(src)) @@ -359,6 +407,10 @@ public virtual void AddBuff(HexCoords src, StatsBuffs buff) ApplyBuffs(); } + /// + /// Remove a previously applied buff from this tile + /// + /// The tile that applied the buff public virtual void RemoveBuff(HexCoords src) { if (buffSources.ContainsKey(src)) @@ -369,6 +421,9 @@ public virtual void RemoveBuff(HexCoords src) } } + /// + /// Applies the current set of buffs to this tile + /// protected virtual void ApplyBuffs() { if (!isBuilt || !_isRendered) @@ -391,6 +446,9 @@ protected virtual void ApplyBuffs() Map.EM.SetComponentData(e, curHealth); } + /// + /// Deconstruct this building, reverting it to it's original tile + /// public virtual void Deconstruct() { if (buildingInfo.useMetaTiles) @@ -401,6 +459,11 @@ public virtual void Deconstruct() map.RevertTile(this); } + /// + /// Can this tile be deconstructed by the provided faction + /// + /// The faction attempting to deconstruct this tile + /// public virtual bool CanDeconstruct(Faction faction) => buildingInfo.faction == faction; public override void OnSerialize(Dictionary tileData) @@ -417,12 +480,18 @@ public override void OnDeSerialized(Dictionary tileData) base.OnDeSerialized(tileData); } + /// + /// Kills this bulding, causing it to be destroyed + /// public void Die() { OnDeath(); map.ReplaceTile(this, buildingInfo.customDeathTile ? buildingInfo.deathTile : originalTile); } + /// + /// Callback for when this building dies + /// public virtual void OnDeath() { NotificationsUI.NotifyWithTarget(NotifType.Warning, $"Building Destroyed: {buildingInfo.GetNameString()}", Coords); @@ -436,6 +505,9 @@ public override void Destroy() DestroyBuilding(); } + /// + /// Destorys the entities associated with this building and frees memory + /// protected virtual void DestroyBuilding() { if (World.DefaultGameObjectInjectionWorld != null) diff --git a/Assets/Scripts/Map/Tiles/MappedTiles/Buildings/MetaTile.cs b/Assets/Scripts/Map/Tiles/MappedTiles/Buildings/MetaTile.cs index e0d059a6..64ca4ead 100644 --- a/Assets/Scripts/Map/Tiles/MappedTiles/Buildings/MetaTile.cs +++ b/Assets/Scripts/Map/Tiles/MappedTiles/Buildings/MetaTile.cs @@ -16,8 +16,8 @@ public class MetaTile : PoweredBuildingTile public BuildingTile ParentTile { get; private set; } private PoweredBuildingTile _poweredParent; - private bool _isPowered; - private bool _isConduit; + private bool _isPoweredTile; + private bool _isConduitTile; public MetaTile(HexCoords coords, float height, Map map, TileEntity tInfo, BuildingTile parentTile) : base(coords, height, map, parentTile.buildingInfo, 0) { @@ -26,9 +26,9 @@ public MetaTile(HexCoords coords, float height, Map map, TileEntity tInfo, Build if (ParentTile is PoweredBuildingTile powered) { _poweredParent = powered; - _isPowered = true; + _isPoweredTile = true; } - _isConduit = ParentTile is ResourceConduitTile; + _isConduitTile = ParentTile is ResourceConduitTile; } protected override void StartConstruction() @@ -42,6 +42,12 @@ protected override void OnBuilt() public override void RenderBuilding() { hasBuilding = false; + + } + + protected override void ApplyBuffs() + { + } public override void AddBuff(HexCoords src, StatsBuffs buff) @@ -68,21 +74,30 @@ public override StringBuilder GetNameString() #endif } + public override void Start() + { + base.Start(); + FindConduitConnections(); + } + + public override bool MetaTilesHasConnection() + { + return false; + } + protected override void ApplyTileProperites() { } public override void HQConnected() { - Debug.Log("Meta Connect"); - if (_isPowered && !_isConduit) + if (_isPoweredTile && !_isConduitTile) _poweredParent.HQConnected(); } public override void HQDisconnected() { - Debug.Log("Meta Discconnect"); - if (_isPowered && !_isConduit) + if (_isPoweredTile && !_isConduitTile) _poweredParent.HQDisconnected(); } @@ -114,7 +129,7 @@ public override void OnPlaced() } - protected override void ApplyBonuses() + protected override void ApplyAdjacencyBonuses() { } @@ -145,7 +160,7 @@ public override void OnDeSerialized(Dictionary tileData) ParentTile = map[coord] as BuildingTile; if (ParentTile is PoweredBuildingTile powered) { - _isPowered = true; + _isPoweredTile = true; _poweredParent = powered; } } diff --git a/Assets/Scripts/Map/Tiles/MappedTiles/Buildings/PoweredBuildingTile.cs b/Assets/Scripts/Map/Tiles/MappedTiles/Buildings/PoweredBuildingTile.cs index 147f98eb..3567c427 100644 --- a/Assets/Scripts/Map/Tiles/MappedTiles/Buildings/PoweredBuildingTile.cs +++ b/Assets/Scripts/Map/Tiles/MappedTiles/Buildings/PoweredBuildingTile.cs @@ -33,6 +33,9 @@ public override void OnPlaced() base.OnPlaced(); } + /// + /// Callback for when this building is both powered and finished the build phase. Called after whichever happens last + /// protected virtual void OnBuiltAndPowered() { @@ -44,6 +47,9 @@ protected override void ApplyTileProperites() FindConduitConnections(); } + /// + /// Find a connection to nearby conduits + /// public virtual void FindConduitConnections() { Profiler.BeginSample("Find Conduit Connections"); @@ -75,6 +81,10 @@ public virtual void FindConduitConnections() Profiler.EndSample(); } + /// + /// Check if any of this building's meta tiles has a connection to the HQ + /// + /// public virtual bool MetaTilesHasConnection() { if (!buildingInfo.useMetaTiles) @@ -87,6 +97,9 @@ public virtual bool MetaTilesHasConnection() return false; } + /// + /// Callback for when this tile receives an HQ connection + /// public virtual void HQConnected() { if (connectionInit) @@ -103,6 +116,9 @@ public virtual void HQConnected() OnConnected(); } + /// + /// Callback for the this tile loses it's HQ connection + /// public virtual void HQDisconnected() { if (connectionInit) @@ -127,6 +143,9 @@ public virtual void HQDisconnected() OnDisconnected(); } + /// + /// Callback for sucessful connection to the HQ + /// public virtual void OnConnected() { if (IsBuilt) @@ -138,6 +157,9 @@ public virtual void OnConnected() } } + /// + /// Callback for when this tile lost connection to the HQ and could not reconnect + /// public virtual void OnDisconnected() { if(_connectionNotif == -1) diff --git a/Assets/Scripts/Map/Tiles/MappedTiles/Tile.cs b/Assets/Scripts/Map/Tiles/MappedTiles/Tile.cs index 007b4b74..1e3f282c 100644 --- a/Assets/Scripts/Map/Tiles/MappedTiles/Tile.cs +++ b/Assets/Scripts/Map/Tiles/MappedTiles/Tile.cs @@ -36,6 +36,13 @@ public class Tile protected bool _isInit; private bool _decorRendered; + /// + /// Create a tile + /// + /// The tile's location on the grid + /// The height of the tile + /// The game map that this tile belongs to + /// The tile Entity that will be used to create the tile public Tile(HexCoords coords, float height, Map map, TileEntity tInfo = null) { Coords = coords; @@ -54,46 +61,73 @@ public Tile(HexCoords coords, float height, Map map, TileEntity tInfo = null) } } - public Tile SetBiome(int biome, float moisture, float temperature) - { - this.moisture = moisture; - this.temperature = temperature; - biomeId = biome; - return this; - } - - public virtual StringBuilder GetNameString() + /// + /// Callback for when this tile is added to the map + /// + public virtual void OnPlaced() { -#if UNITY_EDITOR - return info.GetNameString().Append(" ").Append(Coords); -#else - return info.GetNameString(); -#endif + BroadcastTileUpdate(TileUpdateType.Placed); } - public virtual StringBuilder GetDescriptionString() + /// + /// Creates all the entities releated to this tile and shows it in the world + /// + /// + public virtual Entity Render() { - return new StringBuilder(info.description).AppendLine(); + IsShown = true; + _isRendered = true; + _tileEntity = MeshEntity.Instantiate(Coords, Height); + return _tileEntity; } - // override object.Equals - public override bool Equals(object obj) + /// + /// Render the decorators for this tile + /// + public virtual void RenderDecorators() { - if (obj == null || GetType() != obj.GetType()) + if (info.decorators.Length == 0) + return; + if (_decorRendered) + return; + _decorRendered = true; + _decor = new NativeArray(info.decorators.Sum(t => t.GetDecorEntityCount(this)), Allocator.Persistent, NativeArrayOptions.UninitializedMemory); + int lastIndex = 0; + for (int i = 0; i < info.decorators.Length; i++) { - return false; + var count = info.decorators[i].GetDecorEntityCount(this); + info.decorators[i].Render(this, _decor.Slice(lastIndex, count)); + lastIndex += count; } + } - var t = (Tile)obj; - return (t.Coords == Coords); + /// + /// Called after Render and it is safe to perform operations on entities of this tile and other tiles + /// + public virtual void Start() + { + _isInit = true; } - // override object.GetHashCode - public override int GetHashCode() + /// + /// Set biome data for the tile + /// + /// Biome ID + /// Moisture level + /// Tempurature + /// + public Tile SetBiome(int biome, float moisture, float temperature) { - return Coords.GetHashCode(); + this.moisture = moisture; + this.temperature = temperature; + biomeId = biome; + return this; } + /// + /// Update the height of a tile + /// + /// The new hieght of the tile public void UpdateHeight(float height) { Height = height; @@ -108,10 +142,13 @@ public void UpdateHeight(float height) SurfacePoint = new Vector3(Coords.WorldPos.x, Height, Coords.WorldPos.z); } OnHeightChanged(); - UpdateDecorations(); + UpdateDecorationHeight(); BroadcastTileUpdate(TileUpdateType.Height); } + /// + /// Callback triggered when the height of a tile is changed + /// public virtual void OnHeightChanged() { if (!_isRendered) @@ -121,7 +158,31 @@ public virtual void OnHeightChanged() Map.EM.SetComponentData(_tileEntity, new Translation { Value = SurfacePoint }); } - private void UpdateDecorations() + /// + /// Get the Rich Text name of this tile + /// + /// String Builder containing the name + public virtual StringBuilder GetNameString() + { +#if UNITY_EDITOR + return info.GetNameString().Append(" ").Append(Coords); +#else + return info.GetNameString(); +#endif + } + + /// + /// Get the Rich Text description of this tile + /// + /// + public virtual StringBuilder GetDescriptionString() + { + return new StringBuilder(info.description).AppendLine(); + } + /// + /// Applies the height of the tile to the decorators + /// + private void UpdateDecorationHeight() { if (!_isRendered) return; @@ -134,18 +195,10 @@ private void UpdateDecorations() lastIndex += count; } } - - public virtual void OnRemoved() - { - - BroadcastTileUpdate(TileUpdateType.Removed); - } - - public virtual void OnPlaced() - { - BroadcastTileUpdate(TileUpdateType.Placed); - } - + /// + /// Sends a tile update to neighboring tiles + /// + /// The update type public virtual void BroadcastTileUpdate(TileUpdateType type) { var neighbors = map.GetNeighbors(this); @@ -153,6 +206,9 @@ public virtual void BroadcastTileUpdate(TileUpdateType type) neighbors[i]?.TileUpdated(this, type); } + /// + /// Tile updates enum + /// public enum TileUpdateType { Placed, @@ -160,24 +216,18 @@ public enum TileUpdateType Height } + /// + /// Receiver for tile updates + /// + /// The tile that send the update + /// The type of update that occured public virtual void TileUpdated(Tile src, TileUpdateType updateType) { } - - public virtual void Destroy() - { - if(World.DefaultGameObjectInjectionWorld != null) - { - Map.EM.DestroyEntity(_tileEntity); - if(_decor.IsCreated) - Map.EM.DestroyEntity(_decor); - } - if (_decor.IsCreated) - { - _decor.Dispose(); - } - } - + /// + /// Sets the visibility of this tile + /// + /// Whether or not the tile should be shown public void Show(bool isShown) { IsShown = isShown; @@ -187,30 +237,25 @@ public void Show(bool isShown) OnHide(); } + /// + /// Callback for when the tile marked as visible + /// public virtual void OnShow() { Map.EM.RemoveComponent(_decor, typeof(DisableRendering)); } + /// + /// Callback for when the tile is marked as not visible + /// public virtual void OnHide() { Map.EM.AddComponent(_decor, typeof(DisableRendering)); } - - - public virtual Entity Render() - { - IsShown = true; - _isRendered = true; - _tileEntity = MeshEntity.Instantiate(Coords, Height); - return _tileEntity; - } - - public virtual void Start() - { - _isInit = true; - } - + /// + /// Convert this tile to a SerializedTile + /// + /// A serilaized version of this tile, contains all the information needed to recreate this tile's current state public SerializedTile Serialize() { var t = new SerializedTile @@ -226,31 +271,65 @@ public SerializedTile Serialize() return t; } + /// + /// Called during to serialization process to write additional data + /// + /// Dictionary to write tile data to public virtual void OnSerialize(Dictionary tileData) { } + /// + /// Called as the tile is deserialized to read additional data + /// + /// Dictionary containg the data to be read public virtual void OnDeSerialized(Dictionary tileData) { } + /// + /// Callback for when this tile is being removed from the map + /// + public virtual void OnRemoved() + { - public virtual void RenderDecorators() + BroadcastTileUpdate(TileUpdateType.Removed); + } + + /// + /// Destorys all entities associated with this tile and cleans up allocated memory + /// + public virtual void Destroy() { - if (info.decorators.Length == 0) - return; - if (_decorRendered) - return; - _decorRendered = true; - _decor = new NativeArray(info.decorators.Sum(t => t.GetDecorEntityCount(this)), Allocator.Persistent, NativeArrayOptions.UninitializedMemory); - int lastIndex = 0; - for (int i = 0; i < info.decorators.Length; i++) + if(World.DefaultGameObjectInjectionWorld != null) { - var count = info.decorators[i].GetDecorEntityCount(this); - info.decorators[i].Render(this, _decor.Slice(lastIndex, count)); - lastIndex += count; + Map.EM.DestroyEntity(_tileEntity); + if(_decor.IsCreated) + Map.EM.DestroyEntity(_decor); + } + if (_decor.IsCreated) + { + _decor.Dispose(); } } + + // override object.Equals + public override bool Equals(object obj) + { + if (obj == null || GetType() != obj.GetType()) + { + return false; + } + + var t = (Tile)obj; + return (t.Coords == Coords); + } + + // override object.GetHashCode + public override int GetHashCode() + { + return Coords.GetHashCode(); + } } } \ No newline at end of file