public void InitScene(EntityManager manager, ChunckConfig config) { var random = new Unity.Mathematics.Random((uint)System.DateTime.Now.Millisecond); for (int i = 0; i < config.count; i++) { var position = random.NextFloat3(config.center - config.size, config.center + config.size); var volume = random.NextFloat(config.volumeRange.start, config.volumeRange.start + config.volumeRange.length); var speed = random.NextFloat3(config.minSpeed, config.maxSpeed); var entity = manager.Instantiate(config.entity); Joiner_C joiner = new Joiner_C() { Volume = volume }; Mover_C mover = new Mover_C() { direction = speed }; Translation translation = new Translation() { Value = position }; manager.AddComponentData(entity, joiner); manager.AddComponentData(entity, mover); manager.AddComponentData(entity, translation); } }
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem) { Joiner_C joiner = new Joiner_C(); joiner.Volume = Volume; dstManager.AddComponentData(entity, joiner); }
public void Execute(int index) { Entity entity = entities[index]; Joiner_C joiner = joiners[index]; Translation translation = translations[index]; Mover_C mover = movers[index]; NonUniformScale scale = scales[index]; // 增加的体积 double addVolume = 0; // 增加的质量 double addMass = 0; // 增加的动量 double3 addMomentum = double3.zero; for (int i = 0; i < joiners.Length; i++) { if (i == index) { continue; } var joiner2 = joiners[i]; if (joiner.Volume <= 0 || joiner2.Volume <= 0) { continue; } if (joiner2.Range == joiner.Range && entity.Index > entities[i].Index) { continue; } var translation2 = translations[i]; var dis = math.distance(translation.Value, translation2.Value); if (dis == 0) { continue; } if (joiner.Range + joiner2.Range > dis) { //如果被吸收则减少当前体积,如果吸收则增加对方的体积 bool isOut = joiner2.Range > joiner.Range; double v = isOut ? joiner.Volume : joiner2.Volume; //最少吸收每秒吸收1体积 if (v > 0.2) { v = math.max(0.2, v * deltaTime * 2); } if (!isOut) { var m = UnitHelper.Volume2Mass(v); addMass += m; addMomentum += m * movers[i].direction; } addVolume += isOut ? -v : v; } } joiner.Volume += addVolume; //根据物质量显示物体 if (joiner.Volume < 0.01) { concurrent.DestroyEntity(index, entity); return; } if (addVolume > 0) { //添加动量 Momentum_C momentumIn = new Momentum_C(); momentumIn.mass = addMass * 0.1; momentumIn.speed = addMomentum / (addMass + 1); momentumIn.target = entity; concurrent.AddComponent(index, concurrent.CreateEntity(index), momentumIn); } scale.Value = (float3)(new double3(1, 1, 1) * 2 * joiner.Range); concurrent.SetComponent(index, entity, scale); concurrent.SetComponent(index, entity, joiner); }