AI

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

public enum FeastUIChangedType
{
    FeastUIChangedType_Congratulated = 1,
    FeastUIChangedType_InvitePlayerSuc = 2,
    FeastUIChangedType_GetInfo = 3,
    FeastUIChangedType_GetInvitePlayer = 4,
    FeastUIChangedType_Owner_Operation_Money = 5,
    FeastUIChangedType_Owner_Operation_Food = 6,
    FeastUIChangedType_Owner_Operation_Wine = 7,
    FeastUIChangedType_Owner_Operation_Dance = 8,
    FeastUIChangedType_Guest_Operation_Money = 9,
    FeastUIChangedType_Guest_Operation_Food = 10,
    FeastUIChangedType_Guest_Operation_Wine = 11,
    FeastUIChangedType_Guest_Operation_Dance = 12,

    FeastUIChangedType_GetMoneyRecord = 13,

    FeastUIChangedType_Time_Up = 14,
    FeastUIChangedType_EnterGuest = 15,
    FeastUIChangedType_OwnerTalk = 16,
    FeastUIChangedType_GuestTalk = 17,
}

public class FeastCtrl : NTFrame.SingletonMono<FeastCtrl>
{
    public static Vector3 gFoodOffest = new Vector3(0, -1.5f, 0.65f);
    public static Vector3 gWineOffest = new Vector3(0, -1.5f, 0.66f);
    public static Vector3 gFoodAndWineOffestRot = new Vector3(90, 0, 0);

    public delegate void FestChangeFunc(FeastUIChangedType type);
    public FestChangeFunc mChangeFunc;

    private GameObject mSceneRootObj;
    private Animator mOwner;
    private Dictionary<int, Animator> mGuests = new Dictionary<int, Animator>();
    private Dictionary<int, bool> mGuestIsInit = new Dictionary<int, bool>();
    private Dictionary<int, GameObject> mDesks = new Dictionary<int, GameObject>();
    private Dictionary<int, Animator> mDancers = new Dictionary<int, Animator>();
    private bool mbLoadDancer = false;

    private bool mNeedInitGuest = true;
    private bool mInitComponent = false;

    private bool mCurIsFeast = false;
    private int mFinishedTime = 0;

    private bool mSelfIsGuest = false;

    private void InitComponent()
    {
        mInitComponent = true;
        mSceneRootObj = GameObject.Find("Main_building_neizheng");
        mGuests.Clear();
        mOwner = null;
        mNeedInitGuest = true;

        mDesks.Clear();
        for(int i = 1; i <= 10; i++)
        {
            GameObject obj = ObjectCommon.GetChild(mSceneRootObj, "Model/Main_building_neizheng_kezuo/Main_building_neizheng_kezuo" + i.ToString("00"));
            mDesks[i] = obj;
        }
        mDancers.Clear();
        mbLoadDancer = false;
    }

    private void ClearComponent()
    {
        mSceneRootObj = null;
        mGuests.Clear();
        mOwner = null;
        mDesks.Clear();
        mInitComponent = false;
        mGuestIsInit.Clear();
        mDancers.Clear();
        mbLoadDancer = false;
    }

    public void AddChangeFunc(FestChangeFunc func)
    {
        mChangeFunc += func;
    }

    public void RemoveChangeFunc(FestChangeFunc func)
    {
        mChangeFunc -= func;
    }

    public void FeastChange(FeastUIChangedType type)
    {
        if (mChangeFunc != null)
        {
            mChangeFunc(type);
        }
    }

    public void EnterScene()
    {
        InitComponent();

        UIManager.OpenUIFeast();

        FeastStatu curState = FeastManager.GetInstance().GetCurStatu();
        switch(curState)
        {
            case FeastStatu.FeastStatu_Owner:
                break;
            case FeastStatu.FeastStatu_Guest:
                break;
            case FeastStatu.FeastStatu_NoFeast:
                break;
            default :
                break;
        }

        if(mSelfIsGuest)
        {
            mSelfIsGuest = false;
            GuestInterScene(PlayerManager.GetInstance().GetPlayerId());
        }
    }

    public void CloseScene()
    {
        ClearComponent();
        StopAllCoroutines();
        NetWorkManager.GetInstance().SendWatchLeave();
    }

    public void GuestJoin()
    {
        FeastInfo info = FeastManager.GetInstance().GetFeastInfo();
        if(info != null 
            && info.GetGuestIsIn10(PlayerManager.GetInstance().GetPlayerId()))
            mSelfIsGuest = true;
        UIManager.OpenFeastScene();
    }

    public void OwnerOperate(FeastOperation opType)
    {
        switch(opType)
        {
            case FeastOperation.FeastOperation_Money:
                break;
            case FeastOperation.FeastOperation_Food:
                break;
            case FeastOperation.FeastOperation_Wine:
                break;
            case FeastOperation.FeastOperation_Dance:
                break;
            default:
                break;
        }
    }

    public void GuestOperate(FeastOperation opType)
    {
        switch (opType)
        {
            case FeastOperation.FeastOperation_Money:
                break;
            case FeastOperation.FeastOperation_Food:
                break;
            case FeastOperation.FeastOperation_Wine:
                break;
            case FeastOperation.FeastOperation_Dance:
                break;
            default:
                break;
        }
    }

    //没有宴会的显示
    public void InitMyKing()
    {
        string ownerModle = PlayerManager.GetInstance().GetPlayerModel();
        List<string> heroActions = new List<string>();
        heroActions.Add(StringDefine.Idle);
        heroActions.Add(StringDefine.Sit);
        ResourceManager.GetInstance().LoadHero(ownerModle, heroActions, delegate (GameObject obj)
        {
            if (obj != null)
            {
                mOwner = obj.GetComponent<Animator>();
                mOwner.transform.localScale = new Vector3(1.8f, 1.8f, 1.8f);
                mOwner.transform.localRotation = Quaternion.Euler(new Vector3(0, 90f, 0));
                mOwner.transform.localPosition = UINeiZheng.gmainHeroPos;
                if (mSceneRootObj != null)
                    mOwner.transform.SetParent(mSceneRootObj.transform);
                mOwner.Play(StringDefine.Sit);
                mOwner.name = "king";
            }

        });
    }

    //客人进场
    public void GuestInterScene(long guid)
    {
        FeastInfo info = FeastManager.GetInstance().GetFeastInfo();
        if (info == null)
            return;
        int index = info.GetGuestIndex(guid);
        FeastGuest guest = info.GetGuest(guid);
        if (guest == null)
            return;
        if(index >= 1 && index <= 10)
        {
            if(mGuests.Count < index)
            {
                LoadAGuest(guest.GetModel(), index,delegate(GameObject obj)
                {
                    GuestMoveToSeat(obj, index);
                });
            }
            else
            {
                GuestMoveToSeat(mGuests[index].gameObject, index);
            }
        }
    }

    //客人移动到座位
    private void GuestMoveToSeat(GameObject obj , int index)
    {
        if (index < 1 || index > 10)
            return;
        bool isLeft = (index % 2 == 1);

        GameObject seat = mDesks[index];
        if (seat == null)
            return;
        Vector3 endPosition = seat.transform.position;
        obj.transform.position = endPosition;
        Vector3 localP = obj.transform.localPosition;
        //localP.y += 0.2f;
        obj.transform.localPosition = localP;
        endPosition = obj.transform.position;

        if (isLeft)
            localP.z -= 3;
        else
            localP.z += 3;
        obj.transform.localPosition = localP;
        Vector3 middlePosition = obj.transform.position;
        localP = obj.transform.localPosition;
        localP.x = 25;
        obj.transform.localPosition = localP;
        Vector3 startPosition = obj.transform.position;

        obj.transform.position = startPosition;
        float distance = Vector3.Distance(startPosition,middlePosition);
        float time = 0.0f;
        time = distance / 8f;
        iTween.MoveTo(obj, iTween.Hash("position", middlePosition, "speed", 8f, "easeType", iTween.EaseType.linear, "looktarget", middlePosition));

        Animator ani = obj.GetComponent<Animator>();
        if (ani != null)
            ani.Play(StringDefine.Run);
        StartCoroutine(GuestSeatFinish(time,ani, endPosition));
    }

    //客人移动到座位结束
    IEnumerator GuestSeatFinish(float time, Animator ani,Vector3 endPosition)
    {
        yield return new WaitForSeconds(time);
        if (ani != null)
        {
            Hashtable tabel = new Hashtable();
            iTween.MoveTo(ani.gameObject, iTween.Hash("position", endPosition, "time", 0.5f, "easeType", iTween.EaseType.linear, "looktarget", endPosition));

            yield return new WaitForSeconds(0.5f);
            ani.Play(StringDefine.Sit);
        }
    }

    //加载一个客人
    private void LoadAGuest(string modle, int index,CommonDelegate.ObjDelegate func = null)
    {
        if (mGuestIsInit.ContainsKey(index))
            return;
        mGuestIsInit[index] = true;
        List<string> heroActions = new List<string>();
        heroActions.Add(StringDefine.Idle);
        heroActions.Add(StringDefine.Sit);
        heroActions.Add(StringDefine.Run);
        ResourceManager.GetInstance().LoadHero(modle, heroActions, delegate (GameObject obj)
        {
            if (obj != null)
            {
                Animator ani = obj.GetComponent<Animator>();
                ani.transform.localScale = new Vector3(1.8f, 1.8f, 1.8f);
                if (mDesks[index] != null)
                {
                    ani.transform.position = mDesks[index].transform.position;
                    Vector3 localpos = ani.transform.localPosition;
                    //localpos.y += 0.2f;
                    ani.transform.localPosition = localpos;
                    ani.transform.eulerAngles = mDesks[index].transform.eulerAngles;
                    Vector3 localeule = ani.transform.localEulerAngles;
                    localeule.x = 0;
                    ani.transform.localEulerAngles = localeule;
                }
                if (mSceneRootObj != null)
                    ani.transform.SetParent(mSceneRootObj.transform);
                ani.Play(StringDefine.Sit);
                ani.name = "guest" + index.ToString();

                mGuests[index] = ani;
                if (func != null)
                    func(obj);
            }

        });
    }

    //初始化客人
    public void InitGuest()
    {
        if (!mInitComponent)
            return;
        if (!mNeedInitGuest)
            return;
        mNeedInitGuest = false;
        FeastInfo info = FeastManager.GetInstance().GetFeastInfo();
        if (info == null)
            return;
        string ownerModle = info.GetOwnerModle();
        List<string> heroActions = new List<string>();
        heroActions.Add(StringDefine.Idle);
        heroActions.Add(StringDefine.Sit);
        if(mOwner == null)
        {
            ResourceManager.GetInstance().LoadHero(ownerModle, heroActions, delegate (GameObject obj)
            {
                if (obj != null)
                {
                    mOwner = obj.GetComponent<Animator>();
                    mOwner.transform.localScale = new Vector3(1.8f, 1.8f, 1.8f);
                    mOwner.transform.localRotation = Quaternion.Euler(new Vector3(0, 90f, 0));
                    mOwner.transform.localPosition = UINeiZheng.gmainHeroPos;
                    if (mSceneRootObj != null)
                        mOwner.transform.SetParent(mSceneRootObj.transform);
                    mOwner.Play(StringDefine.Sit);
                    mOwner.name = "owner";
                }

            });
        }

        //load guest
        List<FeastGuest> guests = info.GetGuests();
        if(guests != null)
        {
            for(int i = 0; i < guests.Count; i++)
            {
                int index = i + 1;
                string modle = guests[i].GetModel();
                LoadAGuest(modle, index);
            }
        }


        //load dancer
        if(mDancers.Count == 0 && mbLoadDancer == false)
        {
            mbLoadDancer = true;
            LoadADancer(StringDefine.LingWu,JudgeNeedDance);
            LoadADancer(StringDefine.BanWu, JudgeNeedDance);
        }
    }

    //开场时是否已经有舞女跳舞了
    private void JudgeNeedDance()
    {
        if (mDancers.Count != 5)
            return;
        FeastInfo info = FeastManager.GetInstance().GetFeastInfo();
        if (info == null)
            return;
        if (info.mbDance)
        {
            for(int i = 0; i < mDancers.Count; i++)
            {
                if(mDancers[i] != null)
                {
                    UITools.setActive(mDancers[i].gameObject, true);
                }
            }
            PlayerDance(StringDefine.Show);
        }
    }


    //舞女跳舞
    private void PlayerDance(string action)
    {
        float random = NGUITools.RandomRange(0, 7);
        random = random / 10.0f;
        for (int i = 0; i < mDancers.Count; i++)
        {
            FeastDancerData data = FeastManager.GetInstance().GetDancerData(i + 1);
         
            if (mDancers[i] != null)
            {
                mDancers[i].Play(action, 0, random);
                mDancers[i].SetBool("Show", true);
                if (data != null)
                {
                    mDancers[i].transform.eulerAngles = data.mRot;
                    mDancers[i].transform.position = data.mPos;
                }
            }
        }
    }

    //舞女进场
    public void DancerEnterScene()
    {
        for(int i = 0; i < mDancers.Count; i++)
        {
            if (mDancers[i] == null)
                continue;
            UITools.setActive(mDancers[i].gameObject, true);
            FeastDancerData configData = FeastManager.GetInstance().GetDancerData(i + 1);
            if(configData != null)
            {
                mDancers[i].Play(StringDefine.Run,-1,0);
                iTween.MoveTo(mDancers[i].gameObject, iTween.Hash("position", configData.mPos, "time", 6f, "easeType", iTween.EaseType.easeOutSine, "looktarget", configData.mPos));
                StartCoroutine(DancerInRightPos(configData, mDancers[i]));
            }
        }
    }

    //舞女就位
    private IEnumerator DancerInRightPos(FeastDancerData data,Animator ani)
    {
        yield return new WaitForSeconds(6f);
        if(data != null && ani != null)
        {
            ani.transform.eulerAngles = data.mRot;
            ani.Play(StringDefine.Show, -1, 0);
            ani.SetBool("Show", true);
        }
    }

    //加载一个舞女
    private void LoadADancer(string modle,CommonDelegate.VoidDelegate func)
    {
        List<string> heroActions = new List<string>();
        heroActions.Add(StringDefine.Idle);
        heroActions.Add(StringDefine.Show);
        heroActions.Add(StringDefine.Run);
        ResourceManager.GetInstance().LoadHero(modle, heroActions, delegate (GameObject obj)
        {
            if (obj != null)
            {
                Animator ani = obj.GetComponent<Animator>();
                ani.transform.localScale = new Vector3(1.8f, 1.8f, 1.8f);
                if (mSceneRootObj != null)
                    ani.transform.SetParent(mSceneRootObj.transform);
                ani.Play(StringDefine.Show);
                if(modle == StringDefine.LingWu)
                {
                    mDancers[0] = obj.GetComponent<Animator>();
                    mDancers[0].name = "lingwu";
                    InitDancePos(0, mDancers[0].gameObject);
                }
                else if(modle == StringDefine.BanWu)
                {
                    mDancers[1] = obj.GetComponent<Animator>();
                    mDancers[1].name = "banwu1";
                    InitDancePos(1, mDancers[1].gameObject);
                    for (int i = 2; i < 5; i++)
                    {
                        GameObject tempObj = NGUITools.AddChild(mSceneRootObj, obj);
                        tempObj.name = "banwu" + i.ToString();
                        mDancers[i] = tempObj.GetComponent<Animator>();
                        InitDancePos(i, mDancers[i].gameObject);
                    }
                }

                if (func != null)
                    func();
            }

        });
    }

    //舞女初始化位置
    private void InitDancePos(int index,GameObject obj)
    {
        FeastDancerData data = FeastManager.GetInstance().GetDancerData(index + 1);
        if (data == null || obj == null)
            return;

        obj.transform.position = data.mPos;
        obj.transform.localScale = data.mScale;

        Vector3 pos = obj.transform.localPosition;
        pos.x = 25;
        obj.transform.localPosition = pos;
        UITools.setActive(obj, false);
    }

    //获取主人在屏幕中的位置
    public Vector3 GetOwnerPositionInScreeen()
    {
        if(mOwner != null)
        {
            Camera camera = Camera.main;
            if(camera != null)
            {
                return camera.WorldToScreenPoint(mOwner.transform.position);
            }
        }
        return Vector3.zero;
    }

    //获取客人在屏幕中的位置
    public Vector3 GetGuestPositionInScreen(long guid)
    {
        FeastInfo info = FeastManager.GetInstance().GetFeastInfo();
        if(info != null)
        {
            int index = info.GetGuestIndex(guid);
            if(index >= 1 && index <= 10 && mDesks.ContainsKey(index))
            {
                Camera camera = Camera.main;
                if (camera != null)
                {
                    return camera.WorldToScreenPoint(mDesks[index].transform.position);
                }
            }
        }
        return Vector3.zero;
    }


    //获取客人课桌
    public GameObject GetGuestDest(int index)
    {
        if(mDesks.ContainsKey(index))
            return mDesks[index];
        return null;
    }

    private void Update()
    {
        if (!mCurIsFeast)
            return;
        if(mFinishedTime <= TimerManager.GetInstance().CurrentServerTime)
        {
            mCurIsFeast = false;
            FeastChange(FeastUIChangedType.FeastUIChangedType_Time_Up);
            mFinishedTime = 0;
            FeastManager.GetInstance().SetFeastInfo(new FeastInfo());
            FeastInfo info = FeastManager.GetInstance().GetFeastInfo();
            if (info != null)
                info.mFeastStatu = FeastStatu.FeastStatu_NoFeast;
        }
    }

    public void SetFeastFinishTime(int time)
    {
        mFinishedTime = time;
        if (mFinishedTime > TimerManager.GetInstance().CurrentServerTime)
            mCurIsFeast = true;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值