예제 #1
0
        //We need to get TechType from parameters because CraftData can't resolve TechType.Cyclops by himself
        public VehicleModel BuildVehicleModelFrom(GameObject gameObject, TechType techType)
        {
            if (VehicleHelper.IsVehicle(techType))
            {
                List <InteractiveChildObjectIdentifier> childIdentifiers = VehicleChildObjectIdentifierHelper.ExtractInteractiveChildren(gameObject);
                Optional <Vehicle> opvehicle = Optional.OfNullable(gameObject.GetComponent <Vehicle>());

                NitroxId        constructedObjectId = NitroxEntity.GetId(gameObject);
                NitroxVector3[] hsb    = VehicleHelper.GetPrimalDefaultColours();
                string          name   = string.Empty;
                float           health = 200f;


                if (opvehicle.HasValue)
                { //Seamoth & Exosuit
                    Optional <LiveMixin> livemixin = Optional.OfNullable(opvehicle.Value.GetComponent <LiveMixin>());

                    if (livemixin.HasValue)
                    {
                        health = livemixin.Value.health;
                    }

                    name = opvehicle.Value.GetName();

                    if (techType == TechType.Exosuit)
                    {   //For odd reasons the default colors aren't set yet for exosuit so we force it
                        opvehicle.Value.ReflectionCall("RegenerateRenderInfo", false, false);
                    }

                    hsb = opvehicle.Value.subName.AliveOrNull()?.GetColors().ToDto();
                }
                else
                { //Cyclops
                    try
                    {
                        GameObject   target        = NitroxEntity.RequireObjectFrom(constructedObjectId);
                        SubNameInput subNameInput  = target.RequireComponentInChildren <SubNameInput>();
                        SubName      subNameTarget = (SubName)subNameInput.ReflectionGet("target");

                        name = subNameTarget.GetName();
                        hsb  = subNameTarget.AliveOrNull()?.GetColors().ToDto();

                        Optional <LiveMixin> livemixin = Optional.OfNullable(target.GetComponent <LiveMixin>());

                        if (livemixin.HasValue)
                        {
                            health = livemixin.Value.health;
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error($"{nameof(Vehicles)}: Error while trying to spawn a cyclops ({constructedObjectId})", ex);
                    }
                }

                return(VehicleModelFactory.BuildFrom(
                           techType.ToDto(),
                           constructedObjectId,
                           gameObject.transform.position.ToDto(),
                           gameObject.transform.rotation.ToDto(),
                           childIdentifiers,
                           Optional.Empty,
                           name,
                           hsb ?? VehicleHelper.GetDefaultColours(techType), //Shouldn't be null now, but just in case
                           health
                           ));
            }
            else
            {
                Log.Error($"{nameof(Vehicles)}: Impossible to build from a non-vehicle GameObject (Received {techType})");
            }

            return(null);
        }