示例#1
0
 public override int GetHashCode()
 {
     if (is3D)
     {
         return(rb3D.GetHashCode());
     }
     else
     {
         return(rb2D.GetHashCode());
     }
 }
示例#2
0
    void Update()
    {
        currentHorizontal = Input.GetAxisRaw("Horizontal");


        if (!currentHorizontal.Equals(0.0f))
        {
            if (currentHorizontal > 0.0f)
            {
                rotationSign         = -1;
                currentJumpHookAngle = jumpHookAngle;
            }
            else
            {
                rotationSign         = 1;
                currentJumpHookAngle = 180f - jumpHookAngle;
            }
        }

        secondsSinceLastAttack += Time.deltaTime;
        isAttacking             = Input.GetKey(KeyCode.F);
        bool startedAttack = Input.GetKeyDown(KeyCode.F);

        if (secondsSinceLastAttack > attackIntervalSeconds && startedAttack)
        {
            secondsSinceLastAttack = 0;
            currentGlove.GetComponent <SpriteRenderer>().enabled = true;
            grappleAnimator.SetTrigger("GrappleAttack");
            attackedBodiesHashCode.Clear();
        }

        isActive = Input.GetKey(KeyCode.G);
        bool startedGrab = Input.GetKeyDown(KeyCode.G);

        if (isAttacking)
        {
            gloveCollider = currentGlove.GetComponent <CircleCollider2D>();
            Collider2D[] results    = new Collider2D[5];
            int          collisions = gloveCollider.OverlapCollider(filter, results);
            for (int i = 0; i < collisions; i++)
            {
                Rigidbody2D body = results[i].gameObject.GetComponent <Rigidbody2D>();
                if (attackedBodiesHashCode.Contains(body.GetHashCode()))
                {
                    Debug.Log("Duplicate hash " + body.GetHashCode());
                    continue;
                }
                attackedBodiesHashCode.Add(body.GetHashCode());
                float angle = rotationSign < 0
                    ? attackAngle
                    : 180 - attackAngle;

                Vector2 forceDirection    = new Vector2(Mathf.Cos(angle * deg2Rad), Mathf.Sin(angle * deg2Rad));
                float   attackForceScale  = baseAttack;
                Vector3 attackForceVector = new Vector3(attackForceScale, attackForceScale, attackForceScale);
                body.AddForce(Vector3.Scale(forceDirection, attackForceVector));
            }
        }
        else if (isActive)
        {
            if (startedGrab)
            {
                grappleAnimator.SetTrigger("GrappleGrab");
            }

            if (hitEntity == null)
            {
                grabHit = Physics2D.Raycast(entity.position, new Vector2(Mathf.Cos(rotation * deg2Rad), Mathf.Sin(rotation * deg2Rad)), currentExtension, mask);
                if (grabHit.collider != null)
                {
                    hitEntity      = grabHit.collider;
                    hitRigidbody2D = hitEntity.GetComponent <Rigidbody2D>();
                    if (hitRigidbody2D == null) // Fixed structure, not an entity
                    {
                        anchorPoint           = new Vector2(entity.position.x + maxExtension * Mathf.Cos(rotation * deg2Rad), entity.position.y + maxExtension * Mathf.Sin(rotation * deg2Rad));
                        joint.connectedAnchor = anchorPoint;
                        joint.enabled         = true;
                        joint.distance        = maxExtension;
                        Debug.Log("hit joint: " + hitEntity.name);
                    }
                }
            }
        }
        else
        {
            if (hitRigidbody2D != null)
            {
                Vector2 rotationDirection = new Vector2(Mathf.Cos(rotation * deg2Rad), Mathf.Sin(rotation * deg2Rad));
                Vector3 forceDirection    = Vector3.Cross(rotationDirection, Vector3.forward).normalized;

                float   dropForceDirection = -dropForce * rotationSign;
                Vector3 forceScale         = new Vector3(dropForceDirection, dropForceDirection, dropForceDirection);
                hitRigidbody2D.AddForce(Vector3.Scale(forceDirection, forceScale));
            }
            hitEntity      = null;
            hitRigidbody2D = null;
            joint.enabled  = false;
        }

        if (hitEntity != null)
        {
            if (hitRigidbody2D != null)
            {
                grabHit.transform.position = new Vector2(entity.position.x + currentExtension * Mathf.Cos(rotation * deg2Rad), entity.position.y + currentExtension * Mathf.Sin(rotation * deg2Rad));
            }
            else
            {
                // swing
            }
        }

        if (!isAttacking)
        {
            currentGlove.GetComponent <SpriteRenderer>().enabled = false;
        }
    }