//加力 public float3 CastForce(ref float3 position, ref CubeComponent b, ForceMode forceMode) { float3 forceDir = (position - b.position); float d = math.length(forceDir); d = math.clamp(d, 1, 25); forceDir = math.normalize(forceDir); // F = GMm / d^2 float strength = (G * Mass * b.mass) / (d * d); forceDir *= strength; return((forceMode == ForceMode.PUSH) ? forceDir * -1 : forceDir); }
protected override void OnUpdate() { if (MusicController.Instance.isPressAndWaitGenerate) { MusicController.Instance.isPressAndWaitGenerate = false; var entityManager = World.Active.GetOrCreateManager <EntityManager>(); for (int i = 0; i < 500 * MusicController.Instance.m_currBeatScore; i++) { Entity cube = entityManager.CreateEntity(CubeArchetype); float3 randomVel = UnityEngine.Random.onUnitSphere; //初始化随机点 float3 initialPosition = new float3(randomVel.x, 0f, randomVel.z); ; //设置给实体 entityManager.SetComponentData(cube, new Position { Value = initialPosition }); //cube属性 CubeComponent c = new CubeComponent { position = initialPosition, radius = 1, mass = 1, maxLength = 20, velocity = new float3(randomVel.x * 100f, 0f, randomVel.z * 100f), acceration = float3.zero, isInEnemy = 0 }; entityManager.SetComponentData(cube, c); //边界 float4 v = new float4(-960f, -540f, 960f, 540f); //力属性初始化 ForceComponent f = new ForceComponent { Mass = 70f, bound = v, frictionCoe = 0.1f }; entityManager.SetComponentData(cube, f); HealthComponent h = new HealthComponent { healthValue = 2, currColor = 0 }; //生命初始化 entityManager.SetComponentData(cube, h); //这里为什么不用Add,因为从Entities中Add或Remove ComponentData会使其archetype改变,大量操作会影响性能 //entityManager.AddSharedComponentData(cube, cubeRenderer); //共享数据,用于可以共享,且不经常改变的component entityManager.SetSharedComponentData(cube, cubeRenderer); } } //for test if (Input.GetKeyDown(KeyCode.E) || (Input.touchCount >= 3 && Input.touches[0].phase == TouchPhase.Ended)) { var entityManager = World.Active.GetOrCreateManager <EntityManager>(); for (int i = 0; i < 10000; i++) { Entity cube = entityManager.CreateEntity(CubeArchetype); //初始化随机点 float3 initialPosition = new float3(UnityEngine.Random.Range(-960f, 960f), 0f, UnityEngine.Random.Range(-540f, 540f)); ; //设置给实体 entityManager.SetComponentData(cube, new Position { Value = initialPosition }); //cube属性 CubeComponent c = new CubeComponent { position = initialPosition, radius = 1, mass = 1, maxLength = 20, velocity = Vector3.zero, acceration = Vector3.zero, isInEnemy = 0 }; entityManager.SetComponentData(cube, c); //边界 float4 v = new float4(-960f, -540f, 960f, 540f); //力属性初始化 ForceComponent f = new ForceComponent { Mass = 70f, bound = v, frictionCoe = 0.1f }; entityManager.SetComponentData(cube, f); HealthComponent h = new HealthComponent { healthValue = 2, currColor = 0 }; //生命初始化 entityManager.SetComponentData(cube, h); entityManager.SetSharedComponentData(cube, cubeRenderer); } } }