static unsafe string MakeRandomFileName() { const int kFilenameLen = 16; var filenameBuffer = stackalloc char[kFilenameLen]; var rng = new Mathematics.Random((uint)Time.realtimeSinceStartup); for (int i = 0; i < kFilenameLen; ++i) { filenameBuffer[i] = rng.NextBool() ? (char)rng.NextInt('a', 'z') : (char)rng.NextInt('0', '9'); } return(new string(filenameBuffer, 0, kFilenameLen)); }
public void Execute() { foreach (var newFlyby in New) { var positional = PositionalFromEntity[newFlyby]; var speed = positional.PrevDist - positional.CurrentDist; speed /= Dt; // Discard slow fly bys. if (speed < Params.MinSpeed) { ECB.DestroyEntity(newFlyby); continue; } bool isFast = speed > Params.Fast; var intensity = (speed - Params.MinSpeed) / (Params.MaxSpeed - Params.MinSpeed); var pitch = 0.5f + intensity * 0.5f; var layerSpecificVolume = isFast ? Params.HighLayer : Params.LowLayer; pitch *= (isFast ? Params.FastPitchMul : 1); // allocate player. var freeList = isFast ? HighFreeList : LowFreeList; var otherList = !isFast ? HighFreeList : LowFreeList; if (freeList.Length == 0) { // steal from other queue. freeList = otherList; if (freeList.Length == 0) { break; } } int freeIndex = Rand.NextInt(freeList.Length - 1); int playerIndex = freeList[freeIndex]; var sample = CurrentSamples[playerIndex]; var distanceAttenuation = Params.Volume * math.min(0.5f, 1 / math.sqrt(math.max(1, positional.CurrentDist - 3))); //Debug.Log($"[{playerIndex}] Creating ({isFast}) player with speed {speed}, intensity {intensity}, distance {positional.CurrentDist}, attenuation {distanceAttenuation}, pitch {pitch}"); ECB.AddComponent(newFlyby, new FlyByState { Timeout = sample.Length / pitch, SampleIndex = playerIndex } ); //Debug.Log("Final volume: " + math.sqrt(intensity) * distanceAttenuation); ECB.AddComponent(sample.PlayerEntity, new SamplePlayback { Left = positional.Left, Right = positional.Right, Pitch = pitch, Volume = layerSpecificVolume * math.min(1, math.sqrt(intensity)) * distanceAttenuation } ); freeList.RemoveAtSwapBack(freeIndex); } foreach (var deadFlyby in Dead) { //Debug.Log($"[{Dead.States[i].SampleIndex}] Dead player"); ECB.RemoveComponent(deadFlyby, ComponentType.ReadWrite <FlyByState>()); } }