Created
November 23, 2018 04:29
-
-
Save nickyeh97/e8ded97c641525872e2a611fe013d1b9 to your computer and use it in GitHub Desktop.
挖掘點的腳本 負責挖掘行為與進度
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEditor; | |
using UnityEngine; | |
using UnityEngine.UI; | |
[RequireComponent(typeof(BoxCollider2D))] | |
public class Dig_Progressing : MonoBehaviour | |
{ | |
public int foodID = -1; | |
public int foodCapacity = 10; | |
public float foodGainTime = 2; | |
public GameObject food; | |
public GameObject UI; | |
public Canvas DigCanvas; | |
public float UI_Offset = .5f; | |
public Transform foodProgressingUI; | |
public Slider gainTimeBar; | |
public Text foodCountText; | |
public bool isTrigger; | |
//public bool hasFood; | |
private CreativeSpore.SmartColliders.PlatformCharacterInput m_platformCtrl; | |
private void Start() | |
{ | |
foodID = GetComponentInParent<FoodPoint>().foodID; | |
if (foodProgressingUI == null || gainTimeBar == null || foodCountText == null) | |
{ | |
GameObject newUI = Instantiate(UI, transform.position, Quaternion.identity, DigCanvas.transform); | |
foodProgressingUI = newUI.GetComponent<Transform>(); | |
gainTimeBar = newUI.GetComponentInChildren<Slider>(); | |
foodCountText = newUI.GetComponentInChildren<Text>(); | |
} | |
foodCountText.text = "x " + foodCapacity; | |
gainTimeBar.maxValue = foodGainTime; | |
foodProgressingUI.position = Camera.main.WorldToScreenPoint(this.transform.position + Vector3.up * UI_Offset); | |
} | |
private void Update() | |
{ | |
if (!m_platformCtrl) return; | |
if (isTrigger && foodCapacity > 0 && !m_platformCtrl.hasFood) | |
Digging(); | |
} | |
private void Digging() | |
{ | |
if (m_platformCtrl != null) | |
gainTimeBar.value += Time.deltaTime; | |
if (gainTimeBar.value == foodGainTime) | |
{ | |
gainTimeBar.value = 0; | |
//Callback here! | |
m_platformCtrl.hasFood = true; | |
foodCapacity--; | |
foodCountText.text = "x " + foodCapacity; | |
GameObject NewFood = Instantiate(food, Vector3.zero, Quaternion.identity, m_platformCtrl.transform); | |
NewFood.transform.localPosition = new Vector3(0,0.15f,-0.1f); | |
//Editor 更換食物貼圖 | |
//Modify DC_MainEventData.MonoFood.FoodID to Public for access. | |
NewFood.GetComponent<DC_Food>().FoodID = foodID; | |
//Runtime更換食物貼圖 | |
//ChangeSprite(NewFood); | |
} | |
} | |
//正式版要用Asset Bundle.load | |
private void ChangeSprite(GameObject Food) | |
{ | |
SpriteRenderer _rdr = Food.GetComponent<SpriteRenderer>(); | |
DC_MainEventData.FoodData value = DC_MainEventData.dictionaryManager.FoodDataDictionary[foodID]; | |
List<Sprite> _sprites = new List<Sprite>(); | |
for (int _a = 0; _a < value.PicturesNameArray.Length; _a++) | |
{ | |
Sprite _sp; | |
string _key = value.PicturesNameArray[_a]; | |
_sp = Resources.Load<Sprite>(string.Format("Image/{0}", _key)); | |
if (_sp == null) | |
Debug.LogError(string.Format("Can Not Find {0} At Image/{0}", _key)); | |
else | |
_sprites.Add(_sp); | |
} | |
for (int _a = 0; _a < _sprites.Count; _a++) | |
{ | |
if (_a == 0) _rdr.sprite = _sprites[_a]; | |
else | |
{ | |
//CreateSpriteObject(); | |
} | |
} | |
} | |
private void OnTriggerStay2D(Collider2D collision) | |
{ | |
if (collision.tag == "Player" && m_platformCtrl == null) | |
{ | |
//改由 temp 取得 PlatformCharacterInput,才能有效解決多個Collision同時Trigger | |
//m_platformCtrl = collision.GetComponent<CreativeSpore.SmartColliders.PlatformCharacterInput>(); | |
//FIX 當有人有food 、有人沒有Food時狀況 , 還是用List處理採集順序! | |
var temp = collision.GetComponent<CreativeSpore.SmartColliders.PlatformCharacterInput>(); | |
if (!temp.hasFood) | |
{ | |
m_platformCtrl = temp; | |
} | |
//持有東西時不能挖掘(食物、廚具) | |
//if(collision.GetComponentInChildren<DC_Food>() == null) hasFood = false; | |
//else | |
//{ | |
// //當有人有food 、有人沒有Food時會有狀況 | |
// hasFood = true; | |
//} | |
} | |
} | |
private void OnTriggerExit2D(Collider2D collision) | |
{ | |
if (collision.tag == "Player" && m_platformCtrl == collision.GetComponent<CreativeSpore.SmartColliders.PlatformCharacterInput>()) | |
{ | |
m_platformCtrl = null; | |
isTrigger = false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment