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


C# Rectangle.GetCollisionType方法代码示例

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


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

示例1: Process

        public override void Process(Entity entity)
        {
            Entity player = (Entity)BlackBoard.GetEntry("Player");
            CollisionBox playerBox = player.GetComponent<CollisionBox>();
            Position playerPosition = player.GetComponent<Position>();
            Rectangle playerRectangle = new Rectangle(
                (int)playerPosition.X + playerBox.OffsetX,
                (int)playerPosition.Y + playerBox.OffsetY,
                playerBox.Width,
                playerBox.Height);

            CollisionBox enemyBox = entity.GetComponent<CollisionBox>();
            Position enemyPosition = entity.GetComponent<Position>();
            Rectangle enemyRectangle = new Rectangle(
                (int)enemyPosition.X + enemyBox.OffsetX,
                (int)enemyPosition.Y + enemyBox.OffsetY,
                enemyBox.Width,
                enemyBox.Height);

            var type = playerRectangle.GetCollisionType(enemyRectangle);

            if (type == CollisionType.None || !entity.HasComponent<Script>()) return;
            Script script = entity.GetComponent<Script>();
            _scriptManager.QueueScript(script.FilePath, entity, "OnTouch");
        }
开发者ID:zunath,项目名称:MMXEngine,代码行数:25,代码来源:EnemySystem.cs

示例2: ProcessLevelCollisions

        private void ProcessLevelCollisions(Entity entity)
        {
            Entity level = _world.EntityManager.GetEntities(Aspect.All(typeof(Map)))[0];
            if (level == null) return;
            Map map = level.GetComponent<Map>();
            CollisionBox box = entity.GetComponent<CollisionBox>();
            Position position = entity.GetComponent<Position>();
            Rectangle bounds = new Rectangle(
                (int)position.X + box.OffsetX,
                (int)position.Y + box.OffsetY,
                box.Width,
                box.Height
                );

            int leftTile = (int) Math.Floor((float) bounds.Left/TilesetConstants.TileWidth);
            int rightTile = (int)Math.Ceiling(((float)bounds.Right / TilesetConstants.TileWidth)) - 1;
            int topTile = (int) Math.Floor((float) bounds.Top/TilesetConstants.TileHeight);
            int bottomTile = (int) Math.Ceiling(((float) bounds.Bottom/TilesetConstants.TileHeight)) - 1;

            for (int y = topTile; y <= bottomTile; ++y)
            {
                for (int x = leftTile; x <= rightTile; ++x)
                {
                    if (x >= map.Width || x < 0 ||
                        y >= map.Height || y < 0)
                        continue;

                    TileData tile = map.Tiles[x, y];

                    if (tile != null && tile.IsCollidable)
                    {
                        Rectangle tileBounds = new Rectangle(
                            x * TilesetConstants.TileWidth,
                            y * TilesetConstants.TileHeight,
                            TilesetConstants.TileWidth,
                            TilesetConstants.TileHeight
                            );

                        Vector2 depth = bounds.GetIntersectionDepth(tileBounds);
                        var type = bounds.GetCollisionType(tileBounds);

                        // Entity's bottom collided with top of tile.
                        if (type == CollisionType.Top)
                        {
                            position.Y += depth.Y;
                            position.IsOnGround = true;
                            break;
                        }
                        else if (type == CollisionType.Left)
                        {

                        }
                        else if (type == CollisionType.Right)
                        {

                        }
                        else if (type == CollisionType.Bottom)
                        {

                        }

                    }
                }
            }
        }
开发者ID:zunath,项目名称:MMXEngine,代码行数:65,代码来源:CollisionSystem.cs


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