public IComponent Clone() { PeriodicAddComponentComponent ret = new PeriodicAddComponentComponent(); ret.ComponentToAdd = ComponentToAdd.Clone(); ret.Period = Period; ret.TimeSinceLastFiring = TimeSinceLastFiring; 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 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); }