private void SetupMonsterDiedEvent(IEcs ecs, Entity geralt) { // Get a trigger for Geralt to comment sometimes after a kill. var geraltGloats = ecs.GetComponent <Talks>(geralt) .GetSpeaker(Talks.GeraltGetsMoney, 0.2); // When Geralt kills a monster ecs.EntityRemoveStarted += entity => { var type = ecs.GetComponent <IsActor>(entity).Type; if (type == ActorType.Monster) { Logger.AddLog($" * {ecs.GetComponent<HasName>(entity).Name} has been killed!"); ecs.GetComponent <HasMoney>(geralt).AddMoney(5); geraltGloats(); } // For debugging also print a message if Geralt dies if (type == ActorType.Witcher) { Logger.AddLog($" * * {ecs.GetComponent<HasName>(entity).Name} has been killed! (wait, what?)"); } }; }
private void EnterGameLoop(IEcs ecs, Entity geralt) { var renderer = new Renderer.Renderer(ecs, this.boundsX, this.boundsY, new RendererCharacters(), Logger, () => ecs.GetComponent <HasMoney>(geralt).Money); // Simulation loop - 5 updates a second var timer = new Timer(200); timer.Elapsed += async(sender, e) => { timer.Stop(); await ecs.UpdateAsync(); if (ecs.GetSystem <IsActor>().AllComponents().Count(c => c.Type == ActorType.Monster) < 3) { this.CreateEntity(ecs, 5, ActorType.Monster); } renderer.Render(); timer.Start(); }; timer.Start(); // Press a key to quit Console.ReadKey(); }
public Renderer(IEcs ecs, int boundsX, int boundsY, RendererCharacters rendererCharacters, Logger logger, Func <int> moneyRetriever) { this.ecs = ecs; this.boundsX = boundsX; this.boundsY = boundsY; this.rendererCharacters = rendererCharacters; this.logger = logger; this.moneyRetriever = moneyRetriever; }
private void SetupMonstersAppearEvent(IEcs ecs) { var lastMonsterEnteredAtTick = 0; ecs.GetSystem <IsActor>().ComponentAdded += (entity, component) => { // Only want to announce once per tick and if the entity is a monster if (ecs.Tick != lastMonsterEnteredAtTick && component.Type == ActorType.Monster) { Logger.AddLog(" * New monsters have entered the fray!"); lastMonsterEnteredAtTick = ecs.Tick; } }; }
private void CreateEntity(IEcs ecs, int number, ActorType actor) { var rng = new Random(); var locationSystem = ecs.GetSystem <HasLocation>() as LocationSystem; for (var i = 0; i < number; i++) { var entity = ecs.NewEntity() .WithComponent(this.FindSafeLocation(locationSystem, rng)); if (actor == ActorType.Tree) { this.MakeEntityIntoTree(entity); } else { this.MakeEntityIntoDrowner(entity); } } }
private Entity CreateGeralt(IEcs ecs) { // Make a pathfinder for Geralt's AI that knows where the trees are. var trees = ecs.EntitiesWithComponent <IsActor>() .Where(e => ecs.GetComponent <IsActor>(e).Type == ActorType.Tree) .Select(e => ecs.GetComponent <HasLocation>(e)) .Select(l => new Coord(l.X, l.Y)); var pathFinder = new PathFinder(new Coord(this.boundsX, this.boundsY), trees); return (ecs.NewEntity() .WithComponent(new HasName("Geralt")) .WithComponent(this.FindSafeLocation(ecs.GetSystem <HasLocation>() as LocationSystem, new Random())) .WithComponent(new Renders('G', ConsoleColor.DarkMagenta)) .WithComponent(new IsActor(ActorType.Witcher)) .WithComponent(new HasAi(new WitcherAi(pathFinder))) .WithComponent(new Talks(Logger.AddLog).RandomlySay(Talks.GeraltSpeaks, 0.02)) .WithComponent <HasMoney>() .Entity); }
/// <summary> /// Is run from within the ECS container when the component is registered /// </summary> /// <param name="ecs"></param> internal void InitFromEcs(IEcs ecs) { this.Ecs = ecs; this.Entity = this.Ecs.GetEntityForComponent(this, this.GetType()); this.Initialise(); }
public IEntityConfigurator Create(IEcs ecs, Entity entity) { return(new EntityConfigurator(ecs, entity)); }
internal EntityConfigurator(IEcs ecs, Entity entity) { this.ecs = ecs; this.Entity = entity; }