aboutsummaryrefslogtreecommitdiffstats
path: root/Assets/Scripts/Items/Whip.cs
blob: 17061f86ebdf8c28429036a62bea9bc47a23c066 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using MontanaJohns.Actors;
using MontanaJohns.Core;
using System.Collections;
using UnityEngine;
using UnityEngine.InputSystem;

namespace MontanaJohns.Items
{
    public class Whip : Active
    {
        [SerializeField] protected GameObject hook;
        [SerializeField] protected GameObject hookNoSwing;
        [SerializeField] protected float maxRopeLength = 20f;
        protected GameObject player;
        protected GameObject currentHook;
        protected LayerMask ropeLayers;
        protected bool ropeExists = false;

        private void Awake()
        {
            stats.damage = 1;
        }

        private void Start()
        {
            player = GameObject.FindGameObjectWithTag("Player");
            ropeLayers = LayerMask.GetMask("Grapple", "Enemy");          
        }

        public override Vector2? Use()
        {
            if (!ropeExists)
            {
                Vector2 clickLocation = Camera.main.ScreenToWorldPoint(Mouse.current.position.ReadValue());
                Vector2 playerPos = player.transform.position;
                Vector2 direction = clickLocation - playerPos;
                RaycastHit2D hit = Physics2D.Raycast(playerPos, direction, maxRopeLength, ropeLayers);
                if(hit.collider != null)
                {
                    GameObject collisionGameObject = hit.collider.gameObject;
                    ropeExists = true;
                    if (LayerMask.LayerToName(collisionGameObject.layer) == "Grapple")
                    {
                        currentHook = Instantiate(hook, collisionGameObject.transform.position, Quaternion.identity);
                        SoundManager.PlaySound("WhipSwoosh");
                        return clickLocation;
                    }
                    else
                    {
                        StartCoroutine(WhipSmack(collisionGameObject));
                    }
                }
                return null;
            }
            else
            {
                Destroy(currentHook);
                ropeExists = false;
                return null;
            }
        }

        private IEnumerator WhipSmack(GameObject collisionGameObject)
        {
            currentHook = Instantiate(hookNoSwing, collisionGameObject.transform.position, Quaternion.identity);
            collisionGameObject.GetComponent<Actor>().TakeDamage(player.GetComponent<Player>().stats.damage);
            SoundManager.PlaySound("WhipSwoosh");
            yield return new WaitForSeconds(0.1f);
            Destroy(currentHook);
            ropeExists = false;
        }
    }
}