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

Commit

Permalink
Code documentation and fixed and issue that prevented meta tiles from…
Browse files Browse the repository at this point in the history
… correctly initializing their connection to the HQ
  • Loading branch information
Amatsugu committed Feb 16, 2021
1 parent 4f0456f commit f30cc73
Show file tree
Hide file tree
Showing 4 changed files with 294 additions and 106 deletions.
102 changes: 87 additions & 15 deletions Assets/Scripts/Map/Tiles/MappedTiles/Buildings/BuildingTile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public BuildingTile(HexCoords coords, float height, Map map, BuildingTileEntity
metaTiles = new MetaTile[tInfo.footprint.footprint.Length - 1];
}

/// <summary>
/// Gets the Rich Text string representing information about the production of this building
/// </summary>
/// <returns>String Builder</returns>
public StringBuilder GetProductionString()
{
if (_productionData.resourceIds == null)
Expand Down Expand Up @@ -80,20 +84,24 @@ public StringBuilder GetProductionString()
return sb;
}

/// <summary>
/// The TileEntity that contains information about the underlying tile
/// </summary>
public override TileEntity MeshEntity => buildingInfo.preserveGroundTile ? originalTile : buildingInfo;

/// <summary>
/// Gets the rotation of the building on this tile
/// </summary>
/// <returns>Quaternion of the building's rotation</returns>
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 });
}

/// <summary>
/// Gets the resources that this building will return when deconstructed
/// </summary>
/// <returns>Array of resource Identifiers</returns>
public ResourceIndentifier[] GetResourceRefund()
{
var res = new ResourceIndentifier[buildingInfo.cost.Length];
Expand All @@ -104,6 +112,7 @@ public ResourceIndentifier[] GetResourceRefund()
return res;
}


public override void OnHeightChanged()
{
base.OnHeightChanged();
Expand All @@ -123,8 +132,12 @@ public override void OnPlaced()
{
base.OnPlaced();
StartConstruction();
CreateMetaTiles();
}

/// <summary>
/// Start the construction animations for this building
/// </summary>
protected virtual void StartConstruction()
{
if (buildingInfo.constructionMesh != null)
Expand All @@ -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()
Expand Down Expand Up @@ -187,6 +199,9 @@ public override void OnShow()
Map.EM.RemoveComponent(subMeshes, typeof(DisableRendering));
}

/// <summary>
/// Completes the build phase of this building
/// </summary>
public void Build()
{
if (isBuilt)
Expand All @@ -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);
}

/// <summary>
/// Callback for when this build phase completes
/// </summary>
protected virtual void OnBuilt()
{
NotificationsUI.NotifyWithTarget(NotifType.Info, $"Construction Complete: {buildingInfo.GetNameString()}", Coords);
}

/// <summary>
/// Instantiates the building entities for this tile
/// </summary>
public virtual void RenderBuilding()
{
if (buildingInfo.buildingMesh.mesh == null)
Expand All @@ -229,15 +257,22 @@ public override void Start()
if (!isBuilt)
return;
ApplyTileProperites();
ApplyBonuses();
ApplyAdjacencyBonuses();
ApplyBuffs();
}

/// <summary>
/// Instantiate the submesses for this building tile
/// </summary>
/// <param name="rot">The rotation of the building</param>
public virtual void RenderSubMeshes(quaternion rot)
{
subMeshes = buildingInfo.buildingMesh.InstantiateSubMeshes(rot, _building);
}

/// <summary>
/// Replace tiles in this building's footprint with meta tiles
/// </summary>
public virtual void CreateMetaTiles()
{
if (buildingInfo.useMetaTiles)
Expand All @@ -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;
}
}
}

/// <summary>
/// Gets the DOTS Entity for the building on this tile
/// </summary>
/// <returns>Entity</returns>
protected virtual Entity GetBuildingEntity()
{
return buildingInfo.buildingMesh.mesh != null ? _building : _tileEntity;
}

/// <summary>
/// Applies component data to entities on this building
/// </summary>
protected virtual void ApplyTileProperites()
{
var entity = GetBuildingEntity();
Expand Down Expand Up @@ -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()
/// <summary>
/// Applies adjanceny bonuses to neighboring tiles
/// </summary>
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<Entity>(6 * 3, Allocator.Persistent);
Expand All @@ -346,6 +389,11 @@ protected virtual void ApplyBonuses()
}
}

/// <summary>
/// Add and apply a buff to this tile
/// </summary>
/// <param name="src">The source tile for the buff</param>
/// <param name="buff">The buff to apply</param>
public virtual void AddBuff(HexCoords src, StatsBuffs buff)
{
if (buffSources.ContainsKey(src))
Expand All @@ -359,6 +407,10 @@ public virtual void AddBuff(HexCoords src, StatsBuffs buff)
ApplyBuffs();
}

/// <summary>
/// Remove a previously applied buff from this tile
/// </summary>
/// <param name="src">The tile that applied the buff</param>
public virtual void RemoveBuff(HexCoords src)
{
if (buffSources.ContainsKey(src))
Expand All @@ -369,6 +421,9 @@ public virtual void RemoveBuff(HexCoords src)
}
}

/// <summary>
/// Applies the current set of buffs to this tile
/// </summary>
protected virtual void ApplyBuffs()
{
if (!isBuilt || !_isRendered)
Expand All @@ -391,6 +446,9 @@ protected virtual void ApplyBuffs()
Map.EM.SetComponentData(e, curHealth);
}

/// <summary>
/// Deconstruct this building, reverting it to it's original tile
/// </summary>
public virtual void Deconstruct()
{
if (buildingInfo.useMetaTiles)
Expand All @@ -401,6 +459,11 @@ public virtual void Deconstruct()
map.RevertTile(this);
}

/// <summary>
/// Can this tile be deconstructed by the provided faction
/// </summary>
/// <param name="faction">The faction attempting to deconstruct this tile</param>
/// <returns></returns>
public virtual bool CanDeconstruct(Faction faction) => buildingInfo.faction == faction;

public override void OnSerialize(Dictionary<string, string> tileData)
Expand All @@ -417,12 +480,18 @@ public override void OnDeSerialized(Dictionary<string, string> tileData)
base.OnDeSerialized(tileData);
}

/// <summary>
/// Kills this bulding, causing it to be destroyed
/// </summary>
public void Die()
{
OnDeath();
map.ReplaceTile(this, buildingInfo.customDeathTile ? buildingInfo.deathTile : originalTile);
}

/// <summary>
/// Callback for when this building dies
/// </summary>
public virtual void OnDeath()
{
NotificationsUI.NotifyWithTarget(NotifType.Warning, $"Building Destroyed: {buildingInfo.GetNameString()}", Coords);
Expand All @@ -436,6 +505,9 @@ public override void Destroy()
DestroyBuilding();
}

/// <summary>
/// Destorys the entities associated with this building and frees memory
/// </summary>
protected virtual void DestroyBuilding()
{
if (World.DefaultGameObjectInjectionWorld != null)
Expand Down
35 changes: 25 additions & 10 deletions Assets/Scripts/Map/Tiles/MappedTiles/Buildings/MetaTile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -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();
}

Expand Down Expand Up @@ -114,7 +129,7 @@ public override void OnPlaced()

}

protected override void ApplyBonuses()
protected override void ApplyAdjacencyBonuses()
{

}
Expand Down Expand Up @@ -145,7 +160,7 @@ public override void OnDeSerialized(Dictionary<string, string> tileData)
ParentTile = map[coord] as BuildingTile;
if (ParentTile is PoweredBuildingTile powered)
{
_isPowered = true;
_isPoweredTile = true;
_poweredParent = powered;
}
}
Expand Down
Loading

0 comments on commit f30cc73

Please sign in to comment.