File size: 9,185 Bytes
3497d64 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ConstructionManager : MonoBehaviour
{
public static ConstructionManager Instance { get; set; }
public GameObject itemToBeConstructed;
public bool inConstructionMode = false;
public GameObject constructionHoldingSpot;
public bool isValidPlacement;
public bool selectingAGhost;
public GameObject selectedGhost;
// Materials we store as refereces for the ghosts
public Material ghostSelectedMat;
public Material ghostSemiTransparentMat;
public Material ghostFullTransparentMat;
public GameObject itemToBeDestroyed;
// We keep a reference to all ghosts currently in our world,
// so the manager can monitor them for various operations
public List<GameObject> allGhostsInExistence = new List<GameObject>();
private void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject);
}
else
{
Instance = this;
}
}
public void ActivateConstructionPlacement(string itemToConstruct)
{
GameObject item = Instantiate(Resources.Load<GameObject>(itemToConstruct));
//change the name of the gameobject so it will not be (clone)
item.name = itemToConstruct;
item.transform.SetParent(constructionHoldingSpot.transform, false);
itemToBeConstructed = item;
itemToBeConstructed.gameObject.tag = "activeConstructable";
// Disabling the non-trigger collider so our mouse can cast a ray
itemToBeConstructed.GetComponent<Constructable>().solidCollider.enabled = false;
// Activating Construction mode
inConstructionMode = true;
}
private void GetAllGhosts(GameObject itemToBeConstructed)
{
List<GameObject> ghostlist = itemToBeConstructed.gameObject.GetComponent<Constructable>().ghostList;
foreach (GameObject ghost in ghostlist)
{
Debug.Log(ghost);
allGhostsInExistence.Add(ghost);
}
}
private void PerformGhostDeletionScan()
{
foreach (GameObject ghost in allGhostsInExistence)
{
if (ghost != null)
{
if (ghost.GetComponent<GhostItem>().hasSamePosition == false) // if we did not already add a flag
{
foreach (GameObject ghostX in allGhostsInExistence)
{
// First we check that it is not the same object
if (ghost.gameObject != ghostX.gameObject)
{
// If its not the same object but they have the same position
if (XPositionToAccurateFloat(ghost) == XPositionToAccurateFloat(ghostX) && ZPositionToAccurateFloat(ghost) == ZPositionToAccurateFloat(ghostX))
{
if (ghost != null && ghostX != null)
{
// setting the flag
ghostX.GetComponent<GhostItem>().hasSamePosition = true;
break;
}
}
}
}
}
}
}
foreach (GameObject ghost in allGhostsInExistence)
{
if (ghost != null)
{
if (ghost.GetComponent<GhostItem>().hasSamePosition)
{
DestroyImmediate(ghost);
}
}
}
}
private float XPositionToAccurateFloat(GameObject ghost)
{
if (ghost != null)
{
// Turning the position to a 2 decimal rounded float
Vector3 targetPosition = ghost.gameObject.transform.position;
float pos = targetPosition.x;
float xFloat = Mathf.Round(pos * 100f) / 100f;
return xFloat;
}
return 0;
}
private float ZPositionToAccurateFloat(GameObject ghost)
{
if (ghost != null)
{
// Turning the position to a 2 decimal rounded float
Vector3 targetPosition = ghost.gameObject.transform.position;
float pos = targetPosition.z;
float zFloat = Mathf.Round(pos * 100f) / 100f;
return zFloat;
}
return 0;
}
private void Update()
{
if (itemToBeConstructed != null && inConstructionMode)
{
if (CheckValidConstructionPosition())
{
isValidPlacement = true;
itemToBeConstructed.GetComponent<Constructable>().SetValidColor();
}
else
{
isValidPlacement = false;
itemToBeConstructed.GetComponent<Constructable>().SetInvalidColor();
}
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
var selectionTransform = hit.transform;
if (selectionTransform.gameObject.CompareTag("ghost"))
{
itemToBeConstructed.SetActive(false);
selectingAGhost = true;
selectedGhost = selectionTransform.gameObject;
}
else
{
itemToBeConstructed.SetActive(true);
selectingAGhost = false;
}
}
}
// Left Mouse Click to Place item
if (Input.GetMouseButtonDown(0) && inConstructionMode)
{
if (isValidPlacement && selectedGhost == false) // We don't want the freestyle to be triggered when we select a ghost.
{
PlaceItemFreeStyle();
}
if (selectingAGhost)
{
PlaceItemInGhostPosition(selectedGhost);
}
}
// Right Mouse Click to Cancel //TODO - don't destroy the ui item until you actually placed it.
if (Input.GetMouseButtonDown(0) && isValidPlacement)
{ // Left Mouse Button
}
}
private void PlaceItemInGhostPosition(GameObject copyOfGhost)
{
Vector3 ghostPosition = copyOfGhost.transform.position;
Quaternion ghostRotation = copyOfGhost.transform.rotation;
selectedGhost.gameObject.SetActive(false);
// Setting the item to be active again (after we disabled it in the ray cast)
itemToBeConstructed.gameObject.SetActive(true);
// Setting the parent to be the root of our scene
itemToBeConstructed.transform.SetParent(transform.parent.transform.parent, true);
itemToBeConstructed.transform.position = ghostPosition;
itemToBeConstructed.transform.rotation = ghostRotation;
// Making the Ghost Children to no longer be children of this item
itemToBeConstructed.GetComponent<Constructable>().ExtractGhostMembers();
// Setting the default color/material
itemToBeConstructed.GetComponent<Constructable>().SetDefaultColor();
itemToBeConstructed.tag = "placedFoundation";
// Enabling back the solider collider that we disabled earlier
itemToBeConstructed.GetComponent<Constructable>().solidCollider.enabled = true;
//Adding all the ghosts of this item into the manager's ghost bank
GetAllGhosts(itemToBeConstructed);
PerformGhostDeletionScan();
itemToBeConstructed = null;
inConstructionMode = false;
}
private void PlaceItemFreeStyle()
{
// Setting the parent to be the root of our scene
itemToBeConstructed.transform.SetParent(transform.parent.transform.parent, true);
// Making the Ghost Children to no longer be children of this item
itemToBeConstructed.GetComponent<Constructable>().ExtractGhostMembers();
// Setting the default color/material
itemToBeConstructed.GetComponent<Constructable>().SetDefaultColor();
itemToBeConstructed.tag = "placedFoundation";
itemToBeConstructed.GetComponent<Constructable>().enabled = false;
// Enabling back the solider collider that we disabled earlier
itemToBeConstructed.GetComponent<Constructable>().solidCollider.enabled = true;
//Adding all the ghosts of this item into the manager's ghost bank
GetAllGhosts(itemToBeConstructed);
PerformGhostDeletionScan();
itemToBeConstructed = null;
inConstructionMode = false;
}
private bool CheckValidConstructionPosition()
{
if (itemToBeConstructed != null)
{
return itemToBeConstructed.GetComponent<Constructable>().isValidToBeBuilt;
}
return false;
}
void DestroyItem(GameObject item)
{
DestroyImmediate(item);
InventorySystem.Instance.ReCalculateList();
CraftingSystem.Instance.RefreshNeededItems();
}
} |