From 928d7c0da4886a9894d13631c7778b6e4b7049e1 Mon Sep 17 00:00:00 2001 From: Niclas Thobaben Date: Sat, 6 May 2017 01:20:13 +0200 Subject: [PATCH] Actor/Ballistic Actor/nhPlayer Actor: +play Sound +select needed buffers by key +get playingState in update Ballistic Actor: +play Sound on fire +play Sound on Hit +destroy object after hit --- NHEngine/BootStrap.cs | 15 ++- NHEngine/OpenGLWindow.cs | 1 + NHEngine/nhengine.csproj | 26 ++++- org.niclasundharald.engine/Actor.cs | 36 +++--- org.niclasundharald.engine/BallisticActor.cs | 37 +++++- .../audio/AudioDelegate.cs | 40 +++---- org.niclasundharald.engine/audio/nhBuffers.cs | 60 +++++----- org.niclasundharald.engine/audio/nhPlayer.cs | 106 +++++++++--------- .../graphics/GLCamera.cs | 17 ++- .../org.niclasundharald.engine.csproj | 7 ++ 10 files changed, 216 insertions(+), 129 deletions(-) diff --git a/NHEngine/BootStrap.cs b/NHEngine/BootStrap.cs index 6dec481..edc04f7 100644 --- a/NHEngine/BootStrap.cs +++ b/NHEngine/BootStrap.cs @@ -8,6 +8,7 @@ using OpenTK; using OpenTK.Audio.OpenAL; using OpenTK.Audio; +using nhengine.Audio; using ImageSharp; using ImageSharp.Processing; @@ -38,6 +39,7 @@ namespace nhengine public BootStrap() { bootGraphics(); + bootAudio(); bootMap(); } @@ -54,9 +56,19 @@ namespace nhengine } public void bootAudio(){ - AL.Enable(ALCapability.Invalid); + + + + AudioDelegate.init(); + AudioDelegate.DistanceModel("exponential"); + nhBuffers.addSearchPath("./sounds"); + nhBuffers.loadSound("HowFire", "HowHit", "st1"); + //AudioDelegate.test(); + } + + public void bootMap(){ ImageSharp.Image i = ImageSharp.Image.Load( new FileStream("M3-wip.png",FileMode.Open)); i.Flip(ImageSharp.Processing.FlipType.Vertical); @@ -81,6 +93,7 @@ namespace nhengine public void run(){ glWindow.MakeCurrent(); glWindow.Run(25,25); + } diff --git a/NHEngine/OpenGLWindow.cs b/NHEngine/OpenGLWindow.cs index e42f8aa..896a1b8 100644 --- a/NHEngine/OpenGLWindow.cs +++ b/NHEngine/OpenGLWindow.cs @@ -202,6 +202,7 @@ namespace nhengine private void fireBallistic(){ Console.WriteLine("Fire Ballistic..."); BallisticActor ba = new BallisticActor(0); + ba.fire(); ba.Position = BootStrap.instance().SquaredMap.ground(new Vector2(10,10)); ba.Velocity = new Vector3(1,1,1);// + (Matrix4.CreateRotationZ(arcLeftRight) * Vector4.UnitY).Xyz; diff --git a/NHEngine/nhengine.csproj b/NHEngine/nhengine.csproj index d25d4b8..55fe5fb 100644 --- a/NHEngine/nhengine.csproj +++ b/NHEngine/nhengine.csproj @@ -65,8 +65,12 @@ - - + + PreserveNewest + + + PreserveNewest + @@ -82,6 +86,21 @@ PreserveNewest + + + + + + PreserveNewest + + + + + PreserveNewest + + + PreserveNewest + @@ -89,5 +108,8 @@ org.niclasundharald.engine + + + \ No newline at end of file diff --git a/org.niclasundharald.engine/Actor.cs b/org.niclasundharald.engine/Actor.cs index 2227466..f5319c9 100644 --- a/org.niclasundharald.engine/Actor.cs +++ b/org.niclasundharald.engine/Actor.cs @@ -11,8 +11,8 @@ namespace org.niclasundharald.engine { public class Actor : WorldObject { - private static List activeActors = new List(); - private static List finishedActors = new List(); + public static List activeActors = new List(); + public static List finishedActors = new List(); public static void updateAll(float timespan) @@ -40,21 +40,22 @@ namespace org.niclasundharald.engine this.id = id; activeActors.Add(this); - _player = new nhPlayer(("Actor" + id), Position); + _player = new nhPlayer(); activePlayers.Add(_player); } public void destroy(){ finishedActors.Add(this); - finishedPlayers.Add(_player); + _player.deletePlayer(); + } public Vector3 Heading { get; set; } public Vector3 Velocity { get; set; } public float Weight { get; set; } - protected virtual void update(float timespan){ + public virtual void update(float timespan){ } public void setHeading(Vector3 heading,Vector3 top){ @@ -85,12 +86,12 @@ namespace org.niclasundharald.engine //>>>>>>>>AUDIO private static List activePlayers = new List(); - private static List finishedPlayers = new List(); - - private static List aBuffers = new List(); + private static Dictionary aBuffers = new Dictionary(); private nhPlayer _player; + public bool isPlaying = false; + public float rollOf { get; set; } public float refDistance { get; set; } public float maxDistance { get; set; } @@ -105,19 +106,28 @@ namespace org.niclasundharald.engine public void setBuffers(params string[] buffers) { - for (int i = 0; i < buffers.Length; i++) + foreach (string i in buffers) { - aBuffers.Add(nhBuffers.buffers[buffers[i]]); + if(!aBuffers.ContainsKey(i)) + aBuffers.Add(i, nhBuffers.buffers[i]); } } - - public void playSound(string sound, float gain, bool loop) + public void playSound(string sound, float gain = 1.0f, bool loop = false) { - _player.play(sound, gain, loop); + + _player.position = this.Position; + _player.velocity = Velocity; + _player.play(aBuffers[sound], gain, loop); + } + public void updateSoundState() + { + isPlaying = _player.state(); + + } } } diff --git a/org.niclasundharald.engine/BallisticActor.cs b/org.niclasundharald.engine/BallisticActor.cs index 90fd0de..de83176 100644 --- a/org.niclasundharald.engine/BallisticActor.cs +++ b/org.niclasundharald.engine/BallisticActor.cs @@ -4,6 +4,8 @@ using org.niclasundharald.engine.graphics; using OpenTK; + + namespace org.niclasundharald.engine { public class BallisticActor : Actor @@ -12,8 +14,11 @@ namespace org.niclasundharald.engine :base(id) { this.Model3D = ModelManager.instance.findModel("ballistisch"); + setBuffers("HowFire", "HowHit", "st1"); } + int hit = 0; + public override void update(float timespan) { base.update(timespan); @@ -26,18 +31,44 @@ namespace org.niclasundharald.engine Console.WriteLine("BallisticActor update: {0} / {1}",Position,Velocity); + updateSoundState(); + Console.WriteLine("Playing:" + isPlaying); + try{ Vector3 ground = SquaredMap.activeMap.ground(Position.Xy); if (Position.Z <= ground.Z){ - destroy(); + + if(hit != 1) + { + playSound("HowHit"); + updateSoundState(); + hit = 1; + } + + + + if(!isPlaying) + { + destroy(); + } } - } catch (OutOfWorldException e){ + } catch (OutOfWorldException e){ + + if(!isPlaying) destroy(); } - } + + + public void fire() + { + playSound("HowFire"); + setDistanceAttenuation(2, 0, 50); + } + + } } diff --git a/org.niclasundharald.engine/audio/AudioDelegate.cs b/org.niclasundharald.engine/audio/AudioDelegate.cs index 411b649..8913249 100644 --- a/org.niclasundharald.engine/audio/AudioDelegate.cs +++ b/org.niclasundharald.engine/audio/AudioDelegate.cs @@ -17,11 +17,11 @@ namespace nhengine.Audio { - private static bool debug = true; + //private static bool debug = true; public static AudioContext context = new AudioContext(); - public static EffectsExtension efx = new EffectsExtension(); + //public static EffectsExtension efx = new EffectsExtension(); //PLAYERS public static readonly List players = new List(); @@ -35,27 +35,23 @@ namespace nhengine.Audio nhBuffers.init(); nhListener.init(); - nhPlayer.init(efx); - //nhMixer.init(); - DistanceModel(); + //nhAux reverb = new nhAux(); - - - - for (int i = 0; i < nhBuffers.buffers.Count; i++) - { - - addPlayer("testplayer", new Vector3(0.0f, 0.0f, 0.0f)); - - } - Console.WriteLine("nhengine.Audio initialized."); } + + public static void test() + { + nhPlayer testPlayer = new nhPlayer(); + testPlayer.play(nhBuffers.buffers["st1"]); + } + /* <<<<<<<<<<
>>>>>>>>>> */ +/* public static void Main() { @@ -75,7 +71,7 @@ namespace nhengine.Audio } - +*/ public static void DistanceModel(string distanceModel = "exponential", bool clamped = false) { @@ -128,7 +124,7 @@ namespace nhengine.Audio return _index; } - +/* public static void play(string playerName, string buffer, float gain = 1.0f, bool loop = false) { @@ -153,7 +149,7 @@ namespace nhengine.Audio } } - +*/ public static void setPlayerPosition(string playerName, Vector3 position) { @@ -197,10 +193,10 @@ namespace nhengine.Audio } } - +/* public static void addPlayer(string name, Vector3 position, float rollOf = 2.0f, float refDist = 0.0f, float maxDist = 100.0f) { - nhPlayer _player = new nhPlayer(name, position, rollOf, refDist, maxDist); + nhPlayer _player = new nhPlayer(name, position); players.Add(_player); playerNames.Add(_player.name); playerNumbers.Add(_player.number); @@ -214,6 +210,10 @@ namespace nhengine.Audio players.RemoveAt(_index); playerNames.RemoveAt(_index); playerNumbers.RemoveAt(_index); + } + +*/ + } } \ No newline at end of file diff --git a/org.niclasundharald.engine/audio/nhBuffers.cs b/org.niclasundharald.engine/audio/nhBuffers.cs index 1fb965b..830f24c 100644 --- a/org.niclasundharald.engine/audio/nhBuffers.cs +++ b/org.niclasundharald.engine/audio/nhBuffers.cs @@ -3,6 +3,7 @@ using System.IO; using System.Collections.Generic; using OpenTK.Audio; using OpenTK.Audio.OpenAL; +using org.niclasundharald.engine; @@ -14,51 +15,58 @@ namespace nhengine.Audio public static class nhBuffers { - public static List buffers = new List(); - public static List buffersNames = new List(); + //public static List buffers = new List(); + //public static List buffersNames = new List(); + + public static Dictionary buffers = new Dictionary(); private static List _files = new List(); - private static string[] _folder; + private static List _searchPaths = new List(); //private static string filepath; + + public static void init() { buffers.Clear(); - buffersNames.Clear(); _files.Clear(); - _openFiles(); - + //_searchPaths = new List(); + _searchPaths.Add("."); } - - - - - private static void _openFiles() + public static void addSearchPath(string sPath) { + _searchPaths.Add(sPath); + } - //_files.Add(filepath); - _folder = Directory.GetFiles(Path.Combine("Data", "Audio", "Sounds"), "*.wav"); - - for (int i = 0; i < _folder.Length; i++) + public static void loadSound(params string[] sName) + { + foreach(string a in sName) { - _createBuffer(_folder[i]); - buffersNames.Add(Path.GetFileName(_folder[i])); + string fullName = FileHelper.findFile(String.Format("{0}.wav", a), _searchPaths.ToArray()); + Console.WriteLine("TEST: " + fullName); + if (fullName != null) + { + Console.WriteLine("Sound found: {0}", fullName); + _createBuffer(fullName, a); + }else + throw new FileNotFoundException("File not found."); } } - private static void _createBuffer(string path) + private static void _createBuffer(string path, string sName) { Stream stream = File.Open(path, FileMode.Open); + string name = sName; if (stream == null) throw new ArgumentNullException(nameof(stream)); @@ -67,7 +75,6 @@ namespace nhengine.Audio { - //RIFF HEADER string signature = new string(reader.ReadChars(4)); if (signature != "RIFF") @@ -127,7 +134,7 @@ namespace nhengine.Audio int buffer = AL.GenBuffer(); AL.BufferData(buffer, fileFormat, soundData, soundData.Length, sample_rate); - buffers.Add(buffer); + buffers.Add(name, buffer); } @@ -136,19 +143,6 @@ namespace nhengine.Audio - public static string getBufferName(int bufferID){ - - return buffersNames[bufferID]; - - } - - public static int getBufferID(string bufferName){ - - - int _index = buffersNames.IndexOf(bufferName); - return buffers[_index]; - } - diff --git a/org.niclasundharald.engine/audio/nhPlayer.cs b/org.niclasundharald.engine/audio/nhPlayer.cs index 5476e71..df3a06c 100644 --- a/org.niclasundharald.engine/audio/nhPlayer.cs +++ b/org.niclasundharald.engine/audio/nhPlayer.cs @@ -20,25 +20,6 @@ namespace nhengine.Audio } - private int _buffer; - public string buffer - { - set - {_buffer = nhBuffers.getBufferID(value);} - get - {return _buffer.ToString();} - } - - - private string _name; - public string name - { - set - {_name = value + ".p" + _playerNumber ;} - get - {return _name;} - } - private static int _playerNumber; public int number { @@ -46,8 +27,8 @@ namespace nhengine.Audio { return _playerNumber;} } - private static EffectsExtension _efx; - private int _filter; + //private static EffectsExtension _efx; + //private int _filter; @@ -76,40 +57,33 @@ namespace nhengine.Audio public Vector3 velocity = new Vector3(0.0f, 0.0f, 0.0f); //public bool loop = false; - private float _rollOf; - private float _refDist; - private float _maxDist; + private float _rollOf = 2.0f; + private float _refDist = 0.0f; + private float _maxDist = 100.0f; - //private int _state; + private int _state; + //private int[] _seqBuffer = { }; - public nhPlayer(string name, Vector3 position, float rollOf = 2.0f, float refDist = 0.0f, float maxDist = 100.0f) + public nhPlayer() { - - this.name = name; - _rollOf = rollOf; - _refDist = refDist; - _maxDist = maxDist; - this.position = position; - _filter = _efx.GenFilter(); - _efx.BindFilterToSource(_source, _filter); + //_filter = _efx.GenFilter(); + //_efx.BindFilterToSource(_source, _filter); AL.Source(_source, ALSourcef.RolloffFactor, _rollOf); AL.Source(_source, ALSourcef.ReferenceDistance, _refDist); AL.Source(_source, ALSourcef.MaxDistance, _maxDist); - AL.Source(_source, ALSourcei.EfxDirectFilter, _filter); + //AL.Source(_source, ALSourcei.EfxDirectFilter, _filter); //AL.Source(_source, ALSourcei.EfxDirectFilter, ) //_filterEfx.Filter(_filter, EfxFilteri.FilterType, EfxFilterType.Lowpass); - _efx.Filter(_filter, EfxFilterf.LowpassGainHF, 0.5f); - - printInfo(); + //_efx.Filter(_filter, EfxFilterf.LowpassGainHF, 0.5f); Interlocked.Increment(ref _playerNumber); } @@ -117,26 +91,40 @@ namespace nhengine.Audio //>>>>>>>>>>>>>>>>>METHODS - public static void init(EffectsExtension effectsExtension) + public bool state() { + AL.GetSource(_source, ALGetSourcei.SourceState, out _state); + + if ((ALSourceState)_state == ALSourceState.Playing) + { + return true; + }else if((ALSourceState)_state == ALSourceState.Stopped) + { + return false; + }else + { + return false; + } - _efx = effectsExtension; } - - - - public void play(string buffer = null, float gain = 1.0f, bool loop = false) + public void play(int buffer = 0, float gain = 1.0f, bool loop = false) { _gain = gain; - this.buffer = buffer; - AL.Source(_source, ALSourcei.Buffer, _buffer); + + if (buffer != 0) + { + AL.Source(_source, ALSourcei.Buffer, buffer); + }else{ + throw new NotSupportedException("Not existing Buffer."); + } + AL.Source(_source, ALSourceb.Looping, loop); AL.SourcePlay(_source); - //_isplaying = true; + AL.Source(_source, ALSourcef.Gain, _gain); AL.Source(_source, ALSource3f.Position, position.X, position.Y, position.Z); @@ -150,6 +138,22 @@ namespace nhengine.Audio AL.SourceStop(_source); } + public void setDistanceAttenuation(float rollOf = 2.0f, float refDistance = 0.0f, float maxDistance = 100.0f) + { + _rollOf = rollOf; + _refDist = refDistance; + _maxDist = maxDistance; + } + + public void setPosition(Vector3 position) + { + this.position = position; + } + + public void setVelocity(Vector3 velocity) + { + this.velocity = velocity; + } /* @@ -218,13 +222,7 @@ namespace nhengine.Audio AL.DeleteSource(_source); } - public void printInfo() - { - Console.WriteLine("<<<>>>"); - Console.WriteLine("Name:" + _name); - Console.WriteLine("Buffer:" + _buffer); - Console.WriteLine("-----------"); - } + } diff --git a/org.niclasundharald.engine/graphics/GLCamera.cs b/org.niclasundharald.engine/graphics/GLCamera.cs index bb3ff44..152fbdb 100644 --- a/org.niclasundharald.engine/graphics/GLCamera.cs +++ b/org.niclasundharald.engine/graphics/GLCamera.cs @@ -3,6 +3,7 @@ using OpenTK.Graphics.OpenGL; using OpenTK.Graphics; using OpenTK; +using nhengine.Audio; namespace org.niclasundharald.engine.graphics { @@ -15,6 +16,8 @@ namespace org.niclasundharald.engine.graphics vView, vTop; + + public GLCamera() { fov = MathHelper.PiOver2; @@ -53,18 +56,18 @@ namespace org.niclasundharald.engine.graphics public Vector3 Position { get { return new Vector3(vPosition); } - set { this.vPosition = new Vector3(value); buildMatrices(); } + set { this.vPosition = new Vector3(value); buildMatrices(); setListener();} } public Vector3 View { get { return new Vector3(vView); } - set { this.vView = new Vector3(value).Normalized(); buildMatrices(); } + set { this.vView = new Vector3(value).Normalized(); buildMatrices(); setListener(); } } public Vector3 Top { get { return new Vector3(vTop); } - set { this.vTop = new Vector3(value).Normalized(); buildMatrices(); } + set { this.vTop = new Vector3(value).Normalized(); buildMatrices(); setListener();} } private void buildMatrices(){ @@ -87,6 +90,14 @@ namespace org.niclasundharald.engine.graphics _mCamera = mTranslation * mRotation; } + private void setListener() + { + nhListener.setPosition(vPosition); + nhListener.setOrientation(vTop, vView); + + } + + } } diff --git a/org.niclasundharald.engine/org.niclasundharald.engine.csproj b/org.niclasundharald.engine/org.niclasundharald.engine.csproj index 1882432..60b7654 100644 --- a/org.niclasundharald.engine/org.niclasundharald.engine.csproj +++ b/org.niclasundharald.engine/org.niclasundharald.engine.csproj @@ -68,11 +68,17 @@ + + + + + + @@ -81,6 +87,7 @@ + \ No newline at end of file