public IComponent Clone() { SpawnEntityComponent ret = new SpawnEntityComponent(); ret.Factory = Factory.Clone(); return ret; }
//Note: timerPhaseAngle is expected to be in the interval [0, 1] private void createPlayerGun(IEntityManager entityStore, Entity player, Entity bullet, float offsetProportion, float timerPhaseAngle) { //compute the gun's offset from the player, then create the gun AABBComponent bulletBox = (AABBComponent)bullet.components[typeof(AABBComponent)]; AABBComponent playerBox = (AABBComponent)player.components[typeof(AABBComponent)]; Entity gun = createPositionSlavedEntity(player, new Vector2(playerBox.Width + 0.1f, playerBox.Height * offsetProportion - bulletBox.Height / 2.0f)); //The gun now has a position coupled to that of the player //So spawn bullets at the gun! SpawnEntityComponent spawner = new SpawnEntityComponent(); spawner.Factory = new ComposedEntityFactory(new List<IEntityFactory>() { new CloneEntityFactory(bullet), new InheritParentComponentEntityFactory(typeof(PositionComponent)) }); //Bullets should be spawned periodically PeriodicAddComponentComponent timer = new PeriodicAddComponentComponent(); timer.Period = 200.0f; timer.TimeSinceLastFiring = timer.Period * timerPhaseAngle; timer.ComponentToAdd = spawner; gun.AddComponent(timer); //The gun should be removed from the world when the player dies DestroyedOnParentDestroyedComponent existentialDependency = new DestroyedOnParentDestroyedComponent(); existentialDependency.parent = player; gun.AddComponent(existentialDependency); //finally, add the gun to the world entityStore.Add(gun); }
private Entity createMineTemplateEntity(Game game) { Entity mineTemplate = new Entity(); //Component: Damage the player. DamageOnContactComponent damage = new DamageOnContactComponent(); damage.Damage = 10; mineTemplate.AddComponent(damage); //Component: Has health HealthComponent health = new HealthComponent(); health.Health = 10; mineTemplate.AddComponent(health); //Component: Has a texture TextureComponent tex = new TextureComponent(); tex.Texture = game.Content.Load<Texture2D>("spaceArt/png/meteorBig"); tex.SourceRect = tex.Texture.Bounds; mineTemplate.AddComponent(tex); //Component: Has a position. PositionComponent pos = new PositionComponent(); pos.Position.X = game.GraphicsDevice.Viewport.Width - 1; pos.Position.Y = game.GraphicsDevice.Viewport.Height / 2 - tex.SourceRect.Height / 2; pos.Rotation = Math.PI / 4; mineTemplate.AddComponent(pos); //Component: Given a random starting vertical position RandomPositionOffsetComponent randomOffset = new RandomPositionOffsetComponent(); randomOffset.Minimum = new Vector2(0.0f, -0.4f * game.GraphicsDevice.Viewport.Height); randomOffset.Maximum = new Vector2(0.0f, 0.4f * game.GraphicsDevice.Viewport.Height); mineTemplate.AddComponent(randomOffset); //Component: Moves linearly, continuously rotates LinearMovementComponent movement = new LinearMovementComponent(); movement.RotationRate = 0.01f; mineTemplate.AddComponent(movement); //Component: Moves at constant speed MoveSpeedComponent speed = new MoveSpeedComponent(); speed.MoveSpeed = -10; mineTemplate.AddComponent(speed); //Component: Has a bounding box AABBComponent aabb = new AABBComponent(); aabb.Height = tex.SourceRect.Height; aabb.Width = tex.SourceRect.Width; mineTemplate.AddComponent(aabb); //Component: Is rendered at a specific layer - just above the player RenderLayerComponent layer = new RenderLayerComponent(); layer.LayerID = 11; mineTemplate.AddComponent(layer); //Component: Is destroyed when it ventures off the (left side of the) screen DestroyedWhenOffScreenComponent destroyer = new DestroyedWhenOffScreenComponent(); mineTemplate.AddComponent(destroyer); //Component: Has a score value // TODO this is more complex than the other ones, involves the changing of global state! //Component: Is destroyed when it runs out of health DestroyedWhenNoHealthComponent destroyer2 = new DestroyedWhenNoHealthComponent(); mineTemplate.AddComponent(destroyer2); //Component: Leaves behind an explosion entity when it is destroyed AddComponentOnDestructionComponent explosionSpawnTriggerer = new AddComponentOnDestructionComponent(); SpawnEntityComponent explosionSpawner = new SpawnEntityComponent(); explosionSpawner.Factory = new ComposedEntityFactory(new List<IEntityFactory>() { new CloneEntityFactory(createExplosionEntity(game, aabb)), new InheritParentComponentEntityFactory(typeof(PositionComponent)) }); explosionSpawnTriggerer.ToAdd = explosionSpawner; mineTemplate.AddComponent(explosionSpawnTriggerer); return mineTemplate; }
private void initializeMinefield(IEntityManager entityStore, Game game) { //Create a mine-spawning entity that spawns mines in random positions every once in a while Entity minespawner = new Entity(); //Component: Spawn an entity SpawnEntityComponent spawnComponent = new SpawnEntityComponent(); spawnComponent.Factory = createAsteroidFactory(game); //Component: Periodically add a spawn entity component PeriodicAddComponentComponent timer = new PeriodicAddComponentComponent(); timer.ComponentToAdd = spawnComponent; timer.Period = 500; timer.TimeSinceLastFiring = 0.0f; minespawner.AddComponent(timer); //Add the minefield to the entity store entityStore.Add(minespawner); }