Ejemplo n.º 1
0
        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();
        }
Ejemplo n.º 2
0
        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;
                }
            };
        }
Ejemplo n.º 3
0
        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);
                }
            }
        }
Ejemplo n.º 4
0
        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);
        }