1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using Jypeli; |
---|
4 | using Jypeli.Assets; |
---|
5 | using Jypeli.Controls; |
---|
6 | using Jypeli.Effects; |
---|
7 | using Jypeli.Widgets; |
---|
8 | |
---|
9 | |
---|
10 | class AudioPlayer |
---|
11 | { |
---|
12 | private G game; |
---|
13 | private int lastStartIndex = -1; |
---|
14 | |
---|
15 | private Timer tick = new Timer(); |
---|
16 | |
---|
17 | private List<String> types = new List<String>(); |
---|
18 | private List<int> max = new List<int>(); |
---|
19 | |
---|
20 | public AudioPlayer(G game, int cInterval) |
---|
21 | { |
---|
22 | this.game = game; |
---|
23 | tick.Interval = cInterval; |
---|
24 | } |
---|
25 | |
---|
26 | public bool RegisterType(String type, int maxValue) |
---|
27 | { |
---|
28 | if (maxValue < 0 || type == "") |
---|
29 | return false; |
---|
30 | |
---|
31 | types.Add(type); |
---|
32 | max.Add(maxValue); |
---|
33 | |
---|
34 | return true; |
---|
35 | } |
---|
36 | |
---|
37 | public bool Play(String type, bool force) |
---|
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; |
---|
52 | } |
---|
53 | |
---|
54 | public void Stop() |
---|
55 | { |
---|
56 | game.MediaPlayer.Stop(); |
---|
57 | } |
---|
58 | |
---|
59 | public void PlayOne(String resource, bool stopAfterThis) |
---|
60 | { |
---|
61 | if (stopAfterThis) |
---|
62 | tick.Stop(); |
---|
63 | |
---|
64 | game.MediaPlayer.Stop(); |
---|
65 | game.MediaPlayer.Play(resource); |
---|
66 | } |
---|
67 | |
---|
68 | public void SetCheckInterval(double interval) |
---|
69 | { |
---|
70 | tick.Interval = interval; |
---|
71 | } |
---|
72 | |
---|
73 | private void Player(string type, int max) |
---|
74 | { |
---|
75 | if (!game.MediaPlayer.IsPlaying) |
---|
76 | { |
---|
77 | lastStartIndex = RandomGen.NextInt(0, max + 1); |
---|
78 | game.MediaPlayer.Play(type + lastStartIndex); |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | #region Console |
---|
83 | |
---|
84 | private void Print(String txt) |
---|
85 | { |
---|
86 | game.MessageDisplay.Add(txt); |
---|
87 | } |
---|
88 | |
---|
89 | public bool Console(String command) |
---|
90 | { |
---|
91 | if (command.Length < 0) |
---|
92 | return false; |
---|
93 | |
---|
94 | String[] commands = command.Split(' '); |
---|
95 | if (commands[0] == "play") |
---|
96 | { |
---|
97 | if (commands.Length < 2) |
---|
98 | return false; |
---|
99 | |
---|
100 | bool force = true; |
---|
101 | if (commands.Length > 2) |
---|
102 | if (commands[2] == "!force") |
---|
103 | force = true; |
---|
104 | |
---|
105 | if (Play(commands[1], force)) |
---|
106 | Print("Playing " + commands[1] + " #" + lastStartIndex); |
---|
107 | else |
---|
108 | Print("Type \"" + commands[1] + "\" is not registered"); |
---|
109 | } |
---|
110 | else if (commands[0] == "playone") |
---|
111 | { |
---|
112 | if (commands.Length < 2) |
---|
113 | return false; |
---|
114 | |
---|
115 | bool stop = false; |
---|
116 | if (commands.Length > 2) |
---|
117 | if (commands[2] == "stop") |
---|
118 | stop = true; |
---|
119 | |
---|
120 | PlayOne(commands[1], stop); |
---|
121 | Print("Playing " + commands[1]); |
---|
122 | } |
---|
123 | else if (commands[0] == "register") |
---|
124 | { |
---|
125 | if (commands.Length < 2) |
---|
126 | return false; |
---|
127 | |
---|
128 | if (commands[1] == "list") |
---|
129 | { |
---|
130 | if (types.Count > 0) |
---|
131 | { |
---|
132 | Print("Registered types: "); |
---|
133 | foreach (string type in types) |
---|
134 | Print(type); |
---|
135 | } |
---|
136 | else { Print("No registered types"); } |
---|
137 | } |
---|
138 | } |
---|
139 | else |
---|
140 | { |
---|
141 | return false; |
---|
142 | } |
---|
143 | |
---|
144 | return true; |
---|
145 | } |
---|
146 | |
---|
147 | #endregion |
---|
148 | } |
---|