Exemplo n.º 1
0
        private void OnSetPassiveRadarTargetEvent(KeyCode keyCode)
        {
            if (StatMaster.isClient)
            {
                return;
            }
            if (!Machine.Active().isSimulating)
            {
                return;
            }
            if (!gameObject.activeSelf)
            {
                return;
            }
            if (!Switch)
            {
                return;
            }
            if (RadarType == RadarTypes.ActiveRadar)
            {
                return;
            }
            KeyCode key = parentBlock.GetComponent <RocketScript>().GroupFireKey.GetKey(0);

            if (key != keyCode)
            {
                return;
            }

            StartCoroutine(DelayedSetTarget());

            IEnumerator DelayedSetTarget()
            {
                yield return(new WaitForFixedUpdate());

                if (tempRadarSet[parentBlock.ParentMachine.PlayerID][key].Count > 0 && RadarTarget == null)
                {
                    System.Random random = new System.Random();
                    int           index  = random.Next(tempRadarSet[parentBlock.ParentMachine.PlayerID][key].Count);
#if DEBUG
                    Debug.Log("Available Radar: " + tempRadarSet.Count);
                    Debug.Log("Choose: " + index);
#endif
                    passiveSourceRadar = tempRadarSet[parentBlock.ParentMachine.PlayerID][key].ElementAt(index);
                }
                SetTarget(passiveSourceRadar?.RadarTarget);
                yield return(null);
            }
        }
 public void Setup(BlockBehaviour sourceBlock, Rigidbody sourceRigidbody, RadarScript sourceRadar, float sourceSpeedPower,
                   float sourceSearchAngle, float sourceTorque, bool constantForce)
 {
     parentBlock             = sourceBlock;
     parentRigidbody         = sourceRigidbody;
     blockRadar              = sourceRadar;
     this.sourceSpeedPower   = sourceSpeedPower;
     enableAerodynamicEffect = false;
     searchAngle             = sourceSearchAngle;
     torque             = sourceTorque;
     this.constantForce = constantForce;
     //preTargetTransform = new BlockBehaviour();
     pFactor = BlockEnhancementMod.Configuration.GetValue <float>("GuideControl P Factor");
     iFactor = BlockEnhancementMod.Configuration.GetValue <float>("GuideControl I Factor");
     dFactor = BlockEnhancementMod.Configuration.GetValue <float>("GuideControl D Factor");
 }
Exemplo n.º 3
0
        public void ClearTarget(bool RemoveTargetFromList = true)
        {
            if (!gameObject.activeSelf)
            {
                return;
            }
            if (parentBlock == null)
            {
                return;
            }

            if (RadarTarget != null)
            {
                if (RemoveTargetFromList)
                {
                    targetList.Remove(RadarTarget);
                }
                SendClientTargetNull();
                RadarTarget = null;
                if (RadarType == RadarTypes.PassiveRadar)
                {
                    passiveSourceRadar = null;
                }
            }

            if (RadarType == RadarTypes.ActiveRadar)
            {
                var rs = parentBlock.GetComponent <RocketScript>();
                if (rs == null)
                {
                    return;
                }

                KeyCode key = rs.GroupFireKey.GetKey(0);
                OnClearPassiveRadarTarget?.Invoke(key);
            }
#if DEBUG
            Debug.Log("clear target");
#endif
        }
Exemplo n.º 4
0
        public MessageController()
        {
            //Initiating messages
            Messages.rocketFiredMsg = ModNetworking.CreateMessageType(DataType.Block);
            Messages.rocketTargetBlockBehaviourMsg = ModNetworking.CreateMessageType(DataType.Block, DataType.Block);
            Messages.rocketTargetEntityMsg         = ModNetworking.CreateMessageType(DataType.Entity, DataType.Block);
            Messages.rocketTargetNullMsg           = ModNetworking.CreateMessageType(DataType.Block);
            Messages.rocketRayToHostMsg            = ModNetworking.CreateMessageType(DataType.Vector3, DataType.Vector3, DataType.Block);
            Messages.rocketHighExploPosition       = ModNetworking.CreateMessageType(DataType.Vector3, DataType.Single);
            Messages.rocketLockOnMeMsg             = ModNetworking.CreateMessageType(DataType.Block, DataType.Integer);
            Messages.rocketLostTargetMsg           = ModNetworking.CreateMessageType(DataType.Block);

            //Initiating callbacks
            ModNetworking.Callbacks[Messages.rocketHighExploPosition] += (Message msg) =>
            {
                if (StatMaster.isClient)
                {
                    Vector3 position            = (Vector3)msg.GetData(0);
                    float   bombExplosiveCharge = (float)msg.GetData(1);
                    int     levelBombCategory   = 4;
                    int     levelBombID         = 5001;
                    float   radius      = 7f;
                    float   power       = 3600f;
                    float   torquePower = 100000f;
                    float   upPower     = 0.25f;
                    try
                    {
                        GameObject bomb = UnityEngine.Object.Instantiate(PrefabMaster.LevelPrefabs[levelBombCategory].GetValue(levelBombID).gameObject);
                        bomb.transform.position = position;
                        ExplodeOnCollide bombControl = bomb.GetComponent <ExplodeOnCollide>();
                        bomb.transform.localScale = Vector3.one * bombExplosiveCharge;
                        bombControl.radius        = radius * bombExplosiveCharge;
                        bombControl.power         = power * bombExplosiveCharge;
                        bombControl.torquePower   = torquePower * bombExplosiveCharge;
                        bombControl.upPower       = upPower;
                        bombControl.Explodey();
                    }
                    catch { }
                }
            };

            ModNetworking.Callbacks[Messages.rocketFiredMsg] += (Message msg) =>
            {
                Block       rocketBlock = (Block)msg.GetData(0);
                TimedRocket rocket      = rocketBlock.GameObject.GetComponent <TimedRocket>();
                RocketsController.Instance.UpdateRocketFiredStatus(rocket);

                RadarScript radar = rocketBlock.GameObject.GetComponentInChildren <RadarScript>();
                radar.Switch = true;
            };

//            ModNetworking.Callbacks[Messages.rocketTargetBlockBehaviourMsg] += (Message msg) =>
//            {
//#if DEBUG
//                Debug.Log("Receive block target");
//#endif
//                Block rocketBlock = (Block)msg.GetData(1);
//                RadarScript radar = rocketBlock.GameObject.GetComponentInChildren<RadarScript>();

//                Block target = ((Block)msg.GetData(0));

//                radar.SetTarget(new Target(target.InternalObject));
//                //radar.target.transform = ((Block)msg.GetData(0)).GameObject.transform;
//                //radar.target.collider = radar.target.transform.GetComponentInChildren<Collider>(true);

//            };

//            ModNetworking.Callbacks[Messages.rocketTargetEntityMsg] += (Message msg) =>
//            {
//#if DEBUG
//                Debug.Log("Receive entity target");
//#endif
//                Block rocketBlock = (Block)msg.GetData(1);
//                RadarScript radar = rocketBlock.GameObject.GetComponentInChildren<RadarScript>();

//                GenericEntity target = ((GenericEntity)msg.GetData(0));

//                radar.SetTarget(new Target(target));

//                //radar.target.transform = ((Entity)msg.GetData(0)).GameObject.transform;
//                //radar.target.collider = radar.target.transform.GetComponentInChildren<Collider>(true);
//            };

            ModNetworking.Callbacks[Messages.rocketTargetNullMsg] += (Message msg) =>
            {
#if DEBUG
                Debug.Log("Clear Target");
#endif
                Block rocketBlock = (Block)msg.GetData(0);
                if (rocketBlock == null)
                {
                    return;
                }

                RadarScript radar = rocketBlock.GameObject.GetComponentInChildren <RadarScript>();
                if (radar == null)
                {
                    return;
                }

                radar.ClearTarget();
                //radar.target.transform = null;
                //radar.target.collider = null;
            };

            ModNetworking.Callbacks[Messages.rocketRayToHostMsg] += (Message msg) =>
            {
                Block       rocketBlock = (Block)msg.GetData(2);
                RadarScript radar       = rocketBlock.GameObject.GetComponentInChildren <RadarScript>();
                radar.rayFromClient         = new Ray((Vector3)msg.GetData(0), (Vector3)msg.GetData(1));
                radar.receivedRayFromClient = true;
            };

            ModNetworking.Callbacks[Messages.rocketLockOnMeMsg] += (Message msg) =>
            {
                Block rocketBlock     = (Block)msg.GetData(0);
                int   targetMachineID = (int)msg.GetData(1);
                RocketsController.Instance.UpdateRocketTarget(rocketBlock.InternalObject, targetMachineID);
            };
            ModNetworking.Callbacks[Messages.rocketLostTargetMsg] += (Message msg) =>
            {
                Block rocketBlock = (Block)msg.GetData(0);
                if (rocketBlock == null)
                {
                    return;
                }

                RocketsController.Instance.RemoveRocketTarget(rocketBlock.InternalObject);
            };
        }
        public override void OnSimulateStart_EnhancementEnabled()
        {
            rocketInBuildSent = removedFromGroup = false;

            // Read the charge from rocket
            explosiveCharge = bombExplosiveCharge = rocket.ChargeSlider.Value;

            // Make sure the high explo mode is not too imba
            if (HighExploToggle.IsActive && !EnhanceMore)
            {
                bombExplosiveCharge = Mathf.Clamp(explosiveCharge, 0f, 1.5f);
            }

            if (GuidedRocketToggle.IsActive)
            {
                // Initialisation for simulation
                launchTimeRecorded = bombHasExploded = rocketExploMsgSent = false;
                float searchAngle = Mathf.Clamp(ActiveGuideRocketSearchAngleSlider.Value, 0, EnhanceMore ? maxSearchAngleNo8 : maxSearchAngleNormal);
                float searchRange = EnhanceMore ? 5000f : 2000f;

                //Add radar
                //Collider[] selfColliders = rocket.gameObject.GetComponentsInChildren<Collider>();
                radarObject = new GameObject("RocketRadar");
                radarObject.transform.SetParent(rocket.transform);
                radarObject.transform.position      = transform.position;
                radarObject.transform.rotation      = transform.rotation;
                radarObject.transform.localPosition = Vector3.forward * 0.5f;
                radarObject.transform.localScale    = restoreScale(rocket.transform.localScale);
                radar = radarObject.GetComponent <RadarScript>() ?? radarObject.AddComponent <RadarScript>();
                radar.Setup(BB, rocketRigidbody, searchRange, searchAngle, RadarTypeMenu.Value, ShowRadarToggle.IsActive);
                radar.Setup(ShowPredictionToggle.IsActive, ProjectileSpeedSlider.Value, DragSlider.Value);

                //Workaround when radar can be ignited hence explode the rocket
                FireTag fireTag = radarObject.AddComponent <FireTag>();
                fireTag.enabled = true;
                Rigidbody rigidbody = radarObject.AddComponent <Rigidbody>();
                rigidbody.isKinematic = true;
                rigidbody.mass        = 0.0001f;
                rigidbody.drag        = 0f;

                //Stop colliding with its own colliders
                //if (selfColliders.Length > 0)
                //{
                //    foreach (var collider in selfColliders)
                //    {
                //        Physics.IgnoreCollision(collider, radar.meshCollider, true);
                //    }
                //}

                //Set up Guide controller
                guideObject = new GameObject("GuideController");
                guideObject.transform.SetParent(rocket.transform);
                guideObject.transform.position   = transform.position;
                guideObject.transform.rotation   = transform.rotation;
                guideObject.transform.localScale = Vector3.one;
                guideController = guideObject.GetComponent <GuideController>() ?? guideObject.AddComponent <GuideController>();
                guideController.Setup(rocket, rocketRigidbody, radar, rocket.PowerSlider.Value, searchAngle, Mathf.Clamp(TorqueSlider.Value, 0, 100), false);

                StopAllCoroutines();
            }

            smokeTrail = null;
            if (NoSmokeToggle.IsActive)
            {
                foreach (var value in rocket.trail)
                {
                    var emission = value.emission;
                    emission.enabled = false;
                }
            }
            else
            {
                foreach (var value in rocket.trail)
                {
                    if (value.name.ToLower() == "smoketrail")
                    {
                        smokeTrail = value;

                        var colt = smokeTrail.colorOverLifetime;

                        //Debug.Log(colt.color.colorMin + "||" + colt.color.colorMax);
                        //Debug.Log(colt.color.mode);
                        //Debug.Log(colt.color.gradient.alphaKeys.Length);
                        //colt.color.gradient.alphaKeys.ToList().ForEach(key => Debug.Log(key.alpha + "-" + key.time));
                        //Debug.Log(colt.color.gradient.colorKeys.Length);
                        //colt.color.gradient.colorKeys.ToList().ForEach(key => Debug.Log(key.color + "-" + key.time));

                        //Debug.Log("??" + trailSmokePropertise.ToString());
                        colt.color = new ParticleSystem.MinMaxGradient(new Gradient()
                        {
                            alphaKeys = new GradientAlphaKey[]
                            {
                                new GradientAlphaKey(0f, 0f),
                                new GradientAlphaKey(0.5f, 0.01f),
                                new GradientAlphaKey(trailSmokePropertise.StartAlpha, trailSmokePropertise.StartAlphaTime),
                                new GradientAlphaKey(trailSmokePropertise.EndAlpha, trailSmokePropertise.EndAlphaTime),
                                new GradientAlphaKey(0f, 0.8f)
                            },
                            colorKeys = new GradientColorKey[]
                            {
                                new GradientColorKey(new Color(1f, 1f, 0f, 1f), 0f),
                                new GradientColorKey(new Color(0.882f, 0.365f, 0.176f, 1f), 0.019f),
                                new GradientColorKey(trailSmokePropertise.StartColor, trailSmokePropertise.StartColorTime),
                                new GradientColorKey(trailSmokePropertise.EndColor, trailSmokePropertise.EndColorTime)
                            }
                        });;
                        break;
                    }
                }
            }


            Vector3 restoreScale(Vector3 rocketScale)
            {
                var single  = 1f / rocketScale.x;
                var single1 = 1f / rocketScale.y;
                var single2 = 1f / rocketScale.z;

                return(new Vector3(single, single1, single2));
            }

            //Initialise Dict in RocketsController
            if (GroupFireKey.GetKey(0) != KeyCode.None && !GroupFireKey.Ignored)
            {
                if (!RocketsController.Instance.playerGroupedRockets.ContainsKey(rocket.ParentMachine.PlayerID))
                {
                    RocketsController.Instance.playerGroupedRockets.Add(rocket.ParentMachine.PlayerID, new Dictionary <KeyCode, HashSet <TimedRocket> >());
                }
                if (!RocketsController.Instance.playerGroupedRockets[rocket.ParentMachine.PlayerID].ContainsKey(GroupFireKey.GetKey(0)))
                {
                    RocketsController.Instance.playerGroupedRockets[rocket.ParentMachine.PlayerID].Add(GroupFireKey.GetKey(0), new HashSet <TimedRocket>());
                }
                if (!RocketsController.Instance.playerGroupedRockets[rocket.ParentMachine.PlayerID][GroupFireKey.GetKey(0)].Contains(rocket))
                {
                    RocketsController.Instance.playerGroupedRockets[rocket.ParentMachine.PlayerID][GroupFireKey.GetKey(0)].Add(rocket);
                }
            }
        }