示例#1
0
        private static PhysicalVector2 GetStartingPosition(
            Physical.PosRectangle playerPosRectangle,
            Physical.Orientation.Cardinal orientation,
            Physical.Rectangle rectangle
            )
        {
            PhysicalVector2 result =
                playerPosRectangle.GetCenteredBound(orientation);

            switch (orientation)
            {
            case Physical.Orientation.Cardinal.Left:
                result.X -= (rectangle.Width / 2);
                break;

            case Physical.Orientation.Cardinal.Right:
                result.X += (rectangle.Width / 2);
                break;

            case Physical.Orientation.Cardinal.Up:
                result.Y -= (rectangle.Height / 2);
                break;

            case Physical.Orientation.Cardinal.Down:
                result.Y += (rectangle.Height / 2);
                break;
            }

            return(result);
        }
示例#2
0
        public static bool CheckCollision(
            Rectangle rectangle1,
            PhysicalVector2 position1,
            Rectangle rectangle2,
            PhysicalVector2 position2
            )
        {
            var posRect1 = new PosRectangle(position1, rectangle1);
            var posRect2 = new PosRectangle(position2, rectangle2);

            // Check for collision.
            return
                (posRect1.Left < posRect2.Right &&
                 posRect1.Right > posRect2.Left &&
                 posRect1.Top < posRect2.Bottom &&
                 posRect1.Bottom > posRect2.Top);
        }
示例#3
0
        // Find the smallest correction we can apply to the first rectangle that
        // makes it so the rectangles are no longer colliding.
        public static PhysicalVector2 GetMinCorrection(
            Rectangle rectangle1,
            PhysicalVector2 position1,
            Rectangle rectangle2,
            PhysicalVector2 position2
            )
        {
            var posRect1 = new PosRectangle(position1, rectangle1);
            var posRect2 = new PosRectangle(position2, rectangle2);

            var xCorrection = new PhysicalVector2(0, 0);
            var yCorrection = new PhysicalVector2(0, 0);

            // Check how far we have to push the rectangle out on the x direction.
            if (position1.X <= position2.X)
            {
                xCorrection.X = posRect2.Left - posRect1.Right;
            }
            else
            {
                xCorrection.X = posRect2.Right - posRect1.Left;
            }

            // Check how far we have to push the rectangle out on the y direction.
            if (position1.Y <= position2.Y)
            {
                yCorrection.Y = posRect2.Top - posRect1.Bottom;
            }
            else
            {
                yCorrection.Y = posRect2.Bottom - posRect1.Top;
            }

            // See which correction pushes the rectangle out the least and use that as our correction.
            if (xCorrection.Length() < yCorrection.Length())
            {
                return(xCorrection);
            }
            else
            {
                return(yCorrection);
            }
        }
示例#4
0
文件: Util.cs 项目: Zeldorks/Zeldorks
        public static System.Collections.IEnumerable OverlappingTiles(
            IMap map,
            Physical.PosRectangle posRectangle
            )
        {
            var startX = (int)(posRectangle.Left / map.TileWidth);
            var startY = (int)(posRectangle.Top / map.TileHeight);
            var endX   = (int)(posRectangle.Right / map.TileWidth) + 1;
            var endY   = (int)(posRectangle.Bottom / map.TileHeight) + 1;

            startX = Math.Clamp(startX, 0, map.Width - 1);
            endX   = Math.Clamp(endX, 0, map.Width - 1);
            startY = Math.Clamp(startY, 0, map.Height - 1);
            endY   = Math.Clamp(endY, 0, map.Height - 1);

            for (int x = startX; x <= endX; x++)
            {
                for (int y = startY; y <= endY; y++)
                {
                    int index = x + y * map.Width;
                    yield return(index);
                }
            }
        }
示例#5
0
        public static Entity Create(
            Comps.Projectile.Kind kind,
            Entity owner,
            Ecs.Registry registry
            )
        {
            // Assume that `owner` has position and orientation
            // components

            var playerPositionComp = registry
                                     .GetComponentUnsafe <Comps.Position>(owner);

            var playerRectangleComp = registry
                                      .GetComponentUnsafe <Comps.Shapes.Rectangle>(owner);

            var playerOrientationComp = registry
                                        .GetComponentUnsafe <Comps.Orientations.Cardinal>(owner);

            var playerPosRectangle = new Physical.PosRectangle(
                playerPositionComp.data,
                playerRectangleComp.data
                );

            // Create projectile

            Physical.Rectangle rectangle = new Physical.Rectangle(
                16.0f * 4,
                16.0f * 4
                );

            PhysicalVector2 position = GetStartingPosition(
                playerPosRectangle,
                playerOrientationComp.data,
                rectangle
                );

            var projectileEntity = registry.CreateEntity();

            registry.AssignComponent(
                projectileEntity,
                new Comps.Shapes.Rectangle {
                data = rectangle
            }
                );

            registry.AssignComponent(
                projectileEntity,
                new Comps.Position {
                data = position
            }
                );

            registry.AssignComponent(
                projectileEntity,
                (Comps.Orientations.Cardinal)playerOrientationComp.Clone()
                );

            PhysicalVector2 velocity =
                playerOrientationComp.data.GetPhysicalVector2() *
                GetStartingSpeed(kind);

            registry.AssignComponent(
                projectileEntity,
                new Comps.Velocity {
                data  = velocity,
                decay = 1.0f
            }
                );

            registry.AssignComponent(
                projectileEntity,
                new Comps.Projectile {
                kind    = kind,
                ownerId = owner.id
            }
                );

            if (IsDamaging(kind))
            {
                registry.AssignComponent(
                    projectileEntity,
                    new Comps.Damaging {
                    damage     = 1,
                    attackerId = owner.id
                }
                    );
            }

            registry.AssignComponent(
                projectileEntity,
                new Comps.Lifetime {
                ticks = GetLifetime(kind)
            }
                );

            registry.AssignComponent(
                projectileEntity,
                new Comps.Solid {
                ephemeral = true
            }
                );

            return(projectileEntity);
        }