using System; using UnityEngine; public class InteractionController : MonoBehaviour { private PlayerController playerController; private BlockDictionary blockDictionary; private MachineInteraction machineInteraction; private StorageInteraction storageInteraction; private BlockInteraction blockInteraction; // Called by unity engine on start up to initialize variables public void Start() { playerController = GetComponent(); blockDictionary = new BlockDictionary(playerController); machineInteraction = new MachineInteraction(playerController, this); storageInteraction = new StorageInteraction(playerController, this); blockInteraction = new BlockInteraction(playerController, this); } // Called once per frame by unity engine public void Update() { // Raycast and associated data for interacting with machines and other objects. Transform camPos = Camera.main.gameObject.transform; if (Physics.Raycast(camPos.position, camPos.forward, out playerController.playerLookHit, 40)) { if (playerController.inventoryOpen == false && playerController.escapeMenuOpen == false && playerController.tabletOpen == false) { playerController.objectInSight = playerController.playerLookHit.collider.gameObject; if (!IsResource(playerController.objectInSight)) { if (IsStorageContainer(playerController.objectInSight)) { storageInteraction.InteractWithStorageContainer(); } else if (playerController.objectInSight.GetComponent() != null) { storageInteraction.InteractWithStorageComputer(); } else if (playerController.objectInSight.GetComponent() != null) { machineInteraction.InteractWithPowerSource(); } else if (playerController.objectInSight.GetComponent() != null) { machineInteraction.InteractWithNuclearReactor(); } else if (playerController.objectInSight.GetComponent() != null) { machineInteraction.InteractWithPowerConduit(); } else if (playerController.objectInSight.GetComponent() != null) { machineInteraction.InteractWithTurret(); } else if (playerController.objectInSight.GetComponent() != null) { machineInteraction.InteractWithUniversalExtractor(); } else if (playerController.objectInSight.GetComponent() != null) { machineInteraction.InteractWithAuger(); } else if (playerController.objectInSight.GetComponent() != null) { machineInteraction.InteractWithDarkMatterCollector(); } else if (playerController.objectInSight.GetComponent() != null) { machineInteraction.InteractWithUniversalConduit(); } else if (playerController.objectInSight.GetComponent() != null) { machineInteraction.InteractWithDarkMatterConduit(); } else if (playerController.objectInSight.GetComponent() != null) { machineInteraction.InteractWithSmelter(); } else if (playerController.objectInSight.GetComponent() != null) { machineInteraction.InteractWithAlloySmelter(); } else if (playerController.objectInSight.GetComponent() != null) { machineInteraction.InteractWithExtruder(); } else if (playerController.objectInSight.GetComponent() != null) { machineInteraction.InteractWithRailCartHub(); } else if (playerController.objectInSight.GetComponent() != null) { machineInteraction.InteractWithRetriever(); } else if (playerController.objectInSight.GetComponent() != null) { machineInteraction.InteractWithAutoCrafter(); } else if (playerController.objectInSight.GetComponent() != null) { machineInteraction.InteractWithHeatExchanger(); } else if (playerController.objectInSight.GetComponent() != null) { machineInteraction.InteractWithGearCutter(); } else if (playerController.objectInSight.GetComponent() != null) { machineInteraction.InteractWithPress(); } else if (playerController.objectInSight.GetComponent() != null) { machineInteraction.InteractWithElectricLight(); } else if (playerController.objectInSight.GetComponent() != null) { machineInteraction.InteractWithAirLock(); } else if (playerController.objectInSight.GetComponent() != null) { blockInteraction.InteractWithIronBlock(); } else if (playerController.objectInSight.GetComponent() != null) { blockInteraction.InteractWithSteelBlock(); } else if (playerController.objectInSight.GetComponent() != null) { blockInteraction.InteractWithGlass(); } else if (playerController.objectInSight.GetComponent() != null) { blockInteraction.InteractWithBricks(); } else if (playerController.objectInSight.tag.Equals("CombinedMesh")) { blockInteraction.InteractWithCombinedMesh(); } else { EndInteraction(); } } } } else { EndInteraction(); } } // Returns true if the object in question is a resource node private Boolean IsResource(GameObject obj) { if (obj.GetComponent() != null || obj.GetComponent() != null) { return true; } return false; } // Returns true if the object in question is a storage container private Boolean IsStorageContainer(GameObject obj) { if (obj.GetComponent() != null && obj.GetComponent() == null && obj.GetComponent() == null) { return true; } return false; } // Destroys an object in the world and adds it's associated inventory item to the player's inventory public void CollectObject(string type) { bool spaceAvailable = false; foreach (InventorySlot slot in playerController.playerInventory.inventory) { if (slot.typeInSlot.Equals("nothing") || slot.typeInSlot.Equals(type) && slot.amountInSlot < 1000) { spaceAvailable = true; } } if (spaceAvailable == true) { playerController.playerInventory.AddItem(type, 1); Destroy(playerController.objectInSight); playerController.PlayCraftingSound(); } else { playerController.cannotCollect = true; playerController.PlayMissingItemsSound(); } } // Opens the machine GUI public void OpenMachineGUI() { if (playerController.machineGUIopen == false) { if (playerController.building == true) { GameManager manager = GameObject.Find("GameManager").GetComponent(); if (manager.working == false) { playerController.stoppingBuildCoRoutine = true; manager.meshManager.CombineBlocks(); playerController.separatedBlocks = false; playerController.destroyTimer = 0; playerController.buildTimer = 0; playerController.building = false; playerController.destroying = false; } else { playerController.requestedBuildingStop = true; } } Cursor.visible = true; Cursor.lockState = CursorLockMode.None; playerController.storageGUIopen = false; playerController.craftingGUIopen = false; playerController.inventoryOpen = false; playerController.machineGUIopen = true; } else { Cursor.visible = false; Cursor.lockState = CursorLockMode.Locked; playerController.inventoryOpen = false; playerController.craftingGUIopen = false; playerController.storageGUIopen = false; playerController.machineGUIopen = false; } } // Called when the player is no longer looking at any interactive objects private void EndInteraction() { if (playerController.machineGUIopen == true) { playerController.machineGUIopen = false; } playerController.lookingAtCombinedMesh = false; playerController.objectInSight = null; playerController.machineInSight = null; playerController.machineInputID = "none"; playerController.machineOutputID = "none"; playerController.machineType = "none"; playerController.machineAmount = 0; playerController.machineInputAmount = 0; playerController.machineOutputAmount = 0; } }