跑酷解析_0

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

// class that animates the coin moving from the player to the coin indicator on the GUI
public class CoinGUICollection : MonoBehaviour
{
    // CoinValueObject is a small class that will make it easy to get the value of the coin value and the game object / transform
    class CoinValueObject
    {
        public CoinValueObject(GameObject go) { coinGameObject = go; coinTransform = go.transform; }
        public GameObject coinGameObject;
        public Transform coinTransform;
        public int coinValue;
    }

    static public CoinGUICollection instance;

    public GameObject guiCoin;
    public Transform collectionPoint;
    public Vector3 startPoint;
    public float collectionSpeed;

    private List<CoinValueObject> pool;
    private List<CoinValueObject> activeCoins;
    private int poolIndex;

    private Transform thisTransform;
    private GameManager gameManager;
    private CameraController cameraController;

    public void Awake()
    {
        instance = this;
    }

    public void Start()
    {
        thisTransform = transform;
        gameManager = GameManager.instance;
        cameraController = CameraController.instance;

        pool = new List<CoinValueObject>();
        poolIndex = 0;
        activeCoins = new List<CoinValueObject>();

        gameManager.onPauseGame += gamePaused;
    }

    // start the animation from the coin to the gui coin location
    public void coinCollected(int coinValue)
    {
        // don't show the animation if the camera isn't in its normal position
        if (!cameraController.isInGameTransform()) {
            gameManager.coinCollected(coinValue);
            return;
        }

        CoinValueObject coin = coinFromPool();
        coin.coinValue = coinValue;
        coin.coinTransform.position = startPoint;
        activeCoins.Add(coin);
    }

    public void Update()
    {
        for (int i = activeCoins.Count - 1; i >= 0; --i) {
            activeCoins[i].coinTransform.position = Vector3.MoveTowards(activeCoins[i].coinTransform.position, collectionPoint.position, collectionSpeed * Time.deltaTime);
            if (Vector3.SqrMagnitude(activeCoins[i].coinTransform.position - collectionPoint.position) < 0.001f) {
                disableCoin(i);
            }
        }
    }

    private CoinValueObject coinFromPool()
    {
        CoinValueObject obj;

        // keep a start index to prevent the constant pushing and popping from the list
#if UNITY_3_5
        if (pool.Count > 0 && pool[poolIndex].coinGameObject.active == false) {
#else
        if (pool.Count > 0 && pool[poolIndex].coinGameObject.activeSelf == false) {
#endif
            obj = pool[poolIndex];
#if UNITY_3_5
            obj.coinGameObject.active = true;
#else
            obj.coinGameObject.SetActive(true);
#endif
            poolIndex = (poolIndex + 1) % pool.Count;
            return obj;
        }

        // No inactive objects, need to instantiate a new one
        obj = new CoinValueObject(GameObject.Instantiate(guiCoin) as GameObject);
        obj.coinTransform.parent = thisTransform;

        pool.Insert(poolIndex, obj);
        poolIndex = (poolIndex + 1) % pool.Count;

        return obj;
    }

    public int getAnimatingCoins()
    {
        return activeCoins.Count;
    }

    private void disableCoin(int activeIndex)
    {
#if UNITY_3_5
        activeCoins[activeIndex].coinGameObject.active = false;
#else
        activeCoins[activeIndex].coinGameObject.SetActive(false);
#endif
        gameManager.coinCollected(activeCoins[activeIndex].coinValue);
        activeCoins.RemoveAt(activeIndex);
    }

    public void gameOver()
    {
        // add any coins animating to the coin gui element before the game is over
        for (int i = activeCoins.Count - 1; i >= 0; --i) {
            disableCoin(i);
        }
    }

    private void gamePaused(bool paused)
    {
        enabled = !paused;
    }
}


 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值