Skip to content

Commit

Permalink
Music playing and syncing
Browse files Browse the repository at this point in the history
  • Loading branch information
David20321 committed Nov 19, 2012
1 parent 9742fab commit 66511c5
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 5 deletions.
Binary file modified FTJ Project/Assets/Scenes/game_scene.unity
Binary file not shown.
8 changes: 4 additions & 4 deletions FTJ Project/Assets/Scripts/NetEventScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ public enum Type{SERVER_INITIALIZED,
public Type type(){return type_;}
virtual public NetworkConnectionError error(){
throw new System.InvalidOperationException("error() should only be called on NetErrorEvent");
return NetworkConnectionError.NoError;
//return NetworkConnectionError.NoError;
}
virtual public MasterServerEvent master_server_event(){
throw new System.InvalidOperationException("master_server_event() should only be called on NetMasterServerEvent");
return MasterServerEvent.RegistrationSucceeded;
//return MasterServerEvent.RegistrationSucceeded;
}
virtual public NetworkPlayer network_player(){
throw new System.InvalidOperationException("network_player() should only be called on NetPlayerEvent");
return new NetworkPlayer();
//return new NetworkPlayer();
}
virtual public NetworkDisconnection network_disconnection(){
throw new System.InvalidOperationException("network_disconnection() should only be called on NetDisconnectionEvent");
return NetworkDisconnection.Disconnected;
//return NetworkDisconnection.Disconnected;
}
public NetEvent(Type type){
type_ = type;
Expand Down
95 changes: 94 additions & 1 deletion FTJ Project/Assets/Scripts/NetUIScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ enum State {NONE, MAIN_MENU, CREATE, CREATING, CREATE_FAIL,
State state_ = State.MAIN_MENU;
const string DEFAULT_GAME_NAME = "Unnamed Game";
const string DEFAULT_PLAYER_NAME = "Unknown Player";
const string GAME_IDENTIFIER = "WolfireFTJGamev38";
const string GAME_IDENTIFIER = "WolfireFTJGamev41";
const int DEFAULT_PORT = 25000;
const int MAX_PLAYERS = 4;
const int MAX_CONNECTIONS = MAX_PLAYERS-1;
Expand All @@ -24,7 +24,12 @@ enum State {NONE, MAIN_MENU, CREATE, CREATING, CREATE_FAIL,
public GameObject token_prefab;
public GUISkin help_gui_skin;
public AudioClip[] chat_sounds;
public AudioClip[] songs;
AudioSource music_;
int current_song_ = -1;
int target_song_ = -1;
int first_state_ui_update = 0;
const float MAX_SONG_VOLUME = 0.4f;
List<GameObject> play_areas = new List<GameObject>();

string queued_join_game_name_ = "";
Expand All @@ -36,6 +41,56 @@ enum State {NONE, MAIN_MENU, CREATE, CREATING, CREATE_FAIL,
void Start() {
RequestPageURLForAutoJoin();
//TryToCreateGame(true);
music_ = gameObject.AddComponent<AudioSource>();
music_.volume = 0.0f;
}

[RPC]
void TargetSongWasSet(int player, int song){
string song_name = "";
switch(song){
case -1:
song_name = "silence"; break;
case 0:
song_name = "\"Forest\""; break;
case 1:
song_name = "\"Dungeon\""; break;
case 2:
song_name = "\"HellGate\""; break;
case 3:
song_name = "\"The Grim\""; break;
}
var player_info_list = PlayerListScript.Instance().GetPlayerInfoList();
string player_name = "You";
if(player_info_list.ContainsKey(player)){
player_name = player_info_list[player].name_;
}
ConsoleScript.Log(player_name + " changed song to " + song_name);
target_song_ = song;
}

[RPC]
void SyncSongWithServer(int player, int current_song, int target_song, float volume, float time){
if(player != Net.GetMyID()){
return;
}
current_song_ = current_song;
target_song_ = target_song;
if(current_song_ != -1){
music_.clip = songs[current_song_];
}
music_.Play();
music_.volume = volume;
music_.time = time;
}


void SetTargetSong(int which) {
if(Network.connections.Length > 0){
networkView.RPC("TargetSongWasSet", RPCMode.All, Net.GetMyID(),which);
} else {
TargetSongWasSet(Net.GetMyID(),which);
}
}

void SpawnHealthTokens() {
Expand Down Expand Up @@ -166,6 +221,23 @@ void NetEventDisconnectedFromServer(NetEvent net_event) {
}

void Update() {
if(current_song_ != target_song_){
if(music_.volume == 0.0f){
current_song_ = target_song_;
if(current_song_ != -1){
music_.clip = songs[current_song_];
} else {
music_.Stop();
}
} else {
music_.volume = Mathf.Max(0.0f, music_.volume - Time.deltaTime);
}
} else if(current_song_ != -1){
music_.volume = Mathf.Min(MAX_SONG_VOLUME, music_.volume + Time.deltaTime);
}
if(!music_.isPlaying){
music_.Play();
}
NetEvent net_event = NetEventScript.Instance().GetEvent();
while(net_event != null){
switch(net_event.type()){
Expand All @@ -186,6 +258,7 @@ void Update() {
break;
case NetEvent.Type.PLAYER_CONNECTED:
ConsoleScript.Log("Player "+net_event.network_player()+" connected");
networkView.RPC("SyncSongWithServer",RPCMode.Others,int.Parse(net_event.network_player().ToString()), current_song_, target_song_, music_.volume, music_.time);
break;
case NetEvent.Type.PLAYER_DISCONNECTED:
NetEventPlayerDisconnected(net_event);
Expand Down Expand Up @@ -341,6 +414,23 @@ void OnGUI() {
break;
}
++first_state_ui_update;

if (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Alpha1) {
SetTargetSong(-1);
}
if (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Alpha2) {
SetTargetSong(0);
}
if (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Alpha3) {
SetTargetSong(1);
}
if (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Alpha4) {
SetTargetSong(2);
}
if (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Alpha5) {
SetTargetSong(3);
}

}

void DrawGameGUI() {
Expand Down Expand Up @@ -428,6 +518,9 @@ void DrawGameGUI() {
GUILayout.BeginHorizontal();
GUILayout.Label("Press 'RETURN' to chat", help_gui_skin.label);
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Press '1-5' to play different songs", help_gui_skin.label);
GUILayout.EndHorizontal();
}
GUILayout.EndArea();

Expand Down

0 comments on commit 66511c5

Please sign in to comment.