public void TestCoroutineExecutionStack([Values] CodeGenMode mode) { SetupTestGraphMultipleFrames(mode, graph => { var query = SetupQuery(graph, "query", new[] { typeof(Translation) }); var onUpdate = SetupOnUpdate(graph, query); var loopNode = onUpdate.CreateStackedNode <CoroutineNodeModel>("UnitTest", setup: n => { n.CoroutineType = typeof(UnitTestCoroutine).GenerateTypeHandle(Stencil); }); var loopStack = graph.CreateLoopStack <CoroutineStackModel>(Vector2.down); graph.CreateEdge(loopStack.InputPort, loopNode.OutputPort); var setProperty = loopStack.CreateSetPropertyGroupNode(0); var member = new TypeMember(TypeHandle.Float, new List <string> { nameof(Translation.Value), nameof(Translation.Value.x) }); setProperty.AddMember(member); ((FloatConstantModel)setProperty.InputConstantsById[member.GetId()]).value = 10f; var translation = graph.CreateVariableNode(onUpdate.FunctionParameterModels.Single(p => p.DataType == typeof(Translation).GenerateTypeHandle(Stencil)), Vector2.zero); graph.CreateEdge(setProperty.InstancePort, translation.OutputPort); }, EachEntity((manager, i, e) => manager.AddComponentData(e, new Translation())), EachEntity((manager, i, e) => {}), // Init State EachEntity((manager, i, e) => {}), // MoveNext -> Execute loop stack EachEntity((manager, i, e) => Assert.That(manager.GetComponentData <Translation>(e).Value.x, Is.EqualTo(10f))) ); }
public void OneGroupIterationSystem([Values] CodeGenMode mode) { SetupTestGraph(mode, g => { ComponentQueryDeclarationModel query = GraphModel.CreateComponentQuery("g1"); TypeHandle positionType = typeof(Translation).GenerateTypeHandle(Stencil); query.AddComponent(Stencil, positionType, ComponentDefinitionFlags.None); IVariableModel queryInstance = GraphModel.CreateVariableNode(query, Vector2.zero); OnUpdateEntitiesNodeModel onUpdateModel = GraphModel.CreateNode <OnUpdateEntitiesNodeModel>("update", Vector2.zero); GraphModel.CreateEdge(onUpdateModel.InstancePort, queryInstance.OutputPort); SetPropertyGroupNodeModel set = onUpdateModel.CreateStackedNode <SetPropertyGroupNodeModel>("set"); var member = new TypeMember(TypeHandle.Float, new List <string> { nameof(Translation.Value), nameof(Translation.Value.x) }); set.AddMember(member); IVariableModel posComponent = GraphModel.CreateVariableNode(onUpdateModel.FunctionParameterModels.Single(p => p.DataType == positionType), Vector2.zero); GraphModel.CreateEdge(set.InstancePort, posComponent.OutputPort); ((FloatConstantModel)set.InputConstantsById[member.GetId()]).value = 2f; }, (manager, entityIndex, e) => manager.AddComponentData(e, new Translation { Value = { x = entityIndex } }), (manager, entityIndex, e) => Assert.That(manager.GetComponentData <Translation>(e).Value.x, Is.EqualTo(2f))); }
public void TestSendEventInCoroutine([Values] CodeGenMode mode) { SetupTestGraphMultipleFrames(mode, graph => { var query = SetupQuery(graph, "query", new[] { typeof(Translation) }); var onUpdate = SetupOnUpdate(graph, query); onUpdate.CreateStackedNode <CoroutineNodeModel>("Wait", 0, setup: n => { n.CoroutineType = typeof(Wait).GenerateTypeHandle(Stencil); }); var eventTypeHandle = typeof(UnitTestEvent).GenerateTypeHandle(Stencil); var sendEvent = onUpdate.CreateStackedNode <SendEventNodeModel>("Send", 1, setup: n => { n.EventType = eventTypeHandle; }); var entityType = typeof(Entity).GenerateTypeHandle(Stencil); var entityVar = graph.CreateVariableNode(onUpdate.FunctionParameterModels.Single(p => p.DataType == entityType), Vector2.zero); graph.CreateEdge(sendEvent.EntityPort, entityVar.OutputPort); var onEvent = graph.CreateNode <OnEventNodeModel>("On Event", preDefineSetup: n => { n.EventTypeHandle = eventTypeHandle; }); graph.CreateEdge(onEvent.InstancePort, query.OutputPort); var setProperty = onEvent.CreateSetPropertyGroupNode(0); var member = new TypeMember(TypeHandle.Float, new List <string> { nameof(Translation.Value), nameof(Translation.Value.x) }); setProperty.AddMember(member); ((FloatConstantModel)setProperty.InputConstantsById[member.GetId()]).value = 10f; var translation = graph.CreateVariableNode(onEvent.FunctionParameterModels.Single(p => p.DataType == typeof(Translation).GenerateTypeHandle(Stencil)), Vector2.zero); graph.CreateEdge(setProperty.InstancePort, translation.OutputPort); }, EachEntity((manager, i, e) => { manager.World.CreateSystem <InitializationSystemGroup>(); manager.AddComponentData(e, new Translation()); }), // Init State EachEntity((manager, i, e) => {}), // Wait MoveNext EachEntity((manager, i, e) => {}), // Send event; EachEntity((manager, i, e) => { Assert.That(manager.GetComponentData <Translation>(e).Value.x, Is.EqualTo(10f)); })); }
public void TestWaitUntilCoroutine([Values] CodeGenMode mode) { float yBeforeWait = 0f; float yAfterWait = 10f; SetupTestGraphMultipleFrames(mode, graph => { var query = SetupQuery(graph, "query", new[] { typeof(Translation) }); var onUpdate = SetupOnUpdate(graph, query); var waitUntil = onUpdate.CreateStackedNode <CoroutineNodeModel>("Wait Until", setup: n => { n.CoroutineType = typeof(WaitUntil).GenerateTypeHandle(Stencil); }); var translation = graph.CreateVariableNode(onUpdate.FunctionParameterModels.Single(p => p.DataType == typeof(Translation).GenerateTypeHandle(Stencil)), Vector2.zero); var getProperty = GraphModel.CreateGetPropertyGroupNode(Vector2.zero); var translationXMember = new TypeMember(TypeHandle.Float, new List <string> { nameof(Translation.Value), nameof(Translation.Value.x) }); getProperty.AddMember(translationXMember); GraphModel.CreateEdge(getProperty.InstancePort, translation.OutputPort); var equalNode = GraphModel.CreateBinaryOperatorNode(BinaryOperatorKind.GreaterThan, Vector2.zero); GraphModel.CreateEdge(equalNode.InputPortA, getProperty.GetPortsForMembers().Single()); ((FloatConstantModel)equalNode.InputConstantsById[equalNode.InputPortB.UniqueId]).value = 0f; var moveNextParam = typeof(WaitUntil).GetMethod(nameof(WaitUntil.MoveNext))?.GetParameters().Single(); Assert.That(moveNextParam, Is.Not.Null); GraphModel.CreateEdge(waitUntil.GetParameterPort(moveNextParam), equalNode.OutputPort); var setProperty = onUpdate.CreateSetPropertyGroupNode(-1); var translationYMember = new TypeMember(TypeHandle.Float, new List <string> { nameof(Translation.Value), nameof(Translation.Value.y) }); setProperty.AddMember(translationYMember); GraphModel.CreateEdge(setProperty.InstancePort, translation.OutputPort); ((FloatConstantModel)setProperty.InputConstantsById[translationYMember.GetId()]).value = yAfterWait; }, EachEntity((manager, i, e) => { manager.AddComponentData(e, new Translation()); }), EachEntity((manager, i, e) => { Assert.That(manager.GetComponentData <Translation>(e).Value.y, Is.EqualTo(yBeforeWait)); manager.SetComponentData(e, new Translation { Value = { x = 10f } }); // any x > 0 should stop this WaitUntil }), (manager, entities) => {}, // Skip Frame where WaitUntil is done and component gets set EachEntity((manager, i, e) => { Assert.That(manager.GetComponentData <Translation>(e).Value.y, Is.EqualTo(yAfterWait)); }) ); }
public void TestCoroutineWithForEachContext([Values] CodeGenMode mode) { SetupTestGraphMultipleFrames(mode, graph => { var translationQuery = SetupQuery(graph, "translationQuery", new[] { typeof(Translation) }); var scaleQuery = SetupQuery(graph, "scaleQuery", new[] { typeof(Scale) }); var onUpdate = SetupOnUpdate(graph, translationQuery); onUpdate.CreateStackedNode <CoroutineNodeModel>("Wait", setup: n => { n.CoroutineType = typeof(Wait).GenerateTypeHandle(Stencil); }); var forAllStack = graph.CreateLoopStack <ForAllEntitiesStackModel>(Vector2.zero); var forAllNode = forAllStack.CreateLoopNode(onUpdate, 1) as ForAllEntitiesNodeModel; Assert.That(forAllNode, Is.Not.Null); graph.CreateEdge(forAllNode.InputPort, scaleQuery.OutputPort); graph.CreateEdge(forAllStack.InputPort, forAllNode.OutputPort); var setProperty = forAllStack.CreateSetPropertyGroupNode(0); var member = new TypeMember(TypeHandle.Float, new List <string> { nameof(Scale.Value) }); setProperty.AddMember(member); ((FloatConstantModel)setProperty.InputConstantsById[member.GetId()]).value = 10f; var scale = graph.CreateVariableNode(forAllStack.FunctionParameterModels.Single(p => p.DataType == typeof(Scale).GenerateTypeHandle(Stencil)), Vector2.zero); graph.CreateEdge(setProperty.InstancePort, scale.OutputPort); }, EachEntity((manager, i, e) => { if (i % 2 == 0) { manager.AddComponentData(e, new Translation()); } else { manager.AddComponentData(e, new Scale()); } }), EachEntity((manager, i, e) => {}), // Init State EachEntity((manager, i, e) => {}), // Wait MoveNext EachEntity((manager, i, e) => // ForEach set Scale { if (manager.HasComponent <Scale>(e)) { Assert.That(manager.GetComponentData <Scale>(e).Value, Is.EqualTo(10f)); } }) ); }
public void NestedIterationSystem_DifferentGroups_DifferentComponents([Values] CodeGenMode mode) { SetupTestGraph(mode, g => { TypeHandle translationType = typeof(Translation).GenerateTypeHandle(Stencil); TypeHandle rotationType = typeof(Rotation).GenerateTypeHandle(Stencil); // query1 - Position ComponentQueryDeclarationModel query1 = GraphModel.CreateComponentQuery("g1"); query1.AddComponent(Stencil, translationType, ComponentDefinitionFlags.None); IVariableModel query1Instance = GraphModel.CreateVariableNode(query1, Vector2.zero); // query2 - Rotation too ComponentQueryDeclarationModel query2 = GraphModel.CreateComponentQuery("g2"); query2.AddComponent(Stencil, rotationType, ComponentDefinitionFlags.None); IVariableModel query2Instance = GraphModel.CreateVariableNode(query2, Vector2.zero); // update query 1 OnUpdateEntitiesNodeModel onUpdateModel = GraphModel.CreateNode <OnUpdateEntitiesNodeModel>("update", Vector2.zero); GraphModel.CreateEdge(onUpdateModel.InstancePort, query1Instance.OutputPort); // nested update query 2 var forAllStack = GraphModel.CreateLoopStack <ForAllEntitiesStackModel>(Vector2.zero); var forAllNode = forAllStack.CreateLoopNode(onUpdateModel, 0) as ForAllEntitiesNodeModel; Assert.That(forAllNode, Is.Not.Null); GraphModel.CreateEdge(forAllNode.InputPort, query2Instance.OutputPort); GraphModel.CreateEdge(forAllStack.InputPort, forAllNode.OutputPort); // set query1.translation = ... SetPropertyGroupNodeModel set = forAllStack.CreateStackedNode <SetPropertyGroupNodeModel>("set"); var member = new TypeMember(TypeHandle.Float, new List <string> { nameof(Translation.Value), nameof(Translation.Value.x) }); set.AddMember(member); IVariableModel posComponent = GraphModel.CreateVariableNode(onUpdateModel.FunctionParameterModels.Single(p => p.DataType == translationType), Vector2.zero); GraphModel.CreateEdge(set.InstancePort, posComponent.OutputPort); ((FloatConstantModel)set.InputConstantsById[member.GetId()]).value = 2f; }, (manager, entityIndex, e) => { manager.AddComponentData(e, new Translation { Value = { x = entityIndex } }); manager.AddComponentData(e, new Rotation()); }, (manager, entityIndex, e) => Assert.That(manager.GetComponentData <Translation>(e).Value.x, Is.EqualTo(2f))); }
public void SetNonGraphVariableDoesntTriggerASingletonUpdate([Values] CodeGenMode mode) { SetupTestGraph(mode, graph => { var query = graph.CreateComponentQuery("m_Query"); var translationType = typeof(Translation).GenerateTypeHandle(Stencil); query.AddComponent(Stencil, translationType, ComponentDefinitionFlags.None); var queryInstance = graph.CreateVariableNode(query, Vector2.zero); var onUpdate = graph.CreateNode <OnUpdateEntitiesNodeModel>("On Update", Vector2.zero); graph.CreateEdge(onUpdate.InstancePort, queryInstance.OutputPort); var floatVariable = onUpdate.CreateFunctionVariableDeclaration("MyFloat", TypeHandle.Float); floatVariable.CreateInitializationValue(); var floatInstance = graph.CreateVariableNode(floatVariable, Vector2.zero); var set = onUpdate.CreateStackedNode <SetVariableNodeModel>("set"); graph.CreateEdge(set.InstancePort, floatInstance.OutputPort); var setProperty = onUpdate.CreateStackedNode <SetPropertyGroupNodeModel>("Set Property"); var member = new TypeMember(TypeHandle.Float, new List <string> { nameof(Translation.Value), nameof(Translation.Value.x) }); setProperty.AddMember(member); var translation = graph.CreateVariableNode(onUpdate.FunctionParameterModels.Single(p => p.DataType == translationType), Vector2.zero); graph.CreateEdge(setProperty.InstancePort, translation.OutputPort); graph.CreateEdge(setProperty.InputsById[member.GetId()], floatInstance.OutputPort); }, (manager, entityIndex, entity) => manager.AddComponentData(entity, new Translation()), (manager, entityIndex, entity) => { // We need to check as we created a singleton entity with no Translation but only the GraphData component if (manager.HasComponent <Translation>(entity)) { Assert.That(manager.GetComponentData <Translation>(entity).Value.x, Is.EqualTo(0f)); } }); }
public void OnEventTest([Values] CodeGenMode mode) { SetupTestGraphMultipleFrames(mode, g => { ComponentQueryDeclarationModel query = GraphModel.CreateComponentQuery("g1"); TypeHandle positionType = typeof(Translation).GenerateTypeHandle(Stencil); query.AddComponent(Stencil, positionType, ComponentDefinitionFlags.None); IVariableModel queryInstance = GraphModel.CreateVariableNode(query, Vector2.zero); TypeHandle eventTypeHandle = typeof(UnitTestEvent).GenerateTypeHandle(Stencil); OnEventNodeModel onUpdateModel = GraphModel.CreateNode <OnEventNodeModel>("update", Vector2.zero, SpawnFlags.Default, n => n.EventTypeHandle = eventTypeHandle); GraphModel.CreateEdge(onUpdateModel.InstancePort, queryInstance.OutputPort); SetPropertyGroupNodeModel set = onUpdateModel.CreateStackedNode <SetPropertyGroupNodeModel>("set", 0); var member = new TypeMember(TypeHandle.Float, new List <string> { nameof(Translation.Value), nameof(Translation.Value.x) }); set.AddMember(member); IVariableModel posComponent = GraphModel.CreateVariableNode(onUpdateModel.FunctionParameterModels.Single(p => p.DataType == positionType), Vector2.zero); GraphModel.CreateEdge(set.InstancePort, posComponent.OutputPort); ((FloatConstantModel)set.InputConstantsById[member.GetId()]).value = 2f; }, // Add translation to even entities EachEntity((manager, i, e) => { if (e.Index % 2 == 0) { manager.AddComponent(e, typeof(Translation)); } }), // Send event on these EachEntity(del: (manager, i, e) => { if (e.Index % 2 == 0) { Assert.That(manager.GetComponentData <Translation>(e).Value.x, Is.EqualTo(0f)); } else { Assert.IsFalse(manager.HasComponent <Translation>(e)); } // Add event on all entities manager.AddBuffer <UnitTestEvent>(e).Add(new UnitTestEvent()); }), // Event handler should set t.x to 2 EachEntity((manager, i, e) => { if (e.Index % 2 == 0) { Assert.That(manager.GetComponentData <Translation>(e).Value.x, Is.EqualTo(2f)); } }), // Reset translation EachEntity((manager, i, e) => { if (e.Index % 2 == 0) { var t = manager.GetComponentData <Translation>(e); t.Value.x = 0; manager.SetComponentData(e, t); } }), // As we send events manually, the event system is not running and won't cleanup events. // Event handler should set t.x to 2 again EachEntity((manager, i, e) => { if (e.Index % 2 == 0) { Assert.That(manager.GetComponentData <Translation>(e).Value.x, Is.EqualTo(2f)); } }) ); }
public void ConnectingADifferentNodePreservesConnections([Values] TestingMode mode) { var memberX = new TypeMember(Stencil.GenerateTypeHandle(typeof(Vector3)), new List <string> { nameof(Vector3.x) }); var memberY = new TypeMember(Stencil.GenerateTypeHandle(typeof(Vector3)), new List <string> { nameof(Vector3.y) }); { var iDecl = GraphModel.CreateGraphVariableDeclaration("myInt", TypeHandle.Int, true); var myInt = GraphModel.CreateVariableNode(iDecl, Vector2.up); var vDecl = GraphModel.CreateGraphVariableDeclaration("myVec", typeof(Vector3).GenerateTypeHandle(Stencil), true); var myVec = GraphModel.CreateVariableNode(vDecl, Vector2.left); var getProperty = GraphModel.CreateGetPropertyGroupNode(Vector2.zero); GraphModel.CreateEdge(getProperty.InstancePort, myVec.OutputPort); getProperty.AddMember(memberX); getProperty.AddMember(memberY); var stack = GraphModel.CreateStack("myStack", Vector2.right); var log1 = stack.CreateStackedNode <LogNodeModel>("log1"); var log2 = stack.CreateStackedNode <LogNodeModel>("log2"); GraphModel.CreateEdge(log1.InputPort, getProperty.OutputsById[memberX.GetId()]); GraphModel.CreateEdge(log2.InputPort, getProperty.OutputsById[memberY.GetId()]); } TestPrereqActionPostreq(mode, () => { var logStack = GetAllStacks().Single(); var log1 = logStack.NodeModels[0] as LogNodeModel; var log2 = logStack.NodeModels[1] as LogNodeModel; var myInt = GetAllNodes().OfType <VariableNodeModel>().Single(n => n.DataType == TypeHandle.Int); var getProperty = GetAllNodes().OfType <GetPropertyGroupNodeModel>().Single(); var portX = getProperty.OutputsById[memberX.GetId()]; var portY = getProperty.OutputsById[memberY.GetId()]; Assert.That(myInt.OutputPort.Connected, Is.False); Assert.That(log1.InputPort, Is.ConnectedTo(portX)); Assert.That(log2.InputPort, Is.ConnectedTo(portY)); return(new CreateEdgeAction(log1.InputPort, myInt.OutputPort, new List <IEdgeModel> { GraphModel.GetEdgesConnections(log1.InputPort).Single() })); }, () => { var logStack = GetAllStacks().Single(); var log1 = logStack.NodeModels[0] as LogNodeModel; var log2 = logStack.NodeModels[1] as LogNodeModel; var myInt = GetAllNodes().OfType <VariableNodeModel>().Single(n => n.DataType == TypeHandle.Int); var getProperty = GetAllNodes().OfType <GetPropertyGroupNodeModel>().Single(); var portX = getProperty.OutputsById[memberX.GetId()]; var portY = getProperty.OutputsById[memberY.GetId()]; Assert.That(myInt.OutputPort.Connected, Is.True); Assert.That(portX.Connected, Is.False); Assert.That(log1.InputPort, Is.ConnectedTo(myInt.OutputPort)); Assert.That(log2.InputPort, Is.ConnectedTo(portY)); }); }