public void OnCollisionEnter(Collision collision)
    {
        MovingElement movingElement = collision.gameObject.GetComponent <MovingElement>();

        if (movingElement != null)
        {
            movingPlateform = movingElement;
        }
    }
Example #2
0
 private void SpawnHiddenElements(List <NewElementInfo> newElements, out List <MovingElement> hiddenElements)
 {
     hiddenElements = new List <MovingElement>();
     foreach (NewElementInfo newElement in newElements)
     {
         SpawnVisualElement(newElement.coordinate.x, newElement.coordinate.y, newElement.element, out RectTransform cell);
         MovingElement movingElement = new MovingElement(newElement.coordinate, cell);
         hiddenElements.Add(movingElement);
     }
 }
Example #3
0
        private List <MovingElement> GetOldElements(List <Vector2Int> coordinates)
        {
            List <MovingElement> oldElements = new List <MovingElement>();

            foreach (var coord in coordinates)
            {
                RectTransform elementTransform = _elements[coord.x, coord.y];
                MovingElement oldElement       = new MovingElement(coord, elementTransform);
                oldElements.Add(oldElement);
            }

            return(oldElements);
        }
    private void FixedUpdate()
    {
        if (Horizontal == null || JumpKey == KeyCode.None)
        {
            return;
        }

        //Check if player is outside the map bondaries
        if (gameIsRunning && topLeftDeath.magnitude != 0 && (transform.position.x < topLeftDeath.x || transform.position.x > bottomRightDeath.x || transform.position.y > topLeftDeath.y || transform.position.y < bottomRightDeath.y))
        {
            Die();
        }

        bool isGrounded = IsGrounded();
        bool isSliding  = IsSliding();
        bool isPushing  = IsPushing();
        bool airControl = AirControl(isGrounded);

        //Check if we have left the plateform
        if (movingPlateform != null && !isGrounded && !groundedLastFrame && !isSliding)
        {
            movingPlateform = null;
        }

        //Reload dash
        if (isGrounded || isPushing)
        {
            canDash = true;
        }


        if (-1 < velocity && velocity < 1)
        {
            velocity = 0;
        }

        if (velocity > 0)
        {
            velocity -= 0.5f;
        }
        else if (velocity < 0)
        {
            velocity += 0.5f;
        }

        float horizontalVel = Input.GetAxis(Horizontal);

        //Change air velocity after wall jump
        if (wallJumped)
        {
            if (-0.3 < horizontalVel && horizontalVel < 0.3)
            {
                horizontalVel = 0.6f * jumpDirection;
            }
            if (Mathf.Sign(horizontalVel) == Mathf.Sign(horizontalVel))
            {
                horizontalVel /= 2;
            }
        }

        //Handle hook display
        if (hookType == HookType.Wall)
        {
            if (hook != null)
            {
                hook.GetComponent <LineRenderer>().SetPosition(0, rb.position);
            }
        }
        else if (hookType == HookType.SmallProjectile && objectHooked != null && hook != null)
        {
            hook.GetComponent <LineRenderer>().SetPositions(new Vector3[] { rb.position, objectHooked.transform.position });
        }
        else if (hookType == HookType.LargeProjectile)
        {
            if (objectHooked != null)
            {
                if (hook != null)
                {
                    hook.GetComponent <LineRenderer>().SetPositions(new Vector3[] { rb.position, objectHooked.transform.position });
                }

                hookPosition = objectHooked.transform.position;
                GetComponent <SpringJoint>().connectedAnchor = hookPosition;
            }
        }

        //Handle dash after user pressed the button
        if (dashTime > 0)
        {
            rb.velocity     = dashVelocity;
            dashVelocity.y -= dashVelocity.y / 10;
            dashTime--;

            if (dashTime == 0)
            {
                //Create projectile
                GameObject proj = Instantiate(smallProjectile, rb.position + new Vector3(rb.velocity.x / 10, rb.velocity.y / 5, 0), Quaternion.identity);
                proj.name = "SmallProjectile";
                proj.GetComponent <Rigidbody>().velocity = new Vector3(dashVelocity.x * sProjSpeed, dashVelocity.y * sProjSpeed, 0);
                proj.GetComponent <Projectile>().sender  = this;

                dashVelocity = Vector3.zero;
            }
        }

        //Make player move
        if (!isSliding || (isSliding && Mathf.Sign(wallDirection) != Mathf.Sign(Input.GetAxis(Horizontal))))
        {
            if (dashTime <= 0)
            {
                if (hookType != HookType.Wall && hookType != HookType.LargeProjectile)
                {
                    //Normal movement
                    if (!channeling)
                    {
                        rb.AddForce(new Vector3(horizontalVel * (isPushing ? pushSpeed : 1) * (airControl ? airSpeed : speed) - (rb.velocity.x - velocity), 0, 0), ForceMode.Impulse);
                    }
                }
                else
                {
                    //Rope swing movement
                    if (rb.position.y < hookPosition.y - hookLength / 2)
                    {
                        rb.AddForce(new Vector3(Input.GetAxis(Horizontal) * ropeSwing, 0, 0));
                    }
                }
            }
        }

        //Channel energy
        if (channeling)
        {
            if (!Input.GetKey(ChannelKey) && !Input.GetKey(ChannelKey2))
            {
                StartCoroutine(CancelChanneling());
            }
            else
            {
                energy += Time.deltaTime;
                energy  = Mathf.Clamp(energy, 0, 3);
            }
        }
        else if (Input.GetKey(ChannelKey) || Input.GetKey(ChannelKey2))
        {
            channeling = true;
        }

        //Cancel player movement and abilities if he is channeling
        if (channeling)
        {
            return;
        }

        //Make user jump
        if (Input.GetKey(JumpKey) && isGrounded)
        {
            jumpDirection = Input.GetAxis(Horizontal);
            rb.velocity   = new Vector3(rb.velocity.x, jumpForce + (movingPlateform != null ? movingPlateform.rb.velocity.y : 0), rb.velocity.z);
        }
        //Make a small jump if user drop the button
        if (Input.GetKeyUp(JumpKey) && !isGrounded && rb.velocity.y > smallJump)
        {
            if (wallJumped && Mathf.Abs(rb.velocity.x) > smallJumpPush)
            {
                velocity    = smallJumpPush * jumpDirection;
                rb.velocity = new Vector3(rb.velocity.x - (wallJumpPush - smallJumpPush) * jumpDirection, rb.velocity.y, rb.velocity.z);
            }
            else
            {
                rb.velocity = new Vector3(rb.velocity.x, smallJump, rb.velocity.z);
            }
        }

        //Move with the plateform
        if (movingPlateform != null)
        {
            rb.AddForce(new Vector3(movingPlateform.rb.velocity.x, 0, 0), ForceMode.Impulse);
        }

        //Apply more gravity
        if (rb.velocity.y < 1 && !isSliding && hookType == HookType.None)
        {
            rb.AddForce(new Vector3(0, gravity, 0), ForceMode.Acceleration);
        }

        //WallSlide
        if (wallJumpTimer > 0)
        {
            //Wall Jump
            if (Input.GetKeyDown(JumpKey) && !isGrounded)
            {
                wallJumped    = true;
                jumpDirection = -wallDirection;
                velocity      = wallJumpPush * jumpDirection;
                rb.AddForce(new   Vector3(wallJumpPush * jumpDirection, wallJump, 0), ForceMode.Impulse);
            }
            else if (isSliding)
            {
                rb.AddForce(new Vector3(0, Mathf.Abs(rb.velocity.y) + gravity / 2, 0), ForceMode.Acceleration);
            }
        }

        //Hook
        if (hookType != HookType.None)
        {
            if (!Input.GetKey(HookKey) || (objectHooked == null && (hookType == HookType.SmallProjectile || hookType == HookType.LargeProjectile)))
            {
                Destroy(hook);
                SpringJoint spring = GetComponent <SpringJoint>();
                if (spring != null)
                {
                    spring.connectedAnchor = new Vector3(0, 0, 0);
                    spring.connectedBody   = null;
                    spring.spring          = 0;
                    spring.damper          = 0;
                    spring.maxDistance     = 0;
                }
                hookType     = HookType.None;
                hookPosition = Vector3.zero;
                hookLength   = 0;
            }
        }
        else
        {
            if (Input.GetKeyDown(HookKey))
            {
                if (hook != null)
                {
                    Destroy(hook);
                }

                Vector3 direction = NormaliseMovement(Input.GetAxis(Horizontal), Input.GetAxis(Vertical));

                RaycastHit hit;
                if (Physics.Raycast(rb.position, direction, out hit, hookRange))
                {
                    if (hit.collider.tag == "Player")
                    {
                        hit.collider.GetComponent <PlayerMovement>().Hooked(direction, rb.position);
                        hookType = HookType.Player;
                    }
                    else if (hit.collider.tag == "SmallProjectile")
                    {
                        hookType = HookType.SmallProjectile;
                        hook     = Instantiate(hookObject, rb.position, Quaternion.identity);
                        hook.GetComponent <LineRenderer>().SetPositions(new Vector3[] { rb.position, hit.point });
                        objectHooked = hit.collider.gameObject;

                        SpringJoint spring = GetComponent <SpringJoint>();
                        spring.spring        = springForce;
                        spring.damper        = damperForce;
                        spring.maxDistance   = Vector3.Distance(rb.position, hit.point);
                        spring.connectedBody = hit.collider.GetComponent <Rigidbody>();
                        StartCoroutine(CancelHook());
                    }
                    else if (hit.collider.tag == "LargeProjectile")
                    {
                        hookType = HookType.LargeProjectile;
                        hook     = Instantiate(hookObject, rb.position, Quaternion.identity);
                        hook.GetComponent <LineRenderer>().SetPositions(new Vector3[] { rb.position, hit.point });
                        objectHooked = hit.collider.gameObject;

                        SpringJoint spring = GetComponent <SpringJoint>();
                        spring.connectedAnchor = hit.point;
                        spring.spring          = springForce;
                        spring.damper          = largeDamper;
                        spring.maxDistance     = Vector3.Distance(rb.position, hit.point);

                        hookPosition = hit.point;
                        hookLength   = Vector3.Distance(rb.position, hit.point);
                    }
                    else
                    {
                        hookType = HookType.Wall;
                        SpringJoint spring = GetComponent <SpringJoint>();
                        spring.connectedAnchor = hit.point;
                        spring.spring          = springForce;
                        spring.damper          = damperForce;
                        spring.breakForce      = Mathf.Infinity;
                        spring.maxDistance     = Vector3.Distance(rb.position, hit.point);

                        hook = Instantiate(hookObject, rb.position, Quaternion.identity);
                        hook.GetComponent <LineRenderer>().SetPositions(new Vector3[] { rb.position, hit.point });

                        hookPosition = hit.point;
                        hookLength   = Vector3.Distance(rb.position, hit.point);
                    }
                }
                else
                {
                    print("Mised");
                }
            }
        }

        //Dash and small attack
        if (canDash && Input.GetKeyDown(DashKey))
        {
            Vector3 movement = NormaliseMovement(Input.GetAxis(Horizontal), Input.GetAxis(Vertical));

            dashTime     = 8;
            dashVelocity = new Vector3(dashSpeed * movement.x, dashSpeed * movement.y, 0);

            rb.velocity   = new Vector3(dashSpeed * movement.x, dashSpeed * movement.y, 0);
            velocity      = dashSpeed * movement.x;
            jumpDirection = movement.x;
            canDash       = false;
        }

        //Ult
        if (Input.GetKeyDown(UltKey))
        {
            Vector3 direction = NormaliseMovement(Input.GetAxis(Horizontal), Input.GetAxis(Vertical));

            if (energy < 1)
            {
                //Boop
            }
            else if (energy < 3)
            {
                //Large projectile
                energy -= 1;

                GameObject proj = Instantiate(largeProjectile, rb.position + direction * 4, Quaternion.identity);
                proj.name = "LargeProjectile";
                proj.GetComponent <Rigidbody>().velocity = new Vector3(direction.x * lProjSpeed, direction.y * lProjSpeed, 0);
                proj.GetComponent <Projectile>().sender  = this;
            }
            else if (energy == 3)
            {
                //Swap projectile
                energy -= 3;
                GameObject proj = Instantiate(swapProjectile, rb.position + direction, Quaternion.identity);
                proj.name = "SwapProjectile";
                proj.GetComponent <Rigidbody>().velocity = new Vector3(direction.x * SwapProjSpeed, direction.y * SwapProjSpeed, 0);
                proj.GetComponent <Projectile>().sender  = this;
            }
        }
    }