Skip to content

Commit

Permalink
Add Sound support
Browse files Browse the repository at this point in the history
  • Loading branch information
greggman committed Jul 26, 2015
1 parent ed986ca commit ad3f1e0
Show file tree
Hide file tree
Showing 27 changed files with 524 additions and 2 deletions.
Binary file not shown.
8 changes: 8 additions & 0 deletions Assets/HappyFunTimes/Samples/Prefabs/SoundTester.prefab.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
8 changes: 8 additions & 0 deletions Assets/HappyFunTimes/Samples/Prefabs/TestPlayer.prefab.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
8 changes: 8 additions & 0 deletions Assets/HappyFunTimes/Samples/Scenes/SoundTestScene.unity.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions Assets/HappyFunTimes/Samples/Scripts/ControllerTestScript.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ControllerTestScript : MonoBehaviour {

private int m_id;
private HFTGamepad m_gamepad;
private static Dictionary<int, bool> s_ids= new Dictionary<int, bool>();
// private HFTInput m_hftInput;

void Start ()
{
// Find an empty id;
bool foo = false;
for (int ii = 0; ii < 1000; ++ii) {
if (!s_ids.TryGetValue(ii, out foo)) {
m_id = ii;
s_ids[ii] = true;
break;
}
}

m_gamepad = GetComponent<HFTGamepad>();
// m_hftInput = GetComponent<HFTInput>();
m_gamepad.NetPlayer.OnDisconnect += OnDisconnect;
}

void OnDisconnect(object sender, System.EventArgs args)
{
s_ids.Remove(m_id);
}

void OnGUI()
{
int unitWidth = 20;
int unitHeight = 20;
int xx = 10 + 110 * m_id;
int yy = 10;
GUI.Box(new Rect(xx, 10, 100, unitHeight), m_gamepad.Name);
yy += unitHeight;
GUI.Box(new Rect(xx, yy, 100, unitHeight), "buttons");
yy += unitHeight;
for (int ii = 0; ii < m_gamepad.buttons.Length; ++ii) {
int x = ii % 4;
int y = ii / 4;
GUI.Box(new Rect(xx + x * unitWidth, yy + y * unitHeight, unitWidth, unitHeight), m_gamepad.buttons[ii].pressed ? "*" : "");
}
yy += unitHeight * ((m_gamepad.buttons.Length + 3) / 4);

GUI.Box(new Rect(xx, yy, 100, unitHeight), "axes");
yy += unitHeight;
for (int ii = 0; ii < m_gamepad.axes.Length; ++ii) {
int x = ii % 4;
int y = ii / 4;
GUI.Box(new Rect(xx + x * unitWidth, yy + y * unitHeight, unitWidth, unitHeight), m_gamepad.axes[ii].ToString());
}

}
}
12 changes: 12 additions & 0 deletions Assets/HappyFunTimes/Samples/Scripts/ControllerTestScript.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions Assets/HappyFunTimes/Samples/Scripts/SoundTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using UnityEngine;
using System.Collections;

public class SoundTest : MonoBehaviour {

void Awake ()
{
m_soundPlayer = GetComponent<HFTSoundPlayer>();
}

void Start()
{
StartCoroutine("Play");
}

IEnumerator Play()
{
while (true)
{
yield return new WaitForSeconds(1.0f);
m_soundPlayer.PlaySound("explosion");
yield return new WaitForSeconds(1.0f);
m_soundPlayer.PlaySound("gameover");
yield return new WaitForSeconds(1.0f);
m_soundPlayer.PlaySound("launch");
}
}

private HFTSoundPlayer m_soundPlayer;
}
12 changes: 12 additions & 0 deletions Assets/HappyFunTimes/Samples/Scripts/SoundTest.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions Assets/HappyFunTimes/Scripts/HFTGlobalSoundHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using HappyFunTimes;
using HFTSounds;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;

public class HFTGlobalSoundHelper : MonoBehaviour {

public static Sounds GetSounds() {
return s_sounds;
}

void Awake()
{
if (s_sounds == null) {
InitSounds();
}
}

void InitSounds()
{
s_sounds = new Sounds();
string baseFolder = Path.Combine(Path.Combine(Application.dataPath, "WebPlayerTemplates"), "HappyFunTimes");
string soundFolder = Path.Combine(baseFolder, "sounds");
if (Directory.Exists(soundFolder)) {
AddSoundFiles(baseFolder, Directory.GetFiles(soundFolder, "*.mp3"));
AddSoundFiles(baseFolder, Directory.GetFiles(soundFolder, "*.wav"));
}
}

void AddSoundFiles(string baseFolder, string[] filenames)
{
foreach(string filename in filenames)
{
string filepath = filename.Substring(baseFolder.Length + 1).Replace("\\", "/");
s_sounds[Path.GetFileNameWithoutExtension(filename)] = new SoundFile(filepath);
}
}

private static Sounds s_sounds = null;
};

12 changes: 12 additions & 0 deletions Assets/HappyFunTimes/Scripts/HFTGlobalSoundHelper.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions Assets/HappyFunTimes/Scripts/HFTSoundPlayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using HappyFunTimes;
using HFTSounds;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[RequireComponent (typeof (HFTGamepad))]
public class HFTSoundPlayer : MonoBehaviour {

private class MessageLoadSounds : MessageCmdData {
public MessageLoadSounds(Sounds _sounds) {
sounds = _sounds;
}
public Sounds sounds = null;
}

private class MessagePlaySound : MessageCmdData {
public MessagePlaySound(string _name, bool _loop = false) {
name = _name;
loop = _loop;
}
public string name;
public bool loop;
}

void Awake()
{
try {
HFTGlobalSoundHelper.GetSounds();
} catch (System.Exception) {
Debug.LogError("No HFTGlobalSoundHelper in scene. Please add one");
}
m_gamepad = GetComponent<HFTGamepad>();
}

void Start()
{
m_gamepad.NetPlayer.SendCmd("loadSounds", new MessageLoadSounds(HFTGlobalSoundHelper.GetSounds()));
}

public void PlaySound(string name, bool loop = false)
{
m_gamepad.NetPlayer.SendCmd("playSound", new MessagePlaySound(name, loop));
}

private HFTGamepad m_gamepad;

};

12 changes: 12 additions & 0 deletions Assets/HappyFunTimes/Scripts/HFTSoundPlayer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ad3f1e0

Please sign in to comment.