Пример #1
0
    /// <summary>
    /// Add an item to the context menu.
    /// </summary>
    public void AddItem(string Text, Action OnClicked = null)
    {
        GameObject item = _ui.CreateElement(_contextMenu, "item");

        _ui.AddLayout(item, 128.0f, 20.0f, -1, -1);
        _ui.AddImage(item, Color.white);
        _ui.AddHorizontalLayout(item);
        item.GetComponent <HorizontalLayoutGroup>().childAlignment = TextAnchor.MiddleCenter;

        GameObject title = _ui.CreateTextElement(item, Text, "item", CToolkitUI.ETextStyle.TS_HEADING);
        //_ui.AddLayout(title, 128.0f, -1, -1, -1);

        ColorBlock cb = new ColorBlock();

        cb.normalColor      = _style.ThemeColorB;
        cb.highlightedColor = _style.ThemeColorA;
        cb.colorMultiplier  = 1.0f;

        Button     button    = item.AddComponent <Button>();
        Navigation buttonNav = new Navigation();

        buttonNav.mode       = Navigation.Mode.None;
        button.targetGraphic = item.GetComponent <Image>();
        button.navigation    = buttonNav;
        button.colors        = cb;

        if (OnClicked != null)
        {
            button.onClick.AddListener(() =>
            {
                OnClicked();
                Hide();
            });
        }
    }
Пример #2
0
    protected override void _Update(CUserSession UserSession)
    {
        CToolkitUI ui = CGame.ToolkitUI;

        // TODO: Do this after we have moved the unit -__-
        Vector3 pivot = _Gob.transform.position;

        mBounds = new Bounds(pivot + new Vector3(0.0f, 0.5f, 0.0f), new Vector3(1.0f, 1.0f, 1.0f));

        if (_carryGOB != null)
        {
            if (!mCarryingPickup)
            {
                GameObject.Destroy(_carryGOB);
            }
            else
            {
                if (mWalking)
                {
                    _carryGOB.transform.localPosition = new Vector3(0.0f, 1.05f + Mathf.Sin(Time.time * 20.0f) * 0.05f, 0.3f);
                }
            }
        }
        else
        {
            if (mCarryingPickup)
            {
                _carryGOB = CGame.AssetManager.GetAsset <CModelAsset>("pickup").mVectorModel.CreateGameObject();
                _carryGOB.GetComponent <MeshRenderer>().material.SetColor("_FloorColor", new Color(150.0f / 255.0f, 150.0f / 255.0f, 150.0f / 255.0f, 1.0f));

                _carryGOB.transform.SetParent(_Gob.transform);
                _carryGOB.transform.localPosition = new Vector3(0.0f, 1.0f, 0.3f);
                _carryGOB.transform.localRotation = Quaternion.Euler(-10, 0, 0);
            }
        }

        // Icon Stack

        /*
         * {
         *      Vector3 screenPos = Camera.main.WorldToScreenPoint(_Gob.transform.position + new Vector3(0.0f, 1.6f, 0.0f));
         *      Vector2 bubbleUIPos = CGame.UIManager.ConvertScreenSpaceToUISpace(new Vector2(screenPos.x, screenPos.y));
         *      ((RectTransform)_iconStackGob.transform).anchoredPosition = bubbleUIPos;
         * }
         * if (_bubbleTime > 0.0f)
         * {
         *      _UIThought.SetActive(true);
         *      _UIThoughtText.text = _thoughtText;
         *
         *      float y = ((RectTransform)_UIThought.transform).anchoredPosition.y;
         *      // TODO: Safegaurd lerp.
         *      y = Mathf.Lerp(y, _iconStackBaselineTarget, 10.0f * Time.deltaTime);
         *      ((RectTransform)_UIThought.transform).anchoredPosition = new Vector2(0.0f, y);
         *      _bubbleTime -= Time.deltaTime;
         * }
         * else
         * {
         *      _UIThought.SetActive(false);
         * }
         *
         * if (_iconSelectorGob != null)
         * {
         *      _iconSelectorLerp -= Time.deltaTime * 7.0f;
         *
         *      if (_iconSelectorLerp < 0.0f)
         *              _iconSelectorLerp = 0.0f;
         *
         *      ((RectTransform)_iconSelectorGob.transform).anchoredPosition = new Vector2(0.0f, Mathf.Sin(Time.time * 10.0f) * 8.0f - 4.0f + 600.0f * CGame.UIResources.UnitIconDropCurve.Evaluate(1.0f - _iconSelectorLerp));
         * }
         */

        /*
         * if (!mDead)
         * {
         *      _HealthBar.SetActive(true);
         *      Vector3 screenPos = Camera.main.WorldToScreenPoint(GetVisualPos() + new Vector3(0.0f, 2.0f, 0.0f));
         *      ((RectTransform)_HealthBar.transform).anchoredPosition = new Vector2((int)screenPos.x, (int)screenPos.y);
         *      _HealthBarFill.transform.localScale = new Vector3(mStamina / mMaxStamina, 1.0f, 1.0f);
         * }
         * else
         * {
         *      _HealthBar.SetActive(false);
         * }
         */

        /*
         * if (mPathing)
         * {
         *      if (mSpeed >= 4.0f)
         *              _footStepAudioTimer += 3.0f * Time.deltaTime;
         *      else if (mSpeed >= 2.0f)
         *              _footStepAudioTimer += 2.2f * Time.deltaTime;
         *      else if (mSpeed > 0.0f)
         *              _footStepAudioTimer += 1.6f * Time.deltaTime;
         *
         *      if (_footStepAudioTimer > 1.0f)
         *      {
         *              _footStepAudioTimer = 0.0f;
         *              PlaySound(0, 6.0f);
         *      }
         *
         *      // Walking
         *
         *      //_Animator.SetFloat("Speed", mSpeed);
         * }
         * else
         * {
         *      _footStepAudioTimer = 0.0f;
         *      //_Animator.SetFloat("Speed", 0.0f);
         * }
         */

        //_Animator.SetBool("Sitting", mSitting);
        //_Animator.SetBool("Dead", mDead);

        /*
         * if (mFrustrated || mEating)
         *      _Animator.SetLayerWeight(1, 1.0f);
         * else
         *      _Animator.SetLayerWeight(1, 0.0f);
         *
         * _Animator.SetBool("Eating", mEating);
         *
         * if (mFrustrated != _oldFrustrated)
         * {
         *      _oldFrustrated = mFrustrated;
         *
         *      if (mFrustrated)
         *              _Animator.SetTrigger("FrustrateTrigger");
         * }
         * */

        //CDebug.DrawZRect(_unit.mBounds.center, 0.1f, 0.01f * _unit.mStamina, Color.red);

        // Interpolate to simulated position
        Vector3 targetPos  = new Vector3(mPosition.x, 0.0f, mPosition.y);
        Vector3 currentPos = _Gob.transform.position;
        Vector3 dir        = targetPos - currentPos;
        float   distance   = dir.magnitude;

        // Teleport to position if we are too far away
        if (distance >= 3.0f)
        {
            currentPos = targetPos;

            // Reset dynamics on teleport
            Transform neck    = _Gob.transform.FindChild("unit_test/Hips/Spine/Chest/Neck");
            Vector3   forward = _Gob.transform.TransformDirection(Vector3.forward);

            _joints[0] = new SDynamicsJoint(neck.position + forward * 0.1f, true);
            _joints[1] = new SDynamicsJoint(_joints[0].mPosition + Vector3.down * 0.13f, false);
            _joints[2] = new SDynamicsJoint(_joints[1].mPosition + Vector3.down * 0.13f, false);
            _joints[3] = new SDynamicsJoint(_joints[2].mPosition + Vector3.down * 0.13f, false);
        }
        else
        {
            currentPos = Vector3.Lerp(currentPos, targetPos, Mathf.Clamp(Time.deltaTime * 10.0f, 0.0f, 1.0f));
        }

        _prevRotation = Quaternion.Slerp(_prevRotation, mRotation, 5.0f * Time.deltaTime);
        //_prevRotation = Quaternion.RotateTowards(_prevRotation, mRotation, 300.0f * Time.deltaTime);
        _Gob.transform.position = currentPos;
        _Gob.transform.rotation = _prevRotation;

        _Animator.speed = 1.0f;

        if (_Animator.isInitialized)
        {
            if (mDead)
            {
                _PlayAnimation("dying_1", -1);
            }
            else if (mActionAnim != "")
            {
                _Animator.speed = mAnimSpeed;
                _PlayAnimation(mActionAnim, -1, false);
            }
            else if (mWalking)
            {
                if (mCarryingPickup)
                {
                    _PlayAnimation("walking_with_item", -1);
                }
                else
                {
                    if (mSpeed > 2.0f)
                    {
                        _PlayAnimation("running_no_item", -1);
                        _Animator.SetFloat("walkSpeed", mSpeed * 0.6f);
                    }
                    else
                    {
                        if (mStamina < 50.0f)
                        {
                            _Animator.SetFloat("walkSpeed", mSpeed * 1.0f);
                            _PlayAnimation("walking_exhausted", -1);
                        }
                        else
                        {
                            _Animator.SetFloat("walkSpeed", mSpeed * 0.5f + 0.5f);
                            _PlayAnimation("walking_no_item", -1);
                        }
                    }
                }
            }
            else if (mAttacking)
            {
                _PlayAnimation("combat_bashing", -1);
            }
            else
            {
                float r = UnityEngine.Random.value;

                if (r > 0.8f)
                {
                    _PlayAnimation("idle_standing_action_1", 1.0f);
                }
                else if (r > 0.6f)
                {
                    _PlayAnimation("idle_standing_action_2", 1.0f);
                }
                else
                {
                    _PlayAnimation("idle_standing_general", 5.0f);
                }
            }
        }

        // Sound
        if (SoundID > SoundLastPlayedID)
        {
            _AudioSource.pitch = CGame.UniversalRandom.GetNextFloat() * 0.5f + 0.75f;
            _AudioSource.PlayOneShot(CGame.Resources.GetAudioClip(SoundType), SoundVolume);
            SoundLastPlayedID = SoundID;
        }

        if (mAssignedDeskID == -1)
        {
            if (_texState != 0)
            {
                _texState = 0;
                _Gob.transform.GetChild(0).GetChild(0).GetComponent <SkinnedMeshRenderer>().material.SetTexture("_MainTex", CGame.PrimaryResources.UnitTexTie);
            }
        }
        else
        {
            if (_texState == 0)
            {
                _texState = 1;
                //_Gob.transform.GetChild(0).GetChild(0).GetComponent<SkinnedMeshRenderer>().material.SetTexture("_MainTex", CGame.PrimaryResources.UnitTexTieArmband);
            }
        }

        // TODO: Hide briefcase when sitting/using?

        /*
         * if (_briefcaseGOB != null)
         * {
         *      if (mAssignedDeskID == -1)
         *      {
         *              _briefcaseGOB.SetActive(true);
         *      }
         *      else
         *      {
         *              _briefcaseGOB.SetActive(false);
         *      }
         * }
         */

        if (mOwner == UserSession.mPlayerIndex)
        {
            if (_indicatorGob == null)
            {
                _indicatorGob = ui.CreateElement(CGame.UIManager.overlayLayer, "indicator");
                ui.SetAnchors(_indicatorGob, Vector2.zero, Vector2.zero, new Vector2(0.5f, 0.0f));
                ui.SetTransform(_indicatorGob, 50, -200, 56, 65);
                Image indImg = _indicatorGob.AddComponent <Image>();
                indImg.color  = new Color(0.2f, 0.2f, 0.2f, 0.5f);
                indImg.sprite = ui.IndicatorBkg;

                GameObject indIcon = ui.CreateElement(_indicatorGob, "icon");
                ui.SetAnchors(indIcon, new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f));
                ui.SetTransform(indIcon, 0, 3, 56, 56);
                indImg       = indIcon.AddComponent <Image>();
                indImg.color = new Color(1.0f, 1.0f, 1.0f, 1.0f);

                _indicatorGob.transform.localScale = new Vector3(0.75f, 0.75f, 0.75f);
                //_indicatorGob.transform.localScale = new Vector3(0.75f, 0.75f, 0.75f);
            }

            mThoughtTime += Time.deltaTime;

            if (mLastThoughtState != mThoughtState)
            {
                mThoughtTime = 0.0f;
                Image indImg = _indicatorGob.transform.GetChild(0).GetComponent <Image>();
                mLastThoughtState = mThoughtState;

                indImg.sprite = ui.IndicatorNoDesk;
            }

            if (mThoughtTime < 2.0f)
            {
                mThoughtAlpha += Time.deltaTime * 5.0f;
            }
            else
            {
                mThoughtAlpha -= Time.deltaTime * 5.0f;
            }

            mThoughtAlpha = Mathf.Clamp01(mThoughtAlpha);

            if (mThoughtAlpha > 0.0f)
            {
                _indicatorGob.SetActive(true);
                Vector3 screenPos = Camera.main.WorldToScreenPoint(_Gob.transform.position + new Vector3(0.0f, 1.8f, 0.0f));
                ((RectTransform)_indicatorGob.transform).anchoredPosition = new Vector2((int)screenPos.x, (int)screenPos.y);
                _indicatorGob.GetComponent <Image>().color = new Color(0.2f, 0.2f, 0.2f, 0.5f * mThoughtAlpha);
                _indicatorGob.transform.GetChild(0).GetComponent <Image>().color = new Color(1.0f, 1.0f, 1.0f, 1.0f * mThoughtAlpha);
            }
            else
            {
                _indicatorGob.SetActive(false);
            }
        }

        if (mSpeech != "")
        {
            if (mSpeechBubble == null)
            {
                mSpeechBubble = ui.CreateElement(CGame.UIManager.overlayLayer, "speechBubble");
                ui.SetAnchors(mSpeechBubble, Vector2.zero, Vector2.zero, new Vector2(0.5f, 0.0f));
                ui.SetTransform(mSpeechBubble, 50, -200, 100, 20);
                ui.AddImage(mSpeechBubble, new Color(1.0f, 1.0f, 1.0f, 1.0f));
                ui.AddHorizontalLayout(mSpeechBubble, new RectOffset(8, 8, 8, 8));
                ContentSizeFitter fitter = mSpeechBubble.AddComponent <ContentSizeFitter>();
                fitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
                fitter.verticalFit   = ContentSizeFitter.FitMode.PreferredSize;

                GameObject text = ui.CreateElement(mSpeechBubble, "text");
                mSpeechBubbleText       = text.AddComponent <Text>();
                mSpeechBubbleText.font  = CGame.GameUIStyle.FontB;
                mSpeechBubbleText.color = CGame.GameUIStyle.ThemeColorC;
                mSpeechBubbleText.text  = "";

                mSpeechTimer = 0;
            }

            Vector3 screenPos = Camera.main.WorldToScreenPoint(_Gob.transform.position + new Vector3(0.0f, 2.0f, 0.0f));
            ((RectTransform)mSpeechBubble.transform).anchoredPosition = new Vector2((int)screenPos.x, (int)screenPos.y);

            while (mSpeechTimer >= 0.03f)
            {
                mSpeechTimer -= 0.03f;

                if (mSpeechBubbleText.text != mSpeech)
                {
                    if (mSpeech.Length < mSpeechBubbleText.text.Length)
                    {
                        mSpeechBubbleText.text = mSpeech[0].ToString();
                    }
                    else
                    {
                        bool match = true;
                        for (int i = 0; i < mSpeechBubbleText.text.Length; ++i)
                        {
                            if (mSpeechBubbleText.text[i] != mSpeech[i])
                            {
                                mSpeechBubbleText.text = mSpeech[0].ToString();
                                match = false;
                                break;
                            }
                        }

                        if (match)
                        {
                            mSpeechBubbleText.text += mSpeech[mSpeechBubbleText.text.Length];
                        }
                    }
                }
            }

            mSpeechTimer += Time.deltaTime;
        }
        else
        {
            GameObject.Destroy(mSpeechBubble);
            mSpeechBubble     = null;
            mSpeechBubbleText = null;
        }

        if (mOwner == UserSession.mPlayerIndex)
        {
            Vector3 pos = _Gob.transform.position;
            CPunchOut.UpdateMesh(_punchOutMesh, _worldView, pos.ToWorldVec2(), 8.0f);
            _punchOut.transform.position = new Vector3(pos.x, 0.0f, pos.z);
            _punchOut.transform.rotation = Quaternion.identity;

            if (mUIEmployeeEntry != null)
            {
                //mUIEmployeeEntry.mStaminaBar.localScale = new Vector3(mStamina / mStats.mMaxStamina, 1, 1);
            }
        }

        /*
         * if (mID == 6)
         * {
         *      CGame.CameraManager.SetTargetPosition(mPosition.ToWorldVec3());
         * }
         */

        _Gob.transform.GetChild(0).GetChild(0).GetComponent <SkinnedMeshRenderer>().material.SetFloat("_Stam", mStamina / 100.0f);
    }