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


C# Character.BoundingBox方法代码示例

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


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

示例1: checkTileCollision

        /// <summary>
        /// Will check if the given character has collided with a tile
        /// in the level and then handle the position correction.
        /// ENSURE:   the given character will not be colliding with any tiles.
        /// </summary>
        private void checkTileCollision(Character.Character character)
        {
            //for every tile in the level
              foreach (Tile tile in this.level_tiles)
              {
            //save the character's bounding box
            Rectangle character_bounds = character.BoundingBox();

            //now get the character's current velocity
            Vector2 character_velocity = character.currentSpeed();
            //compute the future position of the character
            Rectangle future_bounds = new Rectangle(
              character_bounds.X + (int)character_velocity.X,
              character_bounds.Y + (int)character_velocity.Y,
              character_bounds.Width, character_bounds.Height);

            //check if the character WILL collide with this tile
            if (tile.collidesWith(future_bounds)){
              //find how much overlap is occuring
              Rectangle intersection = Rectangle.Intersect(tile.destinationRectangle, future_bounds);

              //alter the characters velocity to take the collision into account
              Vector2 offset = new Vector2();
              //if the character has horizontal velocity
              //and isn't jumping or falling
              if ((character.isJumping() == false && character.isFalling() == false)
            && character_velocity.X != 0)
              {
            //compute the horizontal offset
            offset.X = (character_velocity.X > 0) ?
              (intersection.X - intersection.Right) : //negative offset if positive velocity
              (intersection.Right - intersection.X); //positive offset if negative velocity

            //add the offset to the velocity value
            //character_velocity.X = character_velocity.X + offset.X;
              }
              //if the character has vertical velocity
              if (character_velocity.Y != 0)
              {
            //they are jumping or falling into this object

            //first fix their vertical forces
            //if the character has a positive vertical velocity
            if (character_velocity.Y > 0)
            {
              //then the offset needs to be negative
              offset.Y = (intersection.Y - intersection.Bottom) -1;
              //this also means the character has hit ground
              //character.ground();
            }
            //otherwise, the velocity is negative
            else
            {
              //so the offset needs to be positive
              offset.Y = (intersection.Bottom - intersection.Y);
              //the character hit the bottom of something, fall
              character.fall();
            }

            //now they might have horizontal forces,
            //before we fix them, see if the character is no longer colliding
            //compute the future position of the character
            Rectangle new_future_bounds = new Rectangle(
              future_bounds.X + (int)offset.X,
              future_bounds.Y + (int)offset.Y,
              future_bounds.Width, future_bounds.Height);
            if (tile.collidesWith(new_future_bounds))
            {
              //then fix the horizontal forces
              Rectangle new_intersection = Rectangle.Intersect(tile.destinationRectangle, new_future_bounds);

              //compute the horizontal offset
              offset.X = (character_velocity.X > 0) ?
                (intersection.X - intersection.Right) : //negative offset if positive velocity
                (intersection.Right - intersection.X); //positive offset if negative velocity
            }
              }
              character_velocity.X = character_velocity.X + offset.X;
              character_velocity.Y = character_velocity.Y + offset.Y;
              //give the player it's new position
              character.setVelocity(character_velocity);
            }
              }
        }
开发者ID:kWhittington,项目名称:CSCI3097_HW3,代码行数:89,代码来源:Level.cs

示例2: isUnder

            /// <summary>
            /// Returns whether or not the tile is under the given character.
            /// ENSURE:   if the tile is immidiately below the character,
            ///            return true
            ///           otherwise,
            ///            return false
            /// </summary>
            internal bool isUnder(Character.Character character)
            {
                bool result = false;
                Rectangle box = character.BoundingBox();

                //if the box is within the correct horizontal scope
                if ((box.Left <= this.destinationRectangle.Right &&
                  box.Left >= this.destinationRectangle.Left) ||
                  (box.Right <= this.destinationRectangle.Right &&
                  box.Right >= this.destinationRectangle.Left))
                {
                  //if this tile is immidiate below the character
                  if (box.Bottom < this.destinationRectangle.Top &&
                box.Bottom > (this.destinationRectangle.Top - 5))
                  {
                result = true;
                  }
                }

                return result;
            }
开发者ID:kWhittington,项目名称:CSCI3097_HW3,代码行数:28,代码来源:Level.cs

示例3: checkCharacterCollision

 /// <summary>
 /// Will check if the given character has collided with another character.
 /// If so, the opposing character will be removed from play.
 /// </summary>
 private void checkCharacterCollision(Character.Character character)
 {
     //for every enemy alive
       foreach (Character.Enemy enemy in this.character_manager.enemyList())
       {
     //if the enemy is alive
     if (enemy.isAlive() == true)
     {
       //check if it is overlapping with the given character
       if (enemy.BoundingBox().Intersects(character.BoundingBox()))
       {
     //then remove the enemy from play
     enemy.kill();
       }
     }
       }
 }
开发者ID:kWhittington,项目名称:CSCI3097_HW3,代码行数:21,代码来源:Level.cs


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