当前位置: 首页>>代码示例>>C#>>正文


C# Projectile.CollideWithObstacle方法代码示例

本文整理汇总了C#中Projectile.CollideWithObstacle方法的典型用法代码示例。如果您正苦于以下问题:C# Projectile.CollideWithObstacle方法的具体用法?C# Projectile.CollideWithObstacle怎么用?C# Projectile.CollideWithObstacle使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Projectile的用法示例。


在下文中一共展示了Projectile.CollideWithObstacle方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: checkVerticalCollision

        private void checkVerticalCollision(Projectile p, int pxDown)
        {
            int forwardEdge = (int)p.Position.Y;

            //1 for down, -1 for up
            int yDirection = pxDown / Math.Abs(pxDown);

            //start at initial forward edge
            int startRow = forwardEdge / _collisionLayer.TileHeight;
            //assume unit moves full distance
            p.Position.Y += pxDown;
            //check up to new assumed unit hitrect edge
            int endRow = (forwardEdge + pxDown) / _collisionLayer.TileHeight;
            startRow = (int)MathHelper.Clamp(startRow, 0, _collisionLayer.LayerHeight - 1);
            endRow = (int)MathHelper.Clamp(endRow, 0, _collisionLayer.LayerHeight - 1);

            for (int row = startRow; row != (endRow + yDirection); row = row + yDirection)
            {
                for (int col = ((int)p.Position.X + 1) / _collisionLayer.TileWidth;
                    col <= (p.Position.X - 1) / _collisionLayer.TileWidth;
                    col++)
                {
                    //boundary checks----------------------------------------------------------------
                    if (col < 0)
                    {
                        p.CollideWithObstacle(Direction.West);
                        p.Position.X = 0;
                        break;
                    }
                    else if (col > _collisionLayer.LayerWidth)
                    {
                        p.CollideWithObstacle(Direction.East);
                        p.Position.Y = _collisionLayer.LayerWidth * _collisionLayer.TileWidth;
                        break;
                    }
                    if (row < 0)
                    {
                        p.CollideWithObstacle(Direction.North);
                        p.Position.Y = 0;
                        break;
                    }
                    else if (row > _collisionLayer.LayerHeight)
                    {
                        p.CollideWithObstacle(Direction.South);
                        p.Position.Y = _collisionLayer.LayerHeight * _collisionLayer.TileHeight;
                        break;
                    }
                    //--------------------------------------------------------------------------------

                    if (_collisionLayer.Tiles[col, row] != null && _collisionLayer.Tiles[col, row].TileIndex != 0)
                    {
                        if (pxDown > 0)
                        {
                            p.CollideWithObstacle(Direction.South);
                            p.Position.Y = row * _collisionLayer.TileHeight;
                        }
                        else
                        {
                            p.CollideWithObstacle(Direction.North);
                            p.Position.Y = (row + 1) * _collisionLayer.TileHeight;
                        }
                        break;  //no need to check more
                    }
                }
            }
        }
开发者ID:jhmills,项目名称:platformer4x4,代码行数:66,代码来源:Level.cs

示例2: checkHorizontalCollision

        private void checkHorizontalCollision(Projectile p, int pxRight)
        {
            //Get the coordinate of the forward-facing edge
            //If walking left, forwardEdge = left of bounding box
            //If walking right, forwardEdge = right of bounding box
            int forwardEdge = (int)p.Position.X;

            //-1 for left, 1 for right
            int xDirection = pxRight / Math.Abs(pxRight);

            int startCol = forwardEdge / _collisionLayer.TileWidth;
            int endCol = (forwardEdge + pxRight) / _collisionLayer.TileWidth;
            startCol = (int)MathHelper.Clamp(startCol, 0, _collisionLayer.LayerWidth - 1);
            endCol = (int)MathHelper.Clamp(endCol, 0, _collisionLayer.LayerWidth - 1);

            p.Position.X += pxRight;

            for (int col = startCol; col != (endCol + xDirection); col = col + xDirection)
            {
                for (int row = (int)p.Position.Y / _collisionLayer.TileHeight;
                    row <= p.Position.Y / _collisionLayer.TileHeight;
                    row++)
                {
                    if (!_collisionLayer.IsValidTileLocation(col, row))
                    {
                        continue;
                    }

                    //within bounds -- check tile collision
                    else if (_collisionLayer.Tiles[col, row] != null && _collisionLayer.Tiles[col, row].TileIndex != 0)
                    {
                        if (pxRight > 0)
                        {
                            p.CollideWithObstacle(Direction.East);
                            p.Position.X = col * _collisionLayer.TileWidth;
                        }
                        else
                        {
                            p.CollideWithObstacle(Direction.West);
                            p.Position.X = (col + 1) * _collisionLayer.TileWidth;
                        }
                        break;  //no need to check more
                    }
                }
            }
        }
开发者ID:jhmills,项目名称:platformer4x4,代码行数:46,代码来源:Level.cs


注:本文中的Projectile.CollideWithObstacle方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。