using UnityEngine;
using System.Collections;
using System.Xml;
/// <summary>
/// 主菜单场景
/// </summary>
public class MainMenu : MonoBehaviour {
Transform Werewolf;//狼人对象
Animation wolfAnimation;//动画组件
NavMeshAgent agent;//导航代理
float stopDistance;//制动距离
bool isNav;//是否开始导航
BgMusic bgMusic;//背景音乐管理器
string index;//读取存档索引
#region 系统设置窗体组件
Transform WinSetting;
UIPopupList RunModeUI;
UISlider SoundEffectsUI;
UILabel SoundEffectsLabel;
UISlider BgMusicUI;
UILabel BgMusicLabel;
UICheckbox JumpFontUI;
UICheckbox TopInfoUI;
UICheckbox WeatherSystemUI;
#endregion
#region 读取游戏窗体组件
Transform WinLoad;
Transform savePic;
UILabel character;
UILabel date;
UILabel level;
UILabel location;
UILabel savenum;
UILabel time;
#endregion
/// <summary>
/// 初始化所有组件
/// </summary>
void Start () {
//角色动画组件
Werewolf = GameObject.Find("Werewolf").transform;
wolfAnimation = Werewolf.animation;
wolfAnimation.wrapMode = WrapMode.Loop;
//导航组件
agent = Werewolf.GetComponent<NavMeshAgent>();
stopDistance = agent.stoppingDistance;
//背景音乐组件
bgMusic = GameObject.Find("MainUI").GetComponent<BgMusic>();
BgMusic.PlayMusic("MainMenu");
BgMusic.SetVolume(1f);
//初始化窗体组件
initSetting();
initLoadUI();
}
/// <summary>
/// 初始化系统设置窗体组件
/// </summary>
void initSetting(){
WinSetting = GameObject.Find("Win_Setting").transform;
RunModeUI = WinSetting.Find("Options/RunMode/Popup List").GetComponent<UIPopupList>();
SoundEffectsUI = WinSetting.Find("Options/SoundEffects/Slider").GetComponent<UISlider>();
SoundEffectsLabel = WinSetting.Find("Options/SoundEffects/Slider/Label").GetComponent<UILabel>();
BgMusicUI = WinSetting.Find("Options/BgMusic/Slider").GetComponent<UISlider>();
BgMusicLabel = WinSetting.Find("Options/BgMusic/Slider/Label").GetComponent<UILabel>();
JumpFontUI = WinSetting.Find("Options/JumpFont").GetComponent<UICheckbox>();
TopInfoUI = WinSetting.Find("Options/TopInfo").GetComponent<UICheckbox>();
WeatherSystemUI = WinSetting.Find("Options/WeatherSystem").GetComponent<UICheckbox>();
BgMusicUI.sliderValue = 0.5f;
SoundEffectsUI.sliderValue = 0.5f;
BgMusicLabel.text = (int)(BgMusicUI.sliderValue * 100) + "%";
SoundEffectsLabel.text = (int)(SoundEffectsUI.sliderValue * 100) + "%";
WinSetting.gameObject.SetActive(false);
}
/// <summary>
/// 初始化读取游戏窗体组件
/// </summary>
public void initLoadUI(){
WinLoad = GameObject.Find("MainUI/Camera/Anchor/Win_Load").transform;
savePic = WinLoad.Find("Panel2/SavePic");
character = WinLoad.Find("Panel2/Labels/character").GetComponent<UILabel>();
date = WinLoad.Find("Panel2/Labels/date").GetComponent<UILabel>();
level = WinLoad.Find("Panel2/Labels/level").GetComponent<UILabel>();
location = WinLoad.Find("Panel2/Labels/location").GetComponent<UILabel>();
savenum = WinLoad.Find("Panel2/Labels/savenum").GetComponent<UILabel>();
time = WinLoad.Find("Panel2/Labels/time").GetComponent<UILabel>();
WinLoad.gameObject.SetActive(false);
}
/// <summary>
/// 到达摄像机前,播放动画后,开始游戏
/// </summary>
void Update () {
float distance = agent.remainingDistance;
if(isNav && distance != 0 && distance <= stopDistance){
isNav = false;
agent.Stop();
wolfAnimation.wrapMode = WrapMode.Once;
wolfAnimation.Play("Attack");
StartCoroutine(delayStart());
}
}
/// <summary>
/// 开始延迟,跳转到角色选择场景
/// </summary>
IEnumerator delayStart(){
yield return new WaitForSeconds(2f);
Application.LoadLevel("Character");
}
#region 主菜单按钮事件
/// <summary>
/// 开始游戏事件
/// </summary>
public void StartGame(){
wolfAnimation.Play("Walk");
Vector3 dest = GameObject.Find("Main Camera").transform.position;
agent.SetDestination(dest);
isNav = true;
}
/// <summary>
/// 打开系统设置窗体
/// </summary>
public void OpenSetting(){
if(!WinSetting.gameObject.activeSelf){
WinLoad.gameObject.SetActive(false);
WinSetting.gameObject.SetActive(true);
}else{
WinSetting.gameObject.SetActive(false);
}
}
/// <summary>
/// 打开读取存档窗体
/// </summary>
public void OpenLoading(){
if(!WinLoad.gameObject.activeSelf){
WinLoad.gameObject.SetActive(true);
WinSetting.gameObject.SetActive(false);
clearRecord();
}else{
WinLoad.gameObject.SetActive(false);
}
}
/// <summary>
/// 退出游戏
/// </summary>
public void ExitGame(){
Application.Quit();
}
#endregion
#region 读取记录函数
/// <summary>
/// 读取存档记录,并显示到UI上
/// </summary>
void LoadRecord(string index){
XmlNodeList recordList = Record.LoadRecord(index);
if(recordList != null){
this.index = index;
string imageName = recordList.Item(1).InnerText;
if(imageName != null && imageName.Length > 0){
string path = "file://" + Application.dataPath + "/StreamingAssets/Xml/Persistence/Save/" + imageName + ".png";
WWW www = new WWW(path);
Texture image = www.texture;
savePic.renderer.material.mainTexture = image;
character.text = recordList.Item(2).InnerText;
date.text = recordList.Item(3).InnerText;
level.text = recordList.Item(4).InnerText;
location.text = recordList.Item(5).InnerText;
savenum.text = recordList.Item(6).InnerText;
time.text = recordList.Item(7).InnerText;
}else{
clearRecord();
}
}
}
/// <summary>
/// 选择记录,加载存档记录
/// </summary>
public void LoadGame(){
if(index != null){
Record.RecordName = index;
PlayerPrefs.SetString("LevelName","LevelName");
Application.LoadLevel("Load");
}else{
UIMessageBox.ShowMessage("请 选 择 有 效 的 存 档",3f);
}
}
/// <summary>
/// 清空UI存档记录
/// </summary>
public void clearRecord(){
savePic.renderer.material.mainTexture = null;
character.text = "";
date.text = "";
level.text = "";
location.text = "";
savenum.text = "";
time.text = "";
index = null;
}
public void LoadRecord1(){LoadRecord("01");}
public void LoadRecord2(){LoadRecord("02");}
public void LoadRecord3(){LoadRecord("03");}
public void LoadRecord4(){LoadRecord("04");}
public void LoadRecord5(){LoadRecord("05");}
public void LoadRecord6(){LoadRecord("06");}
public void LoadRecord7(){LoadRecord("07");}
public void LoadRecord8(){LoadRecord("08");}
#endregion
#region 系统设置函数
/// <summary>
/// 保存系统设置
/// </summary>
public void SaveSetting(){
Setting setting = new Setting();
setting.TopInfoEff = TopInfoUI.isChecked;
setting.JumpFontEff = JumpFontUI.isChecked;
setting.WeatherEff = WeatherSystemUI.isChecked;
setting.Window = (RunModeUI.selection == "Window");
setting.BgMusicVolume = BgMusicUI.sliderValue;
setting.SoundEffectsVolume = SoundEffectsUI.sliderValue;
Persistence.Save("Setting",setting);
}
/// <summary>
/// 设置背景音量
/// </summary>
public void SetVolumeBg(){
if(BgMusicUI){
float BgMusicVolume = BgMusicUI.sliderValue;
BgMusicLabel.text = (int)(BgMusicVolume * 100) + "%";
BgMusic.SetVolume(BgMusicVolume);
}
}
/// <summary>
/// 设置音效音量
/// </summary>
public void SetVolumeEff(){
if(SoundEffectsUI)
SoundEffectsLabel.text = (int)(SoundEffectsUI.sliderValue * 100) + "%";
}
#endregion
}