public static void Launch_Network(Message message)
        {
            if (StatMaster.isClient)
            {
                var block        = (Block)message.GetData(0);
                var bulletNumber = (int)message.GetData(1);
                var guid         = new Guid(message.GetData(2).ToString());
                var position     = (Vector3)message.GetData(3);

                RocketPodBlockScript rocketPodBlockScript = block.GameObject.GetComponent <RocketPodBlockScript>();
                rocketPodBlockScript.BulletCurrentNumber = bulletNumber;
                rocketPodBlockScript.Fire_Network();

                var rocketPool = rocketPodBlockScript.rocketPool;

                GameObject gameObject = rocketPool.Work.GetChild(0).gameObject;
                gameObject.transform.localPosition += position;
                gameObject.transform.SetParent(/*transform.parent*/ block.Machine.InternalObject.transform);
                gameObject.SetActive(true);

                RocketScript rocketScript = gameObject.GetComponent <RocketScript>();
                rocketScript.Guid          = guid;
                rocketScript.LaunchEnabled = true;

                //block.GameObject.SetActive(false);
            }
        }
Example #2
0
        void initRocketScript()
        {
            Rigidbody rigidbody = Rigidbody;

            rigidbody.centerOfMass               += Vector3.forward * 0.5f;
            rocketScript                          = GetComponent <RocketScript>() ?? gameObject.AddComponent <RocketScript>();
            rocketScript.ThrustDirection          = Vector3.right;
            rocketScript.ThrustPoint              = rigidbody.centerOfMass;
            rocketScript.DelayEnableCollisionTime = 0.02f;
            rocketScript.ExplodePower             = 1f;
            rocketScript.ExplodeRadius            = 10f;
            rocketScript.effectOffset             = new Vector3(-1.4f, 0, 0.5f);
            rocketScript.OnExplodeFinal          += () => { Destroy(rocketScript.transform.gameObject); };
            rocketScript.OnExplode               += () => { isExploded = true; healthBar.health = 0; };
        }
        private void Rocket_Reusing(int index)
        {
            //火箭弹安装位置 本地坐标转世界坐标
            Vector3 offset = new Vector3(-0.375f, 0f, 0.15f);
            Vector3 pos    = getRealPosition(index, offset);
            //火箭弹重新设置
            GameObject rocket = Rockets[index] = rocketPool.Idle.GetChild(0).gameObject;

            rocket.transform.SetParent(rocketPool.Work);
            rocket.transform.position = pos;
            rocket.transform.rotation = transform.rotation;
            rocket.name = "Rocket " + index;
            rocket.SetActive(true);
            //火箭弹脚本 参数重新设置
            RocketScript rocketScript = rocket.GetComponent <RocketScript>();

            rocketScript.Reusing(thrustForce_slider.Value, thrustTime_slider.Value * 10f, DragForce_slider.Value);
        }
        public void Fire()
        {
            if (StatMaster.isClient)
            {
                return;
            }
            StartCoroutine(Launch(fireEvent));

            void fireEvent()
            {
                GameObject gameObject = rocketPool.Work.GetChild(0).gameObject;

                Vector3 position = Vector3.right * Vector3.Project(Rigidbody.velocity, transform.right).magnitude *transform.localScale.x *Time.fixedDeltaTime * 3f;

                gameObject.transform.localPosition += position;
                gameObject.transform.SetParent(/*transform.parent*/ Machine.InternalObject.transform);
                gameObject.SetActive(true);

                Rigidbody rigidbody = gameObject.GetComponent <Rigidbody>();

                rigidbody.isKinematic = false;
                rigidbody.velocity    = Rigidbody.velocity;
                rigidbody.AddRelativeForce(Vector3.right * 25f, ForceMode.Impulse);
                RocketScript rocketScript = gameObject.GetComponent <RocketScript>();

                rocketScript.LaunchEnabled = true;

                if (StatMaster.isHosting)
                {
                    var message = LaunchMessage.CreateMessage(BlockBehaviour, BulletCurrentNumber, rocketScript.Guid.ToString(), position);
                    ModNetworking.SendToAll(message);
                }

                StartCoroutine(delay());
                IEnumerator delay()
                {
                    yield return(new WaitForSeconds(rocketScript.DelayEnableCollisionTime));

                    rigidbody.detectCollisions = true;
                    gameObject.GetComponentInChildren <CapsuleCollider>().isTrigger = false;
                    yield break;
                }
            }
        }
        //火箭弹实例化
        private void Rocket_Instantiate(int index)
        {
            //火箭弹安装位置 本地坐标转世界坐标
            Vector3 offset = new Vector3(-0.375f, 0f, 0.15f);
            Vector3 pos    = getRealPosition(index, offset);

            //火箭弹实例化
            GameObject rocket = (GameObject)Instantiate(BulletObject, pos, transform.rotation, rocketPool.Work);

            rocket.transform.localScale = Vector3.Scale(rocket.transform.localScale, transform.localScale);
            rocket.name = "Rocket " + index;
            rocket.SetActive(true);
            Rockets[index] = rocket;

            //火箭弹刚体 不开启碰撞 不受物理影响
            Rigidbody rigidbody = rocket.GetComponent <Rigidbody>();

            rigidbody.detectCollisions = false;
            rigidbody.isKinematic      = true;
            //火箭弹脚本 初始化
            RocketScript rocketScript = rocket.GetComponent <RocketScript>();

            rocketScript.ThrustDirection          = Vector3.right;
            rocketScript.ThrustPoint              = rigidbody.centerOfMass;
            rocketScript.ThrustForce              = thrustForce_slider.Value;
            rocketScript.ThrustTime               = thrustTime_slider.Value * 10f;
            rocketScript.DelayLaunchTime          = 0f;
            rocketScript.DelayEnableCollisionTime = 0.04f;
            rocketScript.ExplodePower             = 1f;
            rocketScript.ExplodeRadius            = 10f;
            rocketScript.DragClamp = DragForce_slider.Value;

            rocketScript.effectOffset    = new Vector3(-1.15f, 0, 0f);
            rocketScript.OnExplodeFinal += () =>
            {
                string i = rocketScript.gameObject.name;
                i = i.Substring(i.LastIndexOf(' '));
                Rockets[int.Parse(i)] = null;
                rocketScript.gameObject.transform.SetParent(rocketPool.Idle);
            };
        }
        public static void ExplodeNetworkingEvent(Message message)
        {
            if (StatMaster.isClient)
            {
                var guid = new Guid(((string)message.GetData(0)));

                try
                {
                    RocketScript rocketScript = GameObject.FindObjectsOfType <RocketScript>().ToList().Find(match => match.Guid == guid);

                    rocketScript.Explody_Network();
                }
                catch { }

                try
                {
                    RocketScript rocketScript = GameObject.FindObjectsOfType <RocketBlockScript>().ToList().Find(match => match.rocketScript.Guid == guid).rocketScript;

                    rocketScript.Explody_Network();
                }
                catch { }
            }
        }