public void Update(EntityRegistry registry, EventSystem eventSystem) { int count = mTounguesWithPosition.Entity.Count; for (int i = 0; i < count; ++i) { Entity parentFrog = mTounguesWithPosition.ParentFrog[i].Parent; //TODO: The following could be done with a frog slice if there was a clean way to fetch random entities from the slice. Entity output could do that. Debug.Assert(registry.IsValid(parentFrog)); Debug.Assert(registry.HasComponent <Facing>(parentFrog)); Debug.Assert(registry.HasComponent <Rect>(parentFrog)); Rect frogRect = registry.GetComponent <Rect>(parentFrog); int facing = registry.GetComponent <Facing>(parentFrog).CurrentFacing; Vector2 extendDirection; Vector2 anchor; if (facing >= 0f) { extendDirection = Vector2.UnitX; anchor = frogRect.MaxCorner; } else { extendDirection = -Vector2.UnitX; anchor = frogRect.MinMaxCorner; } RectShape toungueShape = mTounguesWithPosition.CurrentShape[i]; anchor -= (toungueShape.Height * mShapeData.RelativeVerticalOffset * Vector2.UnitY); anchor += toungueShape.Width * extendDirection * 0.5f; Entity toungueEntity = mTounguesWithPosition.Entity[i]; registry.SetComponent <Position>(toungueEntity, new Position(anchor)); } }
public void TestComponents() { EntityRegistry registry = new EntityRegistry(100); Entity entity1 = registry.Create(); Entity entity2 = registry.Create(); PositionComponent pos1 = new PositionComponent(1, 0); PositionComponent pos2 = new PositionComponent(0, 1); registry.AddComponent(entity1, new TagComponent()); registry.AddComponent(entity1, pos1); registry.AddComponent(entity2, pos2); registry.AddComponent(entity2, new ParentedComponent(entity1)); Assert.IsTrue(registry.HasComponent <TagComponent>(entity1)); Assert.IsFalse(registry.HasComponent <TagComponent>(entity2)); Assert.IsTrue(registry.HasComponent <PositionComponent>(entity1)); Assert.IsTrue(registry.HasComponent <PositionComponent>(entity2)); Assert.IsFalse(registry.HasComponent <ParentedComponent>(entity1)); Assert.IsTrue(registry.HasComponent <ParentedComponent>(entity2)); Assert.AreEqual(registry.GetComponent <PositionComponent>(entity1), pos1); Assert.AreEqual(registry.GetComponent <PositionComponent>(entity2), pos2); registry.Destroy(entity1); Assert.AreEqual(registry.GetComponent <PositionComponent>(entity2) , pos2); Assert.IsFalse(registry.IsValid(registry.GetComponent <ParentedComponent>(entity2).Parent)); Assert.IsFalse(registry.HasComponent <TagComponent>(entity2)); Assert.IsTrue(registry.HasComponent <PositionComponent>(entity2)); Assert.IsTrue(registry.HasComponent <ParentedComponent>(entity2)); Entity entity3 = registry.Create(); Entity entity4 = registry.Create(); registry.AddComponent(entity4, pos1); registry.AddComponent(entity3, pos1); registry.AddComponent(entity3, new ParentedComponent()); registry.AddComponent(entity4, new ParentedComponent()); Assert.IsFalse(registry.HasComponent <TagComponent>(entity3)); Assert.IsTrue(registry.HasComponent <PositionComponent>(entity3)); Assert.IsTrue(registry.HasComponent <PositionComponent>(entity4)); Assert.IsTrue(registry.HasComponent <ParentedComponent>(entity3)); Assert.IsTrue(registry.HasComponent <ParentedComponent>(entity4)); Assert.IsFalse(registry.HasComponent <TagComponent>(entity2)); Assert.IsTrue(registry.HasComponent <PositionComponent>(entity2)); Assert.IsTrue(registry.HasComponent <ParentedComponent>(entity2)); }
private void UpdateToungueState(EntityRegistry registry) { int count = mAllToungues.Entity.Count; for (int i = 0; i < count; ++i) { ToungueState state = mAllToungues.State[i]; float newTime = state.TimeLeft - mTime.DeltaTime; if (newTime > 0) { state.TimeLeft = newTime; registry.SetComponent(mAllToungues.Entity[i], state); } else { int stateIndex = (int)state.Stage; if (stateIndex > 0) { ToungueStages nextStage = (ToungueStages)(stateIndex - 1); float nextTime = mData.GetStateTime(nextStage); registry.SetComponent(mAllToungues.Entity[i], new ToungueState(nextStage, nextTime)); } else { mTransitioning.Add(mAllToungues.Entity[i]); } } } foreach (Entity toungue in mTransitioning) { Entity frog = registry.GetComponent <ParentEntity>(toungue).Parent; registry.RemoveComponent <ControlledEntity>(frog); registry.Destroy(toungue); } mTransitioning.Clear(); }
public void TestSliceInjection() { Random randomizer = new Random(); const int kMaxEntities = 2000; EntityRegistry registry = new EntityRegistry(kMaxEntities); ParentedPosition parentedPositionSlice = new ParentedPosition(); registry.SetupSlice(ref parentedPositionSlice); List <Entity> activeEntities = new List <Entity>(kMaxEntities); //Create and destroy various waves of entities to ensure things are shuffled enough int shufles = 10; while (--shufles >= 0) { int randomCreate = randomizer.Next(kMaxEntities - activeEntities.Count); Entity previousEntity = Entity.NullEntity; for (int i = 0; i < randomCreate; ++i) { Entity entity = registry.Create(); if (i % 2 == 0) { registry.AddComponent(entity, new PositionComponent(i, i)); } if (i % 3 == 0) { registry.AddComponent(entity, new ParentedComponent(previousEntity)); } activeEntities.Add(entity); previousEntity = entity; } int randomDestroy = randomizer.Next(activeEntities.Count); for (int i = 0; i < randomDestroy; ++i) { int randomIndex = randomizer.Next(activeEntities.Count); registry.Destroy(activeEntities[randomIndex]); activeEntities.RemoveAt(randomIndex); } for (int i = 0; i < parentedPositionSlice.Entities.Count; ++i) { Entity entity = parentedPositionSlice.Entities[i]; Assert.IsTrue(registry.IsValid(entity)); Assert.IsTrue(registry.HasComponent <PositionComponent>(entity)); Assert.AreEqual(registry.GetComponent <PositionComponent>(entity), parentedPositionSlice.Positions[i]); Assert.IsTrue(registry.HasComponent <ParentedComponent>(entity)); Assert.AreEqual(registry.GetComponent <ParentedComponent>(entity), parentedPositionSlice.Parents[i]); } for (int i = 0; i < parentedPositionSlice.Entities.Count; ++i) { Entity entity = parentedPositionSlice.Entities[i]; PositionComponent position = parentedPositionSlice.Positions[i]; PositionComponent newPosition = new PositionComponent(position.X + 1, position.Y + 1); registry.SetComponent(entity, newPosition); Assert.IsTrue(registry.HasComponent <PositionComponent>(entity)); Assert.AreEqual(registry.GetComponent <PositionComponent>(entity), newPosition); Assert.AreEqual(newPosition, parentedPositionSlice.Positions[i]); } } }