Esempio n. 1
0
    public void DropWeapon()
    {
        int i = 0;

        //find active weapon in weaponholder & its weapon_UI slot position
        foreach (Transform weapon in weaponHolder.transform)
        {
            //get active weapon
            if (weapon.gameObject.activeSelf == true && weapon.tag == "Weapon")
            {
                activeWeaponPos  = i;
                activeWeapon     = weapon.gameObject;
                activeWeaponAmmo = activeWeapon.GetComponent <Weapon>().currentAmmo;
                active_slot_no   = activeWeapon.GetComponent <Weapon>().weaponSlotPosUI;
                weaponPickUp     = activeWeapon.GetComponent <Weapon>().weaponPickUp;
                Debug.Log("Got active Weapon!");
                Debug.Log("Element i ==> " + i);
                break;
            }
            else
            {
                i++;
            }
        }

        //create instance of the active weapon
        Vector2    dropWeaponPos = new Vector2(player.transform.position.x, player.transform.position.y + dropWeaponOffset);
        GameObject droppedPickup = Instantiate(weaponPickUp, dropWeaponPos, Quaternion.identity);

        Debug.Log("Made new weapon!");

        //set the current ammo of the droppedWeapon to the previous active weapon
        Weapon droppedWeapon = droppedPickup.GetComponent <Pickup>().weaponToEquip;

        droppedWeapon.maxAmmo = activeWeaponAmmo;

        // get the Active WeaponSlot Script
        WeaponSlot WeaponSlot = player.GetComponent <Player>().weaponsSlots[active_slot_no].GetComponent <WeaponSlot>();

        //set the weaponslot empty to true
        WeaponSlot.empty = true;

        //reduce the number of currentlyAvailableWeapons
        weaponHolder.GetComponent <WeaponSwitching>().currentAvailableWeapons--;

        //destroy the weapon in the weaponholder & WeaponUI
        WeaponSlot.DestroyWeapon();
        Destroy(activeWeapon);
    }
Esempio n. 2
0
    // Update is called once per frame
    void Update()
    {
        //get the direction of the mouse position form the weapon position
        Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
        //tranform the direction to an angles
        float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
        //convert the angle to a unity rotation
        Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);

        //make the weapon rotation face the direction of the mouse cursor position
        transform.rotation = rotation;

        //check if gun ammo is replenished
        if (currentAmmo <= 0 && player != null)
        {
            weaponSwitching.currentAvailableWeapons--;

            //get the WeaponSlot for the current active weapon
            WeaponSlot weaponSlot = player.weaponsSlots[currentWeaponSlot].GetComponent <WeaponSlot>();
            weaponSlot.empty = true;
            weaponSlot.DestroyWeapon();

            //check if only one weapon is left in the weapon slot & make it active
            if (weaponSwitching.currentAvailableWeapons == 1)
            {
                foreach (Transform weapon in weaponSwitching.transform)
                {
                    if (weapon.gameObject.activeSelf == false)
                    {
                        weapon.gameObject.SetActive(true);
                        int slot = weapon.gameObject.GetComponent <Weapon>().weaponSlotPosUI;
                        GameObject.FindGameObjectWithTag("WeaponHolder").GetComponent <WeaponSwitching>().activeSlotImage[slot].SetActive(true);
                    }
                }
            }

            Destroy(gameObject);
        }

        //Fire projectile
        if (Input.GetMouseButton(0))
        {
            shoot();
        }
    }