protected override void OnUpdate() { Entities.ForEach((Gate gate) => { var gateEntity = GetPrimaryEntity(gate); DstEntityManager.AddComponents(gateEntity, new ComponentTypes(new ComponentType[] { typeof(GateInfo), typeof(NodeOutput), typeof(DagDepth), })); DstEntityManager.SetComponentData(gateEntity, new GateInfo { Type = gate.gateType }); }); }
protected override void OnUpdate() { Entities.ForEach((Button button, BoxCollider2D box) => { var buttonEntity = GetPrimaryEntity(button); DstEntityManager.AddComponents(buttonEntity, new ComponentTypes(new ComponentType[] { typeof(ClickableNode), typeof(ToggleCount), typeof(IsMouseHovering), })); // Convert bounding box to ClickableNode var bounds = box.bounds; var boundsMin = bounds.min; var boundsMax = bounds.max; DstEntityManager.SetComponentData(buttonEntity, new ClickableNode { RectMin = new float2(boundsMin.x, boundsMin.y), RectMax = new float2(boundsMax.x, boundsMax.y), }); // redundant, but hey DstEntityManager.SetComponentData(buttonEntity, new ToggleCount { Value = 0 }); if (button.initiallyOn) { // Set initial NodeOutput DstEntityManager.SetComponentData(buttonEntity, new NodeOutput { Value = 1 }); } }); }
protected override void OnUpdate() { Entities.ForEach((Branch branch, BoxCollider2D box) => { var branchEntity = GetPrimaryEntity(branch); // Branch prefabs have a child object that is also a gate var partnerGate = branch.partnerGate; Assert.IsNotNull(partnerGate); Assert.AreEqual(branch.gameObject, partnerGate.transform.parent.gameObject); var partnerEntity = GetPrimaryEntity(partnerGate); // And both entities should already have their NodeInput component added Assert.IsTrue(DstEntityManager.HasComponent <NodeInput>(branchEntity)); Assert.IsTrue(DstEntityManager.HasComponent <NodeInput>(partnerEntity)); DstEntityManager.AddComponents(branchEntity, new ComponentTypes(new ComponentType[] { typeof(ClickableNode), typeof(ToggleCount), typeof(IsMouseHovering), typeof(BranchPartner), typeof(BranchState), })); // Partner entity should use the same input node as the base branch entity DstEntityManager.SetComponentData(branchEntity, new BranchPartner { PartnerEntity = partnerEntity }); // Partner entity should use the same input node as the base branch entity var branchInputBuffer = DstEntityManager.GetBuffer <NodeInput>(branchEntity); var partnerInputBuffer = DstEntityManager.GetBuffer <NodeInput>(partnerEntity); Assert.AreEqual(1, branchInputBuffer.Length); Assert.AreEqual(1, partnerInputBuffer.Length); partnerInputBuffer[0] = branchInputBuffer[0]; // Convert bounding box to ClickableNode var bounds = box.bounds; var boundsMin = bounds.min; var boundsMax = bounds.max; DstEntityManager.SetComponentData(branchEntity, new ClickableNode { RectMin = new float2(boundsMin.x, boundsMin.y), RectMax = new float2(boundsMax.x, boundsMax.y), }); // redundant, but hey DstEntityManager.SetComponentData(branchEntity, new ToggleCount { Value = 0 }); DstEntityManager.SetComponentData(branchEntity, new BranchState { Value = branch.initiallyRight ? 1.0f : 0.0f }); if (branch.initiallyRight) { // Set initial GateType for both sides of the branch var branchGateInfo = DstEntityManager.GetComponentData <GateInfo>(branchEntity); branchGateInfo.Type = GateType.BranchOff; DstEntityManager.SetComponentData(branchEntity, branchGateInfo); var partnerGateInfo = DstEntityManager.GetComponentData <GateInfo>(partnerEntity); partnerGateInfo.Type = GateType.BranchOn; DstEntityManager.SetComponentData(partnerEntity, partnerGateInfo); } else { // Set initial GateType for both sides of the branch var branchGateInfo = DstEntityManager.GetComponentData <GateInfo>(branchEntity); branchGateInfo.Type = GateType.BranchOn; DstEntityManager.SetComponentData(branchEntity, branchGateInfo); var partnerGateInfo = DstEntityManager.GetComponentData <GateInfo>(partnerEntity); partnerGateInfo.Type = GateType.BranchOff; DstEntityManager.SetComponentData(partnerEntity, partnerGateInfo); } }); }
void CreateWireEntities(Component rootComponent, Wire wirePrefab, Vector3 wireEndPos, params Gate[] inputNodes) { if (wirePrefab == null) { return; } var wireMeshFilter = wirePrefab.GetComponent <MeshFilter>(); var wireTransform = wirePrefab.GetComponent <Transform>(); var wireMaterial = wirePrefab.GetComponent <MeshRenderer>().sharedMaterial; // Copy & modify the gate's RenderMesh to reference the correct mesh/material. var gateEntity = GetPrimaryEntity(rootComponent); var baseRenderMesh = DstEntityManager.GetSharedComponentData <RenderMesh>(gateEntity); baseRenderMesh.mesh = wireMeshFilter.sharedMesh; baseRenderMesh.material = wireMaterial; foreach (var inputNode in inputNodes) { // Create a linked entity for the wires leading from the input to this gate var wireStartPos = inputNode.outputAttachTransform.position; // TODO(cort): conversionSystem.InstantiateAdditionalEntity() would be ideal here. Instead, copy what we need from the prefab. var wireEntity = CreateAdditionalEntity(rootComponent); #if UNITY_EDITOR DstEntityManager.SetName(wireEntity, "Wire"); // TODO: make this call [Conditional] #endif DstEntityManager.AddComponents(wireEntity, new ComponentTypes(new ComponentType[] { typeof(LocalToWorld), typeof(RenderBounds), typeof(Translation), typeof(Rotation), typeof(NonUniformScale), typeof(WireInput), typeof(WireState), })); DstEntityManager.AddSharedComponentData(wireEntity, baseRenderMesh); // TODO(https://github.com/cdwfs/RubyGates/issues/9): pre-bake the LocalToWorld and add the Static tag DstEntityManager.SetComponentData(wireEntity, new Translation { Value = wireStartPos, }); var wireScale = wireTransform.localScale; wireScale.y = Vector3.Distance(wireStartPos, wireEndPos); DstEntityManager.SetComponentData(wireEntity, new NonUniformScale { Value = wireScale, }); // We want to measure an angle relative to +Y while looking in -Z. float3 wireDir = wireEndPos - wireStartPos; float wireAngle = math.atan2(-wireDir.x, wireDir.y); DstEntityManager.SetComponentData(wireEntity, new Rotation { Value = quaternion.RotateZ(wireAngle), }); DstEntityManager.SetComponentData(wireEntity, new WireInput { InputEntity = GetPrimaryEntity(inputNode.gameObject), }); } }
/// <summary> /// Create several new entities for a joint associated with the specified authoring component. /// Other systems can later find these joints by using <see cref="GetJointEntities"/>. /// Use this method instead of <see cref="CreateJointEntity"/> when the behavior specified by the authoring component requires multiple <see cref="PhysicsJoint"/> components to describe. /// The new entities will have all required component types, <see cref="PhysiscsJointCompanion"/> buffers, as well as readable names in the Entity Debugger. /// </summary> /// <param name="authoringComponent">An authoring component being converted.</param> /// <param name="constrainedBodyPair">A component describing the bodies constrained by the joints.</param> /// <param name="joints">The set of reference frames and corresponding constraints to apply to the bodies.</param> /// <param name="newJointEntities">An optional list to populate with all of the new entities.</param> public void CreateJointEntities( UnityComponent authoringComponent, PhysicsConstrainedBodyPair constrainedBodyPair, NativeArray <PhysicsJoint> joints, NativeList <Entity> newJointEntities = default ) { if (!joints.IsCreated || joints.Length == 0) { return; } if (newJointEntities.IsCreated) { newJointEntities.Clear(); } else { newJointEntities = new NativeList <Entity>(joints.Length, Allocator.Temp); } // find existing joints associated with the authoring component, if any if (!m_JointEntitiesPerAuthoringComponent.TryGetValue(authoringComponent, out var allJointEntities)) { m_JointEntitiesPerAuthoringComponent[authoringComponent] = allJointEntities = new NativeList <Entity>(joints.Length, Allocator.Persistent); } // create all new joints var multipleJoints = joints.Length > 1; #if UNITY_EDITOR var nameEntityA = DstEntityManager.GetName(constrainedBodyPair.EntityA); var nameEntityB = constrainedBodyPair.EntityB == Entity.Null ? "PhysicsWorld" : DstEntityManager.GetName(constrainedBodyPair.EntityB); var baseName = $"Joining {nameEntityA} + {nameEntityB}"; #endif for (var i = 0; i < joints.Length; ++i) { var jointEntity = CreateAdditionalEntity(authoringComponent); #if UNITY_EDITOR DstEntityManager.SetName(jointEntity, $"{baseName} ({joints[i].JointType})"); #endif DstEntityManager.AddComponents( jointEntity, multipleJoints ? k_JointComponentsMultiple : k_JointComponentsSingle ); DstEntityManager.SetComponentData(jointEntity, constrainedBodyPair); DstEntityManager.SetComponentData(jointEntity, joints[i]); newJointEntities.Add(jointEntity); allJointEntities.Add(jointEntity); } if (!multipleJoints) { return; } // set companion buffers for new joints for (var i = 0; i < joints.Length; ++i) { var companions = DstEntityManager.GetBuffer <PhysicsJointCompanion>(newJointEntities[i]); for (var j = 0; j < joints.Length; ++j) { if (i == j) { continue; } companions.Add(new PhysicsJointCompanion { JointEntity = newJointEntities[j] }); } } }