Ejemplo n.º 1
0
        public void Wander(float deltaTime)
        {
            if (character.IsClimbing)
            {
                return;
            }
            //steer away from edges of the hull
            var currentHull = character.CurrentHull;

            if (currentHull != null)
            {
                float roomWidth = currentHull.Rect.Width;
                if (roomWidth < WallAvoidDistance * 4)
                {
                    PathSteering.Reset();
                }
                else
                {
                    float leftDist  = character.Position.X - currentHull.Rect.X;
                    float rightDist = currentHull.Rect.Right - character.Position.X;
                    if (leftDist < WallAvoidDistance && rightDist < WallAvoidDistance)
                    {
                        if (Math.Abs(rightDist - leftDist) > WallAvoidDistance / 2)
                        {
                            PathSteering.SteeringManual(deltaTime, Vector2.UnitX * Math.Sign(rightDist - leftDist));
                        }
                        else
                        {
                            PathSteering.Reset();
                        }
                    }
                    else if (leftDist < WallAvoidDistance)
                    {
                        float speed = (WallAvoidDistance - leftDist) / WallAvoidDistance;
                        PathSteering.SteeringManual(deltaTime, Vector2.UnitX * MathHelper.Clamp(speed, 0.25f, 1));
                        PathSteering.WanderAngle = 0.0f;
                    }
                    else if (rightDist < WallAvoidDistance)
                    {
                        float speed = (WallAvoidDistance - rightDist) / WallAvoidDistance;
                        PathSteering.SteeringManual(deltaTime, -Vector2.UnitX * MathHelper.Clamp(speed, 0.25f, 1));
                        PathSteering.WanderAngle = MathHelper.Pi;
                    }
                    else
                    {
                        SteeringManager.SteeringWander();
                    }
                }
            }
            else
            {
                SteeringManager.SteeringWander();
            }
            if (!character.AnimController.InWater)
            {
                //reset vertical steering to prevent dropping down from platforms etc
                character.AIController.SteeringManager.ResetY();
            }
        }
Ejemplo n.º 2
0
        private void UpdateEscape(float deltaTime)
        {
            if (selectedAiTarget == null || selectedAiTarget.Entity == null || selectedAiTarget.Entity.Removed)
            {
                state = AIState.None;
                return;
            }

            SteeringManager.SteeringManual(deltaTime, Vector2.Normalize(SimPosition - selectedAiTarget.SimPosition) * 5);
            SteeringManager.SteeringWander(1.0f);
            SteeringManager.SteeringAvoid(deltaTime, 2f);
        }
Ejemplo n.º 3
0
        private void UpdateEscape(float deltaTime)
        {
            if (selectedAiTarget == null || selectedAiTarget.Entity == null || selectedAiTarget.Entity.Removed)
            {
                state = AIState.None;
                return;
            }

            Vector2 escapeDir = Vector2.Normalize(SimPosition - selectedAiTarget.SimPosition);

            if (!MathUtils.IsValid(escapeDir))
            {
                escapeDir = Vector2.UnitY;
            }
            SteeringManager.SteeringManual(deltaTime, escapeDir * 5);
            SteeringManager.SteeringWander(1.0f);
            SteeringManager.SteeringAvoid(deltaTime, 2f);
        }
Ejemplo n.º 4
0
 private void UpdateEscape(float deltaTime)
 {
     SteeringManager.SteeringManual(deltaTime, Vector2.Normalize(SimPosition - selectedAiTarget.SimPosition) * 5);
     SteeringManager.SteeringWander(1.0f);
     SteeringManager.SteeringAvoid(deltaTime, 2f);
 }
Ejemplo n.º 5
0
        public void Update(float deltaTime)
        {
            position += new Vector2(velocity.X, velocity.Y) * deltaTime;
            depth     = MathHelper.Clamp(depth + velocity.Z * deltaTime, 0.0f, MaxDepth);

            checkWallsTimer -= deltaTime;
            if (checkWallsTimer <= 0.0f && Level.Loaded != null)
            {
                checkWallsTimer = CheckWallsInterval;

                obstacleDiff = Vector2.Zero;
                if (position.Y > Level.Loaded.Size.Y)
                {
                    obstacleDiff = Vector2.UnitY;
                }
                else if (position.Y < 0.0f)
                {
                    obstacleDiff = -Vector2.UnitY;
                }
                else if (position.X < 0.0f)
                {
                    obstacleDiff = Vector2.UnitX;
                }
                else if (position.X > Level.Loaded.Size.X)
                {
                    obstacleDiff = -Vector2.UnitX;
                }
                else
                {
                    var cells = Level.Loaded.GetCells(position, 1);
                    if (cells.Count > 0)
                    {
                        foreach (Voronoi2.VoronoiCell cell in cells)
                        {
                            obstacleDiff += cell.Center - position;
                        }
                        obstacleDiff /= cells.Count;

                        obstacleDiff = Vector2.Normalize(obstacleDiff) * prefab.Speed;
                    }
                }
            }



            if (Swarm != null)
            {
                Vector2 midPoint     = Swarm.MidPoint();
                float   midPointDist = Vector2.Distance(SimPosition, midPoint);

                if (midPointDist > Swarm.MaxDistance)
                {
                    steeringManager.SteeringSeek(midPoint, (midPointDist / Swarm.MaxDistance) * prefab.Speed);
                }
            }

            if (prefab.WanderAmount > 0.0f)
            {
                steeringManager.SteeringWander(prefab.Speed);
            }

            //Level.Loaded.Size


            if (obstacleDiff != Vector2.Zero)
            {
                steeringManager.SteeringSeek(SimPosition - obstacleDiff, prefab.Speed * 2.0f);
            }

            steeringManager.Update(prefab.Speed);

            if (prefab.WanderZAmount > 0.0f)
            {
                ang       += Rand.Range(-prefab.WanderZAmount, prefab.WanderZAmount);
                velocity.Z = (float)Math.Sin(ang) * prefab.Speed;
            }

            velocity = Vector3.Lerp(velocity, new Vector3(Steering.X, Steering.Y, velocity.Z), deltaTime);
        }
Ejemplo n.º 6
0
        public void Update(float deltaTime)
        {
            position += new Vector2(velocity.X, velocity.Y) * deltaTime;
            depth     = MathHelper.Clamp(depth + velocity.Z * deltaTime, 0.0f, MaxDepth);

            checkWallsTimer -= deltaTime;
            if (checkWallsTimer <= 0.0f && Level.Loaded != null)
            {
                checkWallsTimer = CheckWallsInterval;

                obstacleDiff = Vector2.Zero;
                if (position.Y > Level.Loaded.Size.Y)
                {
                    obstacleDiff = Vector2.UnitY;
                }
                else if (position.Y < 0.0f)
                {
                    obstacleDiff = -Vector2.UnitY;
                }
                else if (position.X < 0.0f)
                {
                    obstacleDiff = Vector2.UnitX;
                }
                else if (position.X > Level.Loaded.Size.X)
                {
                    obstacleDiff = -Vector2.UnitX;
                }
                else
                {
                    var cells = Level.Loaded.GetCells(position, 1);
                    if (cells.Count > 0)
                    {
                        int cellCount = 0;
                        foreach (Voronoi2.VoronoiCell cell in cells)
                        {
                            Vector2 diff = cell.Center - position;
                            if (diff.LengthSquared() > 5000.0f * 5000.0f)
                            {
                                continue;
                            }
                            obstacleDiff += diff;
                            cellCount++;
                        }
                        if (cellCount > 0)
                        {
                            obstacleDiff /= cellCount;
                            obstacleDist  = obstacleDiff.Length();
                            obstacleDiff  = Vector2.Normalize(obstacleDiff);
                        }
                    }
                }
            }

            if (Swarm != null)
            {
                Vector2 midPoint     = Swarm.MidPoint();
                float   midPointDist = Vector2.Distance(SimPosition, midPoint) * 100.0f;
                if (midPointDist > Swarm.MaxDistance)
                {
                    steeringManager.SteeringSeek(midPoint, ((midPointDist / Swarm.MaxDistance) - 1.0f) * prefab.Speed);
                }
                steeringManager.SteeringManual(deltaTime, Swarm.AvgVelocity() * Swarm.Cohesion);
            }

            if (prefab.WanderAmount > 0.0f)
            {
                steeringManager.SteeringWander(prefab.Speed);
            }

            if (obstacleDiff != Vector2.Zero)
            {
                steeringManager.SteeringManual(deltaTime, -obstacleDiff * (1.0f - obstacleDist / 5000.0f) * prefab.Speed);
            }

            steeringManager.Update(prefab.Speed);

            if (prefab.WanderZAmount > 0.0f)
            {
                wanderZPhase += Rand.Range(-prefab.WanderZAmount, prefab.WanderZAmount);
                velocity.Z    = (float)Math.Sin(wanderZPhase) * prefab.Speed;
            }

            velocity = Vector3.Lerp(velocity, new Vector3(Steering.X, Steering.Y, velocity.Z), deltaTime);
        }
Ejemplo n.º 7
0
        public void Update(float deltaTime)
        {
            position += new Vector2(velocity.X, velocity.Y) * deltaTime;
            depth     = MathHelper.Clamp(depth + velocity.Z * deltaTime, Prefab.MinDepth, Prefab.MaxDepth * 10);

            if (Prefab.FlashInterval > 0.0f)
            {
                flashTimer -= deltaTime;
                if (flashTimer > 0.0f)
                {
                    alpha = 0.0f;
                }
                else
                {
                    //value goes from 0 to 1 and back to 0 during the flash
                    alpha = (float)Math.Sin(-flashTimer / Prefab.FlashDuration * MathHelper.Pi) * PerlinNoise.GetPerlin((float)Timing.TotalTime * 0.1f, (float)Timing.TotalTime * 0.2f);
                    if (flashTimer < -Prefab.FlashDuration)
                    {
                        flashTimer = Prefab.FlashInterval;
                    }
                }
            }

            checkWallsTimer -= deltaTime;
            if (checkWallsTimer <= 0.0f && Level.Loaded != null)
            {
                checkWallsTimer = CheckWallsInterval;

                obstacleDiff = Vector2.Zero;
                if (position.Y > Level.Loaded.Size.Y)
                {
                    obstacleDiff = Vector2.UnitY;
                }
                else if (position.Y < 0.0f)
                {
                    obstacleDiff = -Vector2.UnitY;
                }
                else if (position.X < 0.0f)
                {
                    obstacleDiff = Vector2.UnitX;
                }
                else if (position.X > Level.Loaded.Size.X)
                {
                    obstacleDiff = -Vector2.UnitX;
                }
                else
                {
                    var cells = Level.Loaded.GetCells(position, 1);
                    if (cells.Count > 0)
                    {
                        int cellCount = 0;
                        foreach (Voronoi2.VoronoiCell cell in cells)
                        {
                            Vector2 diff = cell.Center - position;
                            if (diff.LengthSquared() > 5000.0f * 5000.0f)
                            {
                                continue;
                            }
                            obstacleDiff += diff;
                            cellCount++;
                        }
                        if (cellCount > 0)
                        {
                            obstacleDiff /= cellCount;
                            obstacleDist  = obstacleDiff.Length();
                            obstacleDiff  = Vector2.Normalize(obstacleDiff);
                        }
                    }
                }
            }

            if (Swarm != null)
            {
                Vector2 midPoint     = Swarm.MidPoint();
                float   midPointDist = Vector2.Distance(SimPosition, midPoint) * 100.0f;
                if (midPointDist > Swarm.MaxDistance)
                {
                    steeringManager.SteeringSeek(midPoint, ((midPointDist / Swarm.MaxDistance) - 1.0f) * Prefab.Speed);
                }
                steeringManager.SteeringManual(deltaTime, Swarm.AvgVelocity() * Swarm.Cohesion);
            }

            if (Prefab.WanderAmount > 0.0f)
            {
                steeringManager.SteeringWander(Prefab.Speed);
            }

            if (obstacleDiff != Vector2.Zero)
            {
                steeringManager.SteeringManual(deltaTime, -obstacleDiff * (1.0f - obstacleDist / 5000.0f) * Prefab.Speed);
            }

            steeringManager.Update(Prefab.Speed);

            if (Prefab.WanderZAmount > 0.0f)
            {
                wanderZPhase += Rand.Range(-Prefab.WanderZAmount, Prefab.WanderZAmount);
                velocity.Z    = (float)Math.Sin(wanderZPhase) * Prefab.Speed;
            }

            velocity = Vector3.Lerp(velocity, new Vector3(Steering.X, Steering.Y, velocity.Z), deltaTime);

            UpdateDeformations(deltaTime);
        }