Esempio n. 1
0
        protected bool doITouchTheBall(Ball b)
        {
            bool landImpliesFly = !isLandingNeeded || (Math.Round(b.Rotation).Equals(Math.Round(perspective)));
            bool touch          = CollisionUtilities.CircleIntersectsRectangle(360, b.Center, b.Radium, bounds());

            return(isEnabled && touch && landImpliesFly);
        }
    private void OnCollisionEnter2D(Collision2D collision)
    {
        // First contact with platform.
        if (collision.collider.CompareTag("Platform"))
        {
            this.platformCollider = collision.collider;
            this.enemyOffset      = (Vector3)this.enemyRB.position - this.platformCollider.bounds.center;
        }

        else if (collision.collider.CompareTag("Player"))
        {
            switch (CollisionUtilities.GetCollisionPosition(collision))
            {
            // Player hit the enemy on the top of the enemy's collision box.
            case CollisionUtilities.CollisionPosition.TOP:
                this.PlayerJumpOnHead(collision.gameObject);
                break;

            // Player hit on side.
            case CollisionUtilities.CollisionPosition.LEFT:
            case CollisionUtilities.CollisionPosition.RIGHT:
                this.StunPlayer(collision.gameObject);
                break;
            }
        }
    }
        public virtual void HanldeCollisions(IPhysicsHandler physics, ITransform transform)
        {
            UpdateRectangle(transform.Position);
            Vector2 collisionDisplacment = new Vector2(0, 0);

            foreach (var collidableObject in collidableObstacles)
            {
                if (CollisionUtilities.CheckRectangleCollision(CollisionRectangle, collidableObject.CollisionRectangle)) //Check of er een collision is
                {
                    collisionDisplacment = collidableObject.CollisionFix(physics, this);                                 //Los collision op met collisionfix van object waartegen gebotst werd
                }
            }
            transform.Position += collisionDisplacment;
            UpdateRectangle(transform.Position);
            physics.CollisionLeft  = false;
            physics.CollisionRight = false;
            physics.OnGround       = false;
            physics.CollisionTop   = false;
            foreach (var objectB in collidableObstacles) //Check of je ergens opstaat of niet
            {
                objectB.CollisionCheck(CollisionRectangle, physics);
            }
            UpdateRectangle(transform.Position); //Opnieuw update want nieuwe positie
            CollisionRectangleOld = CollisionRectangle;
        }
Esempio n. 4
0
    public static bool FullyContactingPlatform(Bounds platform, Bounds inner, float buffer)
    {
        float outerMin, outerMax;
        float innerMin, innerMax;

        CollisionUtilities.GetBoundsXLimits(platform, out outerMin, out outerMax);
        CollisionUtilities.GetBoundsXLimits(inner, out innerMin, out innerMax);

        return(outerMin - buffer <= innerMin && outerMax + buffer >= innerMax);
    }
 public override void HanldeCollisions(IPhysicsHandler physics, ITransform transform)
 {
     UpdateRectangle(transform.Position);
     foreach (var collidableObject in collidableObstacles)
     {
         if (CollisionUtilities.CheckRectangleCollision(CollisionRectangle, collidableObject.CollisionRectangle)) //Check of er een collision is
         {
             Explode(collidableObstacles);
         }
     }
     CollisionRectangleOld = CollisionRectangle;
 }
Esempio n. 6
0
 private void OnCollisionEnter2D(Collision2D collision)
 {
     switch (collision.gameObject.tag)
     {
     case "Player":
     case "Enemy":
         if (CollisionUtilities.GetCollisionPosition(collision) == CollisionUtilities.CollisionPosition.TOP)
         {
             collision.transform.parent = this.transform;
         }
         break;
     }
 }
Esempio n. 7
0
 protected void OnCollisionStay2D(Collision2D collision)
 {
     switch (collision.gameObject.tag)
     {
     case "Platform":
     case "Player":
         if (CollisionUtilities.GetCollisionPosition(collision) == CollisionUtilities.CollisionPosition.BOTTOM)
         {
             this.grounded = true;
         }
         break;
     }
 }
Esempio n. 8
0
 private void OnCollisionStay2D(Collision2D collision)
 {
     if (
         collision.gameObject.CompareTag("Platform") &&
         CollisionUtilities.GetCollisionPosition(collision) == CollisionUtilities.CollisionPosition.BOTTOM &&
         CollisionUtilities.FullyContactingPlatform(collision.collider.bounds, this.playerCollider.bounds, 0.25f)
         )
     {
         if (collision.collider != this.lastPlatformCollider)
         {
             this.lastPlatformCollider = collision.collider;
         }
         this.lastOffset = this.transform.position - this.lastPlatformCollider.bounds.center;
     }
 }
Esempio n. 9
0
 public override void HanldeCollisions(IPhysicsHandler physics, ITransform transform)
 {
     UpdateRectangle(transform.Position);
     foreach (var collidableObject in collidableObstacles)
     {
         if (CollisionUtilities.CheckRectangleCollision(CollisionRectangle, collidableObject.CollisionRectangle)) //Check of er een collision is
         {
             if (collidableObject.Parent is Human)
             {
                 ((Human)collidableObject.Parent).Health -= ((Bullet)Parent).Damage; //Als het mens is verminder HP met 1
                 ((Human)collidableObject.Parent).Hit     = true;
             }
             Parent.Alive = false; //Bullet destroys itself when hit
         }
     }
     CollisionRectangleOld = CollisionRectangle;
 }
Esempio n. 10
0
    public static CollisionPosition GetCollisionPosition(Collision2D collision)
    {
        ContactPoint2D[] contacts = collision.contacts;

        Vector2 point1 = contacts.FirstOrDefault().point;
        Vector2 point2 = contacts.LastOrDefault().point;

        Bounds otherColliderBounds = collision.collider.bounds;
        float  ignore, maxPoint;

        if (point1.x == point2.x)
        {
            CollisionUtilities.GetBoundsXLimits(otherColliderBounds, out ignore, out maxPoint);
            return(Mathf.Abs(maxPoint - point1.x) <= 0.05 ? CollisionPosition.LEFT : CollisionPosition.RIGHT);
        }

        CollisionUtilities.GetBoundsYLimits(otherColliderBounds, out ignore, out maxPoint);
        return(Mathf.Abs(maxPoint - point1.y) <= 0.05 ? CollisionPosition.BOTTOM : CollisionPosition.TOP);
    }
Esempio n. 11
0
    void FixedUpdate()
    {
        speedStretch = m_Rigidbody.velocity.y * Time.fixedDeltaTime;
        if (speedStretch > -0.05f)
        {
            speedStretch = -0.05f;
        }
        collisionDownEnter = collisionDown;
        collisionDown      = CollisionUtilities.GetCollision(this.gameObject,
                                                             //Vector3.down * (m_boxCollider.size.y / 2f), new Vector2(0.55f, -speedStretch * 2), layer, true);
                                                             Vector3.down * (m_boxCollider.size.y / 2f), new Vector2(0.55f, 0.1f), layer, true);
        collisionDownEnter = collisionDown != collisionDownEnter && collisionDown;

        if (collisionDown)
        {
            collisionDownTimer = coyoteTime;
        }
        collisionDownTimer -= Time.fixedDeltaTime;

        //speedStretch = m_Rigidbody.velocity.y * Time.fixedDeltaTime;
        //if (speedStretch < 0.05f) speedStretch = 0.05f;
        collisionUpEnter = collisionUp;
        collisionUp      = CollisionUtilities.GetCollision(this.gameObject,
                                                           //Vector3.up * (m_boxCollider.size.y / 2f), new Vector2(0.5f, speedStretch * 2), collisionUpLayer, true);
                                                           Vector3.up * (m_boxCollider.size.y / 2f), new Vector2(0.5f, 0.1f), collisionUpLayer, true);
        collisionUpEnter = collisionUp != collisionUpEnter;

        collisionUpLeft = CollisionUtilities.GetCollision(this.gameObject,
                                                          Vector3.up * (m_boxCollider.size.y / 2f + 0.1f) + Vector3.left * 0.24f, new Vector2(0.1f, 0.3f), collisionUpLayer);
        collisionUpRight = CollisionUtilities.GetCollision(this.gameObject,
                                                           Vector3.up * (m_boxCollider.size.y / 2f + 0.1f) + Vector3.right * 0.24f, new Vector2(0.1f, 0.3f), collisionUpLayer);
        collisionUpMiddle = CollisionUtilities.GetCollision(this.gameObject,
                                                            Vector3.up * (m_boxCollider.size.y / 2f + 0.1f), new Vector2(0.4f, 0.3f), collisionUpLayer);

        collisionDanger = CollisionUtilities.GetCollision(this.gameObject,
                                                          Vector3.zero, new Vector2(0.6f, 1.2f), dangerLayer);

        collisionLeftStep = CollisionUtilities.GetCollisionDistance(this.gameObject,
                                                                    Vector2.left * (m_boxCollider.size.x / 2f + (0.1f * detectDistance)), Vector2.down, m_boxCollider.size.y / 2f, layer);
        collisionRightStep = CollisionUtilities.GetCollisionDistance(this.gameObject,
                                                                     Vector2.right * (m_boxCollider.size.x / 2f + (0.1f * detectDistance)), Vector2.down, m_boxCollider.size.y / 2f, layer);
    }
    private void FixedUpdate()
    {
        if (this.platformCollider != null)
        {
            if (!CollisionUtilities.FullyContactingPlatform(this.platformCollider.bounds, this.enemyCollider.bounds, 0f))
            {
                this.direction *= -1;
            }

            this.enemyOffset     += Vector3.right * this.direction * Time.deltaTime;
            this.enemyRB.position = this.platformCollider.bounds.center + this.enemyOffset;
        }
        else if (this.dead)
        {
            Vector3 positionInViewport = UnityEngine.Camera.main.WorldToViewportPoint(this.enemyRB.position);
            if (!new Rect(0, 0, 1, 1).Contains(positionInViewport))
            {
                Object.Destroy(this.gameObject);
            }
        }
    }
Esempio n. 13
0
    void FixedUpdate()
    {
        if (isCarried)
        {
            collisionDownLayer = collisionUpLayer;
            gameObject.layer   = LayerUtilities.LayerNumber(carriedIdolLayer);
        }
        else
        {
            collisionDownLayer = layer;
            gameObject.layer   = LayerUtilities.LayerNumber(idolLayer);
        }

        speedStretchX = m_Rigidbody.velocity.x * Time.fixedDeltaTime;
        speedStretchY = m_Rigidbody.velocity.y * Time.fixedDeltaTime;
        if (speedStretchY > -0.05f)
        {
            speedStretchY = -0.05f;
        }
        collisionDownEnter = collisionDown;
        collisionDown      = CollisionUtilities.GetCollision(this.gameObject,
                                                             Vector3.down * -speedStretchY + Vector3.right * speedStretchX,
                                                             new Vector2(0.25f, 0.6f), collisionDownLayer, true) ||
                             (m_Velocity.y > yVelocityBuffer && yVelocityBuffer <= 0f && !isCarried);
        collisionDownEnter = collisionDown != collisionDownEnter;

        speedStretchY = m_Rigidbody.velocity.y * Time.fixedDeltaTime;
        if (speedStretchY < 0.05f)
        {
            speedStretchY = 0.05f;
        }
        collisionUpEnter = collisionUp;
        collisionUp      = CollisionUtilities.GetCollision(this.gameObject,
                                                           Vector3.up * (m_boxCollider.size.y / 2f + 0.05f), new Vector2(0.25f, (speedStretchY + 0.05f) * 2f), collisionUpLayer, true);
        collisionUpEnter = collisionUp != collisionUpEnter;

        collisionLeft = CollisionUtilities.GetCollision(this.gameObject,
                                                        Vector3.left * (m_boxCollider.size.x / 2f) + Vector3.down * 0.05f, new Vector2(0.1f, 0.5f), collisionUpLayer);

        collisionRight = CollisionUtilities.GetCollision(this.gameObject,
                                                         Vector3.right * (m_boxCollider.size.x / 2f) + Vector3.down * 0.05f, new Vector2(0.1f, 0.5f), collisionUpLayer);

        collisionDanger = CollisionUtilities.GetCollision(this.gameObject,
                                                          Vector3.zero, new Vector2(0.3f, 0.6f), dangerLayer);

        collisionCarryPlayer = CollisionUtilities.GetCollisionDistance(player.gameObject,
                                                                       Vector2.zero, Vector2.up, carryHeight + 0.6f, player.GetCollisionUpLayer());

        collisionCarry = CollisionUtilities.GetCollisionDistance(player.gameObject,
                                                                 new Vector2(m_Rigidbody.position.x - playerRigidbody.position.x, 0f),
                                                                 Vector2.up, carryHeight + 0.6f, collisionUpLayer);

        collisionTeleport = CollisionUtilities.GetCollision(this.gameObject,
                                                            Vector3.up * 0.35f + Vector3.left * 0.225f, new Vector2(0.15f, 1.1f), collisionUpLayer) &&
                            CollisionUtilities.GetCollision(this.gameObject,
                                                            Vector3.up * 0.35f + Vector3.right * 0.225f, new Vector2(0.15f, 1.1f), collisionUpLayer);

        float n; if (teleportRechargeTimer < 0)

        {
            n = 0;
        }
        else
        {
            n = teleportRechargeTimer;                                                 /////
        }
        CollisionUtilities.GetCollision(this.gameObject,
                                        Vector3.right * 0.5f, new Vector2(0.1f, n * 3f), layer, true);

        teleportTimer         -= Time.fixedDeltaTime;
        teleportRechargeTimer -= Time.fixedDeltaTime;
        rechargeIndicator.SetFill(teleportRechargeTimer / teleportRechargeDuration);
        yVelocityBuffer = m_Velocity.y;
    }
Esempio n. 14
0
 protected bool doITouchBall(Ball b)
 {
     return(CollisionUtilities.CircleIntersectsRectangle(30, b.Center, b.Radium, getBounds()));
 }
Esempio n. 15
0
        protected bool doITouchTheBall(Ball b)
        {
            RectangleF block = new RectangleF(currentX - 1f, currentY - 1f, Constants.BlockWidth + 2f, Constants.BlockWidth + 2f);

            return(CollisionUtilities.CircleIntersectsRectangle(30, b.Center, b.Radium, block));
        }
Esempio n. 16
0
        public void ProcessMovementAndRotation(float deltaX, float deltaY)
        {
            float     targetAngle;
            Direction direction = Direction.D;

            // No input.
            if (deltaX == 0 && deltaY == 0)
            {
                return;
            }

            // Find the Direction we are trying to move towards.
            // U
            if (deltaX == 0 && deltaY < 0)
            {
                direction = Direction.U;
            }

            // UR
            if (deltaX > 0 && deltaY < 0)
            {
                direction = Direction.UR;
            }

            // R
            if (deltaX > 0 && deltaY == 0)
            {
                direction = Direction.R;
            }

            // DR
            if (deltaX > 0 && deltaY > 0)
            {
                direction = Direction.DR;
            }

            // D
            if (deltaX == 0 && deltaY > 0)
            {
                direction = Direction.D;
            }

            // DL
            if (deltaX < 0 && deltaY > 0)
            {
                direction = Direction.DL;
            }

            // L
            if (deltaX < 0 && deltaY == 0)
            {
                direction = Direction.L;
            }

            // UL
            if (deltaX < 0 && deltaY < 0)
            {
                direction = Direction.UL;
            }

            // Rotate at max rotate speed towards our target angle;
            targetAngle = DirectionAngleMapping.map[direction];

            if (this.Rotation != targetAngle)
            {
                if (Math.Abs(this.Rotation - targetAngle) % MathHelper.TwoPi < this.RotationSpeed)
                {
                    this.Rotation = targetAngle;
                }

                // 0 is an annoying special case.
                else if (this.Rotation == 0)
                {
                    if (targetAngle >= Math.PI)
                    {
                        this.Rotation -= this.RotationSpeed;
                    }

                    else
                    {
                        this.Rotation += this.RotationSpeed;
                    }
                }

                else
                {
                    if (this.Rotation >= targetAngle)
                    {
                        if (this.Rotation - Math.PI >= targetAngle)
                        {
                            this.Rotation += this.RotationSpeed;
                        }
                        else
                        {
                            this.Rotation -= this.RotationSpeed;
                        }
                    }
                    else
                    {
                        if (this.Rotation + Math.PI >= targetAngle)
                        {
                            this.Rotation += this.RotationSpeed;
                        }
                        else
                        {
                            this.Rotation -= this.RotationSpeed;
                        }
                    }
                }

                // counterclockwise from 0 means we subtract from two pi
                if (this.Rotation < 0f)
                {
                    this.Rotation = MathHelper.TwoPi + this.Rotation;
                }

                // if we have gone above two pi, mod it, two pi is a full rotation.
                this.Rotation = this.Rotation % MathHelper.TwoPi;

                return;
            }

            // If we are facing our target angle, then move that direction.
            if (deltaX != 0 && deltaY != 0)
            {
                deltaX = deltaX / 2;
                deltaY = deltaY / 2;
            }

            int MaxX = Game.viewport.Width - this.Sprite.Width;
            int MinX = 0 + this.Sprite.Width;
            int MaxY = Game.viewport.Height - this.Sprite.Height;
            int MinY = 0 + Sprite.Height;

            this.Position.X += deltaX;
            this.Position.Y += deltaY;

            bool willCollide = Utils.CollisionUtilities.CheckWallCollision(this);

            for (int i = 0; i < GhostGame.Game.Hunters.Length; i++)
            {
                if (i == this.PlayerNumber || GhostGame.Game.Hunters[i] == null || willCollide)
                {
                    continue;
                }

                if (CollisionUtilities.SmartCollisionCheck(this, GhostGame.Game.Hunters[i]))
                {
                    willCollide = true;
                    break;
                }
            }

            if (willCollide)
            {
                this.Position.X -= deltaX;
                this.Position.Y -= deltaY;
            }

            if (this.Position.X > MaxX)
            {
                this.Position.X = MaxX;
            }

            else if (this.Position.X < MinX)
            {
                this.Position.X = MinX;
            }

            if (this.Position.Y > MaxY)
            {
                this.Position.Y = MaxY;
            }

            else if (this.Position.Y < MinY)
            {
                this.Position.Y = MinY;
            }
        }