Exemple #1
0
        public override void Bind(Entity entity, Main main, bool creating = false)
        {
            BlockCloud blockCloud = entity.GetOrCreate <BlockCloud>("BlockCloud");

            blockCloud.Add(new CommandBinding(blockCloud.Delete, entity.Delete));
            blockCloud.Add(new CommandBinding <Collidable, ContactCollection>(blockCloud.Collided, delegate(Collidable other, ContactCollection contacts)
            {
                if (other.Tag != null && other.Tag.GetType() == typeof(Character))
                {
                    // Damage the player
                    Entity p = PlayerFactory.Instance;
                    if (p != null && p.Active)
                    {
                        p.Get <Agent>().Damage.Execute(0.1f);
                    }
                }
            }));
            blockCloud.Type.Value = Voxel.t.Black;

            Transform transform = entity.GetOrCreate <Transform>("Transform");

            Sound.AttachTracker(entity);

            AI ai = entity.GetOrCreate <AI>("AI");

            if (!main.EditorEnabled)
            {
                entity.Add(new PostInitialization(delegate()
                {
                    AkSoundEngine.PostEvent(AK.EVENTS.PLAY_EVIL_CUBES, entity);
                    AkSoundEngine.PostEvent(ai.CurrentState == "Chase" ? AK.EVENTS.EVIL_CUBES_CHASE : AK.EVENTS.EVIL_CUBES_IDLE, entity);
                }));

                SoundKiller.Add(entity, AK.EVENTS.STOP_EVIL_CUBES);
            }

            Agent agent = entity.GetOrCreate <Agent>();

            agent.Add(new Binding <Vector3>(agent.Position, transform.Position));

            RaycastAI raycastAI = entity.GetOrCreate <RaycastAI>("RaycastAI");

            raycastAI.BlendTime.Value = 1.0f;
            raycastAI.Add(new TwoWayBinding <Vector3>(transform.Position, raycastAI.Position));
            raycastAI.Add(new Binding <Quaternion>(transform.Quaternion, raycastAI.Orientation));

            RaycastAIMovement movement = entity.GetOrCreate <RaycastAIMovement>("Movement");

            blockCloud.Add(new Binding <Vector3>(blockCloud.Position, transform.Position));

            AI.Task checkOperationalRadius = new AI.Task
            {
                Interval = 2.0f,
                Action   = delegate()
                {
                    bool shouldBeActive = (transform.Position.Value - main.Camera.Position).Length() < movement.OperationalRadius;
                    if (shouldBeActive && ai.CurrentState == "Suspended")
                    {
                        ai.CurrentState.Value = "Idle";
                    }
                    else if (!shouldBeActive && ai.CurrentState != "Suspended")
                    {
                        ai.CurrentState.Value = "Suspended";
                    }
                },
            };

            AI.Task updatePosition = new AI.Task
            {
                Action = delegate()
                {
                    raycastAI.Update();
                },
            };

            ai.Add(new AI.AIState
            {
                Name  = "Suspended",
                Tasks = new[] { checkOperationalRadius, },
            });

            const float sightDistance   = 25.0f;
            const float hearingDistance = 0.0f;

            ai.Add(new AI.AIState
            {
                Name  = "Idle",
                Tasks = new[]
                {
                    checkOperationalRadius,
                    updatePosition,
                    new AI.Task
                    {
                        Interval = 1.0f,
                        Action   = delegate()
                        {
                            raycastAI.Move(new Vector3(((float)this.random.NextDouble() * 2.0f) - 1.0f, ((float)this.random.NextDouble() * 2.0f) - 1.0f, ((float)this.random.NextDouble() * 2.0f) - 1.0f));
                        }
                    },
                    new AI.Task
                    {
                        Interval = 1.0f,
                        Action   = delegate()
                        {
                            Agent a = Agent.Query(transform.Position, sightDistance, hearingDistance, x => x.Entity.Type == "Player");
                            if (a != null)
                            {
                                ai.CurrentState.Value = "Alert";
                            }
                        },
                    },
                },
            });

            ai.Add(new AI.AIState
            {
                Name  = "Alert",
                Tasks = new[]
                {
                    checkOperationalRadius,
                    updatePosition,
                    new AI.Task
                    {
                        Interval = 1.0f,
                        Action   = delegate()
                        {
                            if (ai.TimeInCurrentState > 3.0f)
                            {
                                ai.CurrentState.Value = "Idle";
                            }
                            else
                            {
                                Agent a = Agent.Query(transform.Position, sightDistance, hearingDistance, x => x.Entity.Type == "Player");
                                if (a != null)
                                {
                                    ai.TargetAgent.Value  = a.Entity;
                                    ai.CurrentState.Value = "Chase";
                                }
                            }
                        },
                    },
                },
            });

            AI.Task checkTargetAgent = new AI.Task
            {
                Action = delegate()
                {
                    Entity target = ai.TargetAgent.Value.Target;
                    if (target == null || !target.Active || (target.Get <Transform>().Position.Value - transform.Position.Value).Length() > sightDistance * 1.25f)
                    {
                        ai.TargetAgent.Value  = null;
                        ai.CurrentState.Value = "Alert";
                    }
                },
            };

            Action findNextPosition = delegate()
            {
                movement.LastPosition.Value = transform.Position.Value;
                float   radius = 5.0f;
                Vector3 center = ai.TargetAgent.Value.Target.Get <Transform>().Position;
                Vector3 candidate;
                do
                {
                    candidate = center + new Vector3((float)this.random.NextDouble() - 0.5f, (float)this.random.NextDouble(), (float)this.random.NextDouble() - 0.5f) * radius;
                    radius   += 1.0f;
                }while (!RaycastAI.DefaultPositionFilter(candidate));

                Vector3     toCandidate         = candidate - movement.LastPosition;
                float       distance            = toCandidate.Length();
                const float maxMovementDistance = 9.0f;
                if (distance > maxMovementDistance)
                {
                    toCandidate *= maxMovementDistance / distance;
                }

                movement.NextPosition.Value  = movement.LastPosition.Value + toCandidate;
                movement.PositionBlend.Value = 0.0f;
            };

            // Chase AI state

            ai.Add(new AI.AIState
            {
                Name  = "Chase",
                Enter = delegate(AI.AIState previous)
                {
                    AkSoundEngine.PostEvent(AK.EVENTS.EVIL_CUBES_CHASE, entity);
                    findNextPosition();
                },
                Exit = delegate(AI.AIState next)
                {
                    AkSoundEngine.PostEvent(AK.EVENTS.EVIL_CUBES_IDLE, entity);
                },
                Tasks = new[]
                {
                    checkOperationalRadius,
                    checkTargetAgent,
                    new AI.Task
                    {
                        Action = delegate()
                        {
                            if (ai.TimeInCurrentState.Value > 15.0f)
                            {
                                Entity voxel = raycastAI.Voxel.Value.Target;
                                if (voxel != null && voxel.Active)
                                {
                                    raycastAI.Coord.Value = raycastAI.LastCoord.Value = voxel.Get <Voxel>().GetCoordinate(transform.Position);
                                }
                                raycastAI.Move(new Vector3(((float)this.random.NextDouble() * 2.0f) - 1.0f, ((float)this.random.NextDouble() * 2.0f) - 1.0f, ((float)this.random.NextDouble() * 2.0f) - 1.0f));
                                ai.CurrentState.Value = "Idle";
                            }
                            else
                            {
                                movement.PositionBlend.Value += (main.ElapsedTime.Value / 1.0f);
                                if (movement.PositionBlend > 1.0f)
                                {
                                    findNextPosition();
                                }
                                transform.Position.Value = Vector3.Lerp(movement.LastPosition, movement.NextPosition, movement.PositionBlend);
                            }
                        }
                    },
                },
            });

            this.SetMain(entity, main);
        }
Exemple #2
0
        public override void Bind(Entity entity, Main main, bool creating = false)
        {
            if (ParticleSystem.Get(main, "SnakeSparks") == null)
            {
                ParticleSystem.Add(main, "SnakeSparks",
                                   new ParticleSystem.ParticleSettings
                {
                    TextureName           = "Particles\\splash",
                    MaxParticles          = 1000,
                    Duration              = TimeSpan.FromSeconds(1.0f),
                    MinHorizontalVelocity = -7.0f,
                    MaxHorizontalVelocity = 7.0f,
                    MinVerticalVelocity   = 0.0f,
                    MaxVerticalVelocity   = 7.0f,
                    Gravity        = new Vector3(0.0f, -10.0f, 0.0f),
                    MinRotateSpeed = -2.0f,
                    MaxRotateSpeed = 2.0f,
                    MinStartSize   = 0.3f,
                    MaxStartSize   = 0.7f,
                    MinEndSize     = 0.0f,
                    MaxEndSize     = 0.0f,
                    BlendState     = Microsoft.Xna.Framework.Graphics.BlendState.AlphaBlend,
                    MinColor       = new Vector4(2.0f, 2.0f, 2.0f, 1.0f),
                    MaxColor       = new Vector4(2.0f, 2.0f, 2.0f, 1.0f),
                });
                ParticleSystem.Add(main, "SnakeSparksRed",
                                   new ParticleSystem.ParticleSettings
                {
                    TextureName           = "Particles\\splash",
                    MaxParticles          = 1000,
                    Duration              = TimeSpan.FromSeconds(1.0f),
                    MinHorizontalVelocity = -7.0f,
                    MaxHorizontalVelocity = 7.0f,
                    MinVerticalVelocity   = 0.0f,
                    MaxVerticalVelocity   = 7.0f,
                    Gravity        = new Vector3(0.0f, -10.0f, 0.0f),
                    MinRotateSpeed = -2.0f,
                    MaxRotateSpeed = 2.0f,
                    MinStartSize   = 0.3f,
                    MaxStartSize   = 0.7f,
                    MinEndSize     = 0.0f,
                    MaxEndSize     = 0.0f,
                    BlendState     = Microsoft.Xna.Framework.Graphics.BlendState.AlphaBlend,
                    MinColor       = new Vector4(1.4f, 0.8f, 0.7f, 1.0f),
                    MaxColor       = new Vector4(1.4f, 0.8f, 0.7f, 1.0f),
                });
                ParticleSystem.Add(main, "SnakeSparksYellow",
                                   new ParticleSystem.ParticleSettings
                {
                    TextureName           = "Particles\\splash",
                    MaxParticles          = 1000,
                    Duration              = TimeSpan.FromSeconds(1.0f),
                    MinHorizontalVelocity = -7.0f,
                    MaxHorizontalVelocity = 7.0f,
                    MinVerticalVelocity   = 0.0f,
                    MaxVerticalVelocity   = 7.0f,
                    Gravity        = new Vector3(0.0f, -10.0f, 0.0f),
                    MinRotateSpeed = -2.0f,
                    MaxRotateSpeed = 2.0f,
                    MinStartSize   = 0.3f,
                    MaxStartSize   = 0.7f,
                    MinEndSize     = 0.0f,
                    MaxEndSize     = 0.0f,
                    BlendState     = Microsoft.Xna.Framework.Graphics.BlendState.AlphaBlend,
                    MinColor       = new Vector4(1.4f, 1.4f, 0.7f, 1.0f),
                    MaxColor       = new Vector4(1.4f, 1.4f, 0.7f, 1.0f),
                });
            }

            Snake snake = entity.GetOrCreate <Snake>("Snake");

            entity.CannotSuspendByDistance = true;
            Transform transform = entity.GetOrCreate <Transform>("Transform");

            AI ai = entity.GetOrCreate <AI>("AI");

            Agent agent = entity.GetOrCreate <Agent>("Agent");

            const float defaultSpeed    = 5.0f;
            const float chaseSpeed      = 18.0f;
            const float closeChaseSpeed = 12.0f;
            const float crushSpeed      = 125.0f;

            VoxelChaseAI chase = entity.GetOrCreate <VoxelChaseAI>("VoxelChaseAI");

            chase.Add(new TwoWayBinding <Vector3>(transform.Position, chase.Position));
            chase.Speed.Value             = defaultSpeed;
            chase.EnablePathfinding.Value = ai.CurrentState.Value == "Chase";
            chase.Filter = delegate(Voxel.State state)
            {
                if (state == Voxel.States.Infected || state == Voxel.States.Neutral || state == Voxel.States.Hard || state == Voxel.States.HardInfected)
                {
                    return(true);
                }
                return(false);
            };
            entity.Add(new CommandBinding(chase.Delete, entity.Delete));

            PointLight positionLight = null;

            if (!main.EditorEnabled)
            {
                positionLight                   = new PointLight();
                positionLight.Serialize         = false;
                positionLight.Color.Value       = new Vector3(1.5f, 0.5f, 0.5f);
                positionLight.Attenuation.Value = 20.0f;
                positionLight.Add(new Binding <bool, string>(positionLight.Enabled, x => x != "Suspended", ai.CurrentState));
                positionLight.Add(new Binding <Vector3, string>(positionLight.Color, delegate(string state)
                {
                    switch (state)
                    {
                    case "Chase":
                    case "Crush":
                        return(new Vector3(1.5f, 0.5f, 0.5f));

                    case "Alert":
                        return(new Vector3(1.5f, 1.5f, 0.5f));

                    default:
                        return(new Vector3(1.0f, 1.0f, 1.0f));
                    }
                }, ai.CurrentState));
                entity.Add("PositionLight", positionLight);
                ParticleEmitter emitter = entity.GetOrCreate <ParticleEmitter>("Particles");
                emitter.Serialize = false;
                emitter.ParticlesPerSecond.Value = 100;
                emitter.Add(new Binding <string>(emitter.ParticleType, delegate(string state)
                {
                    switch (state)
                    {
                    case "Chase":
                    case "Crush":
                        return("SnakeSparksRed");

                    case "Alert":
                        return("SnakeSparksYellow");

                    default:
                        return("SnakeSparks");
                    }
                }, ai.CurrentState));
                emitter.Add(new Binding <Vector3>(emitter.Position, transform.Position));
                emitter.Add(new Binding <bool, string>(emitter.Enabled, x => x != "Suspended", ai.CurrentState));

                positionLight.Add(new Binding <Vector3>(positionLight.Position, transform.Position));
                emitter.Add(new Binding <Vector3>(emitter.Position, transform.Position));
                agent.Add(new Binding <Vector3>(agent.Position, transform.Position));
                Sound.AttachTracker(entity);
            }

            AI.Task checkMap = new AI.Task
            {
                Action = delegate()
                {
                    if (chase.Voxel.Value.Target == null || !chase.Voxel.Value.Target.Active)
                    {
                        entity.Delete.Execute();
                    }
                },
            };

            AI.Task checkOperationalRadius = new AI.Task
            {
                Interval = 2.0f,
                Action   = delegate()
                {
                    bool shouldBeActive = (transform.Position.Value - main.Camera.Position).Length() < snake.OperationalRadius;
                    if (shouldBeActive && ai.CurrentState == "Suspended")
                    {
                        ai.CurrentState.Value = "Idle";
                    }
                    else if (!shouldBeActive && ai.CurrentState != "Suspended")
                    {
                        ai.CurrentState.Value = "Suspended";
                    }
                },
            };

            AI.Task checkTargetAgent = new AI.Task
            {
                Action = delegate()
                {
                    Entity target = ai.TargetAgent.Value.Target;
                    if (target == null || !target.Active)
                    {
                        ai.TargetAgent.Value  = null;
                        ai.CurrentState.Value = "Idle";
                    }
                },
            };

            Func <Voxel, Direction> randomValidDirection = delegate(Voxel m)
            {
                Voxel.Coord c    = chase.Coord;
                Direction[] dirs = new Direction[6];
                Array.Copy(DirectionExtensions.Directions, dirs, 6);

                // Shuffle directions
                int i = 5;
                while (i > 0)
                {
                    int       k    = this.random.Next(i);
                    Direction temp = dirs[i];
                    dirs[i] = dirs[k];
                    dirs[k] = temp;
                    i--;
                }

                foreach (Direction dir in dirs)
                {
                    if (chase.Filter(m[c.Move(dir)]))
                    {
                        return(dir);
                    }
                }
                return(Direction.None);
            };

            Direction currentDir = Direction.None;

            chase.Add(new CommandBinding <Voxel, Voxel.Coord>(chase.Moved, delegate(Voxel m, Voxel.Coord c)
            {
                if (chase.Active)
                {
                    string currentState = ai.CurrentState.Value;
                    Voxel.t id          = m[c].ID;
                    if (id == Voxel.t.Hard)
                    {
                        m.Empty(c);
                        m.Fill(c, Voxel.States.HardInfected);
                        m.Regenerate();
                    }
                    else if (id == Voxel.t.Neutral)
                    {
                        m.Empty(c);
                        m.Fill(c, Voxel.States.Infected);
                        m.Regenerate();
                    }
                    else if (id == Voxel.t.Empty)
                    {
                        m.Fill(c, Voxel.States.Infected);
                        m.Regenerate();
                    }

                    if (currentState == "Idle")
                    {
                        if (currentDir == Direction.None || !chase.Filter(m[chase.Coord.Value.Move(currentDir)]) || this.random.Next(8) == 0)
                        {
                            currentDir = randomValidDirection(m);
                        }
                        chase.Coord.Value = chase.Coord.Value.Move(currentDir);
                    }
                    else if (snake.Path.Length > 0)
                    {
                        chase.Coord.Value = snake.Path[0];
                        snake.Path.RemoveAt(0);
                    }
                }
            }));

            Sound.AttachTracker(entity);
            SoundKiller.Add(entity, AK.EVENTS.STOP_SNAKE);
            ai.Add(new ChangeBinding <string>(ai.CurrentState, delegate(string old, string value)
            {
                if (value == "Suspended" || value == "Alert")
                {
                    AkSoundEngine.PostEvent(AK.EVENTS.STOP_SNAKE, entity);
                }
                else if (old != "Idle" && old != "Chase" && old != "Crush")
                {
                    AkSoundEngine.PostEvent(AK.EVENTS.PLAY_SNAKE, entity);
                }
            }));

            const float sightDistance   = 50.0f;
            const float hearingDistance = 0.0f;

            ai.Setup
            (
                new AI.AIState
            {
                Name  = "Suspended",
                Tasks = new[] { checkOperationalRadius },
            },
                new AI.AIState
            {
                Name  = "Idle",
                Enter = delegate(AI.AIState previous)
                {
                    Entity voxelEntity = chase.Voxel.Value.Target;
                    if (voxelEntity != null)
                    {
                        Voxel m = voxelEntity.Get <Voxel>();
                        if (currentDir == Direction.None || !chase.Filter(m[chase.Coord.Value.Move(currentDir)]))
                        {
                            currentDir = randomValidDirection(m);
                        }
                        chase.Coord.Value = chase.Coord.Value.Move(currentDir);
                    }
                },
                Tasks = new[]
                {
                    checkMap,
                    checkOperationalRadius,
                    new AI.Task
                    {
                        Interval = 1.0f,
                        Action   = delegate()
                        {
                            Agent a = Agent.Query(transform.Position, sightDistance, hearingDistance, x => x.Entity.Type == "Player");
                            if (a != null)
                            {
                                ai.CurrentState.Value = "Alert";
                            }
                        },
                    },
                },
            },
                new AI.AIState
            {
                Name  = "Alert",
                Enter = delegate(AI.AIState previous)
                {
                    chase.EnableMovement.Value = false;
                },
                Exit = delegate(AI.AIState next)
                {
                    chase.EnableMovement.Value = true;
                },
                Tasks = new[]
                {
                    checkMap,
                    checkOperationalRadius,
                    new AI.Task
                    {
                        Interval = 0.4f,
                        Action   = delegate()
                        {
                            if (ai.TimeInCurrentState > 3.0f)
                            {
                                ai.CurrentState.Value = "Idle";
                            }
                            else
                            {
                                Agent a = Agent.Query(transform.Position, sightDistance, hearingDistance, x => x.Entity.Type == "Player");
                                if (a != null)
                                {
                                    ai.TargetAgent.Value  = a.Entity;
                                    ai.CurrentState.Value = "Chase";
                                }
                            }
                        },
                    },
                },
            },
                new AI.AIState
            {
                Name  = "Chase",
                Enter = delegate(AI.AIState previousState)
                {
                    chase.EnablePathfinding.Value = true;
                    chase.Speed.Value             = chaseSpeed;
                },
                Exit = delegate(AI.AIState nextState)
                {
                    chase.EnablePathfinding.Value = false;
                    chase.Speed.Value             = defaultSpeed;
                },
                Tasks = new[]
                {
                    checkMap,
                    checkOperationalRadius,
                    checkTargetAgent,
                    new AI.Task
                    {
                        Interval = 0.07f,
                        Action   = delegate()
                        {
                            Vector3 targetPosition = ai.TargetAgent.Value.Target.Get <Agent>().Position;

                            float targetDistance = (targetPosition - transform.Position).Length();

                            chase.Speed.Value = targetDistance < 15.0f ? closeChaseSpeed : chaseSpeed;

                            if (targetDistance > 50.0f || ai.TimeInCurrentState > 30.0f)                                     // He got away
                            {
                                ai.CurrentState.Value = "Alert";
                            }
                            else if (targetDistance < 4.0f)                                     // We got 'im
                            {
                                // First, make sure we're not near a reset block
                                Voxel v = chase.Voxel.Value.Target.Get <Voxel>();
                                if (VoxelAStar.BroadphaseSearch(v, chase.Coord, 6, x => x.Type == Lemma.Components.Voxel.States.Reset) == null)
                                {
                                    ai.CurrentState.Value = "Crush";
                                }
                            }
                            else
                            {
                                chase.Target.Value = targetPosition;
                            }
                        },
                    },
                },
            },
                new AI.AIState
            {
                Name  = "Crush",
                Enter = delegate(AI.AIState lastState)
                {
                    // Set up cage
                    Voxel.Coord center = chase.Voxel.Value.Target.Get <Voxel>().GetCoordinate(ai.TargetAgent.Value.Target.Get <Agent>().Position);

                    int radius = 1;

                    // Bottom
                    for (int x = center.X - radius; x <= center.X + radius; x++)
                    {
                        for (int z = center.Z - radius; z <= center.Z + radius; z++)
                        {
                            snake.Path.Add(new Voxel.Coord {
                                X = x, Y = center.Y - 4, Z = z
                            });
                        }
                    }

                    // Outer shell
                    radius = 2;
                    for (int y = center.Y - 3; y <= center.Y + 3; y++)
                    {
                        // Left
                        for (int z = center.Z - radius; z <= center.Z + radius; z++)
                        {
                            snake.Path.Add(new Voxel.Coord {
                                X = center.X - radius, Y = y, Z = z
                            });
                        }

                        // Right
                        for (int z = center.Z - radius; z <= center.Z + radius; z++)
                        {
                            snake.Path.Add(new Voxel.Coord {
                                X = center.X + radius, Y = y, Z = z
                            });
                        }

                        // Backward
                        for (int x = center.X - radius; x <= center.X + radius; x++)
                        {
                            snake.Path.Add(new Voxel.Coord {
                                X = x, Y = y, Z = center.Z - radius
                            });
                        }

                        // Forward
                        for (int x = center.X - radius; x <= center.X + radius; x++)
                        {
                            snake.Path.Add(new Voxel.Coord {
                                X = x, Y = y, Z = center.Z + radius
                            });
                        }
                    }

                    // Top
                    for (int x = center.X - radius; x <= center.X + radius; x++)
                    {
                        for (int z = center.Z - radius; z <= center.Z + radius; z++)
                        {
                            snake.Path.Add(new Voxel.Coord {
                                X = x, Y = center.Y + 3, Z = z
                            });
                        }
                    }

                    chase.EnablePathfinding.Value = false;
                    chase.Speed.Value             = crushSpeed;

                    snake.CrushCoordinate.Value = chase.Coord;
                },
                Exit = delegate(AI.AIState nextState)
                {
                    chase.Speed.Value = defaultSpeed;
                    chase.Coord.Value = chase.LastCoord.Value = snake.CrushCoordinate;
                    snake.Path.Clear();
                },
                Tasks = new[]
                {
                    checkMap,
                    checkOperationalRadius,
                    checkTargetAgent,
                    new AI.Task
                    {
                        Interval = 0.01f,
                        Action   = delegate()
                        {
                            Agent a = ai.TargetAgent.Value.Target.Get <Agent>();
                            a.Damage.Execute(0.01f / 1.5f);                                     // seconds to kill
                            if (!a.Active)
                            {
                                ai.CurrentState.Value = "Alert";
                            }
                            else
                            {
                                if ((a.Position - transform.Position.Value).Length() > 5.0f)                                         // They're getting away
                                {
                                    ai.CurrentState.Value = "Chase";
                                }
                            }
                        }
                    }
                },
            }
            );

            this.SetMain(entity, main);

            entity.Add("OperationalRadius", snake.OperationalRadius);
        }
        public override void Bind(Entity entity, Main main, bool creating = false)
        {
            SpotLight light = entity.Create <SpotLight>();

            light.Enabled.Value = !main.EditorEnabled;

            light.FieldOfView.Value = 1.0f;
            light.Attenuation.Value = 75.0f;

            Transform transform = entity.GetOrCreate <Transform>("Transform");

            light.Add(new Binding <Vector3>(light.Position, transform.Position));

            Turret turret = entity.GetOrCreate <Turret>("Turret");

            Command die = new Command
            {
                Action = delegate()
                {
                    AkSoundEngine.PostEvent(AK.EVENTS.PLAY_TURRET_DEATH, entity);
                    ParticleSystem shatter = ParticleSystem.Get(main, "InfectedShatter");
                    Random         random  = new Random();
                    for (int i = 0; i < 50; i++)
                    {
                        Vector3 offset = new Vector3((float)random.NextDouble() - 0.5f, (float)random.NextDouble() - 0.5f, (float)random.NextDouble() - 0.5f);
                        shatter.AddParticle(transform.Position + offset, offset);
                    }
                    entity.Delete.Execute();
                }
            };
            VoxelAttachable attachable = VoxelAttachable.MakeAttachable(entity, main, true, true, die);

            attachable.Enabled.Value = true;
            attachable.Offset.Value  = 2;

            PointLight pointLight = entity.GetOrCreate <PointLight>();

            pointLight.Serialize = false;
            pointLight.Add(new Binding <Vector3>(pointLight.Position, transform.Position));

            LineDrawer laser = new LineDrawer {
                Serialize = false
            };

            entity.Add(laser);
            laser.DrawOrder.Value = 0;

            AI ai = entity.GetOrCreate <AI>("AI");

            ModelAlpha model = entity.Create <ModelAlpha>();

            model.Add(new Binding <Matrix>(model.Transform, transform.Matrix));
            model.Filename.Value = "AlphaModels\\pyramid";

            const float defaultModelScale = 0.75f;

            model.Scale.Value = new Vector3(defaultModelScale);

            const float sightDistance     = 80.0f;
            const float hearingDistance   = 0.0f;
            const float operationalRadius = 100.0f;

            model.Add(new Binding <Vector3, string>(model.Color, delegate(string state)
            {
                switch (state)
                {
                case "Alert":
                    return(new Vector3(1.0f, 1.0f, 0.25f));

                case "Aggressive":
                    return(new Vector3(1.0f, 0.5f, 0.25f));

                case "Firing":
                    return(new Vector3(1.0f, 0.0f, 0.0f));

                case "Disabled":
                    return(new Vector3(0.0f, 0.0f, 0.0f));

                default:
                    return(new Vector3(1.0f, 1.0f, 1.0f));
                }
            }, ai.CurrentState));
            laser.Add(new Binding <bool, string>(laser.Enabled, x => x != "Disabled" && x != "Suspended", ai.CurrentState));

            light.Add(new Binding <Vector3>(light.Color, model.Color));
            pointLight.Add(new Binding <Vector3>(pointLight.Color, model.Color));

            Voxel.GlobalRaycastResult rayHit = new Voxel.GlobalRaycastResult();
            Vector3 toReticle = Vector3.Zero;

            AI.Task checkOperationalRadius = new AI.Task
            {
                Interval = 2.0f,
                Action   = delegate()
                {
                    bool shouldBeActive = (transform.Position.Value - main.Camera.Position).Length() < operationalRadius && Water.Get(transform.Position) == null;
                    if (shouldBeActive && ai.CurrentState == "Suspended")
                    {
                        ai.CurrentState.Value = "Idle";
                    }
                    else if (!shouldBeActive && ai.CurrentState != "Suspended")
                    {
                        ai.CurrentState.Value = "Suspended";
                    }
                },
            };

            turret.Add(new CommandBinding(turret.PowerOn, delegate()
            {
                if (ai.CurrentState == "Disabled")
                {
                    ai.CurrentState.Value = "Suspended";
                    checkOperationalRadius.Action();
                }
            }));

            turret.Add(new CommandBinding(turret.PowerOff, delegate()
            {
                ai.CurrentState.Value = "Disabled";
            }));

            light.Add(new Binding <Quaternion>(light.Orientation, delegate()
            {
                return(Quaternion.CreateFromYawPitchRoll(-(float)Math.Atan2(toReticle.Z, toReticle.X) - (float)Math.PI * 0.5f, (float)Math.Asin(toReticle.Y), 0));
            }, transform.Position, turret.Reticle));

            AI.Task updateRay = new AI.Task
            {
                Action = delegate()
                {
                    toReticle = Vector3.Normalize(turret.Reticle.Value - transform.Position.Value);
                    rayHit    = Voxel.GlobalRaycast(transform.Position, toReticle, operationalRadius);
                    laser.Lines.Clear();

                    Microsoft.Xna.Framework.Color color = new Microsoft.Xna.Framework.Color(model.Color);
                    laser.Lines.Add(new LineDrawer.Line
                    {
                        A = new Microsoft.Xna.Framework.Graphics.VertexPositionColor(transform.Position, color),
                        B = new Microsoft.Xna.Framework.Graphics.VertexPositionColor(rayHit.Position, color),
                    });
                }
            };

            ai.Add(new AI.AIState
            {
                Name  = "Disabled",
                Tasks = new AI.Task[] { },
            });

            ai.Add(new AI.AIState
            {
                Name  = "Suspended",
                Tasks = new[] { checkOperationalRadius, },
            });

            ai.Add(new AI.AIState
            {
                Name  = "Idle",
                Tasks = new[]
                {
                    checkOperationalRadius,
                    updateRay,
                    new AI.Task
                    {
                        Interval = 1.0f,
                        Action   = delegate()
                        {
                            Agent a = Agent.Query(transform.Position, sightDistance, hearingDistance, x => x.Entity.Type == "Player");
                            if (a != null)
                            {
                                ai.CurrentState.Value = "Alert";
                            }
                        },
                    },
                },
            });

            ai.Add(new AI.AIState
            {
                Name  = "Alert",
                Tasks = new[]
                {
                    checkOperationalRadius,
                    updateRay,
                    new AI.Task
                    {
                        Interval = 1.0f,
                        Action   = delegate()
                        {
                            if (ai.TimeInCurrentState > 3.0f)
                            {
                                ai.CurrentState.Value = "Idle";
                            }
                            else
                            {
                                Agent a = Agent.Query(transform.Position, sightDistance, hearingDistance, x => x.Entity.Type == "Player");
                                if (a != null)
                                {
                                    ai.TargetAgent.Value  = a.Entity;
                                    ai.CurrentState.Value = "Aggressive";
                                }
                            }
                        },
                    },
                },
            });

            AI.Task checkTargetAgent = new AI.Task
            {
                Action = delegate()
                {
                    Entity target = ai.TargetAgent.Value.Target;
                    if (target == null || !target.Active)
                    {
                        ai.TargetAgent.Value  = null;
                        ai.CurrentState.Value = "Idle";
                    }
                },
            };

            float lastSpotted = 0.0f;

            ai.Add(new AI.AIState
            {
                Name  = "Aggressive",
                Tasks = new[]
                {
                    checkTargetAgent,
                    updateRay,
                    new AI.Task
                    {
                        Action = delegate()
                        {
                            Entity target         = ai.TargetAgent.Value.Target;
                            turret.Reticle.Value += (target.Get <Transform>().Position - turret.Reticle.Value) * Math.Min(3.0f * main.ElapsedTime, 1.0f);
                        }
                    },
                    new AI.Task
                    {
                        Interval = 0.1f,
                        Action   = delegate()
                        {
                            if (Agent.Query(transform.Position, sightDistance, hearingDistance, ai.TargetAgent.Value.Target.Get <Agent>()))
                            {
                                lastSpotted = main.TotalTime;
                            }

                            if (ai.TimeInCurrentState.Value > 1.5f)
                            {
                                if (lastSpotted < main.TotalTime - 2.0f)
                                {
                                    ai.CurrentState.Value = "Alert";
                                }
                                else
                                {
                                    Vector3 targetPos = ai.TargetAgent.Value.Target.Get <Transform>().Position.Value;
                                    Vector3 toTarget  = Vector3.Normalize(targetPos - transform.Position.Value);
                                    if (Vector3.Dot(toReticle, toTarget) > 0.95f)
                                    {
                                        ai.CurrentState.Value = "Firing";
                                    }
                                }
                            }
                        }
                    },
                }
            });

            ai.Add(new AI.AIState
            {
                Name  = "Firing",
                Enter = delegate(AI.AIState last)
                {
                    AkSoundEngine.PostEvent(AK.EVENTS.PLAY_TURRET_CHARGE, entity);
                },
                Exit = delegate(AI.AIState next)
                {
                    Voxel.State attachedState = attachable.AttachedVoxel.Value.Target.Get <Voxel>()[attachable.Coord];
                    if (!attachedState.Permanent && rayHit.Voxel != null && (rayHit.Position - transform.Position).Length() < 8.0f)
                    {
                        return;                         // Danger close, cease fire!
                    }
                    AkSoundEngine.PostEvent(AK.EVENTS.PLAY_TURRET_FIRE, entity);

                    bool hitVoxel = true;

                    Entity target = ai.TargetAgent.Value.Target;
                    if (target != null && target.Active)
                    {
                        Vector3 targetPos = target.Get <Transform>().Position;
                        Vector3 toTarget  = targetPos - transform.Position.Value;
                        if (Vector3.Dot(toReticle, Vector3.Normalize(toTarget)) > 0.2f)
                        {
                            float distance = toTarget.Length();
                            if (distance < rayHit.Distance)
                            {
                                Sound.PostEvent(AK.EVENTS.PLAY_TURRET_MISS, transform.Position + toReticle * distance);
                            }
                        }

                        BEPUutilities.RayHit physicsHit;
                        if (target.Get <Player>().Character.Body.CollisionInformation.RayCast(new Ray(transform.Position, toReticle), rayHit.Voxel == null ? float.MaxValue : rayHit.Distance, out physicsHit))
                        {
                            Explosion.Explode(main, targetPos, 6, 8.0f);
                            hitVoxel = false;
                        }
                    }

                    if (hitVoxel && rayHit.Voxel != null)
                    {
                        Explosion.Explode(main, rayHit.Position + rayHit.Voxel.GetAbsoluteVector(rayHit.Normal.GetVector()) * 0.5f, 6, 8.0f);
                    }

                    Vector3 splashPos;
                    Water w = Water.Raycast(transform.Position, toReticle, rayHit.Distance, out splashPos);
                    if (w != null)
                    {
                        Sound.PostEvent(AK.EVENTS.PLAY_WATER_SPLASH, splashPos);
                        Water.SplashParticles(main, splashPos, 3.0f);
                    }
                },
                Tasks = new[]
                {
                    checkTargetAgent,
                    updateRay,
                    new AI.Task
                    {
                        Action = delegate()
                        {
                            if (ai.TimeInCurrentState.Value > 0.75f)
                            {
                                ai.CurrentState.Value = "Aggressive";                                 // This actually fires (in the Exit function)
                            }
                        }
                    }
                }
            });

            this.SetMain(entity, main);

            entity.Add("On", turret.On);
            entity.Add("PowerOn", turret.PowerOn);
            entity.Add("PowerOff", turret.PowerOff);
        }
Exemple #4
0
        public override void Bind(Entity entity, Main main, bool creating = false)
        {
            PointLight light = entity.GetOrCreate <PointLight>("PointLight");

            light.Serialize = false;

            const float defaultLightAttenuation = 15.0f;

            light.Attenuation.Value = defaultLightAttenuation;

            Transform transform = entity.GetOrCreate <Transform>("Transform");

            light.Add(new Binding <Vector3>(light.Position, transform.Position));

            if (!main.EditorEnabled)
            {
                AkGameObjectTracker.Attach(entity);
                SoundKiller.Add(entity, AK.EVENTS.STOP_GLOWSQUARE);
                entity.Add(new PostInitialization
                {
                    delegate()
                    {
                        AkSoundEngine.PostEvent(AK.EVENTS.PLAY_GLOWSQUARE, entity);
                        AkSoundEngine.SetRTPCValue(AK.GAME_PARAMETERS.SFX_GLOWSQUARE_PITCH, -1.0f, entity);
                    }
                });
            }

            AI ai = entity.GetOrCreate <AI>("AI");

            ModelAlpha model = entity.GetOrCreate <ModelAlpha>();

            model.Add(new Binding <Matrix>(model.Transform, transform.Matrix));
            model.Filename.Value  = "AlphaModels\\box";
            model.Serialize       = false;
            model.DrawOrder.Value = 15;

            RaycastAIMovement movement  = entity.GetOrCreate <RaycastAIMovement>("Movement");
            Levitator         levitator = entity.GetOrCreate <Levitator>("Levitator");

            const float defaultModelScale = 1.0f;

            model.Scale.Value = new Vector3(defaultModelScale);

            model.Add(new Binding <Vector3, string>(model.Color, delegate(string state)
            {
                switch (state)
                {
                case "Alert":
                    return(new Vector3(1.5f, 1.5f, 0.5f));

                case "Chase":
                    return(new Vector3(1.5f, 0.5f, 0.5f));

                case "Levitating":
                    return(new Vector3(2.0f, 1.0f, 0.5f));

                default:
                    return(new Vector3(1.0f, 1.0f, 1.0f));
                }
            }, ai.CurrentState));

            entity.Add(new Updater
                       (
                           delegate(float dt)
            {
                float source            = 1.0f + ((float)this.random.NextDouble() - 0.5f) * 2.0f * 0.05f;
                model.Scale.Value       = new Vector3(defaultModelScale * source);
                light.Attenuation.Value = defaultLightAttenuation * source;
            }
                       ));

            light.Add(new Binding <Vector3>(light.Color, model.Color));

            Agent agent = entity.GetOrCreate <Agent>();

            agent.Add(new Binding <Vector3>(agent.Position, transform.Position));

            RaycastAI raycastAI = entity.GetOrCreate <RaycastAI>("RaycastAI");

            raycastAI.Add(new TwoWayBinding <Vector3>(transform.Position, raycastAI.Position));
            raycastAI.Add(new Binding <Quaternion>(transform.Quaternion, raycastAI.Orientation));

            AI.Task checkOperationalRadius = new AI.Task
            {
                Interval = 2.0f,
                Action   = delegate()
                {
                    bool shouldBeActive = (transform.Position.Value - main.Camera.Position).Length() < movement.OperationalRadius;
                    if (shouldBeActive && ai.CurrentState == "Suspended")
                    {
                        ai.CurrentState.Value = "Idle";
                    }
                    else if (!shouldBeActive && ai.CurrentState != "Suspended")
                    {
                        ai.CurrentState.Value = "Suspended";
                    }
                },
            };

            AI.Task updatePosition = new AI.Task
            {
                Action = delegate()
                {
                    raycastAI.Update();
                },
            };

            ai.Add(new AI.AIState
            {
                Name  = "Suspended",
                Tasks = new[] { checkOperationalRadius, },
            });

            const float sightDistance   = 30.0f;
            const float hearingDistance = 15.0f;

            ai.Add(new AI.AIState
            {
                Name  = "Idle",
                Enter = delegate(AI.AIState previous)
                {
                    AkSoundEngine.SetRTPCValue(AK.GAME_PARAMETERS.SFX_GLOWSQUARE_PITCH, -1.0f, entity);
                },
                Tasks = new[]
                {
                    checkOperationalRadius,
                    updatePosition,
                    new AI.Task
                    {
                        Interval = 1.0f,
                        Action   = delegate()
                        {
                            raycastAI.Move(new Vector3(((float)this.random.NextDouble() * 2.0f) - 1.0f, ((float)this.random.NextDouble() * 2.0f) - 1.0f, ((float)this.random.NextDouble() * 2.0f) - 1.0f));
                        }
                    },
                    new AI.Task
                    {
                        Interval = 1.0f,
                        Action   = delegate()
                        {
                            Agent a = Agent.Query(transform.Position, sightDistance, hearingDistance, x => x.Entity.Type == "Player");
                            if (a != null)
                            {
                                ai.CurrentState.Value = "Alert";
                            }
                        },
                    },
                },
            });

            ai.Add(new AI.AIState
            {
                Name  = "Alert",
                Enter = delegate(AI.AIState previous)
                {
                    AkSoundEngine.PostEvent(AK.EVENTS.STOP_GLOWSQUARE, entity);
                },
                Exit = delegate(AI.AIState next)
                {
                    AkSoundEngine.PostEvent(AK.EVENTS.PLAY_GLOWSQUARE, entity);
                },
                Tasks = new[]
                {
                    checkOperationalRadius,
                    updatePosition,
                    new AI.Task
                    {
                        Interval = 1.0f,
                        Action   = delegate()
                        {
                            if (ai.TimeInCurrentState > 3.0f)
                            {
                                ai.CurrentState.Value = "Idle";
                            }
                            else
                            {
                                Agent a = Agent.Query(transform.Position, sightDistance, hearingDistance, x => x.Entity.Type == "Player");
                                if (a != null)
                                {
                                    ai.TargetAgent.Value  = a.Entity;
                                    ai.CurrentState.Value = "Chase";
                                }
                            }
                        },
                    },
                },
            });

            AI.Task checkTargetAgent = new AI.Task
            {
                Action = delegate()
                {
                    Entity target = ai.TargetAgent.Value.Target;
                    if (target == null || !target.Active)
                    {
                        ai.TargetAgent.Value  = null;
                        ai.CurrentState.Value = "Idle";
                    }
                },
            };

            // Levitate

            const int levitateRipRadius = 4;

            Func <bool> tryLevitate = delegate()
            {
                Entity voxelEntity = raycastAI.Voxel.Value.Target;
                if (voxelEntity == null)
                {
                    return(false);
                }

                Voxel       map       = voxelEntity.Get <Voxel>();
                Voxel.Coord?candidate = map.FindClosestFilledCell(raycastAI.Coord, 3);

                if (!candidate.HasValue)
                {
                    return(false);
                }

                if (VoxelRip.Go(map, candidate.Value, levitateRipRadius, delegate(List <DynamicVoxel> spawnedMaps)
                {
                    foreach (DynamicVoxel spawnedMap in spawnedMaps)
                    {
                        if (spawnedMap[candidate.Value] != Voxel.States.Empty)
                        {
                            levitator.LevitatingVoxel.Value = spawnedMap.Entity;
                            break;
                        }
                    }
                }))
                {
                    levitator.GrabCoord.Value = candidate.Value;
                    return(true);
                }

                return(false);
            };

            Action delevitateMap = delegate()
            {
                Entity levitatingMapEntity = levitator.LevitatingVoxel.Value.Target;
                if (levitatingMapEntity == null || !levitatingMapEntity.Active)
                {
                    return;
                }

                DynamicVoxel dynamicMap = levitatingMapEntity.Get <DynamicVoxel>();
                VoxelRip.Consolidate(main, dynamicMap);
            };

            // Chase AI state

            ai.Add(new AI.AIState
            {
                Name  = "Chase",
                Enter = delegate(AI.AIState previous)
                {
                    AkSoundEngine.SetRTPCValue(AK.GAME_PARAMETERS.SFX_GLOWSQUARE_PITCH, 0.0f, entity);
                },
                Tasks = new[]
                {
                    checkOperationalRadius,
                    checkTargetAgent,
                    new AI.Task
                    {
                        Interval = 0.35f,
                        Action   = delegate()
                        {
                            raycastAI.Move(ai.TargetAgent.Value.Target.Get <Transform>().Position.Value - transform.Position);
                        }
                    },
                    updatePosition,
                    new AI.Task
                    {
                        Interval = 0.1f,
                        Action   = delegate()
                        {
                            Entity target          = ai.TargetAgent.Value.Target;
                            Vector3 targetPosition = target.Get <Transform>().Position;
                            if ((targetPosition - transform.Position).Length() < 15.0f)
                            {
                                if (tryLevitate())
                                {
                                    ai.CurrentState.Value = "Levitating";
                                }
                            }
                        }
                    }
                },
            });

            Action findNextPosition = delegate()
            {
                movement.LastPosition.Value = transform.Position.Value;
                float   radius = 5.0f;
                Vector3 center = ai.TargetAgent.Value.Target.Get <Transform>().Position;
                Vector3 candidate;
                do
                {
                    candidate = center + new Vector3((float)this.random.NextDouble() - 0.5f, (float)this.random.NextDouble(), (float)this.random.NextDouble() - 0.5f) * radius;
                    radius   += 1.0f;
                }while (!RaycastAI.DefaultPositionFilter(candidate));

                movement.NextPosition.Value  = candidate;
                movement.PositionBlend.Value = 0.0f;
            };

            ai.Add(new AI.AIState
            {
                Name  = "Levitating",
                Enter = delegate(AI.AIState previous)
                {
                    findNextPosition();
                },
                Exit = delegate(AI.AIState next)
                {
                    delevitateMap();
                    levitator.LevitatingVoxel.Value = null;

                    //volume.Value = defaultVolume;
                    //pitch.Value = 0.0f;
                },
                Tasks = new[]
                {
                    checkTargetAgent,
                    new AI.Task
                    {
                        Action = delegate()
                        {
                            //volume.Value = 1.0f;
                            //pitch.Value = 1.0f;
                            Entity levitatingMapEntity = levitator.LevitatingVoxel.Value.Target;
                            if (levitatingMapEntity == null || !levitatingMapEntity.Active || ai.TimeInCurrentState.Value > 8.0f)
                            {
                                Entity voxel = raycastAI.Voxel.Value.Target;
                                if (voxel != null && voxel.Active)
                                {
                                    raycastAI.Coord.Value = raycastAI.LastCoord.Value = voxel.Get <Voxel>().GetCoordinate(transform.Position);
                                }
                                raycastAI.Move(new Vector3(((float)this.random.NextDouble() * 2.0f) - 1.0f, ((float)this.random.NextDouble() * 2.0f) - 1.0f, ((float)this.random.NextDouble() * 2.0f) - 1.0f));
                                ai.CurrentState.Value = "Alert";
                                return;
                            }

                            DynamicVoxel dynamicMap = levitatingMapEntity.Get <DynamicVoxel>();

                            movement.PositionBlend.Value += (main.ElapsedTime.Value / 1.0f);
                            if (movement.PositionBlend > 1.0f)
                            {
                                findNextPosition();
                            }

                            transform.Position.Value = Vector3.Lerp(movement.LastPosition, movement.NextPosition, movement.PositionBlend);

                            Vector3 grabPoint = dynamicMap.GetAbsolutePosition(levitator.GrabCoord);
                            Vector3 diff      = transform.Position.Value - grabPoint;
                            if (diff.Length() > 15.0f)
                            {
                                ai.CurrentState.Value = "Chase";
                                return;
                            }

                            diff *= (float)Math.Sqrt(dynamicMap.PhysicsEntity.Mass) * 0.5f;
                            dynamicMap.PhysicsEntity.ApplyImpulse(ref grabPoint, ref diff);
                        },
                    },
                },
            });

            this.SetMain(entity, main);
        }
Exemple #5
0
        public override void Bind(Entity entity, Main main, bool creating = false)
        {
            BlockCloud blockCloud = entity.GetOrCreate <BlockCloud>("BlockCloud");

            blockCloud.Add(new CommandBinding(blockCloud.Delete, entity.Delete));
            blockCloud.Add(new CommandBinding <Collidable, ContactCollection>(blockCloud.Collided, delegate(Collidable other, ContactCollection contacts)
            {
                if (other.Tag != null && other.Tag.GetType() == typeof(Character))
                {
                    // Damage the player
                    Entity p = PlayerFactory.Instance;
                    if (p != null && p.Active)
                    {
                        p.Get <Agent>().Damage.Execute(0.1f);
                    }
                }
            }));
            blockCloud.Type.Value = Voxel.t.Black;

            Transform transform = entity.GetOrCreate <Transform>("Transform");

            AkGameObjectTracker.Attach(entity);

            AI ai = entity.GetOrCreate <AI>("AI");

            if (!main.EditorEnabled)
            {
                entity.Add(new PostInitialization
                {
                    delegate()
                    {
                        AkSoundEngine.PostEvent(AK.EVENTS.PLAY_EVIL_CUBES, entity);
                        AkSoundEngine.PostEvent(ai.CurrentState == "Chase" ? AK.EVENTS.EVIL_CUBES_CHASE : AK.EVENTS.EVIL_CUBES_IDLE, entity);
                    }
                });

                SoundKiller.Add(entity, AK.EVENTS.STOP_EVIL_CUBES);
            }

            Agent agent = entity.GetOrCreate <Agent>();

            agent.Add(new Binding <Vector3>(agent.Position, transform.Position));

            RaycastAI raycastAI = entity.GetOrCreate <RaycastAI>("RaycastAI");

            raycastAI.BlendTime.Value = 1.0f;
            raycastAI.Add(new TwoWayBinding <Vector3>(transform.Position, raycastAI.Position));
            raycastAI.Add(new Binding <Quaternion>(transform.Quaternion, raycastAI.Orientation));

            blockCloud.Add(new Binding <Vector3>(blockCloud.Position, transform.Position));

            const float operationalRadius = 100.0f;

            AI.Task checkOperationalRadius = new AI.Task
            {
                Interval = 2.0f,
                Action   = delegate()
                {
                    bool shouldBeActive = (transform.Position.Value - main.Camera.Position).Length() < operationalRadius;
                    if (shouldBeActive && ai.CurrentState == "Suspended")
                    {
                        ai.CurrentState.Value = "Idle";
                    }
                    else if (!shouldBeActive && ai.CurrentState != "Suspended")
                    {
                        ai.CurrentState.Value = "Suspended";
                    }
                },
            };

            AI.Task updatePosition = new AI.Task
            {
                Action = delegate()
                {
                    raycastAI.Update();
                },
            };

            ai.Add(new AI.AIState
            {
                Name  = "Suspended",
                Tasks = new[] { checkOperationalRadius, },
            });

            const float sightDistance   = 30.0f;
            const float hearingDistance = 15.0f;

            ai.Add(new AI.AIState
            {
                Name  = "Idle",
                Tasks = new[]
                {
                    checkOperationalRadius,
                    updatePosition,
                    new AI.Task
                    {
                        Interval = 1.0f,
                        Action   = delegate()
                        {
                            raycastAI.Move(new Vector3(((float)this.random.NextDouble() * 2.0f) - 1.0f, ((float)this.random.NextDouble() * 2.0f) - 1.0f, ((float)this.random.NextDouble() * 2.0f) - 1.0f));
                        }
                    },
                    new AI.Task
                    {
                        Interval = 1.0f,
                        Action   = delegate()
                        {
                            Agent a = Agent.Query(transform.Position, sightDistance, hearingDistance, x => x.Entity.Type == "Player");
                            if (a != null)
                            {
                                ai.CurrentState.Value = "Alert";
                            }
                        },
                    },
                },
            });

            ai.Add(new AI.AIState
            {
                Name  = "Alert",
                Tasks = new[]
                {
                    checkOperationalRadius,
                    updatePosition,
                    new AI.Task
                    {
                        Interval = 1.0f,
                        Action   = delegate()
                        {
                            if (ai.TimeInCurrentState > 3.0f)
                            {
                                ai.CurrentState.Value = "Idle";
                            }
                            else
                            {
                                Agent a = Agent.Query(transform.Position, sightDistance, hearingDistance, x => x.Entity.Type == "Player");
                                if (a != null)
                                {
                                    ai.TargetAgent.Value  = a.Entity;
                                    ai.CurrentState.Value = "Chase";
                                }
                            }
                        },
                    },
                },
            });

            AI.Task checkTargetAgent = new AI.Task
            {
                Action = delegate()
                {
                    Entity target = ai.TargetAgent.Value.Target;
                    if (target == null || !target.Active)
                    {
                        ai.TargetAgent.Value  = null;
                        ai.CurrentState.Value = "Idle";
                    }
                },
            };

            // Chase AI state

            ai.Add(new AI.AIState
            {
                Name  = "Chase",
                Enter = delegate(AI.AIState previous)
                {
                    AkSoundEngine.PostEvent(AK.EVENTS.EVIL_CUBES_CHASE, entity);
                    raycastAI.BlendTime.Value = 0.5f;
                },
                Exit = delegate(AI.AIState next)
                {
                    raycastAI.BlendTime.Value = 1.0f;
                    AkSoundEngine.PostEvent(AK.EVENTS.EVIL_CUBES_IDLE, entity);
                },
                Tasks = new[]
                {
                    checkOperationalRadius,
                    checkTargetAgent,
                    new AI.Task
                    {
                        Interval = 0.5f,
                        Action   = delegate()
                        {
                            raycastAI.Move(ai.TargetAgent.Value.Target.Get <Transform>().Position.Value - transform.Position);
                        }
                    },
                    updatePosition,
                },
            });

            this.SetMain(entity, main);
        }
Exemple #6
0
        public override void Bind(Entity entity, Main main, bool creating = false)
        {
            PointLight light = entity.GetOrCreate <PointLight>("PointLight");

            light.Serialize = false;

            const float defaultLightAttenuation = 15.0f;

            light.Attenuation.Value = defaultLightAttenuation;

            Transform transform = entity.GetOrCreate <Transform>("Transform");

            light.Add(new Binding <Vector3>(light.Position, transform.Position));

            if (!main.EditorEnabled)
            {
                Sound.AttachTracker(entity);
                SoundKiller.Add(entity, AK.EVENTS.STOP_GLOWSQUARE);
                entity.Add(new PostInitialization(delegate()
                {
                    AkSoundEngine.PostEvent(AK.EVENTS.PLAY_GLOWSQUARE, entity);
                    AkSoundEngine.SetRTPCValue(AK.GAME_PARAMETERS.SFX_GLOWSQUARE_PITCH, -1.0f, entity);
                }));
            }

            AI ai = entity.GetOrCreate <AI>("AI");

            ModelAlpha model = entity.GetOrCreate <ModelAlpha>();

            model.Add(new Binding <Matrix>(model.Transform, transform.Matrix));
            model.Filename.Value  = "AlphaModels\\box";
            model.Serialize       = false;
            model.DrawOrder.Value = 15;

            const float defaultModelScale = 1.0f;

            model.Scale.Value = new Vector3(defaultModelScale);

            model.Add(new Binding <Vector3, string>(model.Color, delegate(string state)
            {
                switch (state)
                {
                case "Alert":
                    return(new Vector3(1.5f, 1.5f, 0.5f));

                case "Chase":
                    return(new Vector3(1.5f, 0.5f, 0.5f));

                case "Explode":
                    return(new Vector3(2.0f, 1.0f, 0.5f));

                default:
                    return(new Vector3(1.0f, 1.0f, 1.0f));
                }
            }, ai.CurrentState));

            entity.Add(new Updater
                       (
                           delegate(float dt)
            {
                float source            = 1.0f + ((float)this.random.NextDouble() - 0.5f) * 2.0f * 0.05f;
                model.Scale.Value       = new Vector3(defaultModelScale * source);
                light.Attenuation.Value = defaultLightAttenuation * source;
            }
                       ));

            model.Add(new Binding <bool, string>(model.Enabled, x => x != "Exploding", ai.CurrentState));

            light.Add(new Binding <Vector3>(light.Color, model.Color));

            Agent agent = entity.GetOrCreate <Agent>();

            agent.Add(new Binding <Vector3>(agent.Position, transform.Position));

            RaycastAIMovement movement = entity.GetOrCreate <RaycastAIMovement>("Movement");
            Exploder          exploder = entity.GetOrCreate <Exploder>("Exploder");

            AI.Task checkOperationalRadius = new AI.Task
            {
                Interval = 2.0f,
                Action   = delegate()
                {
                    bool shouldBeActive = (transform.Position.Value - main.Camera.Position).Length() < movement.OperationalRadius;
                    if (shouldBeActive && ai.CurrentState == "Suspended")
                    {
                        ai.CurrentState.Value = "Idle";
                    }
                    else if (!shouldBeActive && ai.CurrentState != "Suspended")
                    {
                        ai.CurrentState.Value = "Suspended";
                    }
                },
            };

            RaycastAI raycastAI = entity.GetOrCreate <RaycastAI>("RaycastAI");

            raycastAI.Add(new TwoWayBinding <Vector3>(transform.Position, raycastAI.Position));
            raycastAI.Add(new Binding <Quaternion>(transform.Quaternion, raycastAI.Orientation));

            AI.Task updatePosition = new AI.Task
            {
                Action = delegate()
                {
                    raycastAI.Update();
                },
            };

            ai.Add(new AI.AIState
            {
                Name  = "Suspended",
                Tasks = new[] { checkOperationalRadius, },
            });

            const float sightDistance   = 40.0f;
            const float hearingDistance = 0.0f;

            ai.Add(new AI.AIState
            {
                Name  = "Idle",
                Enter = delegate(AI.AIState previous)
                {
                    AkSoundEngine.SetRTPCValue(AK.GAME_PARAMETERS.SFX_GLOWSQUARE_PITCH, -1.0f, entity);
                },
                Tasks = new[]
                {
                    checkOperationalRadius,
                    updatePosition,
                    new AI.Task
                    {
                        Interval = 1.0f,
                        Action   = delegate()
                        {
                            raycastAI.Move(new Vector3(((float)this.random.NextDouble() * 2.0f) - 1.0f, ((float)this.random.NextDouble() * 2.0f) - 1.0f, ((float)this.random.NextDouble() * 2.0f) - 1.0f));
                        }
                    },
                    new AI.Task
                    {
                        Interval = 0.5f,
                        Action   = delegate()
                        {
                            Agent a = Agent.Query(transform.Position, sightDistance, hearingDistance, x => x.Entity.Type == "Player");
                            if (a != null)
                            {
                                ai.CurrentState.Value = "Alert";
                            }
                        },
                    },
                },
            });

            ai.Add(new AI.AIState
            {
                Name  = "Alert",
                Enter = delegate(AI.AIState previous)
                {
                    AkSoundEngine.PostEvent(AK.EVENTS.STOP_GLOWSQUARE, entity);
                },
                Exit = delegate(AI.AIState next)
                {
                    AkSoundEngine.PostEvent(AK.EVENTS.PLAY_GLOWSQUARE, entity);
                },
                Tasks = new[]
                {
                    checkOperationalRadius,
                    updatePosition,
                    new AI.Task
                    {
                        Interval = 1.0f,
                        Action   = delegate()
                        {
                            if (ai.TimeInCurrentState > 3.0f)
                            {
                                ai.CurrentState.Value = "Idle";
                            }
                            else
                            {
                                Agent a = Agent.Query(transform.Position, sightDistance, hearingDistance, x => x.Entity.Type == "Player");
                                if (a != null)
                                {
                                    ai.TargetAgent.Value  = a.Entity;
                                    ai.CurrentState.Value = "Chase";
                                }
                            }
                        },
                    },
                },
            });

            AI.Task checkTargetAgent = new AI.Task
            {
                Action = delegate()
                {
                    Entity target = ai.TargetAgent.Value.Target;
                    if (target == null || !target.Active)
                    {
                        ai.TargetAgent.Value  = null;
                        ai.CurrentState.Value = "Idle";
                    }
                },
            };

            ai.Add(new AI.AIState
            {
                Name  = "Chase",
                Enter = delegate(AI.AIState previous)
                {
                    AkSoundEngine.SetRTPCValue(AK.GAME_PARAMETERS.SFX_GLOWSQUARE_PITCH, 0.0f, entity);
                },
                Tasks = new[]
                {
                    checkOperationalRadius,
                    checkTargetAgent,
                    new AI.Task
                    {
                        Interval = 0.35f,
                        Action   = delegate()
                        {
                            raycastAI.Move(ai.TargetAgent.Value.Target.Get <Transform>().Position.Value - transform.Position);
                        }
                    },
                    new AI.Task
                    {
                        Action = delegate()
                        {
                            if ((ai.TargetAgent.Value.Target.Get <Transform>().Position.Value - transform.Position).Length() < 10.0f)
                            {
                                ai.CurrentState.Value = "Explode";
                            }
                        }
                    },
                    updatePosition,
                },
            });

            EffectBlockFactory factory = Factory.Get <EffectBlockFactory>();

            ai.Add(new AI.AIState
            {
                Name  = "Explode",
                Enter = delegate(AI.AIState previous)
                {
                    exploder.CoordQueue.Clear();

                    Entity voxelEntity = raycastAI.Voxel.Value.Target;
                    if (voxelEntity == null || !voxelEntity.Active)
                    {
                        ai.CurrentState.Value = "Alert";
                        return;
                    }

                    Voxel m = voxelEntity.Get <Voxel>();

                    Voxel.Coord c = raycastAI.Coord.Value;

                    Direction toSupport = Direction.None;

                    foreach (Direction dir in DirectionExtensions.Directions)
                    {
                        if (m[raycastAI.Coord.Value.Move(dir)].ID != 0)
                        {
                            toSupport = dir;
                            break;
                        }
                    }

                    if (toSupport == Direction.None)
                    {
                        ai.CurrentState.Value = "Alert";
                        return;
                    }

                    Direction up = toSupport.GetReverse();

                    exploder.ExplosionOriginalCoord.Value = raycastAI.Coord;

                    Direction right;
                    if (up.IsParallel(Direction.PositiveX))
                    {
                        right = Direction.PositiveZ;
                    }
                    else
                    {
                        right = Direction.PositiveX;
                    }
                    Direction forward = up.Cross(right);

                    for (Voxel.Coord y = c.Clone(); y.GetComponent(up) < c.GetComponent(up) + 3; y = y.Move(up))
                    {
                        for (Voxel.Coord x = y.Clone(); x.GetComponent(right) < c.GetComponent(right) + 2; x = x.Move(right))
                        {
                            for (Voxel.Coord z = x.Clone(); z.GetComponent(forward) < c.GetComponent(forward) + 2; z = z.Move(forward))
                            {
                                exploder.CoordQueue.Add(z);
                            }
                        }
                    }
                },
                Exit = delegate(AI.AIState next)
                {
                    exploder.CoordQueue.Clear();
                },
                Tasks = new[]
                {
                    checkOperationalRadius,
                    new AI.Task
                    {
                        Interval = 0.15f,
                        Action   = delegate()
                        {
                            if (exploder.CoordQueue.Length > 0)
                            {
                                raycastAI.MoveTo(exploder.CoordQueue[0]);

                                exploder.CoordQueue.RemoveAt(0);

                                Entity blockEntity = factory.CreateAndBind(main);
                                Voxel.States.Infected.ApplyToEffectBlock(blockEntity.Get <ModelInstance>());

                                Entity mapEntity = raycastAI.Voxel.Value.Target;
                                if (mapEntity != null && mapEntity.Active)
                                {
                                    EffectBlock effectBlock = blockEntity.Get <EffectBlock>();
                                    Voxel m = mapEntity.Get <Voxel>();

                                    effectBlock.Offset.Value = m.GetRelativePosition(raycastAI.Coord);

                                    Vector3 absolutePos = m.GetAbsolutePosition(raycastAI.Coord);

                                    effectBlock.StartPosition    = absolutePos + new Vector3(0.05f, 0.1f, 0.05f);
                                    effectBlock.StartOrientation = Quaternion.CreateFromYawPitchRoll(0.15f, 0.15f, 0);
                                    effectBlock.TotalLifetime    = 0.05f;
                                    effectBlock.Setup(raycastAI.Voxel.Value.Target, raycastAI.Coord, Voxel.t.Infected);
                                    main.Add(blockEntity);
                                }
                            }
                        }
                    },
                    new AI.Task
                    {
                        Action = delegate()
                        {
                            AkSoundEngine.SetRTPCValue(AK.GAME_PARAMETERS.SFX_GLOWSQUARE_PITCH, MathHelper.Lerp(0.0f, 1.0f, ai.TimeInCurrentState.Value / 2.0f), entity);
                            if (exploder.CoordQueue.Length == 0)
                            {
                                // Explode
                                ai.CurrentState.Value = "Exploding";
                            }
                        },
                    },
                    updatePosition,
                },
            });

            ai.Add(new AI.AIState
            {
                Name  = "Exploding",
                Enter = delegate(AI.AIState previous)
                {
                    exploder.Exploded.Value = false;
                    AkSoundEngine.PostEvent(AK.EVENTS.STOP_GLOWSQUARE, entity);
                },
                Exit = delegate(AI.AIState next)
                {
                    exploder.Exploded.Value = false;
                    AkSoundEngine.PostEvent(AK.EVENTS.PLAY_GLOWSQUARE, entity);
                },
                Tasks = new[]
                {
                    new AI.Task
                    {
                        Interval = 0.1f,
                        Action   = delegate()
                        {
                            const int radius = 9;

                            float timeInCurrentState = ai.TimeInCurrentState;
                            if (timeInCurrentState > 1.0f && !exploder.Exploded)
                            {
                                Entity mapEntity = raycastAI.Voxel.Value.Target;
                                if (mapEntity != null && mapEntity.Active)
                                {
                                    Explosion.Explode(main, mapEntity.Get <Voxel>(), raycastAI.Coord, radius, 18.0f);
                                }

                                exploder.Exploded.Value = true;
                            }

                            if (timeInCurrentState > 2.0f)
                            {
                                raycastAI.Move(new Vector3(((float)this.random.NextDouble() * 2.0f) - 1.0f, ((float)this.random.NextDouble() * 2.0f) - 1.0f, ((float)this.random.NextDouble() * 2.0f) - 1.0f));
                                ai.CurrentState.Value = "Alert";
                            }
                        },
                    },
                    updatePosition,
                },
            });

            this.SetMain(entity, main);

            entity.Add("OperationalRadius", movement.OperationalRadius);
        }