Skip to content

Commit

Permalink
Fixed placing more than one tower on a single cell
Browse files Browse the repository at this point in the history
  • Loading branch information
ddabble committed Nov 24, 2021
1 parent 80b15cb commit 930e6e3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
3 changes: 1 addition & 2 deletions Assets/Prefabs/Terrain/HexCell.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,6 @@ MonoBehaviour:
coordinates:
x: 0
z: 0
isOccupied: 0
occupier: {fileID: 0}
mesh: {fileID: 816763163768567949}
pertubValue: 0.63529414
materials:
Expand All @@ -145,3 +143,4 @@ MonoBehaviour:
- {fileID: 0}
- {fileID: 0}
- {fileID: 0}
_occupyingObject: {fileID: 0}
2 changes: 1 addition & 1 deletion Assets/Scripts/Player/PlayerStateController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ private void OnInteract()

focusedInteractable.Interact(this); // interact with the current target

if (CurrentState == PlayerStates.BUILDING)
if (CurrentState == PlayerStates.BUILDING && !targetCell.IsOccupied)
{
// Build the turret we are holding
focusedInteractable.GetComponent<TurretPrefabConstruction>().Construct(targetCell);
Expand Down
39 changes: 29 additions & 10 deletions Assets/Scripts/Terrain/HexCell.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections;
using UnityEngine;
using Unity.Mathematics;
Expand All @@ -7,10 +8,6 @@ public class HexCell : MonoBehaviour {

public HexCoordinates coordinates;

// Is an object currently occupying (is placed on) the HexCell(tm)? This bit of code has the answers!
public bool isOccupied;
public GameObject occupier;

[SerializeField]
private MeshRenderer mesh;
//public RectTransform uiRect;
Expand Down Expand Up @@ -50,6 +47,34 @@ public Vector3 Position
[SerializeField]
HexCell[] neighbors;

// Is an object currently occupying (is placed on) the HexCell(tm)? This bit of code has the answers!
[SerializeField]
private GameObject _occupyingObject;

public GameObject OccupyingObject
{
get => _occupyingObject;
set
{
if (value && IsOccupied)
{
Destroy(value);
throw new ArgumentException(
$"Cannot place {value} on cell at {coordinates}, as it's already occupied by {_occupyingObject}!"
);
}
_occupyingObject = value;
}
}

/// <returns>
/// Whether this cell has a game object placed on it or not.
/// <br/><br/>
/// (Destroying a game object that was occupying this cell, will make this property return `true`
/// - without having to change the value of `OccupyingObject`.)
/// </returns>
public bool IsOccupied => OccupyingObject;

private Transform hq;

void Awake()
Expand Down Expand Up @@ -86,10 +111,4 @@ public HexEdgeType GetEdgeType(HexCell otherCell)
{
return HexMetrics.GetEdgeType(elevation, otherCell.elevation);
}

public void SetTower(GameObject tower)
{
isOccupied = true;
occupier = tower;
}
}
4 changes: 2 additions & 2 deletions Assets/Scripts/Towers/TurretPrefabConstruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public class TurretPrefabConstruction : Interactable

public void Construct(HexCell targetCell)
{
GameObject t = Instantiate(tower, targetCell.transform.position, Quaternion.identity);
targetCell.SetTower(t);
GameObject t = Instantiate(tower, targetCell.transform.position, tower.transform.rotation);
targetCell.OccupyingObject = t;
Destroy(gameObject);
}

Expand Down

0 comments on commit 930e6e3

Please sign in to comment.