//On start, push all information up to WorldData2 void Start() { foreach (plantInfo plant in plants) { WorldData2.CreateAndAddPlant(plant.plantName, plant.plantObject); } }
private IEnumerator PickupDelay(Item2 i, int ID) { yield return(new WaitForSeconds(0.3f)); //For plant pickups/ dynamically spawned in stuff WorldData2.RemoveItemsWithID(ID); //for everything else if (i != null) { //Destroy(i.transform.gameObject); i.transform.position = new Vector3(-500, 0, 0); } }
private void RpcSetBearTrap(GameObject trap, string userTag) { if (trap == null) { Debug.Log("Trap is null"); } BearTrap bearTrap = trap.GetComponent <BearTrap>(); bearTrap.trapOwnerTag = userTag; WorldData2.AddTrap(bearTrap); if (userTag == PlayerData2.localPlayer.tag) { ItemUsed(); } else { // For the other player, disable collider that prevents additional traps from being placed nearby trap.transform.GetChild(0).GetComponent <CircleCollider2D>().enabled = false; } }
private void RpcPlantSeed(Vector3Int location, string plantName, float growthRate) { Debug.Log("Have plant growth of: " + growthRate); WorldData2.AddPlantedLocation(location, plantName, growthRate); }
public void UseSelectedItem(Vector2 location) { if (selectedSlot.Count != 0) { Item2 item = selectedSlot.First.Value; Vector3Int tileCoord = WorldData2.p1DiggableLayer.WorldToCell(location); //Get cell location // if item is a seed it adds a tile based on the editor if (item.is_seed) { if ((WorldData2.p1DiggableLayer.GetTile(tileCoord) == null && WorldData2.p2DiggableLayer.GetTile(tileCoord) == null) && (WorldData2.plantableLayer.GetTile(tileCoord) != null) && (WorldData2.CheckPlantedLocation(tileCoord))) { float growthRate = PlayerData2.localGrowSpeed; //Extract vegetable name by taking off last 5 characters (" seed") string vegetableString = item.itemName.Substring(0, item.itemName.Length - 5); // Place item in middle of cell, track planted location. All handled by world data bool plantAttempt = WorldData2.AddPlantedLocation(tileCoord, vegetableString, growthRate); //On succesful plant, tell other clients to add a plant at that location as well if (plantAttempt) { SoundControl.PlayPlantSound(); Debug.Log("here"); //Tell others to add the plant Debug.Log("Sending through growth rate of " + growthRate.ToString()); CmdPlantSeed(tileCoord, vegetableString, growthRate); ItemUsed(); } } } else { switch (item.itemName) { case "Shovel": // locally update our tile WorldData2.p1DiggableLayer.SetTile(tileCoord, null); WorldData2.p2DiggableLayer.SetTile(tileCoord, null); SoundControl.PlayShovelSound(); // tell the server to tell other clients about our click if (isServer) { RpcSetTile(tileCoord); } else { CmdSetTile(tileCoord); } break; case "BearTrap": //selectedItemObject = selectedItem.actionPrefab; /* if (isServer) * RpcSetBearTrap(location, gameObject.tag); * else*/ CmdSetBearTrap(location, gameObject.tag); break; } } } }
private void Update() { // only listen for clicks on our own game object. if (!hasAuthority) { return; } // highlight interactable at mouseover if in range to grab if (highlightedInteractable != null) { if (canInteract) { highlightedInteractable.GetComponent <SpriteRenderer>().color = new Color(1, 1, 1, 0.5f); } else { highlightedInteractable.GetComponent <SpriteRenderer>().color = new Color(1, 1, 1, 1); } } // Get mouse coordinates (for highlight tile) Vector2 mousePos = Input.mousePosition; Vector2 mouseWorldPos = Camera.main.ScreenToWorldPoint(mousePos); Vector3Int tileCoordinate = WorldData2.highlighter.WorldToCell(mouseWorldPos); if (Vector2.Distance(gameObject.transform.position - new Vector3(0, 0.4f, 0), mouseWorldPos) < interactionRange) { canInteract = true; } else { canInteract = false; } // If placable item is selected form inventory, draw item at cursor if (inventory.selectedSlot.First != null && inventory.selectedSlot.First.Value.itemName == "BearTrap") { WorldData2.highlighter.SetTile(tileCoordinate, null); WorldData2.highlighter.SetTile(previousTileCoordinate, null); DrawItemAtCursor(mouseWorldPos); } // Highlight tile at mouseover else { Color color = selectedItemSR.material.color; color.a = 0; selectedItemSR.material.color = color; CursorHighlight(tileCoordinate); // Cursor disappears when out of range } //Scroll to change items if (Input.mouseScrollDelta.y < 0) { if (oldSlotNumber < 4) { SetSlot(slotNames[oldSlotNumber + 1], oldSlotNumber + 1); } else { SetSlot(slotNames[0], 0); } } if (Input.mouseScrollDelta.y > 0) { if (oldSlotNumber > 0) { SetSlot(slotNames[oldSlotNumber - 1], oldSlotNumber - 1); } else { SetSlot(slotNames[4], 4); } } if (Input.GetKeyDown(KeyCode.R)) { inventory.DropItem(); } // Change Item Cursor with keys 1-5 if (Input.GetKeyDown(KeyCode.Alpha1)) { SetSlot("Slot1UI", 0); } if (Input.GetKeyDown(KeyCode.Alpha2)) { SetSlot("Slot2UI", 1); } if (Input.GetKeyDown(KeyCode.Alpha3)) { SetSlot("Slot3UI", 2); } if (Input.GetKeyDown(KeyCode.Alpha4)) { SetSlot("Slot4UI", 3); } if (Input.GetKeyDown(KeyCode.Alpha5)) { SetSlot("Slot5UI", 4); } // Interact if (Input.GetMouseButtonDown(0) && (canInteract || canInteractWithTile)) { ShopSystem shop; // used to store shop script if exists on interactable // Check if interactable object was clicked if (highlightedInteractable != null) { // Check if interactable object is an item if (highlightedInteractable.GetComponent <Item2>() != null) { highlightedInteractable.gameObject.GetComponent <SpriteRenderer>().color = new Color(1, 1, 1, 1); Item2 itemToAdd = highlightedInteractable.GetComponent <Item2>(); highlightedInteractable = null; bool couldAdd = inventory.AddItem(itemToAdd); if (couldAdd) { SoundControl.PlayHarvestSound(); //Remove the pickup and planted location from user and all other clients int removedID = WorldData2.RemovePlantedLocation(WorldData2.p1DiggableLayer.WorldToCell(mouseWorldPos)); if (itemToAdd.gameObject.GetComponent <plantID>() != null) { removedID = itemToAdd.gameObject.GetComponent <plantID>().ID; } CmdAddItem(itemToAdd, removedID); } } // Check if interactable object is a seed bin else if (highlightedInteractable.GetComponent <SeedBin>() != null) { SoundControl.PlayButtonSound(); inventory.AddItem(highlightedInteractable.GetComponent <SeedBin>().seed); } // Check if interactable object is a sell bin else if (highlightedInteractable.GetComponent <SellingBin>() != null) { inventory.SellItem(); } // Check if interactable object is the shop else if ((shop = highlightedInteractable.GetComponent <ShopSystem>()) != null) { shop.OpenShopWindow(); } // Check if interactable object is a trap else if (highlightedInteractable.GetComponent <BearTrap>() != null)// && highlightedInteractable.GetComponent<BearTrap>().trapOwnerTag != PlayerData2.localPlayer.tag) { BearTrap bt = highlightedInteractable.GetComponent <BearTrap>(); if (isServer) { RpcDamageTrap(bt.id); } else { CmdDamageTrap(bt.id); } //highlightedInteractable.GetComponent<BearTrap>().Damage(); } } // Use item else if (inventory.selectedSlot.First != null) { Debug.Log("Using Item"); if (inventory.selectedSlot.First.Value.itemName == "BearTrap" && canInteract) { if (canPlaceItem) { inventory.UseSelectedItem(mouseWorldPos); } } else // if item interacts with tiles (e.g. shovel and seeds) if (canInteractWithTile) { inventory.UseSelectedItem(mouseWorldPos); } } // No actions else { // Can test new functions here, Activates when empty item slot is selected when click in radius occurs Debug.Log("false"); } } }