小地圖

using System.Collections.Generic;
using UnityEngine;

public class MiniMap : BaseUI
{
    public Camera cameMiniMap; // 小地圖攝像機
    public UITexture miniMap; // 小地圖圖片
    public List<UITexture> roleSpts; // 英雄角色圖片
    public List<UITexture> otherSpts; // 英雄角色圖片

    private Transform roleSprite; // 戰鬥主角
    private BaseSprite spt;
    private Vector3 lastRolePosition; //上次主角對象的Position
    private float lastCheckTime; // 上次檢查的時間
    private float checkTimeInterval = 0.3f; // 檢查的時間週期
    private float checkSqrDistanceInterval = 0.25f;

    private float mapWidth; //大地圖的寬度
    private float mapHeight;//大地圖的高度
    bool isStart = false;

    private float movedX;
    private float movedY;

    Texture A1;
    Texture A2;
    Texture B1;
    Texture B2;

    public void InitMap()
    {
        roleSprite = Game.Instance.GetCurScene().Star.transform;
        spt = Game.Instance.GetCurScene().Star;
        //原始寬度乘以縮放比例計算出真實寬度
        mapWidth = 95.5f; // size_x* scal_x;
        mapHeight = 159.8f; // size_z* scal_z;

        lastRolePosition = roleSprite.position;
        lastCheckTime = Time.time;
        isStart = true;
        A1 = Instantiate(Resources.Load("MiniMap/CampA1")) as Texture;
        A2 = Instantiate(Resources.Load("MiniMap/CampA2")) as Texture;
        B1 = Instantiate(Resources.Load("MiniMap/CampB1")) as Texture;
        B2 = Instantiate(Resources.Load("MiniMap/CampB2")) as Texture;
    }

    void Update()
    {
        if (!isStart)
            return;

        if (Time.time - lastCheckTime >= checkTimeInterval)
        {
            PKUnionScene scene = Game.Instance.GetCurScene() as PKUnionScene;
            // 自我陣營
            for (int i = 0; i < scene.StarGroupPlayerList.Count; i++)
            {
                NGUITools.SetActive(roleSpts[i].gameObject, true);

                if (scene.StarGroupPlayerList[i].PlayerID != spt.ID)
                {
                    movedX = miniMap.width / mapWidth * scene.StarGroupPlayerList[i].sprite.transform.position.x;
                    movedY = miniMap.height / mapHeight * scene.StarGroupPlayerList[i].sprite.transform.position.z;

                    if (scene.StarGroupPlayerList[i].Camp == EnumPKCamp.Group1)
                        roleSpts[i].mainTexture = B2;
                    else
                        roleSpts[i].mainTexture = A2;

                    roleSpts[i].transform.localPosition = new Vector3(movedX, movedY, roleSpts[i].transform.localPosition.z);
                }
                else
                {
                    movedX = miniMap.width / mapWidth * roleSprite.position.x;
                    movedY = miniMap.height / mapHeight * roleSprite.position.z;

                    if (scene.StarGroupPlayerList[i].Camp == EnumPKCamp.Group1)
                        roleSpts[i].mainTexture = B1;
                    else
                        roleSpts[i].mainTexture = A1;
                    // 設置本身的位置
                    cameMiniMap.transform.localPosition = new Vector3(movedX, movedY, cameMiniMap.transform.localPosition.z);
                    // 設置本身的方向
                    roleSpts[i].transform.eulerAngles = new Vector3(0, 0, -(roleSprite.eulerAngles.y));
                }
            }
            // 敵對陣營
            for (int i = 0; i < scene.OtherGrouPlayerList.Count; i++)
            {
                NGUITools.SetActive(otherSpts[i].gameObject, true);
                movedX = miniMap.width / mapWidth * scene.OtherGrouPlayerList[i].sprite.transform.position.x;
                movedY = miniMap.height / mapHeight * scene.OtherGrouPlayerList[i].sprite.transform.position.z;

                if (scene.OtherGrouPlayerList[i].Camp == EnumPKCamp.Group2)
                    otherSpts[i].mainTexture = A2;
                else
                    otherSpts[i].mainTexture = B2;

                otherSpts[i].transform.localPosition = new Vector3(movedX, movedY, 0);// -cameMiniMap.transform.localPosition.z);
            }
            lastCheckTime = Time.time;
        }
    }
}
相關文章
相關標籤/搜索