示例#1
0
    /// <summary>
    /// this class can be overridden to add additional stats, but
    /// NOTE: remember to include base.AddStats in the override.
    /// also don't call 'AddStats' from the derived class but
    /// leave this to 'Awake' in this class
    /// </summary>
    public virtual void InitStats()
    {
        // -------- getters --------

        if (NPlayer == null)
        {
            Debug.LogError("Error (" + this + ") Found no vp_MPNetworkPlayer! Aborting ...");
            return;
        }

        Getters.Add("Type", delegate() { return(NPlayer.PlayerType.name); });
        Getters.Add("Team", delegate() { return(NPlayer.TeamNumber); });
        Getters.Add("Health", delegate() { return(Health); });
        Getters.Add("Shots", delegate() { return(NPlayer.Shots); });
        Getters.Add("Position", delegate() { return(NPlayer.Transform.position); });
        Getters.Add("Rotation", delegate() { return(NPlayer.Transform.root.rotation); });
        Getters.Add("Items", delegate()
        {
            //Debug.Log("--------> GET ITEMS OF " + Inventory.transform.root.gameObject.name);

            ExitGames.Client.Photon.Hashtable items = new ExitGames.Client.Photon.Hashtable();

            if (Inventory == null)
            {
                return(items);
            }

            foreach (vp_ItemInstance i in Inventory.ItemInstances)
            {
                if (!items.ContainsKey(i.Type.name))
                {
                    //Debug.Log("ADDING ITEM TO HASHTABLE: " + i.Type.name + ", amount: " + Inventory.GetItemCount(i.Type));
                    items.Add(i.Type.name, Inventory.GetItemCount(i.Type));
                }
            }
            foreach (vp_UnitBankInstance u in Inventory.UnitBankInstances)
            {
                //Debug.Log("u: " + u);
                //Debug.Log("u.Type: " + u.Type);
                //Debug.Log("u.Type.name: " + u.Type.name);
                if (!items.ContainsKey(u.Type.name))
                {
                    //Debug.Log("ADDING UNITBANK TO HASHTABLE: " + u.Type.name + ", units: " + u.Count);
                    items.Add(u.Type.name, u.Count);
                }
            }
            foreach (vp_UnitBankInstance iu in Inventory.InternalUnitBanks)
            {
                //Debug.Log("iu: " + iu);
                //Debug.Log("iu.UnitType: " + iu.UnitType);
                //Debug.Log("iu.UnitType.name: " + iu.UnitType.name);
                if (iu.Count == 0)
                {
                    continue;
                }
                if (!items.ContainsKey(iu.UnitType.name))
                {
                    //Debug.Log("ADDING UNITS TO HASHTABLE: " +iu.UnitType.name + ", units: " + iu.Count);
                    items.Add(iu.UnitType.name, iu.Count);
                }
            }
            return(items);
        });
        Getters.Add("Weapon", delegate()
        {
            if (NPlayer.WeaponHandler == null)
            {
                return(0);
            }
            return(NPlayer.WeaponHandler.CurrentWeaponIndex);
        });


        // -------- setters --------


        Setters.Add("Type", delegate(object val) { NPlayer.PlayerType = vp_MPPlayerSpawner.GetPlayerTypeByName((string)val); });
        Setters.Add("Team", delegate(object val) { NPlayer.TeamNumber = (int)val; });
        Setters.Add("Health", delegate(object val) { Health = (float)val; });
        // NOTE: 'Shots' must never be updated with a lower (lagged) value or
        // simulation will go out of sync. however, we should be able
        // to set it to zero for game reset purposes (?)
        Setters.Add("Shots", delegate(object val) { NPlayer.Shots = (((int)val > 0) ? Mathf.Max(NPlayer.Shots, (int)val) : 0); });
        Setters.Add("Position", delegate(object val) { NPlayer.LastMasterPosition = (Vector3)val; NPlayer.SetPosition(NPlayer.LastMasterPosition); });
        Setters.Add("Rotation", delegate(object val) { NPlayer.LastMasterRotation = (Quaternion)val; NPlayer.SetRotation(NPlayer.LastMasterRotation); });
        Setters.Add("Items", delegate(object val)
        {
            //Debug.Log("--------> TRYING TO SET ITEMS");

            ExitGames.Client.Photon.Hashtable items = val as ExitGames.Client.Photon.Hashtable;
            if (items == null)
            {
                Debug.Log("failed to cast items as hashtable");
                return;
            }
            foreach (string s in items.Keys)
            {
                object amount;
                items.TryGetValue(s, out amount);
                //Debug.Log("trying to set: " + s + " to amount: " + (int)amount);

                // try to give item ...
                // NOTE: the following only has effect locally. unless master
                // triggered this call, items will be missing on remote machines

                //bool success =
                vp_MPItemList.TryGiveItem(transform, s, (int)amount);                                           // TODO: cache transform
                //if (success)
                //	Debug.Log("TryGiveItem SUCCESS: '" + s + "', amount: " + amount);
                //else
                //	Debug.Log("TryGiveItem FAIL: '" + s + "', amount: " + amount);
            }

            // refresh 1st person materials. this must be done after switching
            // weapons since 3rd person arms may have been toggled
            if (NPlayer is vp_MPLocalPlayer)
            {
                (NPlayer as vp_MPLocalPlayer).RefreshMaterials();
            }
        });
        Setters.Add("Weapon", delegate(object val)
        {
            if (NPlayer.Player == null)
            {
                return;
            }
            //bool r =
            NPlayer.Player.SetWeapon.TryStart((int)val);
            //Debug.Log("setting weapon of player " + NPlayer.Player + " to weapon: " + ((int)val).ToString() + ", result: " + r);
        });
    }