Project Training--Unity Multiplayer Game Development--Part 11

props game production

review

This time I mainly introduce the last game of the group, and I mainly participate in the development of some props.
The props information and code implementation are introduced below.

content

tool

In this game, props are randomly generated, including mines, missiles, random (with sprint and stealth) and blood bottles. Their control is determined by a script.

//生成道具
            if(PhotonNetwork.IsMasterClient && PropsGameManager.instance.isGameStart)
            {
    
    
                //计时
                if(currentTime > 0)
                {
    
    
                    currentTime -= Time.deltaTime;
                }
                else
                {
    
    
                    //判断道具数是否到达上限
                    if (currentCountInScene < maxSpawnCountPermit)
                    {
    
    
                        for (int i = 0; i < spawnCountOneTime; i++)
                        {
    
    

                            Vector3 pos = new Vector3(Random.Range(-18f, 18f), 1, Random.Range(-18f, 18f));
                            PhotonNetwork.Instantiate(Const.PUN_PROPS_GAME_PREFABS_BASEPATH 
                                + propsPrefabNameWithRandom[Random.Range(0, propsPrefabNameWithRandom.Length)]
                                , pos
                                , Quaternion.identity);
                            currentCountInScene++;
                        }
                    }
                    currentTime = spawnInterval;
                }
            }

Use an attribute to determine which type of prop it is

public enum PropsType
        {
    
    
            None,
            MissileProp,  //炮弹
            LandmineProp, //地雷
            CloakProp,
            HealthBottleProp, //血瓶
            SpeedUpProp,//疾步
            FlashSkill
        }

For each random birthday prop, there is a script on it to control the animation and type of the prop.

public PropsManager.PropsType propType = PropsManager.PropsType.HealthBottleProp;

The other is suspension control

  //悬浮
        private void Move()
        {
    
    
            startPos = transform.position;
            transform.DOPath(new Vector3[] {
    
     startPos, transform.position + new Vector3(0, 0.5f, 0), startPos }, 2)//Vector3.up//new Vector3(0,0.5f,0)
                .SetLoops(-1, LoopType.Restart);
            transform.DOBlendableLocalRotateBy(new Vector3(0, 360, 0), 4f, RotateMode.FastBeyond360);
                .SetLoops(-1, LoopType.Restart);//循环设置为-1为一直
        }

Every prop needs to be picked up, and this logic is done in the character's prop controller.

 public void OnTriggerEnter(Collider other)//TODO:同步背包
        {
    
    
            //IsPickingPropUp
            if (!other.gameObject.tag.Equals("DroppedProps"))
            {
    
    
                return;
            }
            if(other.gameObject.GetComponent<DroppedProps>() != null)
            {
    
    
                if (other.gameObject.GetComponent<DroppedProps>().propType == PropsManager.PropsType.HealthBottleProp)//血瓶不参与背包计算,且血瓶一个单独脚本执行逻辑
                {
    
    
                    return;
                }
            }

            //是否销毁(是否捡的起来)由玩家拥有者的背包判决。
            int index = 0;
            //***:主要用以通知主机,因为只有主机控制着销毁悬浮道具,可换为PhotonNetwork.IsMasterClient
            if (!photonView.IsMine)
            {
    
    
                if (PhotonNetwork.IsMasterClient)
                {
    
    
                    PhotonNetwork.Destroy(other.gameObject);
                    PropsManager.DecreaseCurrentCount();//场上数量-1
                }
                else
                {
    
    
                    other.gameObject.SetActive(false);
                }
            }
            else//自己
            {
    
    
                index = FindVacantIndex();
                DroppedProps dp = other.gameObject.GetComponent<DroppedProps>();
                DroppedRandomProps drp = other.gameObject.GetComponent<DroppedRandomProps>();
                if (dp != null)
                {
    
    
                    itemBag[index] = dp.propType; //添加背包里捡的道具的类型
                    //UI
                    bagItemsPanel.GetChild(index).GetComponent<Image>().sprite
                        = GetSpriteByPropType(dp.propType);
                }
                else if(drp != null)
                {
    
    
                    itemBag[index] = drp.propType; //添加背包里捡的道具的类型
                    //UI
                    bagItemsPanel.GetChild(index).GetComponent<Image>().sprite
                        = GetSpriteByPropType(drp.propType);
                }
                other.gameObject.SetActive(false);
            }
            
            AudioManager.instance.PropsGamePickUp();
        }

Each pickup is prompted by a sound effect.

Second, use props

The first is the landmine. After picking up the mine, if you use it, it takes 3 seconds to place it, and it cannot be interrupted during the period. Then, after placing it, there will be some particle effects to remind you that there are landmines in the area.

 private void UseProp(PropsManager.PropsType type)//TODO:E技能计时UI
        {
    
    
            switch (type)
            {
    
    
                case PropsManager.PropsType.None:
                    AudioManager.instance.NoProps();
                    break;
                case PropsManager.PropsType.LandmineProp:
                    //先判断当前执行的动画是不是站立idel
                    if (animator.GetCurrentAnimatorStateInfo(0).IsName("IdleNormal02_AR_Anim"))
                    {
    
    
                        Debug.Log("执行放置地雷的操作1");
                        DoAnimationWithEffect(2);
                        //创建地雷的方法
                        StartCoroutine(CreateLandMine(this.gameObject.transform.position));
                    }
                    else//不在空闲状态无法放置
                    {
    
    
                        tipText.text = "放置被打断";
                        Invoke("EmptyTipText", 1.5f);
                    }
                    break;

                default:
                    break;
            }
        }

Use rpc to generate mines

        //生成地雷
        private IEnumerator CreateLandMine(Vector3 v) 
        {
    
    
            AudioManager.instance.SetLandmine(v);
            yield return new WaitForSeconds(3); //等待3秒
            v.y = 0.001f;
            photonView.RPC("LandMineCreate", RpcTarget.AllViaServer, v);
       
        }

        [PunRPC]
        public void LandMineCreate(Vector3 pos) //使用rpc进行生成地雷,同步其他所有的人,都会生成地雷。
        {
    
    
            GameObject landmine = Instantiate(landminePrefab, pos, Quaternion.identity);
        }

Summarize

This time the main introduction is the generation and placement of mines, and the destruction of mines and other props will be introduced later.

Guess you like

Origin blog.csdn.net/qq_53259920/article/details/125242793