Changeset 4529
- Timestamp:
- 2013-07-22 15:01:25 (10 years ago)
- Location:
- 2013/27/TeemuM/Game/Game/Game
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
2013/27/TeemuM/Game/Game/Game/AudioPlayer.cs
r4484 r4529 1 1 using System; 2 using System.Collections; 2 3 using System.Collections.Generic; 3 4 using Jypeli; … … 11 12 { 12 13 private G game; 13 private int lastStartIndex = -1;14 private string currentType; 14 15 15 private Timer tick = new Timer(); 16 Timer tick; 17 Hashtable lengths = new Hashtable(); 16 18 17 private List<String> types = new List<String>(); 18 private List<int> max = new List<int>(); 19 20 public AudioPlayer(G game, int cInterval) 19 public AudioPlayer(G game, int updateInterval) 21 20 { 22 this.game = game; 23 tick.Interval = cInterval; 21 Timer tick = new Timer(); 22 tick.Interval = updateInterval; 23 tick.Timeout += delegate() { UpdateAudio(); }; 24 24 } 25 25 26 public bool RegisterType(String type, int maxValue) 26 #region Length register managment 27 28 public bool Register(string type, int length) 27 29 { 28 if ( maxValue < 0 || type == "")30 if (lengths.ContainsKey(type)) 29 31 return false; 30 32 31 types.Add(type); 32 max.Add(maxValue); 33 33 lengths[type] = length; 34 34 return true; 35 35 } 36 36 37 public bool Play(String type, bool force)37 public bool IsRegistered(string type) 38 38 { 39 for (int i = 0; i < types.Count; i++) 40 if (types[i].ToLower() == type.ToLower()) 41 { 42 if (force) 43 game.MediaPlayer.Stop(); 44 45 tick.Timeout += delegate() { Player(types[i], max[i]); }; 46 tick.Start(); 47 Player(types[i], max[i]); 48 return true; 49 } 50 51 return false; 39 if (lengths.ContainsKey(type)) 40 return true; 41 else 42 return false; 52 43 } 53 44 54 public bool PlayEffect(String type)45 public void Unregister(string type) 55 46 { 56 for (int i = 0; i < types.Count; i++) 57 if (types[i].ToLower() == type.ToLower()) 58 { 59 Game.LoadSoundEffect(type + RandomGen.NextInt(0, max[i])).Play(); 60 return true; 61 } 47 if (lengths.ContainsKey(type)) 48 lengths.Remove(type); 49 } 62 50 63 return false; 51 #endregion 52 53 #region Controls 54 55 public bool Play(string type) 56 { 57 if (!lengths.ContainsKey(type)) 58 return false; 59 60 currentType = type; 61 tick.Start(); 62 return true; 64 63 } 65 64 66 65 public void Stop() 67 66 { 67 tick.Stop(); 68 68 game.MediaPlayer.Stop(); 69 69 } 70 70 71 public void PlayOne(String resource, bool stopAfterThis) 71 #endregion 72 73 private void UpdateAudio() 72 74 { 73 if (stopAfterThis) 74 tick.Stop(); 75 76 game.MediaPlayer.Stop(); 77 game.MediaPlayer.Play(resource); 75 if (!game.MediaPlayer.IsPlaying) 76 game.MediaPlayer.Play(currentType + RandomGen.NextInt(0, (int)lengths[currentType])); 78 77 } 79 78 80 public void SetCheckInterval(double interval)79 public void Console(String command) 81 80 { 82 tick.Interval = interval; 83 } 81 String[] commands = command.Split(' '); 82 83 if (commands.Length < 1) 84 return; 84 85 85 private void Player(string type, int max)86 {87 if (!game.MediaPlayer.IsPlaying)88 {89 lastStartIndex = RandomGen.NextInt(0, max + 1);90 game.MediaPlayer.Play(type + lastStartIndex);91 }92 }93 94 #region Console95 96 private void Print(String txt)97 {98 game.MessageDisplay.Add(txt);99 }100 101 public bool Console(String command)102 {103 if (command.Length < 0)104 return false;105 106 String[] commands = command.Split(' ');107 86 if (commands[0] == "play") 108 87 { 109 88 if (commands.Length < 2) 110 return false;89 return; 111 90 112 bool force = true; 113 if (commands.Length > 2) 114 if (commands[2] == "!force") 115 force = true; 116 117 if (Play(commands[1], force)) 118 Print("Playing " + commands[1] + " #" + lastStartIndex); 119 else 120 Print("Type \"" + commands[1] + "\" is not registered"); 91 Play(commands[1]); 121 92 } 122 else if (commands[0] == " playone")93 else if (commands[0] == "stop") 123 94 { 124 if (commands.Length < 2) 125 return false; 126 127 bool stop = false; 128 if (commands.Length > 2) 129 if (commands[2] == "stop") 130 stop = true; 131 132 PlayOne(commands[1], stop); 133 Print("Playing " + commands[1]); 95 Stop(); 96 game.MessageDisplay.Add("Audio: Stopped"); 134 97 } 135 98 else if (commands[0] == "register") 136 99 { 137 if (commands.Length < 2)138 return false;100 if (commands.Length < 3) 101 return; 139 102 140 if (commands[1] == "list") 141 { 142 if (types.Count > 0) 143 { 144 Print("Registered types: "); 145 foreach (string type in types) 146 Print(type); 147 } 148 else { Print("No registered types"); } 149 } 103 int length = 0; 104 int.TryParse(commands[2], out length); 105 106 if (Register(commands[1], length)) 107 game.MessageDisplay.Add("Audio: " + commands[1] + " registered"); 108 else 109 game.MessageDisplay.Add("Audio: " + commands[1] + " is already registered, please unregister first"); 150 110 } 151 else 111 else if (commands[0] == "effect") 152 112 { 153 return false; 113 154 114 } 155 156 return true;157 115 } 158 159 #endregion160 116 } -
2013/27/TeemuM/Game/Game/Game/Game.cs
r4498 r4529 13 13 { 14 14 audio = new AudioPlayer(this, 3); 15 16 audio.RegisterType("Background", 4);17 audio.RegisterType("Enemyspawn", 0);18 audio.RegisterType("GameOverSound", 0);19 audio.RegisterType("MenuMusic", 0);20 audio.RegisterType("RandomSound", 0);21 audio.RegisterType("ZombieBite", 0);22 audio.RegisterType("Zombiesound", 2);23 15 } 24 16 … … 46 38 menu.AddItemHandler(2, Exit); 47 39 Add(menu); 48 audio.Play("MenuMusic", true);49 40 } 50 41 … … 75 66 else if (commands[0] == "lol") 76 67 { 77 audio.PlayOne("EasterEgg", false); 68 78 69 } 79 70 else if (commands[0] == "score") … … 202 193 AddScoreMeter(); 203 194 CreateLifeMeter(); 204 205 audio.Play("Background", true);206 195 207 196 Timer t = new Timer(); … … 324 313 ClearControls(); 325 314 Keyboard.Listen(Key.Enter, ButtonState.Pressed, delegate() { MainMenu(); }, null); 326 audio.Play("GameOverSound", true);327 315 GameObject layer = new GameObject(Game.Screen.Width + 100, Game.Screen.Height + 100); 328 316 layer.Color = Color.Transparent;
Note: See TracChangeset
for help on using the changeset viewer.