using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GrenadeLauncher : Relic
{
    //gunshell에서 구현
    public GameObject grenadeGO;
    private bool isReloaded;
    private float reloadTime;
    private float maxReloadTime;

    [System.NonSerialized]
    public Queue<GameObject> grenadePool;
    [System.NonSerialized]
    public GameObject go;

    public override void Init()
    {
        base.Init();
        this.maxReloadTime = 0.5f;
        this.grenadePool = new Queue<GameObject>();
    }

    public void grenadeShoot(GameObject bulletPoint, Quaternion lookRotation)
    {
        if (this.isReloaded)
        {
            if (this.grenadePool.Count > 0)
            {
                go = grenadePool.Dequeue();
                if (go.activeSelf) go = Instantiate(this.grenadeGO);
                go.SetActive(true);
            }
            else
            {
                go = Instantiate(this.grenadeGO);
            }

            this.isReloaded = false;
            go.transform.position = bulletPoint.transform.position;
            go.transform.rotation = lookRotation;
            go.GetComponent<Grenade>().Init(400, 0.6f, this.GetComponent<GrenadeLauncher>());
        }
    }

    private void Update()
    {
        if(this.maxReloadTime + 0.5f > this.reloadTime && !this.isReloaded) this.reloadTime += Time.deltaTime;
        if (this.maxReloadTime <= this.reloadTime) 
        {
            this.isReloaded = true;
            this.reloadTime = 0;
        } 
    }

    public GameObject Grenade()
    {
        return this.go;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Grenade : MonoBehaviour
{
    private float bulletSpeed;
    private float maxExistTime;
    private float existTime;
    private Rigidbody2D rBody2D;

    private Vector2 dir;
    public GameObject impactVfxGo;
    private GrenadeLauncher grenadeLauncher;
    private bool isExploded;

    public void Init(float bulletSpeed, float maxExistTime, GrenadeLauncher grenadeLauncher)
    {
        this.isExploded = false;
        this.existTime = 0;
        this.bulletSpeed = bulletSpeed;
        this.maxExistTime = maxExistTime;
        this.rBody2D = this.gameObject.GetComponent<Rigidbody2D>();
        this.grenadeLauncher = grenadeLauncher;
        float angle = this.transform.eulerAngles.z % 360;
        this.dir = new Vector2(Mathf.Cos(angle * Mathf.Deg2Rad), Mathf.Sin(angle * Mathf.Deg2Rad));
    }

    void FixedUpdate()
    {
        this.rBody2D.velocity = this.dir.normalized * this.bulletSpeed * Time.deltaTime;

        this.existTime += Time.deltaTime;
        if (this.existTime >= this.maxExistTime)
        {
            if (!this.isExploded)
            {
                this.existTime = 0;
                GameObject go = Instantiate(this.impactVfxGo);
                go.transform.position = this.transform.position;
                this.gameObject.SetActive(false);
                this.isExploded = true;
            }
        }
    }

    public void OnDisable()
    {
        if (!this.isExploded)
        {
            GameObject go = Instantiate(this.impactVfxGo);
            go.transform.position = this.transform.position;
            this.grenadeLauncher.grenadePool.Enqueue(this.gameObject);
            this.isExploded = true;
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        this.gameObject.SetActive(false);        
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class VfxGrenadeExplosion : PlayerActiveSkill
{
    public GameObject leftTop;
    public GameObject rightBottom;

    private HashSet<Monster> damagedMonsters;

    protected override void Start()
    {
        this.existTime = 0.85f;
        this.knockbackSpeed = 30000;

        this.damagedMonsters = new HashSet<Monster>();

        base.Start();
    }

    public void Update()
    {
        Collider2D[] colliders = Physics2D.OverlapAreaAll(this.leftTop.transform.position, this.rightBottom.transform.position);

        foreach (Collider2D collider in colliders)
        {
            if (collider.CompareTag("Monster"))
            {
                Monster monster = collider.GetComponent<Monster>();

                if (!damagedMonsters.Contains(monster))
                {
                    this.damagedMonsters.Add(monster);
                    monster.TakeSkillDamage(this.damage, this.existTime);

                    if (monster != null && monster.GetComponent<Rigidbody2D>() != null)
                    {
                        Vector2 dir = monster.transform.position - this.transform.position;
                        monster.GetComponent<PathFinding>().HitPlayerActiveSkill(dir, this.knockbackSpeed);
                    }
                }
            }
        }
    }
}

 그레네이드 런쳐 - 그레네이드 - 폭발 순으로 구현하였습니다. 일반 공격 중  특정시간 마다 자동을 발사되도록 구현하였습니다.

+ Recent posts