void TestMultipleEntities()
        {
            var entities = EntityFramework.GetEntities <ManagedEntityExample>();

            if (entities.Count != 0)
            {
                Log.Info("ManagedEntityExample ({0}): Values are {1}", entities.Count, string.Join(", ", entities.Select(x => x.ExposedInteger).ToArray()));
            }
            else
            {
                Log.Warning("ManagedEntityExample: No entities of type 'ManagedEntityExample' exist.");
            }
        }
Exemple #2
0
        public void Shake(Action onShake = null)
        {
            // Get a random position and amplitude.
            _position = GetRandomPosition(Position, RandomOffset);
            var amplitude = GetAmplitude(MinAmplitude, MaxAmplitude);

            // Get all affectors within our radius.
            var affectors = EntityFramework.GetEntities <Ball>()
                            .Where(x => x.Position.GetDistance(_position) < Radius)
                            .ToList();

            affectors.ForEach(entity =>
            {
                bool apply = false;

                if (!ApplyInAir)
                {
                    // Raycast from each affector to test if it's grounded.
                    var rayOut = RaycastHelper.Raycast(entity.Position, Vec3.Down, entity.Scale.y);

                    // If the ray hit something, it must close enough to the ground to be considered grounded.
                    apply = rayOut.Intersected && !rayOut.HasHitEntity(entity);
                }
                else
                {
                    apply = true;
                }

                // Add an impulse to each object using our position and amplitude.
                if (apply)
                {
                    entity.Physics.Action <pe_action_impulse>((p) =>
                    {
                        p.point   = _position;
                        p.impulse = Vec3.Up * amplitude;
                    });
                }
            });

            if (onShake != null)
            {
                onShake();
            }
        }
Exemple #3
0
        /// <summary>
        /// Sets up the level ready for the round to begin.
        /// </summary>
        public void Setup()
        {
            spawnPoints        = new List <SpawnPoint>();
            constructionPoints = new List <ConstructionPointController>();
            units = new List <Unit>();

            // Set the camera
            var camera = Env.EntitySystem.FindEntityByName(cameraName);

            if (camera != null)
            {
                var host = SceneObject.Instantiate(null);
                Camera.Current = host.AddComponent <Camera>();
                Camera.Current.OnPlayerEntityAssigned += (arg) =>
                {
                    arg.HostEntity.Position = camera.GetPos();
                    arg.HostEntity.Rotation = camera.GetRotation();
                    Env.Console.ExecuteString("gamezero_cam_fov 45");
                };
            }
            else
            {
                Global.gEnv.pLog.LogError("Level Manager: Camera is missing from the level. Make sure there is a Camera named " + cameraName);
            }

            // Find the Base reference entity
            PlayerBase = EntityFramework.GetEntity <Base>();
            if (PlayerBase != null)
            {
                PlayerBase.Setup();
            }
            else
            {
                Global.gEnv.pLog.LogError("Level Manager: Player Base is missing from the level. Make sure their is an entity named " + playerBaseName);
            }

            // Find construction point reference entities
            EntityFramework.GetEntities <ConstructionPoint>().ForEach(x =>
            {
                // Create construction point
                var cell = new ConstructionPointController(x.NativeEntity.GetPos(), x.NativeEntity.GetRotation(), 4, x);
                constructionPoints.Add(cell);

                // Register callback for a Unit is added or removed. We need a reference so we can update the unit with the position of each enemy.
                cell.OnUnitPlaced  += AddUnit;
                cell.OnUnitRemoved += RemoveUnit;
            });

            // Find spawn point reference entities
            var spawnsInLevel = EntityFramework.GetEntities <SpawnPoint>();

            if (spawnsInLevel.Count == 0)
            {
                Log.Warning("Failed to find any spawn points");
            }
            else
            {
                spawnsInLevel.ForEach(x =>
                {
                    // Create spawn point
                    Path path = null;

                    if (PlayerBase != null)
                    {
                        path = new Path(x.NativeEntity.GetPos(), PlayerBase.Position);
                    }

                    var instance = new SpawnPoint(path);
                    spawnPoints.Add(instance);

                    // Register callback for when an enemy is spawned and despawned
                    instance.OnEnemySpawned   += AddUnit;
                    instance.OnEnemyDespawned += RemoveUnit;
                });
            }

            // Create a grid, if the level requires one
            var gridEntity = Env.EntitySystem.FindEntityByName("Grid");

            if (gridEntity != null)
            {
                new Grid(16, 16, 2);
            }
        }