#Help with weapon system & components

4 messages · Page 1 of 1 (latest)

solemn crest
#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public interface IShootable
{
    float nextAttack { get; }
    float shootRange { get; }
    Transform Transform { get; }
    float Damage { get; }
    LayerMask LayerToDamage { get; } 
    DamageType DamageType { get; }
    Action Shoot { get; set; }
    Action ShootType { get; set; }

    public void AssignWeaponType(FireType firetype, AmmoType ammotype)
    {
        if (firetype is FireType.Single) Shoot = SingleFire;
        else if (firetype is FireType.Auto) Shoot = AutoFire;

        if (ammotype is AmmoType.Raycast) ShootType = RaycastAmmo;
    }

    void ShootWeapon()
    {
        Shoot();
    }
    void SingleFire()
    {

        if (Input.GetMouseButtonDown(0))
        {
            if (Time.time > nextAttack)
            {
                ShootType();
            }
        }
    }
    void AutoFire()
    {
        if (Input.GetMouseButton(0))
        {
            if (Time.time > nextAttack)
            {
                ShootType();
            }
        }
    }
    protected virtual void RaycastAmmo()
    {
        RaycastHit hit;

        if (Physics.Raycast(Transform.position, Transform.TransformDirection(Vector3.forward), out hit, shootRange))
        {
            if ((LayerToDamage.value & (1 << hit.transform.gameObject.layer)) > 0)
            {
                hit.collider.GetComponent<IDamageable>().ApplyDamage(Damage, DamageType);
            }
        }
    }
}

Example of my current interface, but Interface can't instantiate an object so i'm not sure how to add that, and charge function requires coroutine which it cant handle either

#
protected virtual void ForceAmmo()
    {
        var projectile = Instantiate(forceAmmoPhysical) as GameObject;
        projectile.transform.localPosition = gunBarrelEnd.transform.position;
        projectile.transform.localRotation = gunBarrelEnd.transform.rotation;
        projectile.GetComponent<Rigidbody>().velocity = gunBarrelEnd.transform.TransformDirection(Vector3.forward) * force * Time.deltaTime;
    }

My projectile function above

#

Charge fire function below

private void PrimaryChargeFire()
    {
        if (Input.GetMouseButtonDown(0)) Charging();
        if (Input.GetMouseButtonUp(0)) ChargeFire();
    }
    protected void Charging()
    {
        PlayerPerforming();
        StartCoroutine(ChargeCounter());
        charging = true;
        currentDamage = 0;
        maxChargeDamage = baseDamage;
    }
    private IEnumerator ChargeCounter()
    {
        chargeTime = 0;
        //VFX
        while (chargeTime < maxChargeTime)
        {
            //VFX brightness         
            yield return new WaitForSeconds(0.25f);
            chargeTime += 0.25f;
            Debug.Log(chargeTime);
        }
        if (chargeTime >= maxChargeTime) StartCoroutine(ChargeOverLoad());
    }
    private IEnumerator ChargeOverLoad()
    {
        //vfx red overload
        yield return new WaitForSeconds(3f);
        chargeTime = 0;
        ChargeFire();
        //VFX overload 
    }

    private void ChargeFire()
    {
        StopAllCoroutines();
        if (chargeTime >= 1f)
        {
            Debug.Log("Fired");
            currentDamage = Mathf.RoundToInt(maxChargeDamage * (chargeTime / maxChargeTime));
            Update_PrimaryElementalType();
        }
        Debug.Log("Charged damage: " + currentDamage + ", chargeTime: " + chargeTime + " out of " + maxChargeTime);
        PlayerNotPerforming();
        charging = false;
    }

So I have trouble implementing these so that they can be added and removed from my guns, or maybe I need to change the functions to be something else?

rain violet
#

so you're not really using an interface correctly. the point of an interface is that, because it promises that certain functions exist on an object, you can have multiple classes that implement those functions differently. then in some other class, you just a have a reference to an instance of one of those classes.