Exemplo n.º 1
0
    void Awake()
    {
        _grappleState   = GrappleState.Ready;
        _grappleSuccess = false;

        _swingTime = ThrowTime;

        _wrap = Wrapping.Wrap;

        _pivotPoints = new List <Vector3>();
        _radii       = new List <float>();

        Indicator         = Instantiate(Indicator, transform.position, transform.rotation);
        IndicatorRenderer = Indicator.GetComponent <SpriteRenderer>();

        grappleSprite               = new GameObject("GrappleSprite");
        grappleSpriteRender         = grappleSprite.AddComponent <SpriteRenderer>();
        grappleSpriteRender.sprite  = GrappleSprite;
        grappleSpriteRender.enabled = false;

        _lineRender            = gameObject.AddComponent <LineRenderer>();
        _lineRender.startColor = Color.white;
        _lineRender.endColor   = Color.green;
        _lineRender.startWidth = 0.1f;
        _lineRender.endWidth   = 0.1f;
        _lineRender.enabled    = false;
    }
Exemplo n.º 2
0
    private bool ActivateGrapple()
    {
        if (seekerWorldPoint != Vector3.zero)
        {
            grapplePoint           = seekerWorldPoint;
            toGrapplePoint         = grapplePoint - transform.position;
            distanceToGrapplePoint = toGrapplePoint.magnitude;
            toGrapplePoint.Normalize();

            if (distanceToGrapplePoint <= 2.1f)
            {
                return(false);
            }

            state                       = GrappleState.ENGAGING;
            grappleAvailable            = false;
            onCooldown                  = true;
            engagementDistanceTravelled = 0f;
            grappleTransform            = Instantiate(grapplePrefab).transform;
            look.LockRotation();

            HideSeeker();

            return(true);
        }

        return(false);
    }
Exemplo n.º 3
0
 public void EnterAbility()
 {
     _grappleState             = GrappleState.Ready;
     _swingTime                = ThrowTime;
     _grappleSuccess           = false;
     IndicatorRenderer.enabled = true;
 }
Exemplo n.º 4
0
    void Awake()
    {
        //Core grapple
        _grappleState   = GrappleState.Ready;
        _grappleSuccess = false;

        _grappleTimer = ThrowTime;

        //Wrapping
        _wrap = Wrapping.Wrap;

        _radii      = new List <float>();
        pivotPoints = new List <Vector3>();

        //Indicator + Sprite
        Indicator                 = Instantiate(Indicator, transform.position, transform.rotation);
        indicatorRenderer         = Indicator.GetComponentInChildren <SpriteRenderer>();
        indicatorRenderer.enabled = false;

        grappleSprite       = new GameObject("GrappleSprite");
        grappleSpriteRender = grappleSprite.AddComponent <SpriteRenderer>();
        grappleSpriteRender.sortingLayerName = "UI";
        grappleSpriteRender.sprite           = GrappleSprite;
        grappleSpriteRender.enabled          = false;

        _lineRender = gameObject.AddComponent <LineRenderer>();
        _lineRender.sortingOrder = 1;
        _lineRender.startWidth   = 0.2f;
        _lineRender.endWidth     = 0.2f;
        _lineRender.enabled      = false;

        _lineRender.material   = new Material(Shader.Find("Sprites/Default"));
        _lineRender.startColor = RopeColor;
        _lineRender.endColor   = RopeColor;
    }
Exemplo n.º 5
0
 void OnTriggerEnter(Collider other)
 {
     if (other.tag == "Anchor")
     {
         state        = GrappleState.HIT;
         hookedObject = other.gameObject;
     }
 }
Exemplo n.º 6
0
    private void DisengageGrapple()
    {
        state = GrappleState.RETURNING;
        engagementDistanceTravelled = 0f;
        kb.gravityMultiplier        = 1f;

        look.UnlockRotation();
    }
Exemplo n.º 7
0
 private void Start()
 {
     DOTween.Init();
     mIsGrappling  = false;
     mScreenCenter = new Vector3(Screen.width / 2, Screen.height / 2, 0);
     mMarkedObject = null;
     mGrappleState = GrappleState.Idle;
 }
Exemplo n.º 8
0
    void Update()
    {
        throwValue = Input.GetAxis("Throw");

        if (state == GrappleState.NONE)
        {
            characterRb.isKinematic = false;
            return;
        }
        if (state == GrappleState.HIT)
        {
            Debug.Log("HitAnchor");
            transform.parent.position = hookedObject.transform.position;

            if (hangingposition != null)
            {
                character.transform.position = hangingposition.transform.position + new Vector3(0f, -4f, 0f);
            }

            if (throwValue < 0.3f)
            {
                state = GrappleState.PULLING;
            }
        }
        if (state == GrappleState.PULLING)
        {
            characterRb.isKinematic = true;
            float dist = Vector3.Distance(hookedObject.transform.position, hangingPoint.transform.position);
            if (dist > 2f)
            {
                character.transform.position = Vector3.Lerp(character.transform.position, hookedObject.transform.position + new Vector3(0f, -4f, 0f), 0.15f);
            }
            else
            {
                state = GrappleState.HANGING;
            }
        }
        if (state == GrappleState.HANGING)
        {
            anim.SetBool("hanging", true);
            characterRb.isKinematic      = true;
            hangingposition              = hookedObject;
            character.transform.position = hangingposition.transform.position + new Vector3(0f, -4f, 0f);
            if (Input.GetButtonDown("Jump") || Input.GetButtonDown("Dash"))
            {
                state = GrappleState.LETGO;
            }
        }
        if (state == GrappleState.LETGO)
        {
            anim.SetBool("hanging", false);
            characterRb.isKinematic = false;
            hookedObject            = null;
            hangingposition         = null;
            state = GrappleState.NONE;
        }
    }
Exemplo n.º 9
0
    IEnumerator Hook()
    {
        state = GrappleState.FIRING;

        float startTime = Time.time;

        gun.showLine = true;

        while (Time.time < startTime + hookTime && !hook.hooked && !hook.deflect)
        {
            grappleHook.localPosition = hook.initialOffset + Vector3.Lerp(Vector3.zero, Vector3.forward * hookDistance, (Time.time - startTime) / hookTime);
            yield return(null);
        }

        state = GrappleState.REELING;

        hook.deflect = false;

        Vector3 gunForward = gun.transform.forward;
        float   oldAngle   = transform.eulerAngles.y;

        Vector3 endPoint = grappleHook.localPosition - hook.initialOffset;

        float newTime = (endPoint.z / hookDistance) * hookTime;

        startTime = Time.time;

        while (Time.time < startTime + newTime)
        {
            grappleHook.localPosition = hook.initialOffset + Vector3.Lerp(endPoint, Vector3.zero, (Time.time - startTime) / newTime);
            gun.LookInDirection(Quaternion.AngleAxis(transform.eulerAngles.y - oldAngle, Vector3.up) * gunForward);
            yield return(null);
        }

        grappleHook.localPosition = hook.initialOffset;

        if (hook.hooked)
        {
            state           = GrappleState.HOOKED;
            hook.targetHook = 1f;
            ammo            = 1f;
            gun.UpdateAmmo(ammo, hook.capturedType);
        }
        else
        {
            state = GrappleState.RELEASED;
        }

        hook.deflect = false;

        gun.showLine = false;

        yield return(null);
    }
Exemplo n.º 10
0
    public void ExitAbility()
    {
        _pivotPoints.Clear();
        _radii.Clear();
        _lineRender.positionCount   = 0;
        _lineRender.enabled         = false;
        grappleSpriteRender.enabled = false;

        _grappleState             = GrappleState.Ready;
        IndicatorRenderer.enabled = false;
    }
Exemplo n.º 11
0
    //Set variables for ability exit
    public void ExitAbility()
    {
        //Clear wrapping points
        pivotPoints.Clear();
        _radii.Clear();

        //Hide Indicators
        _lineRender.positionCount   = 0;
        _lineRender.enabled         = false;
        grappleSpriteRender.enabled = false;

        _grappleState             = GrappleState.Ready;
        indicatorRenderer.enabled = false;
    }
Exemplo n.º 12
0
    /*
     * Resets grapple variables for state switch
     */
    private void ResetGrapple()
    {
        //Clear points
        pivotPoints.Clear();
        _radii.Clear();

        _grappleTimer = ThrowTime;

        //Indicators
        _lineRender.enabled         = false;
        indicatorRenderer.enabled   = true;
        grappleSpriteRender.enabled = false;

        _grappleState = GrappleState.Ready;
    }
Exemplo n.º 13
0
    public void grappleHandler(bool grappleTrigger, bool gunReady, GameObject gun)
    {
        switch (grappleState)
        {
        case GrappleScript.GrappleState.REST:
            gameObject.SetActive(false);
            if (grappleTrigger && gunReady)
            {
                Physics.Raycast(gun.transform.position + gun.transform.forward * 3, gun.transform.forward, out RaycastHit raycastHit);
                if (raycastHit.transform != null)
                {
                    grappleLength   = 0;
                    grapplePosition = raycastHit.point;
                    grappleState    = GrappleScript.GrappleState.THROWING;
                }
            }
            break;

        case GrappleScript.GrappleState.THROWING:
            gameObject.SetActive(true);
            if (grappleTrigger || grappleLength > maxGrappleLength)
            {
                grappleState = GrappleScript.GrappleState.RETURNING;
            }
            break;

        case GrappleScript.GrappleState.RETURNING:
            gameObject.SetActive(true);
            break;

        case GrappleScript.GrappleState.REELING:
            gameObject.SetActive(true);
            if (grappleLength < minReelLength || grappleTrigger || grapplePosition == Vector3.zero)
            {
                //attachedRb.GetComponent<MeshRenderer>().material = unselectedMat;
                grappleState = GrappleScript.GrappleState.RETURNING;
            }
            break;

        case GrappleScript.GrappleState.STRUCK:
            gameObject.SetActive(true);
            if (!grappleTrigger)
            {
                grappleState = GrappleScript.GrappleState.REELING;
            }
            break;
        }
    }
Exemplo n.º 14
0
 void OnTriggerEnter(Collider other)
 {
     if (grappleState == GrappleState.THROWING) // && (other.gameObject.layer == 8)
     {
         grapplePosition = transform.position + transform.forward * grappleLength;
         //LayerMask mask = LayerMask.GetMask("Map");
         Collider[] sphereCast        = Physics.OverlapSphere(grapplePosition, Mathf.Sqrt(grappleSpeed) * 5);
         bool       verifiedCollision = false;
         Debug.Assert(sphereCast.Length > 0, "Spherecast length = 0");
         for (int i = 0; i < sphereCast.Length; i++)
         {
             if (sphereCast[i].Equals(other))
             {
                 verifiedCollision = true;
             }
             else
             {
                 Debug.Log("detected object that was not collider");
             }
         }
         if (verifiedCollision)
         {
             //if (attachedRb != null) attachedRb.GetComponent<MeshRenderer>().material = unselectedMat;
             attachedRb = other.attachedRigidbody;
             //attachedRb.GetComponent<MeshRenderer>().material = seletectMat;
             grapplePositionLocalVector = attachedRb.transform.InverseTransformPoint(grapplePosition);
             grappleState = GrappleState.STRUCK;
             if (transform.parent.tag == "Bot")
             {
                 transform.parent.GetComponent <BotScript>().grappleStrike();
             }
         }
         else
         {
             Debug.Log("nonverified collision");
             grappleState = GrappleState.RETURNING;
         }
     }
     else if (grappleState == GrappleState.REELING && (other.gameObject.layer == 8))
     {
         if (!other.attachedRigidbody.Equals(attachedRb))
         {
             //Debug.Log("wrong attachedRb");
             grappleState = GrappleState.REST;
         }
     }
 }
Exemplo n.º 15
0
    public void grappleHandlerFixed(GameObject player, GameObject gun)
    {
        switch (grappleState)
        {
        case GrappleState.REST:
            break;

        case GrappleState.THROWING:
            transform.position = gun.transform.position;
            grappleLength     += grappleSpeed * 100 * Time.fixedDeltaTime;
            transform.LookAt(grapplePosition);
            transform.localScale = new Vector3(0.2f, 0.2f, grappleLength / 3);
            break;

        case GrappleState.RETURNING:
            transform.position = player.transform.position;
            grappleLength     -= grappleSpeed * 200 * Time.fixedDeltaTime;
            transform.LookAt(grapplePosition);
            transform.localScale = new Vector3(0.2f, 0.2f, grappleLength / 3);
            if (grappleLength < minGrappleLength)
            {
                grappleState = GrappleState.REST;
            }
            break;

        case GrappleState.REELING:
            if (attachedRb == null)
            {
                grappleState = GrappleState.RETURNING;
                break;
            }
            transform.position = player.transform.position;
            grapplePosition    = attachedRb.transform.position + attachedRb.transform.TransformVector(grapplePositionLocalVector);

            transform.LookAt(grapplePosition);
            grappleLength        = (grapplePosition - transform.position).magnitude;
            transform.localScale = new Vector3(0.2f, 0.2f, grappleLength / 3);

            attachedRb.AddForceAtPosition((transform.position - grapplePosition).normalized * grappleForce, grapplePosition);
            player.GetComponent <Rigidbody>().AddForce((grapplePosition - transform.position).normalized * grappleForce, ForceMode.Acceleration);
            break;

        case GrappleState.STRUCK:
            break;
        }
    }
Exemplo n.º 16
0
    private void HandleInput()
    {
        previousState = state;

        if (state == GrappleState.INACTIVE)
        {
            if (FPSInput.grappleDown && !lastGrapple && grappleAvailable)
            {
                state = GrappleState.SEEKING;
                ShowSeeker();
            }
        }
        else if (state == GrappleState.SEEKING)
        {
            if (!FPSInput.grappleDown)
            {
                bool success = ActivateGrapple();
                if (!success)
                {
                    state = GrappleState.INACTIVE;
                    HideSeeker();
                }
            }
            else if (FPSInput.rightMouseDown)
            {
                state = GrappleState.INACTIVE;
                HideSeeker();
            }
        }
        else if (state == GrappleState.ENGAGED)
        {
            /* Detect if cancel key is down/pressed */
            bool cancel = FPSInput.spaceDown || FPSInput.rightMouseDown || playerStats.isDead;
            if (cancel)
            {
                DisengageGrapple();
            }
        }

        lastGrapple       = FPSInput.grappleDown;
        lastLeftMouseDown = FPSInput.leftMouseDown;
    }
Exemplo n.º 17
0
    private void ChangeState(GrappleState targetState)
    {
        if (targetState == GrappleState.firing)
        {
            this.gameObject.transform.parent = null;
            rb.isKinematic = false;
        }
        else if (targetState == GrappleState.idle)
        {
            this.gameObject.transform.parent        = parentObject.transform;
            this.gameObject.transform.localPosition = new Vector3(0 + hookHeadOffsetFromBody, 0, 0);
            this.planted = false;
        }
        else
        {
            rb.isKinematic = true;
        }

        currentState = targetState;
    }
Exemplo n.º 18
0
    private void Update()
    {
        if (grapple.state != lastGrappleState)
        {
            if (grapple.state == GrappleState.INACTIVE)
            {
                Hide();
            }
        }

        if (grapple.state != GrappleState.ENGAGED)
        {
            return;
        }

        if (look.grappleLookDir == Vector2.zero)
        {
            return;
        }

        float   grappleDirSmoothMagnitude = (float)System.Math.Tanh(look.grappleLookDir.magnitude);
        Vector2 grappleDirSmooth          = look.grappleLookDir.normalized * grappleDirSmoothMagnitude;

        Color color = image.color;

        color.a     = grappleDirSmooth.magnitude;
        image.color = color;

        Vector2 pos = new Vector2(mainCamera.pixelWidth / 2f, mainCamera.pixelHeight / 2f) + grappleDirSmooth * pixelRange;

        transform.position = new Vector3(pos.x, pos.y, 0f);

        float theta = Mathf.Atan2(grappleDirSmooth.y, grappleDirSmooth.x) + Mathf.PI / 2f;

        transform.localRotation = Quaternion.Euler(0f, 0f, theta * Mathf.Rad2Deg);

        lastGrappleState = grapple.state;
    }
Exemplo n.º 19
0
    public void update()
    {
        RaycastHit2D hit;

        cooldown_timer += Time.deltaTime;

        switch (grapple_state)
        {
            case GrappleState.SHOOTING_OUT:
                GameObject chain_sprite = (GameObject)Resources.Load("bullets/chain");

                for (int n = chains.Count; n < num_chains; ++n)
                {
                    chain_pos.x += Mathf.Cos(grapple_angle) * chain_seperation;
                    chain_pos.y += Mathf.Sin(grapple_angle) * chain_seperation;
                    GameObject obj = (GameObject)Instantiate(chain_sprite, chain_pos, Quaternion.identity);
                    obj.transform.localEulerAngles = new Vector3(0, 0, grapple_angle * (180.0f / Mathf.PI) + 90);
                    chains.Add(obj);

                    if (n >= num_chains - 1)
                    {
                        if (grapplehookimpact != "") FMOD_StudioSystem.instance.PlayOneShot(grapplehookimpact, transform.position);
                        grapple_state = GrappleState.FOLLOWING;
                    }
                    if (n >= chain_shootout_speed * Time.deltaTime) break;
                }
                break;
            case GrappleState.FOLLOWING:
                Vector3 pos = transform.position;
                pos.x += Mathf.Cos(grapple_angle) * grapple_speed * Time.deltaTime;
                pos.y += Mathf.Sin(grapple_angle) * grapple_speed * Time.deltaTime;
                transform.position = pos;

                hit = Physics2D.Raycast(transform.position, new Vector2(Mathf.Cos(grapple_angle), Mathf.Sin(grapple_angle)),
                                                     grapple_radius, colliders);

                if (hit)
                {
                    if (hit.distance <= 1.0f) grapple_state = GrappleState.NONE;
                    else
                    {
                        non_collide_timer += Time.deltaTime;
                        for (int n = 0; n < chains.Count; ++n)
                        {
                            GameObject chain = chains[n];

                            float dist = Mathf.Sqrt(Mathf.Pow(transform.position.x - chain.transform.position.x, 2) +
                                                    Mathf.Pow(transform.position.y - chain.transform.position.y, 2));
                            if (dist <= 1.0f)
                            {
                                Destroy(chain);
                                chains.RemoveAt(n);
                                --n;
                                non_collide_timer = 0;
                            }
                        }
                        if (non_collide_timer >= chain_break_time) grapple_state = GrappleState.NONE;
                    }
                }else grapple_state = GrappleState.NONE;

                //if grapple state is now none, remove all chains from the list
                if (grapple_state == GrappleState.NONE) {
                    foreach (GameObject chain in chains)
                    {
                        Destroy(chain);
                    }
                    chains.Clear();
                }
                break;
            default:
                if (cooldown_timer >= cooldown_ms && Input.GetMouseButtonUp(1))
                {
                    cooldown_timer = 0;

                    if (grapplehookthrow != "") FMOD_StudioSystem.instance.PlayOneShot(grapplehookthrow, transform.position);
                    hit = Physics2D.Raycast(transform.position,
                                            new Vector2(
                                                Mathf.Cos(parent.inventory.equipped.angle),
                                                Mathf.Sin(parent.inventory.equipped.angle)
                                            ),
                                            grapple_radius, colliders);
                    if (hit && hit.distance >= min_grapple_radius)
                    {
                        grapple_angle = parent.inventory.equipped.angle;
                        grapple_state = GrappleState.SHOOTING_OUT;

                        num_chains = hit.distance / chain_seperation;
                        chain_pos = transform.position;
                        chain_pos.z = -20;
                    }
                }
                break;
        }
    }
Exemplo n.º 20
0
 private void RenderTongue(
     IGameViewport viewport,
     GrappleState grappleState,
     in Vector3 tongueDir,
Exemplo n.º 21
0
 private void DeactivateGrapple()
 {
     state = GrappleState.INACTIVE;
     Destroy(grappleTransform.gameObject);
 }
Exemplo n.º 22
0
 private void EngageGrapple()
 {
     state = GrappleState.ENGAGED;
 }
Exemplo n.º 23
0
    public void update()
    {
        RaycastHit2D hit;

        switch (grapple_state)
        {
        case GrappleState.SHOOTING_OUT:
            GameObject chain_sprite = (GameObject)Resources.Load("bullets/chain");

            for (int n = chains.Count; n < num_chains; ++n)
            {
                chain_pos.x += Mathf.Cos(grapple_angle) * chain_seperation;
                chain_pos.y += Mathf.Sin(grapple_angle) * chain_seperation;
                GameObject obj = (GameObject)Instantiate(chain_sprite, chain_pos, Quaternion.identity);
                obj.transform.localEulerAngles = new Vector3(0, 0, grapple_angle * (180.0f / Mathf.PI) + 90);
                chains.Add(obj);

                if (n >= num_chains - 1)
                {
                    grapple_state = GrappleState.FOLLOWING;
                }
                if (n >= chain_shootout_speed * Time.deltaTime)
                {
                    break;
                }
            }
            break;

        case GrappleState.FOLLOWING:
            Vector3 pos = transform.position;
            pos.x += Mathf.Cos(grapple_angle) * grapple_speed * Time.deltaTime;
            pos.y += Mathf.Sin(grapple_angle) * grapple_speed * Time.deltaTime;
            transform.position = pos;

            hit = Physics2D.Raycast(transform.position, new Vector2(Mathf.Cos(grapple_angle), Mathf.Sin(grapple_angle)),
                                    grapple_radius, colliders);

            if (hit)
            {
                if (hit.distance <= 1.0f)
                {
                    grapple_state = GrappleState.NONE;
                }
                else
                {
                    non_collide_timer += Time.deltaTime;
                    for (int n = 0; n < chains.Count; ++n)
                    {
                        GameObject chain = chains[n];

                        float dist = Mathf.Sqrt(Mathf.Pow(transform.position.x - chain.transform.position.x, 2) +
                                                Mathf.Pow(transform.position.y - chain.transform.position.y, 2));
                        if (dist <= 1.0f)
                        {
                            Destroy(chain);
                            chains.RemoveAt(n);
                            --n;
                            non_collide_timer = 0;
                        }
                    }
                    if (non_collide_timer >= chain_break_time)
                    {
                        grapple_state = GrappleState.NONE;
                    }
                }
            }
            else
            {
                grapple_state = GrappleState.NONE;
            }

            //if grapple state is now none, remove all chains from the list
            if (grapple_state == GrappleState.NONE)
            {
                foreach (GameObject chain in chains)
                {
                    Destroy(chain);
                }
                chains.Clear();
            }
            break;

        default:
            if (Input.GetMouseButtonUp(1))
            {
                hit = Physics2D.Raycast(transform.position,
                                        new Vector2(
                                            Mathf.Cos(parent.weapon_inventory.equipped.angle),
                                            Mathf.Sin(parent.weapon_inventory.equipped.angle)
                                            ),
                                        grapple_radius, colliders);
                if (hit && hit.distance >= min_grapple_radius)
                {
                    grapple_angle = parent.weapon_inventory.equipped.angle;
                    grapple_state = GrappleState.SHOOTING_OUT;

                    num_chains  = hit.distance / chain_seperation;
                    chain_pos   = transform.position;
                    chain_pos.z = -20;
                }
            }
            break;
        }
    }
Exemplo n.º 24
0
    // Update is called once per frame
    void Update()
    {
        if (state == GrappleState.COOLDOWN && Time.time > timeLastCooldown + coolDownTime)
        {
            state         = GrappleState.RELEASED;
            hook.ammo     = 1f;
            hook.cooldown = false;
        }

        if (state == GrappleState.RELEASED && hook.hooked)
        {
            state           = GrappleState.HOOKED;
            hook.targetHook = 1f;
            ammo            = 1f;
            gun.UpdateAmmo(ammo, hook.capturedType);
            hook.ammo = ammo;
        }

        if (state != GrappleState.REELING)
        {
            gun.LookInDirection((currentTarget == null || state == GrappleState.HOOKED) ? transform.forward : currentTarget.position - gun.transform.position);
        }

        if (currentTarget == null)
        {
            isTarget = Physics.BoxCast(transform.position, new Vector3(1.5f, 1.5f, 1.5f), transform.forward, out targetDetect, transform.rotation, 100f, slimeLayer);
            if (isTarget)
            {
                currentTarget = targetDetect.transform;
            }
        }
        else
        {
            /*if ((currentTarget.position - transform.position).sqrMagnitude < 4f && state == GrappleState.RELEASED && !hook.hooked) {
             *  state = GrappleState.HOOKED;
             *  hook.CaptureObject(currentTarget.parent.parent);
             * }*/


            if (Vector3.Angle(currentTarget.position - transform.position, transform.forward) > 45f)
            {
                currentTarget = null;
            }
        }

        if (Input.GetMouseButtonDown(1))
        {
            if (state == GrappleState.RELEASED)
            {
                StartCoroutine(Hook());
                audioSource.PlayOneShot(firingSound);
            }
            else
            {
            }
        }

        if (state == GrappleState.HOOKED)
        {
            if (Input.GetMouseButtonDown(0) && hook.capturedType != SlimeType.Ice)
            {
                gun.Fire();
                if (state == GrappleState.HOOKED)
                {
                    hook.UseSlime();
                    switch (hook.capturedType)
                    {
                    case SlimeType.Wind:
                        audioSource.PlayOneShot(windNoises[Random.Range(0, windNoises.Length)], 0.4f);
                        ammo -= 0.26f;
                        break;

                    case SlimeType.Earth:
                        audioSource.PlayOneShot(earthNoises[Random.Range(0, earthNoises.Length)]);
                        ammo -= 1.01f;
                        break;

                    case SlimeType.Fire:
                        audioSource.PlayOneShot(fireNoises[Random.Range(0, fireNoises.Length)], 0.8f);
                        ammo -= 0.34f;
                        break;
                    }
                    gun.UpdateAmmo(ammo, hook.capturedType);
                    hook.ammo = ammo;
                }
            }
            else if (hook.capturedType == SlimeType.Ice)
            {
                if (Input.GetMouseButton(0))
                {
                    gun.IncreaseSpeed();
                    hook.UseSlime();
                    ammo -= 0.5f * Time.deltaTime;
                    gun.UpdateAmmo(ammo, hook.capturedType);
                    hook.ammo = ammo;
                }
                else if (Input.GetMouseButtonUp(0))
                {
                    hook.PauseIceBeam();
                }
            }
        }



        if (ammo < 0f)
        {
            ammo = 0f;
            gun.UpdateAmmo(ammo, hook.capturedType);
            hook.ammo = 0f;
            hook.DestroySlime();
            hook.cooldown    = true;
            state            = GrappleState.COOLDOWN;
            timeLastCooldown = Time.time;
        }
    }
    // Update is called once per frame
    void Update()
    {
        holdMultiplier = Mathf.Clamp(holdMultiplier, -2, 2);
        Vector2 scale1 = Vector2.one * (1.1f - 0.1f * FindObjectsOfType <SpringJoint2D>().Length);

        //transform.localScale = scale1;
        transform.GetChild(1).localScale         = scale1;
        GetComponent <CircleCollider2D>().radius = scale1.x / 2;

        if (Input.GetKeyDown(shoot) && state == GrappleState.None)
        {
            Vector2 pos = (transform.position - Camera.main.ScreenToWorldPoint(Input.mousePosition));
            pos = pos.normalized;
            Debug.Log(pos);
            ball = Instantiate(ballPrefab, transform.position + (Vector3)pos, Quaternion.identity);
            ball.GetComponent <Rigidbody2D>().velocity = pos * -shootSpeed;

            GameObject _chainStart = Instantiate(chain, transform.position, Quaternion.identity);
            nodes.Add(_chainStart);

            GameObject _node = Instantiate(nodePrefab, transform.position, Quaternion.identity);
            nodes.Add(_node);
            _node.GetComponent <SpringJoint2D>().connectedBody = _chainStart.GetComponent <Rigidbody2D>();
        }
        if (Input.GetKeyDown(shoot) && state == GrappleState.Hooked)
        {
        }
        if (Input.GetKeyUp(shoot) && state == GrappleState.None)
        {
            for (int i = 1; i < nodes.Count; i++)
            {
                nodes[i].GetComponent <CircleCollider2D>().enabled = false;
            }
            Destroy(ball);
            state = GrappleState.Return;
        }
        moveGrapple = MoveState.Normal;
        if (state == GrappleState.Hooked)
        {
            if (Input.GetKey(KeyCode.W))
            {
                moveGrapple     = MoveState.Less;
                holdMultiplier -= 0.2f;
            }
            else if (Input.GetKey(KeyCode.S))
            {
                moveGrapple     = MoveState.More;
                holdMultiplier += 0.2f;
            }
            else
            {
                moveGrapple = MoveState.Hold;
                //holdMultiplier = 0;
            }
        }
        if (nodes.Count > 1)
        {
            endDistance = transform.position.x - nodes[1].transform.position.x;
            if (ball)
            {
                endVector = (transform.position - ball.transform.position) / nodes.Count;
            }
            else
            {
                endVector = Vector2.zero;
            }
            GameObject   shootchain = nodes[nodes.Count - 1];
            LineRenderer rend       = nodes[0].GetComponent <LineRenderer>();
            rend.SetPosition(0, transform.position);
            rend.positionCount = nodes.Count + 1;
            for (int i = 0; i < nodes.Count; i++)
            {
                rend.SetPosition(i + 1, nodes[i].transform.position);
            }
            //shootchain.GetComponent<Rigidbody2D>().velocity = (shootchain.transform.position - Camera.main.ScreenToWorldPoint(Input.mousePosition)).normalized * -25;
            if (state == GrappleState.Return)
            {
                nodes[1].GetComponent <Rigidbody2D>().velocity = (nodes[1].transform.position - transform.position) * -25;
                if (Vector3.Distance(transform.position, shootchain.transform.position) < 1 && nodes.Count == 2)
                {
                    state = GrappleState.None;
                    Destroy(nodes[1]);
                    nodes.RemoveAt(1);
                    rend.positionCount = 0;
                    return;
                }
            }
            else
            {
                Rigidbody2D _rb = ball.GetComponent <Rigidbody2D>();
                shootchain.GetComponent <Rigidbody2D>().velocity = (shootchain.transform.position - ball.transform.position) * -25;
                ball.GetComponent <Rigidbody2D>().AddForce((ball.transform.position - shootchain.transform.position).normalized * -5f);
                //if (Vector2.Distance(transform.position, ball.transform.position) > 6)
                //{
                //    _rb.AddForce((transform.position - ball.transform.position) * 1f);
                //    GetComponent<Rigidbody2D>().AddForce((transform.position - ball.transform.position) * -.05f);
                //}
                //ball.transform.position = Vector2.ClampMagnitude(ball.transform.position - transform.position, 8);

                for (int i = 0; i < hooks.childCount; i++)
                {
                    if (Vector2.Distance(ball.transform.position, hooks.GetChild(i).position) < hookSize)
                    {
                        ball.transform.position = hooks.GetChild(i).position;
                        //ball = null;
                        state           = GrappleState.Hooked;
                        _rb.constraints = RigidbodyConstraints2D.FreezeAll;
                    }
                    //if (Vector2.Distance(transform.position, hooks.GetChild(i).position) < hookSize)
                    //{
                    //    .transform.position = hooks.GetChild(i).position;
                    //    //ball = null;
                    //    state = GrappleState.Hooked;
                    //    _rb.constraints = RigidbodyConstraints2D.FreezeAll;
                    //}
                }
            }
            //endchain.transform.localPosition = Vector3.zero;
            if (Vector3.Distance(transform.position, shootchain.transform.position) < 1 + nodes.Count * 0.25f && nodes.Count > 2 && moveGrapple != MoveState.More) //Delete node
            {
                holdMultiplier -= 0.25f - (Vector3.Distance(transform.position, shootchain.transform.position) / nodes.Count);
            }
            nodes[0].GetComponent <Rigidbody2D>().velocity = (transform.position - nodes[0].transform.position) * 25;
            if (endVector.magnitude > 1)
            {
                GetComponent <Rigidbody2D>().AddForce(endVector.normalized * -1);
            }
            switch (moveGrapple)
            {
            case MoveState.More:
                if (Vector2.Distance(transform.position, nodes[1].transform.position) < 6)
                {
                    GetComponent <Rigidbody2D>().AddForce((transform.position - ball.transform.position).normalized * 1);
                }
                break;

            case MoveState.Less:
                GetComponent <Rigidbody2D>().AddForce((transform.position - ball.transform.position).normalized * -10);
                if (Vector2.Distance(transform.position, nodes[1].transform.position) < 6)
                {
                    GetComponent <Rigidbody2D>().AddForce((transform.position - ball.transform.position).normalized * 1);
                }
                break;

            default:
                break;
            }
            if (holdMultiplier < -1 && nodes.Count > 2 && Vector2.Distance(nodes[0].transform.position, nodes[1].transform.position) < 1) //Delete node
            {
                holdMultiplier = 0;
                Destroy(nodes[1]);
                nodes.RemoveAt(1);
                nodes[1].GetComponent <SpringJoint2D>().connectedBody = nodes[0].GetComponent <Rigidbody2D>();
            }
            if (holdMultiplier > 1 && nodes.Count <= 8) //Add node
            {
                holdMultiplier = 0;
                GameObject _node = Instantiate(nodePrefab, transform.position, Quaternion.identity);
                nodes[1].GetComponent <SpringJoint2D>().connectedBody = _node.GetComponent <Rigidbody2D>();
                nodes.Insert(1, _node);
                nodes[1].GetComponent <SpringJoint2D>().connectedBody = nodes[0].GetComponent <Rigidbody2D>();
            }
            if (!spin)
            {
                nodes[0].GetComponent <CircleCollider2D>().radius = .1f;
                //endnode.GetComponent<SpringJoint2D>().connectedBody = endchain.GetComponent<Rigidbody2D>();
                if (Vector3.Distance(transform.position, nodes[1].transform.position) > 1.5f && nodes.Count <= 8 && moveGrapple != MoveState.Less && moveGrapple != MoveState.Hold) //Add node
                {
                    holdMultiplier += 0.2f;
                }
            }
            else
            {
                transform.position = nodes[0].transform.position;
                nodes[0].GetComponent <CircleCollider2D>().radius = GetComponent <CircleCollider2D>().radius;
            }
        }
        else
        {
            state = GrappleState.None;
            if (nodes.Count >= 1)
            {
                nodes[0].transform.position = transform.position;
            }
        }
        //for
        //{
        //    Rigidbody2D _rb = ball.GetComponent<Rigidbody2D>();
        //    shootchain.GetComponent<Rigidbody2D>().velocity = (shootchain.transform.position - ball.transform.position) * -25;
        //    ball.GetComponent<Rigidbody2D>().AddForce((ball.transform.position - shootchain.transform.position).normalized * -1f);
        //    //if (Vector2.Distance(transform.position, ball.transform.position) > 6)
        //    //{
        //    //    _rb.AddForce((transform.position - ball.transform.position) * 1f);
        //    //    GetComponent<Rigidbody2D>().AddForce((transform.position - ball.transform.position) * -.05f);
        //    //}
        //    //ball.transform.position = Vector2.ClampMagnitude(ball.transform.position - transform.position, 8);

        //    for (int i = 0; i < hooks.childCount; i++)
        //    {
        //        if (Vector2.Distance(ball.transform.position, hooks.GetChild(i).position) < hookSize)
        //        {
        //            ball.transform.position = hooks.GetChild(i).position;
        //            //ball = null;
        //            state = GrappleState.Hooked;
        //            _rb.constraints = RigidbodyConstraints2D.FreezeAll;
        //        }
        //    }
        //}
        //if (Input.GetKeyUp(shoot))
        //{
        //    endnode = null;
        //    GameObject _chain = Instantiate(chain, transform);
        //    _chain.GetComponent<Rigidbody2D>().velocity = (transform.position - Camera.main.ScreenToWorldPoint(Input.mousePosition)).normalized * -shootSpeed;
        //}
    }
Exemplo n.º 26
0
    void Start()
    {
        throwWeapon = FindObjectOfType <ThrowWeapon>();

        state = GrappleState.NONE;
    }
Exemplo n.º 27
0
    //Execution of Grapple ability
    public void Activate(PlayerController controller)
    {
        //TODO functionise indicator updates
        #region Update Indicator
        Vector2 mousedir = controller.PlayerToMouseDir();
        float   angle    = Mathf.Atan2(mousedir.y, mousedir.x) * Mathf.Rad2Deg;

        Indicator.transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
        Indicator.transform.position = controller.transform.position;
        #endregion

        //Switch on core state
        switch (_grappleState)
        {
            #region Ready State
        case GrappleState.Ready:

            //Raycast in direction of mouse position from player, to grapple distance
            _rayCastGrapple = Physics2D.Raycast(controller.transform.position, controller.PlayerToMouseDir(), maxGrappleDistance, LayerMask.GetMask("Default"));

            if (_rayCastGrapple)
            {
                //Reveal indicator sprite at cast hit
                grappleSpriteRender.enabled      = true;
                grappleSprite.transform.position = _rayCastGrapple.point;
            }
            else
            {
                //Hide indicator sprite
                grappleSpriteRender.enabled = false;
            }

            //On input
            if (InputController.Instance.Ability.Down)
            {
                indicatorRenderer.enabled = false;

                if (_rayCastGrapple)
                {
                    //set initial grappling point and radius
                    initPivot = _rayCastGrapple.point;
                    curPivot  = initPivot;
                    _radius   = Vector3.Distance(controller.transform.position, initPivot);

                    _grappleSuccess = true;

                    //Grapple cannot be a success on death blocks
                    if (_rayCastGrapple.transform.CompareTag("Respawn"))
                    {
                        _grappleSuccess = false;
                    }
                }
                else
                {
                    //Set dummy point and radius for grapple anim
                    _radius         = maxGrappleDistance;
                    initPivot       = controller.PlayerToMouseDir() * maxGrappleDistance;
                    initPivot      += controller.transform.position;
                    _grappleSuccess = false;
                }

                //Set swing direction
                if (initPivot.x >= controller.transform.position.x)
                {
                    _swingDir = 1f;
                }
                else
                {
                    _swingDir = -1f;
                }

                //Init grapple sprite movmement during Firing
                grappleSprite.transform.position = controller.transform.position;
                grappleSpriteRender.enabled      = true;
                _grappSpriteMove = (initPivot - controller.transform.position).normalized * _radius / ThrowTime;

                //Init line render for grapple indicator
                _lineRender.enabled       = true;
                _lineRender.positionCount = 2;
                _lineRender.SetPosition(0, grappleSprite.transform.position);
                _lineRender.SetPosition(1, controller.transform.position);

                //Switch state
                _grappleState = GrappleState.Firing;
            }
            break;

            #endregion
            #region Firing State
        case GrappleState.Firing:


            if (_grappleTimer > 0)
            {
                _grappleTimer -= Time.deltaTime;

                //Move sprite along direction
                grappleSprite.transform.position += _grappSpriteMove * Time.deltaTime;

                //Update line render to follow grapple sprite and player pos
                _lineRender.SetPosition(0, grappleSprite.transform.position);
                _lineRender.SetPosition(1, controller.transform.position);
            }
            else
            {
                //If player is grounded at end of firing, do not swing
                if (controller.grounded)
                {
                    _grappleSuccess = false;
                }

                if (_grappleSuccess)
                {
                    //Clear line render
                    _lineRender.positionCount = 0;

                    //If player is above grapple point, wrapping state Floats; otherwise wrapping state wraps
                    _wrap = Wrapping.Wrap;
                    if (initPivot.y <= controller.transform.position.y)
                    {
                        _wrap = Wrapping.Float;
                        //Special case where float will exit into wrap
                        _specialFloatExit = true;
                        //reverse swing direction to swing correct direction when wrap state kicks in
                        _swingDir *= -1;
                    }

                    _grappleTimer = GrappleCooldown;

                    //Set Line Points
                    pivotPoints.Add(initPivot);
                    pivotPoints.Add(controller.transform.position);

                    SetLinePos();

                    _radius = Vector3.Distance(controller.transform.position, initPivot);
                    _radii.Add(_radius);

                    //Indicators
                    _lineRender.enabled         = true;
                    grappleSpriteRender.enabled = true;

                    //State
                    _grappleState = GrappleState.Connected;
                }
                else
                {
                    ResetGrapple();
                }
            }
            break;

            #endregion
            #region Connected State
        case GrappleState.Connected:

            //Always wrap and unwrap, swing by ?



            //Switch on swinging states
            switch (_wrap)
            {
                #region Wrap
            case Wrapping.Wrap:

                //Move the player
                controller.SetVelocity(Swing(controller) * SwingSpeed);

                //Wrap around obstructing obstacles
                RaycastHit2D pivotHit = Physics2D.Linecast(controller.transform.position, curPivot, LayerMask.GetMask("Default"));
                if (pivotHit)
                {
                    if (new Vector3(pivotHit.point.x, pivotHit.point.y, 0f) != curPivot)
                    {
                        curPivot = pivotHit.point;
                        _radius  = Vector3.Distance(controller.transform.position, curPivot);
                        _radii.Add(_radius);
                        pivotPoints.Insert(pivotPoints.Count - 1, pivotHit.point);
                        SetLinePos();
                    }
                }

                //always exits to float when player goes above latest wrap point
                if (controller.transform.position.y > curPivot.y)
                {
                    _wrap = Wrapping.Float;
                }
                break;

                #endregion
                #region Unwrap
            case Wrapping.UnWrap:

                //Move the player
                controller.SetVelocity(Swing(controller) * SwingSpeed);

                //_pivotPoints.Count is adjusted by -2 to ignore: initial grapple point and player position

                //If we have extra pivot points
                if (pivotPoints.Count > 2)
                {
                    //direction between last and second last
                    Vector3 aPivot = pivotPoints[pivotPoints.Count - 2];
                    Vector3 bPivot = pivotPoints[pivotPoints.Count - 3];

                    Vector3 pivDirection = (aPivot - bPivot).normalized;
                    Vector3 plaDirection = (controller.transform.position - aPivot).normalized;
                    //2D cross product
                    float cross = pivDirection.x * plaDirection.y - pivDirection.y * plaDirection.x;

                    if ((cross > 0 && _swingDir > 0) || (cross < 0 && _swingDir < 0))
                    {
                        //drop point
                        pivotPoints.RemoveAt(pivotPoints.Count - 2);
                        curPivot = pivotPoints[pivotPoints.Count - 2];
                        _radii.RemoveAt(_radii.Count - 1);
                        _radius = _radii[_radii.Count - 1];

                        SetLinePos();
                    }
                }

                //Based on swing direction, enter wrap state when passing the pivot point
                if (_swingDir < 0)
                {
                    if (controller.transform.position.x < pivotPoints[0].x)
                    {
                        _wrap = Wrapping.Wrap;
                    }
                }
                else
                {
                    if (controller.transform.position.x > pivotPoints[0].x)
                    {
                        _wrap = Wrapping.Wrap;
                    }
                }

                break;

                #endregion
                #region Float
            case Wrapping.Float:

                //Clamp player x movement while attached in float
                float wallmax = curPivot.x + _radius;
                float wallmin = curPivot.x - _radius;

                //overwrite player movement to clamp
                Vector2 move = controller.velocity * Time.deltaTime;
                if (controller.transform.position.x + move.x < wallmin || controller.transform.position.x + move.x > wallmax)
                {
                    controller.SetHorizontalVelocity(0);
                }

                //Passing over top pivot cuts line
                if ((_swingDir > 0 && controller.transform.position.x < pivotPoints[0].x) || (_swingDir <0 && controller.transform.position.x> pivotPoints[0].x))
                {
                    _lineRender.enabled         = false;
                    grappleSpriteRender.enabled = false;
                    _grappleState = GrappleState.Cooldown;
                }

                //Exits to UnWrap except for special case
                if (controller.transform.position.y < curPivot.y)
                {
                    _swingDir *= -1;
                    _wrap      = Wrapping.UnWrap;

                    if (_specialFloatExit)
                    {
                        _wrap             = Wrapping.Wrap;
                        _specialFloatExit = false;
                    }
                }
                break;
                #endregion
            }


            //Update player linerender point
            _lineRender.SetPosition(pivotPoints.Count - 1, controller.transform.position);
            //_lineRender.SetPosition(_pivotPoints.Count, controller.transform.position);

            //Cancel grapple swing
            //
            if (controller.grounded || InputController.Instance.Jump.Down || InputController.Instance.Ability.Down || controller.sidecollide || controller.ceiling)
            {
                _lineRender.enabled         = false;
                grappleSpriteRender.enabled = false;
                _grappleState = GrappleState.Cooldown;
            }

            break;

            #endregion
            #region Cooldown State
        case GrappleState.Cooldown:
            if (_grappleTimer > 0)
            {
                _grappleTimer -= Time.deltaTime;
            }
            else
            {
                ResetGrapple();
            }
            break;
            #endregion
        }
    }
Exemplo n.º 28
0
    void Update()
    {
        //Reset the last cached markerd object to it's original color
        if (mMarkedObject != null)
        {
            HighlightObject(mMarkedObject, GrappleOption.None);
            mMarkedObject = null;
        }
        Ray ray = Camera.main.ScreenPointToRay(mScreenCenter);

        if (Physics.Raycast(ray, out RaycastHit hit) && !mIsGrappling)
        {
            GameObject      hitCollider   = hit.collider.gameObject;
            GrappableObject grappleObject = hitCollider.GetComponent <GrappableObject>();
            if (grappleObject)
            {
                mHandR_defaultRotation = mHandR.rotation;
                mHandL_defaultRotation = mHandL.rotation;

                //if the collided object is not grappable return
                //get landing zone for hit
                MeshFilter meshFilter = hitCollider.GetComponent <MeshFilter>();

                Vector3 topCenter       = hit.collider.bounds.center + hit.collider.bounds.extents.y * Vector3.up;
                var     grapPosition    = hit.point;
                Vector3 landingPosition = topCenter + Vector3.up * 0.15f;

                //If a pre defined landing position was specified make the one that was defined
                Vector3 predefinedLandingPoint = grappleObject.GetPredefinedLandingPoint();
                if (predefinedLandingPoint != Vector3.zero)
                {
                    landingPosition = predefinedLandingPoint;
                }

                //Calculate object distance to decide of grapple is possible
                float dist = Vector3.Distance(grapPosition, mPlayerBody.position);

                float y_diff = Mathf.Abs(landingPosition.y - transform.position.y);

                Debug.Log("y_diff: " + y_diff);
                Debug.Log("dist: " + dist);

                if (dist < GRAPPLE_RADIUS_LENGTH && y_diff < GRAPPLE_MAX_HEIGHT_DIFF && grappleObject.GetGrappleType() != GrappleType.None)
                {
                    mMarkedObject = hitCollider;

                    //Mark the object as grappable
                    HighlightObject(mMarkedObject, GrappleOption.CanGrapple);


                    //If left mouse button is pressed Initiate grapple
                    if (Input.GetMouseButtonDown(0))
                    {
                        mGrappleState = GrappleState.Shot;

                        grappleObject.OnGrapped();

                        RotateHandToPoint(mHandR, grapPosition);
                        RotateHandToPoint(mHandL, grapPosition);

                        mHandL.DOScaleX(dist, .5f);
                        mHandR.DOScaleX(dist, .5f).OnComplete(() =>
                        {
                            mIsGrappling = true;

                            mGrappleState     = GrappleState.Attached;
                            mAttachedPosition = topCenter;

                            //Get grapping type
                            GrappleType type = grappleObject.GetGrappleType();

                            //Handle different types of grapple
                            switch (type)
                            {
                            case GrappleType.None:
                                _handsTrigger.Trigger();
                                mHandL.DOScaleX(1, .5f);
                                mHandR.DOScaleX(1, .5f).OnComplete(() =>
                                {
                                    FinishGrappling();
                                    _handsTrigger.Stop();
                                });
                                break;

                            case GrappleType.Heavy:
                                //dotween to the wanted position
                                ResetHandsScale();
                                _handsTrigger.Trigger();
                                mPlayerBody.DOMove(landingPosition, .5f).SetEase(Ease.OutQuad).OnComplete(() =>
                                {
                                    FinishGrappling();
                                    _handsTrigger.Stop();
                                });
                                break;

                            case GrappleType.Light:
                                //Snap the object to the player
                                ResetHandsScale();

                                var colliderTransform = hit.collider.gameObject.transform;
                                var dest = mPlayerBody.position + mPlayerBody.forward * 2;
                                _handsTrigger.Trigger();
                                colliderTransform.DOMove(new Vector3(dest.x, colliderTransform.position.y, dest.z), .5f)
                                .OnComplete(() =>
                                {
                                    FinishGrappling();
                                    _handsTrigger.Stop();
                                });
                                break;

                            case GrappleType.VeryLight:
                                //Snap the object to the player
                                ResetHandsScale();

                                hit.collider.gameObject.GetComponent <Rigidbody>().velocity = UnityEngine.Random.onUnitSphere * 20;

                                FinishGrappling();
                                break;

                            case GrappleType.Special:
                                Animator anim = hitCollider.GetComponent <Animator>();
                                if (!anim)
                                {
                                    throw new Exception("Special object should always have animators");
                                }
                                anim.SetTrigger("Hit");

                                _handsTrigger.Trigger();
                                mHandL.DOScaleX(1, .5f);
                                mHandR.DOScaleX(1, .5f).OnComplete(() =>
                                {
                                    FinishGrappling();
                                    _handsTrigger.Stop();
                                });
                                break;
                            }
                        });
                    }
                }
                else
                {
                    //If the object is too far away we would like to mark it as red
                    mMarkedObject = hit.collider.gameObject;

                    //Mark the object as grappable
                    HighlightObject(mMarkedObject, GrappleOption.CannotGrapple);
                }
            }
        }
    }
Exemplo n.º 29
0
    public void Activate(PlayerController player)
    {
        //Debug.DrawRay(player.transform.position, MousePosition(player).normalized * maxGrappleDistance, Color.red);
        Indicator.transform.position = player.transform.position;
        switch (_grappleState)
        {
        case GrappleState.Ready:

            //Ray
            Vector3 mousePos = new Vector3(InputController.Instance.Mouse.mouseX, InputController.Instance.Mouse.mouseY, 0f);
            mousePos = Camera.main.ScreenToWorldPoint(mousePos);

            mousePos.x = mousePos.x - player.transform.position.x;
            mousePos.y = mousePos.y - player.transform.position.y;

            float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
            Indicator.transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));



            _rayCastGrapple = Physics2D.Raycast(player.transform.position, MousePosition(player).normalized, maxGrappleDistance, LayerMask.GetMask("Default"));
            if (_rayCastGrapple)
            {
                //Render sprite at X
                grappleSpriteRender.enabled      = true;
                grappleSprite.transform.position = _rayCastGrapple.point;

                //Draw sprite in?
            }
            else
            {
                grappleSpriteRender.enabled = false;
            }



            if (InputController.Instance.Ability.Down)
            {
                IndicatorRenderer.enabled = false;
                _rayCastGrapple           = Physics2D.Raycast(player.transform.position, MousePosition(player).normalized, maxGrappleDistance, LayerMask.GetMask("Default"));
                if (_rayCastGrapple)
                {
                    _grapplePoint   = _rayCastGrapple.point;
                    _grappleSuccess = true;

                    //set radius on connection
                    _radius = Vector3.Distance(player.transform.position, _grapplePoint);

                    if (_rayCastGrapple.transform.CompareTag("Respawn"))
                    {
                        _grappleSuccess = false;
                    }
                }
                else
                {
                    _radius         = maxGrappleDistance;
                    _grapplePoint   = MousePosition(player).normalized *maxGrappleDistance;
                    _grapplePoint  += player.transform.position;
                    _grappleSuccess = false;
                }


                if (_grapplePoint.x >= player.transform.position.x)
                {
                    _swingDir = 1f;
                }
                else
                {
                    _swingDir = -1f;
                }

                grappleSprite.transform.position = player.transform.position;
                grappleSpriteRender.enabled      = true;
                _grappSpriteMove = (_grapplePoint - player.transform.position).normalized * _radius / ThrowTime;

                //linerender temp and jank
                _lineRender.enabled       = true;
                _lineRender.positionCount = 2;
                _lineRender.SetPosition(0, grappleSprite.transform.position);
                _lineRender.SetPosition(1, player.transform.position);

                _grappleState = GrappleState.Firing;
            }
            break;

        case GrappleState.Firing:

            // Simulate the movement of the grapple heading towards the fire direction
            if (_swingTime > 0)
            {
                _swingTime -= Time.deltaTime;

                //move sprite
                grappleSprite.transform.position += _grappSpriteMove * Time.deltaTime;

                //lineRender temp and jank
                _lineRender.SetPosition(0, grappleSprite.transform.position);
                _lineRender.SetPosition(1, player.transform.position);
            }
            else
            {
                //temp line render cleanup
                _lineRender.positionCount = 0;


                _wrap = Wrapping.Wrap;
                //adjust for y
                if (_grapplePoint.y <= player.transform.position.y)
                {
                    _swingDir *= -1;
                    _wrap      = Wrapping.Float;
                }

                //Set Line Points
                _pivotPoints.Add(_grapplePoint);
                _pivotPoints.Add(player.transform.position);
                SetLinePos();

                _radius = Vector3.Distance(player.transform.position, _grapplePoint);
                _radii.Add(_radius);

                grappleSpriteRender.enabled = false;

                if (player.grounded)
                {
                    _grappleSuccess = false;
                }

                if (_grappleSuccess)
                {
                    _lineRender.enabled         = true;
                    grappleSpriteRender.enabled = true;
                }

                _swingTime = GrappleCooldown;

                _grappleState = GrappleState.Connected;
            }

            break;

        case GrappleState.Connected:

            // Player travels along ray direction to object if raycast successfully hits
            if (_grappleSuccess)
            {
                //swing one way and then the other
                switch (_wrap)
                {
                case Wrapping.Wrap:

                    player.SetVelocity(Swing(player) * SwingSpeed);

                    //wrap
                    RaycastHit2D pivotHit = Physics2D.Linecast(player.transform.position, _grapplePoint, LayerMask.GetMask("Default"));
                    if (pivotHit)
                    {
                        if (new Vector3(pivotHit.point.x, pivotHit.point.y, 0f) != _grapplePoint)
                        {
                            _grapplePoint = pivotHit.point;
                            _radius       = Vector3.Distance(player.transform.position, _grapplePoint);
                            _radii.Add(_radius);
                            _pivotPoints.Insert(_pivotPoints.Count - 1, pivotHit.point);
                            SetLinePos();
                        }
                    }

                    //always exits to float

                    if (player.transform.position.y > _grapplePoint.y)
                    {
                        _wrap = Wrapping.Float;
                    }
                    break;

                case Wrapping.UnWrap:

                    player.SetVelocity(Swing(player) * SwingSpeed);

                    //if moving left
                    if (_swingDir < 0)
                    {
                        if (player.transform.position.y < _pivotPoints[_pivotPoints.Count - 2].y && _pivotPoints.Count > 2)
                        {
                            //drop point
                            _pivotPoints.RemoveAt(_pivotPoints.Count - 2);
                            _grapplePoint = _pivotPoints[_pivotPoints.Count - 2];
                            _radii.RemoveAt(_radii.Count - 1);
                            _radius = _radii[_radii.Count - 1];

                            SetLinePos();
                        }

                        if (player.transform.position.x < _pivotPoints[0].x)
                        {
                            _wrap = Wrapping.Wrap;
                        }
                    }
                    else if (_swingDir > 0)
                    {
                        if (player.transform.position.y < _pivotPoints[_pivotPoints.Count - 2].y && _pivotPoints.Count > 2)
                        {
                            //drop point
                            _pivotPoints.RemoveAt(_pivotPoints.Count - 2);
                            _grapplePoint = _pivotPoints[_pivotPoints.Count - 2];
                            _radii.RemoveAt(_radii.Count - 1);
                            _radius = _radii[_radii.Count - 1];

                            SetLinePos();
                        }

                        if (player.transform.position.x > _pivotPoints[0].x)
                        {
                            _wrap = Wrapping.Wrap;
                        }
                    }

                    break;

                case Wrapping.Float:

                    //Clamp to walls
                    float   wallmax = _grapplePoint.x + _radius;
                    float   wallmin = _grapplePoint.x - _radius;
                    Vector2 move    = player.velocity * Time.deltaTime;
                    if (player.transform.position.x + move.x < wallmin || player.transform.position.x + move.x > wallmax)
                    {
                        player.SetHorizontalVelocity(0);
                    }

                    //Passing over top pivot cuts line
                    //if coming from right
                    if (_swingDir > 0)
                    {
                        if (player.transform.position.x < _pivotPoints[0].x)
                        {
                            //break
                            _lineRender.enabled         = false;
                            grappleSpriteRender.enabled = false;
                            _grappleState = GrappleState.Cooldown;
                        }
                    }
                    else
                    {
                        if (player.transform.position.x > _pivotPoints[0].x)
                        {
                            //break
                            _lineRender.enabled         = false;
                            grappleSpriteRender.enabled = false;
                            _grappleState = GrappleState.Cooldown;
                        }
                    }


                    //always exits to unwrap
                    if (player.transform.position.y < _grapplePoint.y)
                    {
                        _swingDir *= -1;
                        _wrap      = Wrapping.UnWrap;
                    }
                    break;
                }

                _lineRender.SetPosition(_pivotPoints.Count - 1, player.transform.position);

                if (InputController.Instance.Jump.Down || InputController.Instance.Ability.Down || player.grounded || player.sidecollide || player.ceiling)
                {
                    _lineRender.enabled         = false;
                    grappleSpriteRender.enabled = false;
                    _grappleState = GrappleState.Cooldown;
                }
            }
            else
            {
                _pivotPoints.Clear();
                _radii.Clear();

                _lineRender.enabled       = false;
                _swingTime                = ThrowTime;
                IndicatorRenderer.enabled = true;
                _grappleState             = GrappleState.Ready;
            }
            break;

        case GrappleState.Cooldown:
            if (_swingTime > 0)
            {
                _swingTime -= Time.deltaTime;
            }
            else
            {
                _pivotPoints.Clear();
                _radii.Clear();

                _swingTime = ThrowTime;
                IndicatorRenderer.enabled = true;
                _grappleState             = GrappleState.Ready;
            }
            break;
        }
    }