Beispiel #1
0
        public static WorldBuilder AddDestroyFeature <TScope>(this WorldBuilder self)
        {
            // @formatter:off
            return(self.AddFeature(Name)
                   .DependsOn()
                   .Feature(CoreFeature.Name)
                   .End()
                   .Components <TScope>()
                   .Add <Destroyed>()
                   .End()
                   .GlobalComponentNotifications <TScope>()
                   .AddAddedNotifications <Destroyed>()
                   .End()
                   .Systems()
                   .Cleanup((contexts, bus) => {
                var context = contexts.Get <TScope>();

                foreach (var message in bus.All <ComponentAdded <TScope, Destroyed> >())
                {
                    context.DestroyEntity(message.Entity);
                }
            })
                   .End()
                   .End());
            // @formatter:on
        }
        public static WorldBuilder AddCoreFeature(this WorldBuilder self, Func <Configurer, Configurer> configure)
        {
            var configurer = configure(new Configurer());

            // @formatter:off
            return(self.AddFeature(Name)
                   .Contexts()
                   .Add <Game>(c => c.SetInitialCapacity(configurer.InitialCapacity))
                   .Add <Configuration>(c => c.AllowOnlyOneEntity())
                   .Add <Singletons>(c => c.AllowOnlyOneEntity())
                   .End()
                   .Components <Singletons>()
                   .Add <SingletonTag>()
                   .End()
                   .Components <Configuration>()
                   .Add <SingletonTag>()
                   .End()
                   .Systems()
                   .Setup((contexts, bus) => {
                contexts.Get <Singletons>().CreateEntity().Add(new SingletonTag());
                contexts.Get <Configuration>().CreateEntity().Add(new SingletonTag());
            })
                   .End()
                   .End()
                   .AddDestroyFeature <Game>());
            // @formatter:on
        }
Beispiel #3
0
        public static WorldBuilder AddPhysics2DFeature(this WorldBuilder self, Func <ISetTimeStep, IConfigurationEnd> configure)
        {
            var configurer = new Configurer();

            configure(configurer);

            // @formatter:off
            return(self.AddFeature(Name)
                   .DependsOn()
                   .Feature(CoreFeature.Name)
                   .Feature(DestroyFeature.Name)
                   .Feature(TickFeature.Name)
                   .End()
                   .Components <Game>()
                   .Add <Position>()
                   .Add <Velocity>()
                   .Add <Damping>()
                   .Add <Force>()
                   .Add <MaxVelocity>()
                   .Add <Mass>()
                   .Add <Orientation>()
                   .Add <AngularVelocity>()
                   .Add <MaxAngularVelocity>()
                   .Add <AngularDamping>()
                   .Add <Torque>()
                   .Add <Inertia>()
                   .Add <BoundingShapes>()
                   .Add <Collisions>()
                   .End()
                   .Components <Configuration>()
                   .Add <TimeStep>()
                   .Add <GlobalForce>()
                   .End()
                   .Components <Singletons>()
                   .Add <Space>()
                   .End()
                   .Systems()
                   .Setup((contexts, bus) => SetupSystem.Setup(contexts, configurer.TimeStep, configurer.SpatialDatabase))
                   .Update((contexts, bus) => AccelerateSystem.Update(contexts))
                   .Update((contexts, bus) => AngularAccelerateSystem.Update(contexts))
                   .Update((contexts, bus) => MoveSystem.Update(contexts))
                   .Update((contexts, bus) => RotateSystem.Update(contexts))
                   .Update((contexts, bus) => SpaceReindexSystem.Update(contexts))
                   .Update((contexts, bus) => FindCollisionsSystem.Update(contexts))
                   .Cleanup((contexts, bus) => SpatialDatabaseCleanupSystem.Cleanup(contexts))
                   .Cleanup((contexts, bus) => ReturnCollisionsListOnDestroySystem.Cleanup(contexts))
                   .Cleanup((contexts, bus) => ReturnShapesOnDestroySystem.Cleanup(contexts))
                   .End()
                   .GlobalComponentNotifications <Game>()
                   .AddAllNotifications <Position>()
                   .AddAllNotifications <Orientation>()
                   .End()
                   .End());
            // @formatter:on
        }
Beispiel #4
0
 public static WorldBuilder AddViewFeature(this WorldBuilder self)
 {
     // @formatter:off
     return(self.AddFeature(Name)
            .DependsOn()
            .Feature(CoreFeature.Name)
            .End()
            .Components <Game>()
            .Add <View>()
            .End()
            .GlobalComponentNotifications <Game>()
            .AddAllNotifications <View>()
            .End()
            .End());
     // @formatter:on
 }
Beispiel #5
0
        public static WorldBuilder AddBehaviourTreeFeature <TScope>(this WorldBuilder self, Func <Configurer, Configurer> configure)
        {
            var configurer = configure(new Configurer());

            // @formatter:off
            return(self.AddFeature(Name)
                   .Components <Configuration>()
                   .Add <BehaviourTreeUpdatePeriod <TScope> >()
                   .End()
                   .Components <Game>()
                   .Add <BehaviourTreeData <TScope> >()
                   .End()
                   .Systems()
                   .Setup((contexts, bus) => contexts.Configuration().Add(new BehaviourTreeUpdatePeriod <TScope>(configurer.UpdatePeriodInTicks)))
                   .Update((contexts, bus) => {
                var updatePeriod = contexts.Configuration().Get <BehaviourTreeUpdatePeriod <TScope> >().PeriodInTicks;
                var tick = contexts.Singleton().Get <TickCounter>().Value;
                var context = contexts.Get <TScope>();

                if (tick % updatePeriod != 0)
                {
                    return;
                }

                foreach (var entity in context.AllWith <BehaviourTreeData <TScope> >())
                {
                    if (!entity.Is <Destroyed>())
                    {
                        entity.Get <BehaviourTreeData <TScope> >().BehaviourTree.Execute(entity);
                    }
                }
            })
                   .Cleanup((contexts, bus) => {
                var context = contexts.Get <TScope>();

                foreach (var entity in context.AllWith <Destroyed, BehaviourTreeData <TScope> >())
                {
                    var data = entity.Get <BehaviourTreeData <TScope> >().ExecutionData;

                    data.Return();
                    entity.Remove <BehaviourTreeData <TScope> >();
                }
            })
                   .End()
                   .End());
            // @formatter:on
        }
Beispiel #6
0
        public static WorldBuilder AddTickFeature(this WorldBuilder self)
        {
            // @formatter:off
            return(self.AddFeature(Name)
                   .DependsOn()
                   .Feature(CoreFeature.Name)
                   .End()
                   .Components <Singletons>()
                   .Add <TickCounter>()
                   .End()
                   .Systems()
                   .Setup((contexts, bus) => contexts.Singleton().Add(new TickCounter(-1)))
                   .Update((contexts, bus) => {
                var singleton = contexts.Singleton();
                var value = singleton.Get <TickCounter>().Value;

                singleton.Replace(new TickCounter(value + 1));
            })
                   .End()
                   .End());
            // @formatter:on
        }
        public static WorldBuilder AddSteering2DFeature(this WorldBuilder self, Func <Configurer, Configurer> configure)
        {
            var configurer = configure(new Configurer());

            // @formatter:off
            return(self.AddFeature(Name)
                   .DependsOn()
                   .Feature(CoreFeature.Name)
                   .Feature(DestroyFeature.Name)
                   .Feature(TickFeature.Name)
                   .Feature(Physics2DFeature.Name)
                   .End()
                   .Components <Configuration>()
                   .Add <SteeringUpdatePeriod>()
                   .End()
                   .Components <Game>()
                   .Add <Steering>()
                   .Add <FlowField>()
                   .Add <TargetEntity>()
                   .Add <TargetPosition>()
                   .Add <TargetOrientation>()
                   .Add <ArrivalTolerance>()
                   .Add <AlignTolerance>()
                   .End()
                   .Systems()
                   .Setup((contexts, bus) => contexts.Configuration().Add(new SteeringUpdatePeriod(configurer.UpdatePeriodInTicks)))
                   .Update((contexts, bus) => {
                var updatePeriod = contexts.Configuration().Get <SteeringUpdatePeriod>().PeriodInTicks;
                var tick = contexts.Singleton().Get <TickCounter>().Value;
                var context = contexts.Get <Game>();

                if (tick % updatePeriod != 0)
                {
                    return;
                }

                foreach (var entity in context.AllWith <Steering>())
                {
                    if (entity.Is <Destroyed>())
                    {
                        continue;
                    }

                    var steeringVelocity = SteeringVelocity.Zero;
                    var steeringBehaviour = entity.Get <Steering>().SteeringBehaviour;
                    var velocity = steeringBehaviour.Calculate(entity, ref steeringVelocity);

                    if (entity.Has <Velocity>())
                    {
                        entity.Replace(new Velocity(velocity.Linear));
                    }
                    if (entity.Has <AngularVelocity>())
                    {
                        entity.Replace(new AngularVelocity(velocity.Angular));
                    }
                }
            })
                   .End()
                   .End());
            // @formatter:on
        }