Пример #1
0
        public static double GetSpriteAngleToSpriteRadian(AbstractSprite viewerSprite, AbstractSprite viewedSprite)
        {
            double fullVectorX = viewedSprite.PositionX - viewerSprite.PositionX;
            double fullVectorY = viewedSprite.PositionY - viewerSprite.PositionY;

            return(Math.Atan2(fullVectorY, fullVectorX));
        }
Пример #2
0
        public static double GetStraightDistance(AbstractSprite viewerSprite, AbstractSprite viewedSprite)
        {
            double viewAxisX      = Math.Cos(viewerSprite.AngleRadian);
            double viewAxisY      = Math.Sin(viewerSprite.AngleRadian);
            double relativePointX = viewedSprite.PositionX - viewerSprite.PositionX;
            double relativePointY = viewedSprite.PositionY - viewerSprite.PositionY;

            return(viewAxisX * relativePointX + viewAxisY * relativePointY);
        }
Пример #3
0
        public static double GetStraightDistance(AbstractSprite sprite, RayTracerPoint rayTracerPoint)
        {
            double viewAxisX      = Math.Cos(sprite.AngleRadian);
            double viewAxisY      = Math.Sin(sprite.AngleRadian);
            double relativePointX = rayTracerPoint.X - sprite.PositionX;
            double relativePointY = rayTracerPoint.Y - sprite.PositionY;

            return(viewAxisX * relativePointX + viewAxisY * relativePointY);
        }
Пример #4
0
        /// <summary>
        /// Make walk sprite
        /// </summary>
        /// <param name="sprite">sprite</param>
        /// <param name="angleOffsetRadian">angle offset (default 0) (in radian)</param>
        /// <param name="spritePool">other sprites</param>
        /// <param name="map">map</param>
        /// <param name="timeDelta">time delta</param>
        public static bool TryMakeWalk(AbstractSprite sprite, double angleOffsetRadian, SpritePool spritePool, AbstractMap map, double timeDelta)
        {
            double xMove = Math.Cos(sprite.AngleRadian + angleOffsetRadian) * sprite.DefaultWalkingDistance * timeDelta;
            double yMove = Math.Sin(sprite.AngleRadian + angleOffsetRadian) * sprite.DefaultWalkingDistance * timeDelta;

            if (sprite is AbstractHumanoid)
            {
                AbstractHumanoid humanoid = (AbstractHumanoid)sprite;
                humanoid.WalkCycle.UnFire();
                humanoid.WalkCycle.Fire();
                humanoid.WalkCycle.Update(1);

                if (humanoid.IsCrouch)
                {
                    xMove *= humanoid.CrouchSpeedMultiplier;
                    yMove *= humanoid.CrouchSpeedMultiplier;
                }

                //If is attacking and not jumping
                if (humanoid.StrongAttackCycle.IsFired && humanoid.PositionZ <= 0)
                {
                    xMove *= humanoid.AttackWalkSpeedMultiplier;
                    yMove *= humanoid.AttackWalkSpeedMultiplier;
                }
            }

            //If is jump
            if (sprite.PositionZ > 0)
            {
                xMove *= sprite.JumpSpeedMultiplier;
                yMove *= sprite.JumpSpeedMultiplier;
            }

            sprite.PositionX += xMove;

            bool isDetectCollisionX = IsDetectCollision(sprite, spritePool, map);

            if (isDetectCollisionX)
            {
                sprite.PositionX -= xMove;
            }

            sprite.PositionY += yMove;

            bool isDetectCollisionY = IsDetectCollision(sprite, spritePool, map);

            if (isDetectCollisionY)
            {
                sprite.PositionY -= yMove;
            }

            return(!(isDetectCollisionX || isDetectCollisionY));
        }
Пример #5
0
 /// <summary>
 /// Whether sprite is in collision with sprite pool
 /// </summary>
 /// <param name="sprite">sprite</param>
 /// <param name="spritePool">sprite pool</param>
 /// <returns>Whether sprite is in collision with sprite pool</returns>
 public static bool IsDetectSpriteCollision(AbstractSprite sprite, SpritePool spritePool)
 {
     foreach (AbstractHumanoid otherSprite in spritePool)
     {
         if (sprite != otherSprite)
         {
             if (IsDetectSpriteCollision(sprite, otherSprite))
             {
                 return(true);
             }
         }
     }
     return(false);
 }
Пример #6
0
        /// <summary>
        /// Distance for which predator sprite can attack prey
        /// </summary>
        /// <param name="predator">predator</param>
        /// <param name="prey">prey</param>
        /// <returns>Distance for which predator sprite can attack prey</returns>
        private static double GetAttackRange(AbstractHumanoid predator, AbstractSprite prey)
        {
            double predatorAttackRange = predator.AttackRange;

            if (predator.PositionZ > 0)
            {
                predatorAttackRange *= predator.AttackRangeJumpMultiplier;
            }
            else if (predator.IsCrouch)
            {
                predatorAttackRange *= predator.AttackRangeCrouchMultiplier;
            }

            return(predatorAttackRange);
        }
Пример #7
0
        /// <summary>
        /// Whether sprite is in collision with map walls
        /// </summary>
        /// <param name="sprite">sprite</param>
        /// <param name="map">map</param>
        /// <returns>Whether sprite is in collision with map walls</returns>
        public static bool IsDetectMapCollision(AbstractSprite sprite, AbstractMap map)
        {
            double currentRadius = sprite.Radius;

            do
            {
                if (sprite.PositionX < 0 || sprite.PositionY < 0 || sprite.PositionX >= map.Width || sprite.PositionY >= map.Height)
                {
                    return(true);
                }
                else if (map.GetMatterTypeAt(sprite.PositionX - sprite.Radius, sprite.PositionY - sprite.Radius) != null)
                {
                    return(true);
                }
                else if (map.GetMatterTypeAt(sprite.PositionX, sprite.PositionY - sprite.Radius) != null)
                {
                    return(true);
                }
                else if (map.GetMatterTypeAt(sprite.PositionX + sprite.Radius, sprite.PositionY - sprite.Radius) != null)
                {
                    return(true);
                }
                else if (map.GetMatterTypeAt(sprite.PositionX - sprite.Radius, sprite.PositionY) != null)
                {
                    return(true);
                }
                else if (map.GetMatterTypeAt(sprite.PositionX + sprite.Radius, sprite.PositionY) != null)
                {
                    return(true);
                }
                else if (map.GetMatterTypeAt(sprite.PositionX - sprite.Radius, sprite.PositionY + sprite.Radius) != null)
                {
                    return(true);
                }
                else if (map.GetMatterTypeAt(sprite.PositionX, sprite.PositionY + sprite.Radius) != null)
                {
                    return(true);
                }
                else if (map.GetMatterTypeAt(sprite.PositionX + sprite.Radius, sprite.PositionY + sprite.Radius) != null)
                {
                    return(true);
                }

                currentRadius -= spriteToMapCollisionPrecision;
            } while (currentRadius > 0);

            return(false);
        }
Пример #8
0
        /// <summary>
        /// Make jump sprite
        /// </summary>
        /// <param name="sprite">sprite</param>
        /// <param name="timeDelta">time delta</param>
        public static void MakeJump(AbstractSprite sprite, double timeDelta)
        {
            if (!sprite.IsNeedToJumpAgain)
            {
                if (sprite.PositionZ <= 0)
                {
                    sprite.CurrentJumpAcceleration = sprite.MaxJumpAcceleration;
                }
                else
                {
                    sprite.CurrentJumpAcceleration += 0.065 * timeDelta;
                }

                if (sprite.CurrentJumpAcceleration < 0)
                {
                    sprite.IsNeedToJumpAgain = true;
                }
            }
        }
Пример #9
0
        public static bool IsSpriteViewable(AbstractSprite viewerSprite, AbstractSprite viewedSprite, AbstractMap map, int fov)
        {
            if (viewerSprite == viewedSprite)
            {
                return(true);
            }


            double angleRadian = FixAngleRadian(GetSpriteAngleToSpriteRadian(viewerSprite, viewedSprite));
            double angleDegree = FixAngleDegree(angleRadian / Math.PI * 180);


            double minimumViewingAngle = FixAngleDegree(viewerSprite.AngleDegree - (double)fov / 1.8);
            double maximumViewingAngle = FixAngleDegree(viewerSprite.AngleDegree + (double)fov / 1.8);


            if (maximumViewingAngle > minimumViewingAngle)
            {
                if (angleDegree < minimumViewingAngle)
                {
                    return(false);
                }
                if (angleDegree > maximumViewingAngle)
                {
                    return(false);
                }
            }
            else
            {
                if (angleDegree < minimumViewingAngle && angleDegree > maximumViewingAngle)
                {
                    return(false);
                }
            }

            double xMove = Math.Cos(angleRadian) * rayDistanceResolution;
            double yMove = Math.Sin(angleRadian) * rayDistanceResolution;

            double x = viewerSprite.PositionX;
            double y = viewerSprite.PositionY;

            double xDistanceToPerform = Math.Abs(viewerSprite.PositionX - viewedSprite.PositionX);
            double yDistanceToPerform = Math.Abs(viewerSprite.PositionY - viewedSprite.PositionY);



            double xDistance = 0;
            double yDistance = 0;

            while (true)
            {
                x += xMove;
                y += yMove;

                xDistance += Math.Abs(xMove);
                yDistance += Math.Abs(yMove);

                if (xDistance >= xDistanceToPerform && yDistance >= yDistanceToPerform)
                {
                    return(true);
                }

                if (map.GetMatterTypeAt(x, y) != null)
                {
                    return(false);
                }

                if (x < 0 || y < 0 || x > map.Width || y > map.Height)
                {
                    return(false);
                }
            }
        }
Пример #10
0
 /// <summary>
 /// Whether sprite 1 is in collision with sprite 2
 /// </summary>
 /// <param name="sprite1">sprite 1</param>
 /// <param name="sprite2">sprite 2</param>
 /// <returns>Whether sprite 1 is in collision with sprite 2</returns>
 public static bool IsDetectSpriteCollision(AbstractSprite sprite1, AbstractSprite sprite2)
 {
     return(GetSpriteDistance(sprite1, sprite2) < sprite1.Radius + sprite2.Radius);
 }
Пример #11
0
 /// <summary>
 /// Make fall sprite
 /// </summary>
 /// <param name="sprite">sprite</param>
 /// <param name="timeDelta">time delta</param>
 public static void MakeFall(AbstractSprite sprite, double timeDelta)
 {
     sprite.PositionZ += sprite.CurrentJumpAcceleration / 10 * timeDelta;
     sprite.CurrentJumpAcceleration -= 0.1 * timeDelta;
     sprite.PositionZ = Math.Max(0, sprite.PositionZ);
 }
Пример #12
0
 /// <summary>
 /// Whether sprite is in collision with another sprite or the map
 /// </summary>
 /// <param name="sprite">sprite</param>
 /// <param name="spritePool">sprite pool</param>
 /// <param name="map">map</param>
 /// <returns>Whether sprite is in collision with another sprite or the map</returns>
 public static bool IsDetectCollision(AbstractSprite sprite, SpritePool spritePool, AbstractMap map)
 {
     return(IsDetectSpriteCollision(sprite, spritePool) || IsDetectMapCollision(sprite, map));
 }
Пример #13
0
 /// <summary>
 /// Try make rotate sprite from mouse strength
 /// </summary>
 /// <param name="sprite">sprite</param>
 /// <param name="angleRotationStrength">rotation strength</param>
 public static void TryMakeRotate(AbstractSprite sprite, short angleRotationStrength)
 {
     sprite.AngleRadian += ((double)angleRotationStrength / -200);
 }
Пример #14
0
 /// <summary>
 /// Make walk sprite
 /// </summary>
 /// <param name="sprite">sprite</param>
 /// <param name="spritePool">other sprites</param>
 /// <param name="map">map</param>
 /// <param name="timeDelta">time delta</param>
 public static bool TryMakeWalk(AbstractSprite sprite, SpritePool spritePool, AbstractMap map, double timeDelta)
 {
     return(TryMakeWalk(sprite, 0, spritePool, map, timeDelta));
 }
Пример #15
0
 /// <summary>
 /// Returns distance between 2 sprites
 /// </summary>
 /// <param name="sprite1">sprite 1</param>
 /// <param name="sprite2">sprite 2</param>
 /// <returns>distance between 2 sprites</returns>
 public static double GetSpriteDistance(AbstractSprite sprite1, AbstractSprite sprite2)
 {
     return(Math.Sqrt(Math.Pow(sprite2.PositionX - sprite1.PositionX, 2) + Math.Pow(sprite2.PositionY - sprite1.PositionY, 2)));
 }
Пример #16
0
 /// <summary>
 /// Whether prey is withing predator's attack range
 /// </summary>
 /// <param name="predator">predator sprite</param>
 /// <param name="prey">prey sprite</param>
 /// <returns>Whether prey is withing predator's attack range</returns>
 public static bool IsWithinAttackRange(AbstractHumanoid predator, AbstractSprite prey)
 {
     return(Physics.GetSpriteDistance(predator, prey) <= GetAttackRange(predator, prey));// predatorAttackRange + prey.Radius;
 }