Exemplo n.º 1
0
 public override void UnselectWorldSelection()
 {
     if (WorldSelection != null)
     {
         WorldSelection.Unselect();
         WorldSelection = null;
     }
 }
Exemplo n.º 2
0
 public void Deselect()
 {
     if (selected != null)
     {
         selected.Deselect();
         selected = null;
     }
 }
Exemplo n.º 3
0
    private void Update()
    {
        Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Physics.Raycast(ray.origin, ray.direction, out hit, 100f, raycastLayer, QueryTriggerInteraction.Collide))
        {
            hover = hit.transform.GetComponent <IWorldSelectable>();
            if (hover != null)
            {
                if (prevHover != hover)
                {
                    prevHover = hover;
                    hover.Hover();
                }
            }
            else
            {
                if (prevHover != null)
                {
                    prevHover.Dehover();
                    prevHover = null;
                }
            }
        }

        if (Input.GetMouseButtonDown(0))
        {
            mouseDownTime = Time.unscaledTime;
            mouseDownPos  = Input.mousePosition;
        }
        if (Input.GetMouseButtonUp(0))
        {
            if (hover != null && Time.unscaledTime - mouseDownTime < .25f && (Input.mousePosition - mouseDownPos).sqrMagnitude < 25f)
            {
                if (pointerClick != null)
                {
                    pointerClick();
                }
                else
                {
                    Click();
                }
            }
        }
    }
Exemplo n.º 4
0
    void Update()
    {
        if (Global.Paused)
        {
            return;
        }

        UpdateCursor();

        string anim           = "idle";
        bool   previousGround = onGround;

        onGround = collideBottom || (collideLeft && collideRight);
        if (onGround && !previousGround)
        {
            landing = true;
            landTimer.Start(landDuration, null, delegate { landing = false; });
        }
        wallsliding = false;
        // must have input (or push) to move horizontally, so allow no persistent horizontal velocity (without push)
        if (grapPulling)
        {
            velocity = Vector3.zero;
        }
        else if (!(input.MoveRight || input.MoveLeft))
        {
            velocity.x = 0;
        }

        if (carryCharacter != null)
        {
            velocity = carryCharacter.velocity;
        }

        // WEAPONS / ABILITIES
        if (input.Fire)
        {
            Shoot();
        }

        if (input.ChargeStart)
        {
            StartCharge();
        }

        if (input.ChargeEnd)
        {
            ShootCharged();
        }

        if (input.Graphook)
        {
            ShootGraphook();
        }

        if (input.NextWeapon)
        {
            NextWeapon();
        }
        //Shield.SetActive( input.Shield );

        if (input.MoveDown)
        {
            hanging = false;
        }

        if (input.Interact)
        {
            if (WorldSelection != null && WorldSelection != closestISelect)
            {
                WorldSelection.Unselect();
            }
            WorldSelection = closestISelect;
            if (WorldSelection != null)
            {
                WorldSelection.Select();
                if (WorldSelection is Pickup)
                {
                    Pickup pickup = (Pickup)closestISelect;
                    if (pickup.weapon != null)
                    {
                        if (!weapons.Contains(pickup.weapon))
                        {
                            weapons.Add(pickup.weapon);
                            AssignWeapon(pickup.weapon);
                        }
                    }
                    else if (pickup.ability != null)
                    {
                        secondary = pickup.ability;
                        secondary.Activate(this);
                        Destroy(pickup.gameObject);
                    }
                }
            }
        }

        if (takingDamage)
        {
            velocity.y = 0;
        }
        else
        {
            if (input.MoveRight)
            {
                facingRight = true;
                // move along floor if angled downwards
                Vector3 hitnormalCross = Vector3.Cross(hitBottomNormal, Vector3.forward);
                if (onGround && hitnormalCross.y < 0)
                {
                    // add a small downward vector for curved surfaces
                    velocity = hitnormalCross * moveSpeed + Vector3.down * downslopefudge;
                }
                else
                {
                    velocity.x = moveSpeed;
                }
                if (!facingRight && onGround)
                {
                    StopDash();
                }
            }

            if (input.MoveLeft)
            {
                facingRight = false;
                // move along floor if angled downwards
                Vector3 hitnormalCross = Vector3.Cross(hitBottomNormal, Vector3.back);
                if (onGround && hitnormalCross.y < 0)
                {
                    // add a small downward vector for curved surfaces
                    velocity = hitnormalCross * moveSpeed + Vector3.down * downslopefudge;
                }
                else
                {
                    velocity.x = -moveSpeed;
                }
                if (facingRight && onGround)
                {
                    StopDash();
                }
            }

            if (input.DashStart && (onGround || collideLeft || collideRight))
            {
                StartDash();
            }

            if (input.DashEnd && !jumping)
            {
                StopDash();
            }

            if (dashing)
            {
                if (onGround && !previousGround)
                {
                    StopDash();
                }
                else if (facingRight)
                {
                    if (onGround || input.MoveRight)
                    {
                        velocity.x = dashSpeed;
                    }
                    if ((onGround || collideRight) && !dashTimer.IsActive)
                    {
                        StopDash();
                    }
                }
                else
                {
                    if (onGround || input.MoveLeft)
                    {
                        velocity.x = -dashSpeed;
                    }
                    if ((onGround || collideLeft) && !dashTimer.IsActive)
                    {
                        StopDash();
                    }
                }
            }

            if (onGround && input.JumpStart)
            {
                StartJump();
            }
            else
            if (input.JumpEnd)
            {
                StopJump();
            }
            else
            if (collideRight && input.MoveRight && hitRight.normal.y >= 0)
            {
                if (input.JumpStart)
                {
                    walljumping = true;
                    velocity.y  = jumpSpeed;
                    Push(Vector2.left * (input.DashStart ? dashSpeed : wallJumpPushVelocity), wallJumpPushDuration);
                    jumpRepeatTimer.Start(jumpRepeatInterval);
                    walljumpTimer.Start(wallJumpPushDuration, null, delegate { walljumping = false; });
                    audio.PlayOneShot(soundJump);
                    Instantiate(walljumpEffect, WallkickPosition.position, Quaternion.identity);
                }
                else if (!jumping && !walljumping && !onGround && velocity.y < 0)
                {
                    wallsliding = true;
                    velocity.y += (-velocity.y * wallSlideFactor) * Time.deltaTime;
                    dashSmoke.transform.localPosition = new Vector3(0.2f, -0.2f, 0);
                    if (!dashSmoke.isPlaying)
                    {
                        dashSmoke.Play();
                    }
                }
            }
            else if (collideLeft && input.MoveLeft && hitLeft.normal.y >= 0)
            {
                if (input.JumpStart)
                {
                    walljumping = true;
                    velocity.y  = jumpSpeed;
                    Push(Vector2.right * (input.DashStart ? dashSpeed : wallJumpPushVelocity), wallJumpPushDuration);
                    jumpRepeatTimer.Start(jumpRepeatInterval);
                    walljumpTimer.Start(wallJumpPushDuration, null, delegate { walljumping = false; });
                    audio.PlayOneShot(soundJump);
                    Instantiate(walljumpEffect, WallkickPosition.position, Quaternion.identity);
                }
                else if (!jumping && !walljumping && !onGround && velocity.y < 0)
                {
                    wallsliding = true;
                    velocity.y += (-velocity.y * wallSlideFactor) * Time.deltaTime;
                    dashSmoke.transform.localPosition = new Vector3(0.2f, -0.2f, 0);
                    if (!dashSmoke.isPlaying)
                    {
                        dashSmoke.Play();
                    }
                }
            }
        }

        if (velocity.y < 0)
        {
            jumping     = false;
            walljumping = false;
            walljumpTimer.Stop(false);
        }

        if (!((onGround && dashing) || wallsliding))
        {
            dashSmoke.Stop();
        }

        if (grapPulling)
        {
            Vector3 armpos = GetShotOriginPosition();
            grapCableRender.transform.position = armpos;
            grapCableRender.transform.rotation = Quaternion.LookRotation(Vector3.forward, Vector3.Cross(Vector3.forward, (graphookTip.transform.position - armpos)));
            grapSize             = grapCableRender.size;
            grapSize.x           = Vector3.Distance(graphookTip.transform.position, armpos);
            grapCableRender.size = grapSize;

            Vector3 grapDelta = graphitpos - transform.position;
            if (grapDelta.magnitude < grapStopDistance)
            {
                StopGrap();
            }
            else if (grapDelta.magnitude > 0.01f)
            {
                velocity = grapDelta.normalized * grapPullSpeed;
            }
        }

        // add gravity before velocity limits
        velocity.y -= Global.Gravity * Time.deltaTime;
        // grap force
        if (!grapPulling && pushTimer.IsActive)
        {
            velocity.x = pushVelocity.x;
        }

        if (hanging)
        {
            velocity = Vector3.zero;
        }

        // limit velocity before adding to position
        if (collideRight)
        {
            velocity.x     = Mathf.Min(velocity.x, 0);
            pushVelocity.x = Mathf.Min(pushVelocity.x, 0);
        }
        if (collideLeft)
        {
            velocity.x     = Mathf.Max(velocity.x, 0);
            pushVelocity.x = Mathf.Max(pushVelocity.x, 0);
        }
        // "onGround" is not the same as "collideFeet"
        if (onGround)
        {
            velocity.y      = Mathf.Max(velocity.y, 0);
            pushVelocity.x -= (pushVelocity.x * friction) * Time.deltaTime;
        }
        if (collideTop)
        {
            velocity.y = Mathf.Min(velocity.y, 0);
        }
        velocity.y = Mathf.Max(velocity.y, -Global.MaxVelocity);

        transform.position += (Vector3)velocity * Time.deltaTime;
        carryCharacter      = null;

        UpdateHit(Time.deltaTime);
        // update collision flags, and adjust position before render
        UpdateCollision(Time.deltaTime);

        if (health <= 0)
        {
            anim = "death";
        }
        else if (takingDamage)
        {
            anim = "damage";
        }
        else if (walljumping)
        {
            anim = "walljump";
        }
        else if (jumping)
        {
            anim = "jump";
        }
        else if (wallsliding)
        {
            anim = "wallslide";
        }
        else if (onGround)
        {
            if (dashing)
            {
                anim = "dash";
            }
            else if (input.MoveRight || input.MoveLeft)
            {
                anim = "run";
            }
            else if (landing)
            {
                anim = "land";
            }
            else
            {
                anim = "idle";
                // when idling, always face aim direction
                facingRight = shoot.x >= 0;
            }
        }
        else if (!jumping)
        {
            anim = "fall";
        }

        Play(anim);
        transform.localScale = new Vector3(facingRight ? 1 : -1, 1, 1);
        renderer.material.SetInt("_FlipX", facingRight ? 0 : 1);
        arm.localScale = new Vector3(facingRight ? 1 : -1, 1, 1);
        arm.rotation   = Quaternion.LookRotation(Vector3.forward, Vector3.Cross(Vector3.forward, shoot));
        if (wallsliding && collideRight)
        {
            transform.rotation = Quaternion.LookRotation(Vector3.forward, new Vector3(wallSlideNormal.y, -wallSlideNormal.x));
        }
        else if (wallsliding && collideLeft)
        {
            transform.rotation = Quaternion.LookRotation(Vector3.forward, new Vector3(-wallSlideNormal.y, wallSlideNormal.x));
        }
        else
        {
            transform.rotation = Quaternion.Euler(0, 0, 0);
        }

        ResetInput();
    }
Exemplo n.º 5
0
    new void UpdateHit(float dT)
    {
        pups.Clear();
        hitCount = Physics2D.BoxCastNonAlloc(transform.position, box.size, 0, velocity, RaycastHits, Mathf.Max(raylength, velocity.magnitude * dT), HitLayers);
        for (int i = 0; i < hitCount; i++)
        {
            hit = RaycastHits[i];
            if (hit.transform.IsChildOf(transform))
            {
                continue;
            }
            ITrigger tri = hit.transform.GetComponent <ITrigger>();
            if (tri != null)
            {
                tri.Trigger(transform);
            }
            IDamage dam = hit.transform.GetComponent <IDamage>();
            if (dam != null)
            {
                // maybe only damage other if charging weapon or has active powerup?
                if (ContactDamage != null)
                {
                    Damage dmg = Instantiate(ContactDamage);
                    dmg.instigator   = this;
                    dmg.damageSource = transform;
                    dmg.point        = hit.point;
                    dam.TakeDamage(dmg);
                }
            }
        }

        pups.Clear();
        hitCount = Physics2D.CircleCastNonAlloc(transform.position, selectRange, Vector3.zero, RaycastHits, 0, Global.WorldSelectableLayers);
        for (int i = 0; i < hitCount; i++)
        {
            hit = RaycastHits[i];
            IWorldSelectable pup = hit.transform.GetComponent <IWorldSelectable>();
            if (pup != null)
            {
                pups.Add((Component)pup);

                /*if( !highlightedPickups.Contains( pup ) )
                 * {
                 * pup.Highlight();
                 * highlightedPickups.Add( pup );
                 * }*/
            }
        }
        //WorldSelectable closest = (WorldSelectable)FindClosest( transform.position, pups.ToArray() );
        IWorldSelectable closest = (IWorldSelectable)Util.FindSmallestAngle(transform.position, shoot, pups.ToArray());

        if (closest == null)
        {
            if (closestISelect != null)
            {
                closestISelect.Unhighlight();
                closestISelect = null;
            }
            if (WorldSelection != null)
            {
                WorldSelection.Unselect();
                WorldSelection = null;
            }
        }
        else if (closest != closestISelect)
        {
            if (closestISelect != null)
            {
                closestISelect.Unhighlight();
            }
            closestISelect = closest;
            closestISelect.Highlight();
        }

        /*highlightedPickupsRemove.Clear();
         * foreach( var pup in highlightedPickups )
         * if( !pups.Contains( pup ) )
         * {
         *  pup.Unhighlight();
         *  highlightedPickupsRemove.Add( pup );
         * }
         * foreach( var pup in highlightedPickupsRemove )
         * highlightedPickups.Remove( pup );
         */
    }
Exemplo n.º 6
0
 void Click()
 {
     Deselect();
     selected = hover;
     hover.Select();
 }