public void ArchetypeRemoveHolesTest() { EcsWorld world = new EcsWorld(); IEcsArchetype archetype = world.GetArchetype <ComponentA, ComponentB>(); IEcsEntity entity0 = world.CreateEntity(new ComponentA(), new ComponentB()); IEcsEntity entity1 = world.CreateEntity(new ComponentA(), new ComponentB()); IEcsEntity entity2 = world.CreateEntity(new ComponentA(), new ComponentB()); IEcsEntity entity3 = world.CreateEntity(new ComponentA(), new ComponentB()); entity1.RemoveComponent <ComponentA>(); entity2.RemoveComponent <ComponentA>(); Assert.AreEqual(2, archetype.EntitiesCount); Assert.AreEqual(entity0, archetype[0]); Assert.AreEqual(entity3, archetype[1]); entity0.RemoveComponent <ComponentA>(); Assert.AreEqual(1, archetype.EntitiesCount); Assert.AreEqual(entity3, archetype[0]); entity1.AddComponent(new ComponentA()); entity2.AddComponent(new ComponentA()); Assert.AreEqual(3, archetype.EntitiesCount); Assert.AreEqual(entity3, archetype[0]); Assert.AreEqual(entity1, archetype[1]); Assert.AreEqual(entity2, archetype[2]); }
public void GroupIncVersionFilterTest() { EcsWorld world = new EcsWorld(); IEcsEntity entity = world.CreateEntity(new ComponentA(), new ComponentB()); Assert.AreEqual(1, world.Filter(new EcsFilter().AllOf <ComponentB>()).CalculateCount()); entity.AddComponent(new ComponentC()); world.CreateEntity(new ComponentC(), new ComponentD()); Assert.AreEqual(1, world.Filter(new EcsFilter().AllOf <ComponentB>()).CalculateCount()); }
// TODO: configurable texture, mass, speed, kite extend/retract speed, private int CreatePlayer(Texture2D playerTexture) { var playerEntity = world.CreateEntity(512f, 500f); var sprite = world.AddComponent <Sprite>(playerEntity); sprite.Texture = playerTexture; sprite.Origin = playerTexture.Bounds.Center.ToVector2(); var physics = world.AddComponent <Physics>(playerEntity); physics.Mass = 5f; physics.Drag = Vector2.UnitX * 7; physics.Multiplier = Vector2.UnitX; var player = world.AddComponent <Player>(playerEntity); player.MoveForce = 240f * physics.Mass; player.MoveLeft = Keys.Left; player.MoveRight = Keys.Right; player.MaxCastDistance = 256f; player.CastSpeed = 0.1f; player.CastOut = Keys.Up; player.MinPullDistance = 128f; player.PullSpeed = 0.1f; player.PullIn = Keys.Down; return(playerEntity); }
void SpawnSnake() { Debug.Log("Spawn snake"); GameObject headPrefab = Resources.Load <GameObject> ("SnakeHead"); GameObject head = GameObject.Instantiate(headPrefab, Vector3.zero, Quaternion.identity); _snakeEntity = _world.CreateEntity(); SnakeComponent sc = _world.AddComponent <SnakeComponent> (_snakeEntity); sc.Length = 0; sc.Tail = new List <Transform> (); _world.AddComponent <ControlComponent> (_snakeEntity); _world.AddComponent <GameObjectComponent> (_snakeEntity).Init(head); _world.AddComponent <ColliderComponent> (_snakeEntity).Collider = head.GetComponent <Collider2D> (); }
private IEcsEntity CreateEntity(EcsWorld world, Vector2 position, float rotation, ColliderComponent col, float mass, float rayLength) { IEcsEntity entity = world.CreateEntity( col, new RigBodyComponent { Velocity = new float2(Random.Range(-10, 10), Random.Range(-10, 10)), Mass = mass }, new TransformComponent { Position = position, Rotation = rotation } ); if (mass <= 0) { entity.AddComponent(new RigBodyStaticComponent()); } if (rayLength > 0) { entity.AddComponent(new RayComponent { Length = rayLength, Layer = _collisionMatrix.GetLayer("Default") }); } return(entity); }
void IEcsRunSystem.Run() { if (Input.GetMouseButtonDown(0)) { int scoreCount = 1; var hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, 100); if (hit.collider != null) { Transform htr = hit.collider.transform; foreach (var viewEntity in _viewFilter.Entities) { var view = _world.GetComponent <ResViewComponent> (viewEntity, _viewId); var move = _world.GetComponent <ResMoveComponent> (viewEntity, _moveId); if (view.transform == htr && move == null) { var res = _world.GetComponent <ResComponent>(viewEntity, _resId); if (res.type == ResType.blocker) { scoreCount = 10; } var score = _world.AddComponent <ScoreComponent>(_world.CreateEntity()); score.score = scoreCount; res.type = ResType.none; GameObject.Destroy(view.transform.gameObject); //_world.RemoveComponent<ResViewComponent>(viewEntity, _viewId); break; } } } } }
public void RemoveComponentThrowExceptionTest() { EcsWorld world = new EcsWorld(); IEcsEntity entity = world.CreateEntity(); entity.RemoveComponent <ComponentA>(); }
protected override void RunReactive() { for (int i = 0; i < ReactedEntitiesCount; i++) { var entity = ReactedEntities[i]; var spawnAsteroids = _world.GetComponent <SpawnAsteroids> (entity); if (spawnAsteroids != null && spawnAsteroids.value != null) { foreach (var asteroidDesc in spawnAsteroids.value) { var asteroidEntity = _world.CreateEntity(); Asteroid asteroid = _world.AddComponent <Asteroid> (asteroidEntity); asteroid.Id = asteroidDesc.Id; _world.AddComponent <Position> (asteroidEntity).value = asteroidDesc.Position; _world.AddComponent <Rotation> (asteroidEntity).value = asteroidDesc.Rotation; _world.AddComponent <Speed> (asteroidEntity).value = asteroidDesc.Speed; var index = asteroidDesc.Index; asteroid.Index = index; var instance = UnityEngine.Object.Instantiate(_gameConfig.AsteroidsPrefabs[index], asteroidDesc.Position, asteroidDesc.Rotation); _world.AddComponent <TransformRef> (asteroidEntity).value = instance.transform; instance.Entity = asteroidEntity; _gameState.Asteroids[asteroidDesc.Id] = asteroidEntity; } } } }
void IEcsInitSystem.Initialize() { for (int i = 0; i < 50000; i++) { var res = _world.AddComponent <MainComponent>(_world.CreateEntity()); res.id = i + 1; } }
public void AddComponentThrowExceptionTest() { EcsWorld world = new EcsWorld(); IEcsEntity entity = world.CreateEntity(); entity.AddComponent(new ComponentA()); entity.AddComponent(new ComponentA()); }
public int CreateShip(EcsWorld world) { var ship = world.CreateEntity(); world.AddComponent <Ship> (ship); world.AddComponent <TransformRef> (ship).value = Ship.transform; world.AddComponent <Speed> (ship).value = Speed; world.AddComponent <RotationSpeed> (ship).value = RotationSpeed; world.AddComponent <RigidbodyRef> (ship).value = Ship.Rigidbody; Ship.Inject(world); return(ship); }
public void CreateEntityFromProcessingTest() { EcsWorld world = new EcsWorld(); Assert.AreEqual(0, world.EntitiesInProcessing); IEcsEntity entity = world.CreateEntity(new ComponentA(), new ComponentB(), new ComponentC(), new ComponentD()); uint entityId = entity.Id; entity.Destroy(); Assert.AreEqual(1, world.EntitiesInProcessing); IEcsEntity newEntity = world.CreateEntity(new ComponentB()); uint newEntityId = newEntity.Id; Assert.AreEqual(0, world.EntitiesInProcessing); Assert.IsFalse(newEntity.HasComponent <ComponentA>()); Assert.IsTrue(newEntity.HasComponent <ComponentB>()); Assert.IsTrue(newEntityId > entityId); }
void CreateTable() { for (int x = 0; x < countx; x++) { for (int y = 0; y < county; y++) { var res = _world.AddComponent <ResComponent> (_world.CreateEntity()); res.x = x; res.y = y; res.type = (ResType)Random.Range((int)ResType.blocker, (int)ResType.piece5); } } }
public static void InitFilterWorld(TestContext testContext) { EcsComponentType <ComponentA> .Register(); EcsComponentType <ComponentB> .Register(); EcsComponentType <ComponentC> .Register(); EcsComponentType <ComponentD> .Register(); _world = new EcsWorld(); _entityABD = _world.CreateEntity(new ComponentA { Value = 1 }, new ComponentB { Value = 2 }, new ComponentD { Value = 3 }); _entityAC = _world.CreateEntity(new ComponentA { Value = 4 }, new ComponentC { Value = 5 }); _entityBD0 = _world.CreateEntity(new ComponentB { Value = 6 }, new ComponentD { Value = 7 }); _entityBD1 = _world.CreateEntity(new ComponentD { Value = 8 }, new ComponentB { Value = 8 }); _entityBC = _world.CreateEntity(new ComponentC { Value = 9 }, new ComponentB { Value = 10 }); _entityAB = _world.CreateEntity(new ComponentB { Value = 11 }, new ComponentA { Value = 12 }); _entityAD = _world.CreateEntity(new ComponentA { Value = 13 }, new ComponentD { Value = 14 }); }
public void GetSetRemoveComponentTest() { EcsWorld world = new EcsWorld(); ComponentB componentB = new ComponentB(); IEcsEntity entity = world.CreateEntity(); entity.AddComponent(new ComponentA()); entity.AddComponent(componentB); entity.AddComponent(new ComponentC()); Assert.IsNotNull(entity.GetComponent <ComponentA>()); Assert.IsNotNull(entity.GetComponent <ComponentC>()); Assert.AreEqual(componentB, entity.GetComponent <ComponentB>()); Assert.IsFalse(entity.HasComponent <ComponentD>()); entity.RemoveComponent <ComponentB>(); Assert.IsFalse(entity.HasComponent <ComponentB>()); }
public void Setup() { _source = new EcsWorld(); for (int i = 0; i < byte.MaxValue; ++i) { _source.CreateEntity(InstantiateComponentA(), InstantiateComponentB(), new MiniEcsComponentD()); _source.CreateEntity(InstantiateComponentA(), new MiniEcsComponentC()); _source.CreateEntity(InstantiateComponentB(), new MiniEcsComponentD()); _source.CreateEntity(InstantiateComponentB(), new MiniEcsComponentD()); _source.CreateEntity(InstantiateComponentB(), new MiniEcsComponentC()); _source.CreateEntity(InstantiateComponentA(), InstantiateComponentB()); _source.CreateEntity(InstantiateComponentA(), new MiniEcsComponentD()); } _baseline = new Baseline <uint>(); _data = _source.Serialize(_filter, _baseline); }
private void CreatePlayer(int actorNumber) { if (actorNumber < 0) { return; } if (!PlayerCache.Entities.ContainsKey(actorNumber)) { var player = _world.CreateEntity(); _world.AddComponent <GamePlayer> (player).number = actorNumber; PlayerCache.Entities[actorNumber] = player; if (PhotonServer.LocalPlayer.ID == actorNumber) { _world.AddComponent <Local> (player); } } }
public static void RunLeoEcsBenchmark() { EcsWorld world = new EcsWorld().AddSystem(new MoveSys()); for (var i = 0; i < EntitiesCount; i++) { int e = world.CreateEntity(); Pos pos = world.AddComponent <Pos>(e); pos.Location = new Vector3(1f, 2f, 0f); pos.Rotation = 0; Vel vel = world.AddComponent <Vel>(e); vel.Linear = new Vector3(2f, 2f, 1f); vel.Angular = 0.01f; } //warmup world.RunUpdate(); Measure("Leopotam", () => { for (var i = 0; i < RepeatCount; i++) { world.RunUpdate(); } }); }
public void SerializeUpdateBaselineTest() { Baseline <uint> baseline = new Baseline <uint>(); byte[] data = _world.Serialize(new EcsFilter(), baseline); EcsWorld source = new EcsWorld(); source.Update(data); IEcsEntity entityAB = source[_entityAB.Id]; Assert.AreEqual(_entityAB.GetComponent <ComponentA>(), entityAB.GetComponent <ComponentA>()); Assert.AreEqual(_entityAB.GetComponent <ComponentB>(), entityAB.GetComponent <ComponentB>()); IEcsEntity entityABD = source[_entityABD.Id]; Assert.AreEqual(_entityABD.GetComponent <ComponentA>(), entityABD.GetComponent <ComponentA>()); Assert.AreEqual(_entityABD.GetComponent <ComponentB>(), entityABD.GetComponent <ComponentB>()); Assert.AreEqual(_entityABD.GetComponent <ComponentD>(), entityABD.GetComponent <ComponentD>()); IEcsEntity entityAC = source[_entityAC.Id]; Assert.AreEqual(_entityAC.GetComponent <ComponentA>(), entityAC.GetComponent <ComponentA>()); Assert.AreEqual(_entityAC.GetComponent <ComponentC>(), entityAC.GetComponent <ComponentC>()); IEcsEntity entityAD = source[_entityAD.Id]; Assert.AreEqual(_entityAD.GetComponent <ComponentA>(), entityAD.GetComponent <ComponentA>()); Assert.AreEqual(_entityAD.GetComponent <ComponentD>(), entityAD.GetComponent <ComponentD>()); IEcsEntity entityBC = source[_entityBC.Id]; Assert.AreEqual(_entityBC.GetComponent <ComponentB>(), entityBC.GetComponent <ComponentB>()); Assert.AreEqual(_entityBC.GetComponent <ComponentC>(), entityBC.GetComponent <ComponentC>()); IEcsEntity entityBD0 = source[_entityBD0.Id]; Assert.AreEqual(_entityBD0.GetComponent <ComponentB>(), entityBD0.GetComponent <ComponentB>()); Assert.AreEqual(_entityBD0.GetComponent <ComponentD>(), entityBD0.GetComponent <ComponentD>()); IEcsEntity entityBD1 = source[_entityBD1.Id]; Assert.AreEqual(_entityBD1.GetComponent <ComponentB>(), entityBD1.GetComponent <ComponentB>()); Assert.AreEqual(_entityBD1.GetComponent <ComponentD>(), entityBD1.GetComponent <ComponentD>()); data = source.Serialize(new EcsFilter(), baseline); Assert.AreEqual(0, data.Length); EcsWorld target = new EcsWorld(); // change Component entityAB.GetComponent <ComponentA>().Value = int.MaxValue; data = source.Serialize(new EcsFilter(), baseline); target.Update(data); Assert.AreEqual(1, target.EntitiesCount); Assert.AreEqual(entityAB.GetComponent <ComponentA>(), target[entityAB.Id].GetComponent <ComponentA>()); //removeComponent entityAB.RemoveComponent <ComponentA>(); data = source.Serialize(new EcsFilter(), baseline); target.Update(data); Assert.AreEqual(1, target.EntitiesCount); Assert.IsFalse(target[entityAB.Id].HasComponent <ComponentA>()); //AddComponent entityAB.AddComponent(new ComponentA()); data = source.Serialize(new EcsFilter(), baseline); target.Update(data); Assert.AreEqual(1, target.EntitiesCount); Assert.IsTrue(target[entityAB.Id].HasComponent <ComponentA>()); //remove Entity entityAB.Destroy(); data = source.Serialize(new EcsFilter(), baseline); target.Update(data); Assert.AreEqual(0, target.EntitiesCount); //create Entity entityAB = source.CreateEntity(new ComponentA { Value = 100 }, new ComponentB { Value = 200 }); data = source.Serialize(new EcsFilter(), baseline); target.Update(data); Assert.AreEqual(1, target.EntitiesCount); IEcsEntity targetEntityAB = target[entityAB.Id]; Assert.AreEqual(2, targetEntityAB.ComponentsCount); Assert.AreEqual(entityAB.GetComponent <ComponentA>(), targetEntityAB.GetComponent <ComponentA>()); Assert.AreEqual(entityAB.GetComponent <ComponentB>(), targetEntityAB.GetComponent <ComponentB>()); }