Beispiel #1
0
        public void SystemEnable <T>() where T : SystemBase
        {
            var system = FindSystem <T>();

            if (system.Item0 == null)
            {
                return;
            }

            if (BitSet256.IsSet(&_globals->Systems, system.Item1) == false)
            {
                // set flag
                BitSet256.Set(&_globals->Systems, system.Item1);

                try {
                    system.Item0.OnEnabled(this);
                } catch (Exception exn) {
                    Log.Exception(exn);
                }
            }
        }
Beispiel #2
0
        public Frame(SystemBase[] systems, RuntimeConfig runtimeConfig, SimulationConfig simulationConfig, FP deltaTime, Int32 simulationRate)
        {
            _systems          = systems;
            _runtimeConfig    = runtimeConfig;
            _simulationConfig = simulationConfig;
            _simulationRate   = simulationRate;

            _frameEvents  = new FrameEvents(this);
            _frameSignals = new FrameSignals(this);
            _destroy      = new Queue <EntityRef>(1024);

            _entityManifoldFilter = _systems.FirstOrDefault(x => x is IEntityManifoldFilter) as IEntityManifoldFilter;

            // use dummy in case no system implements the filter
            if (_entityManifoldFilter == null)
            {
                _entityManifoldFilter = new DummyManifoldFilter();
            }

            AllocGen();
            InitGen();

            _ISignalOnEntityCreateSystems  = BuildSignalsArray <ISignalOnEntityCreated>();
            _ISignalOnEntityDestroySystems = BuildSignalsArray <ISignalOnEntityDestroy>();

            _ISignalOnCollisionStaticSystems  = BuildSignalsArray <ISignalOnCollisionStatic>();
            _ISignalOnCollisionDynamicSystems = BuildSignalsArray <ISignalOnCollisionDynamic>();

            _ISignalOnTriggerStaticSystems  = BuildSignalsArray <ISignalOnTriggerStatic>();
            _ISignalOnTriggerDynamicSystems = BuildSignalsArray <ISignalOnTriggerDynamic>();

            _ISignalOnNavMeshTargetReachedSystems = BuildSignalsArray <ISignalOnNavMeshTargetReached>();

            // assign map, rng session, etc.
            _globals->Map.Asset  = runtimeConfig.Map.Instance;
            _globals->RngSession = new RNGSession(runtimeConfig.Seed);
            _globals->DeltaTime  = deltaTime;

            // set default enabled systems
            for (Int32 i = 0; i < _systems.Length; ++i)
            {
                if (_systems[i].StartEnabled)
                {
                    BitSet256.Set(&_globals->Systems, i);
                }
            }

            var allEntities = GetAllEntitiesUnsafe();

            // init physics
            var physicsEntities = new List <DynamicScene.DynamicSceneEntity>();

            for (Int32 i = 0; i < allEntities.Length; ++i)
            {
                var e  = allEntities[i];
                var d  = Entity.GetDynamicBody(e.Entity);
                var t  = Entity.GetTransform2D(e.Entity);
                var tv = Entity.GetTransform2DVertical(e.Entity);
                if (tv == null)
                {
                    tv = &_globals->PhysicsSettings.DefaultVerticalTransform;
                }

                if (d != null)
                {
                    physicsEntities.Add(new DynamicScene.DynamicSceneEntity
                    {
                        Entity              = (void *)e.Entity,
                        DynamicBody         = d,
                        Transform2D         = t,
                        Transform2DVertical = tv,
                    });
                }
            }

            // init physics
            _globals->PhysicsSettings.Gravity                        = simulationConfig.Physics.Gravity;
            _globals->PhysicsSettings.SolverIterations               = simulationConfig.Physics.SolverIterations;
            _globals->PhysicsSettings.UseAngularVelocity             = simulationConfig.Physics.UseAngularVelocity;
            _globals->PhysicsSettings.Substeps                       = simulationConfig.Physics.Substeps;
            _globals->PhysicsSettings.RaiseCollisionEventsForStatics = simulationConfig.Physics.RaiseCollisionEventsForStatics;

            // create scene
            _scene = new DynamicScene(&_globals->PhysicsSettings, physicsEntities.ToArray());

            // init animator
            var animatorEntities = new List <AnimatorUpdater.AnimatorEntity>();

            for (Int32 i = 0; i < allEntities.Length; ++i)
            {
                var e = allEntities[i];
                var a = Entity.GetAnimator(e.Entity);
                if (a != null)
                {
                    animatorEntities.Add(new AnimatorUpdater.AnimatorEntity {
                        Entity      = (void *)e.Entity,
                        Animator    = a,
                        Transform   = Entity.GetTransform2D(e.Entity),
                        DynamicBody = Entity.GetDynamicBody(e.Entity)
                    });
                }
            }

            _animator = new AnimatorUpdater(animatorEntities.ToArray());

            // init animator
            var navMeshEntites = new List <NavMeshUpdater.NavMeshEntity>();

            for (Int32 i = 0; i < allEntities.Length; ++i)
            {
                var e = allEntities[i];
                var a = Entity.GetNavMeshAgent(e.Entity);
                if (a != null)
                {
                    navMeshEntites.Add(new NavMeshUpdater.NavMeshEntity
                    {
                        Entity      = (void *)e.Entity,
                        Agent       = a,
                        Transform2D = Entity.GetTransform2D(e.Entity),
                        DynamicBody = Entity.GetDynamicBody(e.Entity),
                        Index       = i
                    });
                }
            }

            NavMeshUpdater.NavMeshUpdaterConfig navMeshConfig = new NavMeshUpdater.NavMeshUpdaterConfig()
            {
                ProximityFactor = simulationConfig.NavMeshAgent.ProximityFactor,
                UpdateInterval  = simulationConfig.NavMeshAgent.UpdateInterval
            };

            _navMeshUpdater = new NavMeshUpdater(navMeshEntites.ToArray(), Scene, Map, navMeshConfig);

            AllocUser();
            InitUser();
        }