/* * Check in front of Beetle and make appropriate changes in response to what is there * returns: true if we changed to a new state, false otherwise */ private bool CheckInFront() { Vector3 facingDirection = AI.DirectionToVector2(direction); // Check to see if we're going to hit any non-trigger colliers in front of us RaycastHit2D[] hits = Physics2D.LinecastAll(transform.position, transform.position + facingDirection, AI.NonPlayerOrEnemyLayermask); foreach (RaycastHit2D hit in hits) { if (hit.collider.isTrigger) { // Check to see if this is a plant that we can eat PlantableZone plantableZone = hit.collider.GetComponentInParent <PlantableZone>(); if (plantableZone != null && plantableZone.IsPlanted()) { SetMotionState(MoveState.EATING); targetPlantZone = plantableZone; return(true); } } else { // We've hit something that isn't a Player or an Enemy or a trigger ChangeDirection(); return(false); } } Debug.DrawLine(transform.position, transform.position + facingDirection); /* * // Assume that we're about to fall off of a cliff in front of ourselves and do a * // check to prove that false. * // This block is basically the difference between green shell and red shell koopas in SMB. * hits = Physics2D.LinecastAll(transform.position + facingDirection, transform.position + facingDirection + Vector3.down, AI.NonPlayerOrEnemyLayermask); * bool nearCliff = true; * foreach (RaycastHit2D hit in hits) { * // Ignore triggers * if (!hit.collider.isTrigger) { * // Okay, we've found solid ground, we're good here * nearCliff = false; * break; * } * } * Debug.DrawLine(transform.position + facingDirection, transform.position + facingDirection + Vector3.down, Color.red); * if (nearCliff) { * return true; * } */ // Otherwise check to see if we've exceeded our max roaming distance float distanceToTarget = Mathf.Abs(transform.position.x - targetXValue); if ((direction == AI.Direction.LEFT && transform.position.x < targetXValue || direction == AI.Direction.RIGHT && transform.position.x > targetXValue) || distanceToTarget > MAX_ROAM_DISTANCE) { ChangeDirection(); } return(false); }
internal void UseTool(Item.Tool toolType) { PlantableZone currentPlantableZone = player.GetAvailablePlantableZone(); switch (toolType) { case Item.Tool.Axe: if (currentPlantableZone != null) { if (currentPlantableZone.IsPlanted()) { currentPlantableZone.Chop(); } } else { // Swiping at empty space with axe } break; case Item.Tool.Shovel: if (currentPlantableZone != null) { // can only dig up empty dirt patch if (!currentPlantableZone.IsPlanted()) { Destroy((currentPlantableZone as MonoBehaviour).gameObject); // reworked the InvetorySystem to track a dictionary of items from enums, this is so much nicer now PickupItem(resourceItemMap[Item.Resource.Dirt]); // seriously look at how gross this used to be // PickupItem(((GameObject)Resources.Load("PlantPrefabs/ItemDirtPatch", typeof(GameObject))).GetComponent<Item>()); } } else { // Swiping at empty space with shovel } break; // handle other tools here } }
private void Update() { if (toolUsageCooldownTimer > 0f) { toolUsageCooldownTimer -= Time.deltaTime; } bool useItemPressed = Input.GetButton("Fire1"); bool useItemSpecialPressed = Input.GetButton("SpecialAttack"); bool waterButtonPressed = Input.GetButton("Fire2"); int indexChange = 0; indexChange = Mathf.RoundToInt(10f * Input.GetAxis("Mouse ScrollWheel")); indexChange += Input.GetButtonDown("NavLeft")? -1 : 0; indexChange += Input.GetButtonDown("NavRight") ? 1 : 0; if (indexChange != 0) { SetSelectedItemIndex(selectedItemIndex += indexChange); } if (useItemPressed) { InventorySlot currentItem = inventorySlots[selectedItemIndex]; // Right now all we can do is use items, so nothing left to do. if (currentItem.IsEmpty()) { return; } // Check to see if we have a variety of things and if we can use them Growable plantableSeed = currentItem.GetGamePrefab().GetComponent <Growable>(); DirtPatch dirtPatch = currentItem.GetGamePrefab().GetComponent <DirtPatch>(); PlantableZone plantableZone = player.GetAvailablePlantableZone(); // Planting a seed if (plantableSeed != null) { ItemSeed seed = (ItemSeed)currentItem.GetItem(); if (plantableZone != null && !plantableZone.IsPlanted()) { plantableZone.PlantSeed(seed); currentItem.Use(); } } // Placing dirt on the ground else if (dirtPatch != null) { if (plantableZone == null && player.OnPlantableGround()) { GameObject dirt = Instantiate(dirtPatch.gameObject); // Assign the dirt patch to be parented to whatever the player is standing on // This allows us to recursively destroy plants after we plant on top of them // (e.g. dirt pile on a leaf platform. Destroy bottom plant, it destroys the rest) Transform parent = player.GetObjectBelow().transform; dirt.transform.parent = parent; // Place this dirt roughly on the ground dirt.transform.position = player.transform.position + Vector3.down * 0.5f; currentItem.Use(); } } else { currentItem.Use(); } } if (useItemSpecialPressed) { InventorySlot currentItem = inventorySlots[selectedItemIndex]; // Make sure we have something equipped. if (currentItem.IsEmpty()) { return; } currentItem.UseSpecial(); } if (waterButtonPressed) { PlantableZone plantableZone = player.GetAvailablePlantableZone(); if (plantableZone != null && plantableZone.CanBeWatered()) { GameObject target = (plantableZone as MonoBehaviour).gameObject; if (waterLevel > 0) { if (!waterSprite.PlanningToVisit(target)) { // Everything that we have that implements interfaces is also a MonoBehavior, so we can // use this as a """safe""" cast in order to find the game object // The water sprite reaching the PlantableZone will handle the watering itself. waterSprite.AddImmediateToTargetList((plantableZone as MonoBehaviour).gameObject); // TODO: Consider implications of this call. It means we can't possibly overwater, but it // also changes the watersprite visual before it actually reaches the PlantableZone ChangeWaterLevel(-1); } else { Debug.LogError("D:"); } } else { // lol you've got no water, nerd } } } // Quick and dirty keypress check for inventory slots if (Input.GetKeyDown(KeyCode.Alpha1)) { SetSelectedItemIndex(0); } else if (Input.GetKeyDown(KeyCode.Alpha2)) { SetSelectedItemIndex(1); } else if (Input.GetKeyDown(KeyCode.Alpha3)) { SetSelectedItemIndex(2); } else if (Input.GetKeyDown(KeyCode.Alpha4)) { SetSelectedItemIndex(3); } else if (Input.GetKeyDown(KeyCode.Alpha5)) { SetSelectedItemIndex(4); } else if (Input.GetKeyDown(KeyCode.Alpha6)) { SetSelectedItemIndex(5); } else if (Input.GetKeyDown(KeyCode.Alpha7)) { SetSelectedItemIndex(6); } else if (Input.GetKeyDown(KeyCode.Alpha8)) { SetSelectedItemIndex(7); } else if (Input.GetKeyDown(KeyCode.Alpha9)) { SetSelectedItemIndex(8); } else if (Input.GetKeyDown(KeyCode.Alpha0)) { SetSelectedItemIndex(9); } else if (Input.GetKeyDown(KeyCode.Minus)) { SetSelectedItemIndex(10); } else if (Input.GetKeyDown(KeyCode.Equals)) { SetSelectedItemIndex(11); } }