private void RayToSpawnPos() { Ray myRay; RaycastHit hitInfo; if (Application.platform == RuntimePlatform.Android) { myRay = Camera.main.ScreenPointToRay(Input.GetTouch(0).position); } else if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer) { myRay = Camera.main.ScreenPointToRay(Input.mousePosition); } else { myRay = Camera.main.ScreenPointToRay(Input.mousePosition); } if (Physics.Raycast(myRay, out hitInfo, Mathf.Infinity, whatCanBeTouched)) { //If ray is within the allowed building area, spawn object if (hitInfo.collider.CompareTag("BuildingArea")) { SpawnItem(hitInfo.point); mainInv.TakeItem(invPosition); itemName = "NULL"; SetSprite(); } } }
public void Invoke() { switch (actionType) { case ItemActionType.Take: _inventoryManager.TakeItem(new InventoryItem() { Name = itemName }); GlobalInstanceManager.GetMainCharacter().GetComponent <MessageManager>() .ShowMessage(youFoundText); StartCoroutine(FadeOutItem()); break; case ItemActionType.Drop: _inventoryManager.DropItem(itemName); break; default: return; } }
public void SwapTile(int x, int y) { // Function used when you want to break tile in the world invManager = GameObject.FindGameObjectWithTag("Player").GetComponent <InventoryManager>(); characterMgr = GameObject.FindGameObjectWithTag("Player").GetComponent <CharacterManager>(); InventorySlot currentSlot = invManager.inventory[invManager.selected]; if (properties[x][y].listType == 1) { // Is it a breakable tile? If so, turn it into that mMap[x][y].GetComponent <AudioSource>().Play(); Destroy(mMap[x][y].child.gameObject); mMap[x][y].replacement.transform.localScale += new Vector3(1.0f, 1.0f, 1.0f); properties[x][y].listType = 3; // 3 is collectible } else if (properties[x][y].listType == 3) { // Decide what kinda collectible it turns into InventorySlot inv = new Empty(); if (properties[x][y].tileType < 4) { inv = new Wood(); } else if (properties[x][y].tileType < 8) { inv = new BigWood(); } else if (properties[x][y].tileType < 14) { inv = new Dirt(); } else if (properties[x][y].tileType < 20) { inv = new Stone(); } else if (properties[x][y].tileType < 21) { inv = new Grass(); } inv.SetValues(ref inv); // necessary to add it to the list bool failed = false; // Checks if it failed to add // Adds the item to the inventory through the manager invManager.AddItem(inv, ref failed); if (!failed) { // Turn it into travesable land Destroy(mMap[x][y].replacement.gameObject); properties[x][y].listType = 0; properties[x][y].tileType = 0; mMap[x][y].IsAccessible = true; } } else if (properties[x][y].listType == 0 && currentSlot.placeable) { // If you're clicking on flat land and there's nothing to break, place down whatever is in your inventory if (invManager.TakeItem(currentSlot.itemType, 1, invManager.selected)) { // If you can place it down then do it, otherwise carry on mMap[x][y].child = PlacedTiles[currentSlot.placeIndex].transform; Instantiate(PlacedTiles[currentSlot.placeIndex].transform, mMap[x][y].transform); properties[x][y].listType = 4; properties[x][y].tileType = currentSlot.placeIndex; mMap[x][y].IsAccessible = false; } } }
private void Update() { // Check to see if the player has clicked a tile and if they have, try to find a path to that // tile. If we find a path then the character will move along it to the clicked tile. if (characterMgr.health < 0 && Input.GetMouseButtonDown(0)) { ShowSettings(); } if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject()) { // Don't move and stuff when the UI is being clicked Ray screenClick = MainCamera.ScreenPointToRay(Input.mousePosition); InventorySlot current = inventoryMgr.inventory[inventoryMgr.selected]; int hits = Physics.RaycastNonAlloc(screenClick, mRaycastHits); if (hits > 0) { EnvironmentTile tile = mRaycastHits[0].transform.GetComponent <EnvironmentTile>(); tempID = new Vector2Int(tile.xID, tile.yID); if (tile != null) { Environment tileProperties = tile.GetComponentInParent <Environment>(); if (current.itemType == 4 && tile.occupant == true && characterMgr.health < characterMgr.GetMaxHP()) { // Eat grass for health tile.occupant.GetComponent <CharacterManager>().TakeDamage(-1); inventoryMgr.TakeItem(4, 1, inventoryMgr.selected); } if (CheckPositions(mCharacter.CurrentPosition, tile)) { // Is he near the tile that needs breaking if (tileProperties.properties[tile.xID][tile.yID].listType != 2 && tileProperties.properties[tile.xID][tile.yID].listType != 4 && (current.itemType < 10 || current.itemType == 15 || current.placeable)) { // Check if the tile is actually breakable, and you're not holding a tool (Hammer doesn't count) if (tileProperties.properties[tile.xID][tile.yID].listType == 1) { // Tiles that need time to break start a timer InventorySlot item = inventoryMgr.SearchItem(tileProperties.properties[tile.xID][tile.yID].tileType, 0); if (current.itemType != 15) { gameObject.AddComponent <Timer>().timeLimit = tile.hardness; } else { gameObject.AddComponent <Timer>().timeLimit = tile.hardness / item.power; } mCharacter.GetComponent <Animator>().SetBool("pickup", true); characterMgr.characterState = 2; // Start working AudioSource audio = GameObject.FindGameObjectWithTag("Player").GetComponent <AudioSource>(); audio.Play(); } else if (tileProperties.properties[tile.xID][tile.yID].listType == 3) { // Otherwise they get collected instantly tileProperties.SwapTile(tempID.x, tempID.y); // Pick up the tile characterMgr.characterState = 0; // Set character back to idle } } if (tileProperties.properties[tile.xID][tile.yID].listType == 4 && tileProperties.properties[tile.xID][tile.yID].tileType == 1) { GameObject.FindGameObjectWithTag("Player").AddComponent <HealTimer>().StartClock(1.0f, 5); tileProperties.properties[tile.xID][tile.yID].listType = 3; } } if (tileProperties.properties[tile.xID][tile.yID].listType == 0) { // Just walk if it's open land if (inventoryMgr.inventory[inventoryMgr.selected].placeable) { if (CheckPositions(mCharacter.CurrentPosition, tile)) { tileProperties.SwapTile(tempID.x, tempID.y); // Replace the tile characterMgr.characterState = 0; // Character is no longer busy } } else if (current.itemType >= 10) { if (tile.occupant != null && CheckPositions(mCharacter.CurrentPosition, tile)) { mMap.DamageOccupant(tile, 1); characterMgr.characterState = 2; // Character is hitting something } } List <EnvironmentTile> route = mMap.Solve(mCharacter.CurrentPosition, tile); mCharacter.GoTo(route); mCharacter.GetComponent <Animator>().SetBool("walking", true); } else if (tileProperties.properties[tile.xID][tile.yID].listType == 4) { tileProperties.SwapTile(tempID.x, tempID.y); characterMgr.characterState = 0; // Character is no longer busy } } } } if (Input.GetMouseButtonDown(1) && !EventSystem.current.IsPointerOverGameObject(1)) { inventoryMgr.UseHand(); } if (GetComponent <Timer>() == null && characterMgr.characterState == 2) { // When the timer is up, break the tile GetComponentInChildren <Environment>().SwapTile(tempID.x, tempID.y); // Replace the tile mCharacter.GetComponent <Animator>().SetBool("pickup", false); // Stop the pickup animation characterMgr.characterState = 0; // Character is no longer busy } if (Input.GetAxis("Mouse ScrollWheel") < 0) { // Scroll backwards through the inventory inventoryMgr.SelectSlot(-1); } else if (Input.GetAxis("Mouse ScrollWheel") > 0) { // Scroll forwards through the inventory inventoryMgr.SelectSlot(1); } if (invasion && GetComponentInChildren <Timer>() == null) { GetComponentInChildren <Environment>().DoomsdaySpawner(); GetComponentsInChildren <CharacterManager>(); } //if (inventoryMgr.inventory[inventoryMgr.selected].itemType >= 10) //{ // Displayer the item in the players hand // inventoryMgr.holding.HoldItem(); //} }
public void OnItemClick() { inventoryManager.TakeItem(inventorySlot); }