using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace WildAttack
{
///
/// 射击类型枚举
///
public enum ShootTypeEnum
{
FixedScreen = 1,
FixedCrossHair = 2,
moreScreen = 3,// 超出范围 调整相机
}
///
/// 游戏管理器
///
public class GameMananger : Singleton
{
#region Members
private int hp = 0;
public int HP { get { return hp; } }
private int hpMax = 0;
public int HPMax { get { return hpMax; } }
private int totalScore = 0;
private int uploadScore = 0;
public int TotalScore { get { return totalScore; } }
public int barrelTriggerCount = 0;
private ShootTypeEnum typeEnum = ShootTypeEnum.FixedScreen;
// 原始旋转
Vector3 originalBowRotation;
// 弓箭旋转对应的对象 对弓箭的操作体现在这个obj上
Transform CrossHairTrans;
// 射箭速度
private float smartBowSpeed;
private float testSpeed;
// 语言类型 配合csv
private LanguageType languageType;
public LanguageType Language { get { return languageType; } }
// 相机初始旋转信息位置信息
private Vector3 camOriEular;
private Vector3 camOriPos;
// 游戏结束时 相机目标点
private Transform gameOverTrans;
// 弓
private Bow bow;
// 城门动画控制
private Animator wallAnim;
// 特效
private ParticleSystem addHpParticle;
private ParticleSystem failParticle;
private ParticleSystem victoryParticle;
//触摸检测器
JCUnityLib.TouchChecker touchChecker = new JCUnityLib.TouchChecker();
UserGameAnalyse1 userGameAnalyse1;
//游戏计算的分数只有射箭出来算
bool bAddCountScore = false;
#endregion
#region Function
public void Init(UserGameAnalyse1 _userGameAnalyse1)
{
userGameAnalyse1 = _userGameAnalyse1;
camOriEular = Camera.main.transform.eulerAngles;
camOriPos = Camera.main.transform.position;
gameOverTrans = GameObject.Find("GameOver").transform;
bow = GameObject.Find("Bow").GetComponent();
languageType = LanguageType.CH;
if (TextAutoLanguage2.GetLanguage() == LanguageEnum.English) languageType = LanguageType.EN;
CrossHairTrans = GameObject.Find("CrossHairTrans").transform;
originalBowRotation = CrossHairTrans.eulerAngles;
wallAnim = GameObject.Find("Wall").GetComponent();
hpMax = Mathf.FloorToInt(GameModule.GetInstance().GetData("wallHp"));
hp = hpMax;
totalScore = 0;
uploadScore = 0;
barrelTriggerCount = 0;
//shootSpawn = GameModule.GetInstance().GetData("downTime");
shootSpawn = 0;
smartBowSpeed = GameModule.GetInstance().GetData("speedTimes");
testSpeed = GameModule.GetInstance().GetData("testspeed");
Transform particleTrans = GameObject.Find("finalPos/ParticleTrans").transform;
addHpParticle = particleTrans.Find("addhp").GetChild(0).GetComponent();
failParticle = particleTrans.Find("fail").GetChild(0).GetComponent();
victoryParticle = particleTrans.Find("victory").GetChild(0).GetComponent();
Physics.autoSyncTransforms = true;
InitForLimitBound();
InitTouchChecker();
}
///
/// 射击类型相关接口
///
///
public ShootTypeEnum GetShootType()
{
return typeEnum;
}
public void SetShootType(ShootTypeEnum type)
{
this.typeEnum = type;
if (this.typeEnum == ShootTypeEnum.FixedScreen)
{
bow.transform.Find("CameraParent").DetachChildren();
GameObject.Find("UIBoxLeft").GetComponent().blocksRaycasts = true;
}
else if (this.typeEnum == ShootTypeEnum.FixedCrossHair)
{
CrossHair.instance._rectTransform.anchoredPosition = new Vector2(0, 0);
Camera.main.transform.SetParent(bow.transform.Find("CameraParent"));
}
else if (this.typeEnum == ShootTypeEnum.moreScreen)
{
bow.transform.Find("CameraParent").DetachChildren();
GameObject.Find("UIBoxLeft").GetComponent().blocksRaycasts = false;
}
}
///
/// 游戏是否结束
///
public bool isOver = false;
///
/// 胜利失败
///
public bool isWin;
///
/// 游戏结束
///
///
public void GameOver(bool isWin)
{
isOver = true;
// 血量回复
this.hp = isWin ? hp : hpMax;
this.isWin = isWin;
// 城门对应动画
wallAnim.SetBool(isWin ? "open" : "dead", true);
AddScore(StagePropertyModule.GetInstance().GetData(1).score * (6 - barrelTriggerCount));
// 特效
if (isWin)
{
failParticle.Stop();
victoryParticle.Play();
}
else
{
failParticle.Play();
victoryParticle.Stop();
// 失败时有城门破坏音效
AudioSource ads = GameObject.Find("ProcessAudioSource").GetComponent();
ads.clip = Resources.Load("Process/Broken");
if (UserSettings.ins.openEffect) ads.Play();
}
//关卡结束时候触发
OnUploadScore();
// 调用其他管理类的GameOver
ProcessManager.GetInstance().GameOver(false);
if (!isWin) EnemyManager.GetInstance().GameOver();
UIManager.GetInstance().HideMainPanel();
}
//荒野射击上传分数到服务器
public void OnUploadScore() {
if (uploadScore > 0)
{
//游戏结束上传一个分数
Debug.Log("荒野射击上传的积分为:" + uploadScore);
RankComp.Instance.uploadSinglePlayerGameRes(uploadScore);
uploadScore = 0;
}
}
///
/// 血量
///
///
public void SubHp(int hp)
{
this.hp -= hp;
UIManager.GetInstance().SubHp();
if (this.hp <= 0)
{
GameOver(false);
}
}
public void AddHp(int hp)
{
this.hp += hp;
if (hp != (int)GameModule.GetInstance().GetData("wallHp"))
{
addHpParticle.Play();
}
if (this.hp > hpMax)
{
this.hp = hpMax;
}
// 血量重置
if (hp == hpMax)
{
UIManager.GetInstance().ResetHpBar();
}
// ui更新血条
UIManager.GetInstance().AddHp();
}
///
/// 计分
///
///
public void AddScore(int score)
{
//在射箭状态下触发的才计算分数,手点触发射箭后不计算分数
if (!bAddCountScore) return;
totalScore += score;
//只记录一个上传的分数
uploadScore += score;
}
public void SetScore(int score)
{
totalScore = score;
barrelTriggerCount = 0;
}
// Update is called once per frame
public void Update()
{
bow.gameObject.SetActive(!isOver);
// BGM
if (!Camera.main.GetComponent().isPlaying)
{
if (UserSettings.ins.openBGM)
Camera.main.GetComponent().Play();
}
else {
//如果正在播放
if (!UserSettings.ins.openBGM)
Camera.main.GetComponent().Stop();
}
// 设计间隔
if (shootSpawn > 0)
{
shootSpawn -= Time.deltaTime;
}
// unity调试用
#if UNITY_EDITOR
#region pc端操作
if (Input.GetKeyDown(KeyCode.N))
{
AddHp(5);
}
if (Input.GetKeyDown(KeyCode.M))
{
SubHp(5);
}
if (Input.GetKeyDown(KeyCode.P))
{
foreach (var item in EnemyManager.GetInstance().enemyList)
{
item.Hitted(4, false);
}
}
UpdateBowUnityEditor();
if (Input.GetKeyDown(KeyCode.Space))
{
ShootOut(testSpeed);
}
if (Input.GetKeyDown(KeyCode.I))
{
GameOver(true);
}
if (Input.GetKeyDown(KeyCode.O))
{
GameOver(false);
}
#endregion
#endif
if (BowCamera.isTouchMode) touchChecker.Update();
// 游戏结束后相机移动到对应位置
if (isOver)
{
Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position, gameOverTrans.position, 2 * Time.deltaTime);
Camera.main.transform.eulerAngles = Vector3.Lerp(Camera.main.transform.eulerAngles, gameOverTrans.eulerAngles, 2 * Time.deltaTime);
// 到达目标点显示ui
if (Vector3.Distance(Camera.main.transform.position, gameOverTrans.position) < 0.01f)
{
UIManager.GetInstance().GameOver(isWin);
InitForLimitBound();
}
}
}
///
/// ui检测
///
///
//private bool CheckCrossHairOverUI()
//{
// if (!isOver) return false;
// EventSystem eventSystem = EventSystem.current;
// PointerEventData pointerEventData = new PointerEventData(eventSystem);
// // 获取准星pos 通过eventsystem检测下方是否有按钮
// pointerEventData.position = new Vector3(UIManager.GetInstance().GetCrossHairPos().x, UIManager.GetInstance().GetCrossHairPos().y, 0);
// //射线检测ui
// List uiRaycastResultCache = new List();
// eventSystem.RaycastAll(pointerEventData, uiRaycastResultCache);
// if (uiRaycastResultCache.Count > 0)
// {
// // 是否有button
// Button btn = uiRaycastResultCache[0].gameObject.GetComponent