void SwitchEquipable(Equipable equipable) { if (currEquip != null) { currEquip.Unequip(); } currEquip = equipable; if (equipable != null) { equipable.Equip(); } }
// Update is called once per frame void Update() { var mousePos3d = Input.mousePosition; mousePos3d.z = Mathf.Abs(Camera.main.transform.position.z); var mousePos = Camera.main.ScreenToWorldPoint(mousePos3d); character.SetTarget(mousePos); if (cursor != null) { cursor.transform.position = Input.mousePosition; } moveInput = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")); if (Input.GetButton("Shoot")) { if (magicWand != null) { magicWand.Shoot(true); } } if (Input.GetButtonDown("Teleport")) { if (boots != null) { boots.Teleport(mousePos); } } Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); var hits = Physics.RaycastAll(ray, 100, equipableMask); Equipable equipable = null; foreach (var hit in hits) { var eq = hit.collider.gameObject.GetComponent <Equipable>(); if (eq == null || !eq.readyToPickUp) { continue; } equipable = eq; if (eq == lastHovered) { break; } } if (equipable != lastHovered) { if (lastHovered != null) { lastHovered.UnHover(); } lastHovered = equipable; } if (equipable != null) { var closeEnough = Vector2.Distance(transform.position, equipable.transform.position) < UseRange; equipable.Hover(closeEnough); if (Input.GetButtonDown("Pick Up") && closeEnough && !equipable.blocked) { equipable.Equip(); MagicWand wand = equipable.GetComponent <MagicWand>(); if (wand != null) { EquipWand(wand); } LevelExit exit = equipable.GetComponent <LevelExit>(); if (exit != null) { exit.Exit(); } Key key = equipable.GetComponent <Key>(); if (key != null) { key.PickUp(); key.transform.position = keyParent.position; key.transform.rotation = keyParent.rotation; key.transform.parent = keyParent; } Potion potion = equipable.GetComponent <Potion>(); if (potion != null) { potion.PickUp(); } Boots boots = equipable.GetComponent <Boots>(); if (boots != null) { EquipBoots(boots); } } if (cursor != null) { cursor.SetInteract(true, closeEnough, !equipable.blocked); } } else { if (cursor != null) { cursor.SetInteract(false, false, false); } } if (invincible) { var color = Color.red; color.a = 0.75f + 0.25f * Mathf.Cos((Time.time - lastHurt) * 5.0f); sprite.color = color; } if (wasInvincible && !invincible) { sprite.color = origColor; } wasInvincible = invincible; }