public IComponent Clone() { DestroyedOnParentDestroyedComponent ret = new DestroyedOnParentDestroyedComponent(); ret.parent = parent; 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); }