Beispiel #1
0
 private void collisionWithObstacle(ObstacleEntity entity)
 {
     if ((this.RendererObj as ObstacleRenderer).State == Shared.Constants.OBSTACLE_CANMOVE)
     {
         ObstacleRenderer renderer = this.RendererObj as ObstacleRenderer;
         switch (renderer.Direction)
         {
             case Shared.Constants.DIRECTION_LEFT:
                 if (entity.RendererObj.Position.X < this.RendererObj.Position.X)
                 {
                     renderer.rollBack();
                     renderer.clearMovement();
                 }
                 break;
             case Shared.Constants.DIRECTION_RIGHT:
                 if (entity.RendererObj.Position.X > this.RendererObj.Position.X)
                 {
                     renderer.rollBack();
                     renderer.clearMovement();
                 }
                 break;
             case Shared.Constants.DIRECTION_UP:
                 if (entity.RendererObj.Position.Y < this.RendererObj.Position.Y)
                 {
                     renderer.rollBack();
                     renderer.clearMovement();
                 }
                 break;
             case Shared.Constants.DIRECTION_DOWN:
                 if (entity.RendererObj.Position.Y > this.RendererObj.Position.Y)
                 {
                     renderer.rollBack();
                     renderer.clearMovement();
                 }
                 break;
         }
     }
 }
Beispiel #2
0
 private void collisionWithObstacle(ObstacleEntity obstacle)
 {
     Rectangle collisionRect = Utilities.Collision.CollisionRange(obstacle.Bound, this.Bound);
     if (collisionRect.Width >= 6 && collisionRect.Height >= 6)
     {
         if (!isChangeDirection)
         {
             (this.RendererObj as EnemyRenderer).ChangeNegativeDirection((this.RendererObj as EnemyRenderer).direction);
             //(this.RendererObj as EnemyRenderer).randomDirection();
             isChangeDirection = true;
         }
     }
 }
Beispiel #3
0
        private void collisionWithObstacle(ObstacleEntity obstacle)
        {
            if (obstacle.isWalkable)
                return;

            BomberRenderer renderer = RendererObj as BomberRenderer;
            BomberLogical logical = LogicalObj as BomberLogical;

            ObstacleRenderer obsRenderer = (obstacle.RendererObj as ObstacleRenderer);
            ObstacleLogical obsLogical = (obstacle.LogicalObj as ObstacleLogical);

            #region Replace Position
            Vector2 newVelocity = Vector2.Zero;

            bool rightDirection = false;

            switch (renderer.direction)
            {
                case Shared.Constants.DIRECTION_LEFT:
                    rightDirection = (obsLogical.Bound.X <= renderer.Position.X) ? true : false;
                    if (rightDirection)
                        newVelocity.X = obsLogical.Bound.X + obsLogical.Bound.Width - renderer.Position.X;
                    break;
                case Shared.Constants.DIRECTION_RIGHT:
                    rightDirection = (obsLogical.Bound.X >= renderer.Position.X) ? true : false;
                    if(rightDirection)
                        newVelocity.X =  obsLogical.Bound.X - logical.Bound.X - logical.Bound.Width;
                    break;
                case Shared.Constants.DIRECTION_UP:
                    rightDirection = (obsLogical.Bound.Y <= renderer.Position.Y) ? true : false;
                    if(rightDirection)
                        newVelocity.Y = obsLogical.Bound.Y + obsLogical.Bound.Height - renderer.Position.Y;
                    break;
                case Shared.Constants.DIRECTION_DOWN:
                    rightDirection = (obsLogical.Bound.Y >= renderer.Position.Y) ? true : false;
                    if (rightDirection)
                        newVelocity.Y = obsLogical.Bound.Y - logical.Bound.Y - logical.Bound.Height;
                    break;
            }

            if (!rightDirection)
            {
                Vector2 newPos = renderer.Position;
                switch (renderer.oldDirection)
                {
                    case Shared.Constants.DIRECTION_LEFT:
                        newPos.X = obsRenderer.Position.X + obsLogical.Bound.Width;
                        break;
                    case Shared.Constants.DIRECTION_RIGHT:
                        newPos.X = obsRenderer.Position.X - logical.Bound.Width;
                        break;
                    case Shared.Constants.DIRECTION_UP:
                        newPos.Y = obsRenderer.Position.Y + obsLogical.Bound.Height;
                        break;
                    case Shared.Constants.DIRECTION_DOWN:
                        newPos.Y = obsRenderer.Position.Y - logical.Bound.Height;
                        break;
                }
                renderer.Position = newPos;
            }
            #endregion

            #region Adjust Position

            Cell obsCell = Grid.Grid.getInst().GetCellAtPosition(obsRenderer.Position);
            if (obsCell != null)
            {
                if (renderer.direction == Shared.Constants.DIRECTION_DOWN || renderer.direction == Shared.Constants.DIRECTION_UP)
                {
                    Cell cellLeft = Grid.Grid.getInst().GetCellAtLocation(new Vector2(obsCell.Location.X, obsCell.Location.Y - 1));
                    Cell cellRight = Grid.Grid.getInst().GetCellAtLocation(new Vector2(obsCell.Location.X, obsCell.Location.Y + 1));
                    if (cellLeft != null && cellLeft.Contents.Count == 0 &&
                        Math.Abs(obsRenderer.Position.X - (renderer.Position.X + LogicalObj.Bound.Width)) <= Shared.Constants.COLLISION_MIN)
                    {
                        newVelocity.X += obsRenderer.Position.X - (renderer.Position.X + LogicalObj.Bound.Width);
                    }
                    else if (cellRight != null && cellRight.Contents.Count == 0 &&
                        Math.Abs((obsRenderer.Position.X + obstacle.LogicalObj.Bound.Width) - renderer.Position.X) <= Shared.Constants.COLLISION_MIN)
                    {
                        newVelocity.X += (obsRenderer.Position.X + obstacle.LogicalObj.Bound.Width) - renderer.Position.X;
                    }
                }
                else if (renderer.direction == Shared.Constants.DIRECTION_LEFT || renderer.direction == Shared.Constants.DIRECTION_RIGHT)
                {
                    Cell cellUp = Grid.Grid.getInst().GetCellAtLocation(new Vector2(obsCell.Location.X - 1, obsCell.Location.Y));
                    Cell cellDown = Grid.Grid.getInst().GetCellAtLocation(new Vector2(obsCell.Location.X + 1, obsCell.Location.Y));
                    if (cellUp != null && cellUp.Contents.Count == 0 &&
                        Math.Abs(obsRenderer.Position.Y - (renderer.Position.Y + LogicalObj.Bound.Height)) <= Shared.Constants.COLLISION_MIN)
                    {
                        newVelocity.Y += obsRenderer.Position.Y - (renderer.Position.Y + LogicalObj.Bound.Height);
                    }
                    else if (cellDown != null && cellDown.Contents.Count == 0 &&
                        Math.Abs((obsRenderer.Position.Y + obstacle.LogicalObj.Bound.Height) - renderer.Position.Y) <= Shared.Constants.COLLISION_MIN)
                    {
                        newVelocity.Y += (obsRenderer.Position.Y + obstacle.LogicalObj.Bound.Height) - renderer.Position.Y;
                    }
                }
            }
            #endregion

            //             #region Collision With Movement Obstacle
            //             // Get obstacle move if it can move
            //             if (rightDirection && obsRenderer.State == Shared.Constants.OBSTACLE_CANMOVE && obsRenderer.Direction == Shared.Constants.DIRECTION_NONE
            //                 && ((Math.Abs(renderer.Position.X - obsRenderer.Position.X) <= 10 && (renderer.direction == Shared.Constants.DIRECTION_UP || renderer.direction == Shared.Constants.DIRECTION_DOWN))
            //                 || (Math.Abs(renderer.Position.Y - obsRenderer.Position.Y) <= 10 && (renderer.direction == Shared.Constants.DIRECTION_LEFT || renderer.direction == Shared.Constants.DIRECTION_RIGHT))))
            //             {
            //                 obsRenderer.Move(renderer.direction);
            //             }
            //             #endregion

            if (newVelocity.Length() != 0)
            {
                renderer.MoveImmediately(newVelocity);
                renderer.updateMovement();
            }

            renderer.refreshAccelerator();
        }
 private void collisionWithObstacle(ObstacleEntity bomber)
 {
 }