private void OnVehiclePrefabLoaded(TechType techType, GameObject prefab, NitroxId id, Vector3 spawnPosition, Quaternion spawnRotation, IEnumerable <InteractiveChildObjectIdentifier> interactiveChildIdentifiers, Optional <NitroxId> dockingBayId, string name, Vector3[] hsb, float health) { // Partially copied from SubConsoleCommand.OnSubPrefabLoaded GameObject gameObject = Utils.SpawnPrefabAt(prefab, null, spawnPosition); gameObject.transform.rotation = spawnRotation; gameObject.SetActive(true); gameObject.SendMessage("StartConstruction", SendMessageOptions.DontRequireReceiver); CrafterLogic.NotifyCraftEnd(gameObject, CraftData.GetTechType(gameObject)); Rigidbody rigidBody = gameObject.RequireComponent <Rigidbody>(); rigidBody.isKinematic = false; NitroxEntity.SetNewId(gameObject, id); // Updates names and colours with persisted data if (techType == TechType.Seamoth || techType == TechType.Exosuit) { Vehicle vehicle = gameObject.GetComponent <Vehicle>(); if (dockingBayId.HasValue) { GameObject dockingBayBase = NitroxEntity.RequireObjectFrom(dockingBayId.Value); VehicleDockingBay dockingBay = dockingBayBase.GetComponentInChildren <VehicleDockingBay>(); dockingBay.DockVehicle(vehicle); } else if (techType == TechType.Exosuit) { // exosuits tend to fall through the ground after spawning. This should prevent that vehicle.ReflectionSet("onGround", true); } if (!string.IsNullOrEmpty(name)) { vehicle.vehicleName = name; vehicle.subName?.DeserializeName(vehicle.vehicleName); } if (hsb != null) { vehicle.vehicleColors = hsb; vehicle.subName?.DeserializeColors(hsb); } vehicle.GetComponent <LiveMixin>().health = health; } else if (techType == TechType.Cyclops) { GameObject target = NitroxEntity.RequireObjectFrom(id); SubNameInput subNameInput = target.RequireComponentInChildren <SubNameInput>(); SubName subNameTarget = (SubName)subNameInput.ReflectionGet("target"); subNameInput.OnNameChange(name); subNameTarget.DeserializeColors(hsb); target.GetComponent <LiveMixin>().health = health; // Set internal and external lights SetCyclopsModes(id); } VehicleChildObjectIdentifierHelper.SetInteractiveChildrenIds(gameObject, interactiveChildIdentifiers); // Send event after everything is created if (VehicleCreated != null) { VehicleCreated(gameObject); } }
//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); Vector3[] 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?.GetColors(); } else { //Cyclops try { GameObject target = NitroxEntity.RequireObjectFrom(constructedObjectId); SubNameInput subNameInput = target.RequireComponentInChildren <SubNameInput>(); SubName subNameTarget = (SubName)subNameInput.ReflectionGet("target"); name = subNameTarget?.GetName(); HSB = subNameTarget?.GetColors(); 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.Model(), constructedObjectId, gameObject.transform.position, gameObject.transform.rotation, 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); }
public void SpawnDefaultBatteries(VehicleModel vehicleModel) { GameObject gameObject = NitroxEntity.RequireObjectFrom(vehicleModel.Id); SpawnDefaultBatteries(gameObject, VehicleChildObjectIdentifierHelper.ExtractInteractiveChildren(gameObject)); }
public void UpdateVehiclePosition(VehicleMovementData vehicleModel, Optional <RemotePlayer> player) { Vector3 remotePosition = vehicleModel.Position; Vector3 remoteVelocity = vehicleModel.Velocity; Quaternion remoteRotation = vehicleModel.Rotation; Vector3 angularVelocity = vehicleModel.AngularVelocity; Vehicle vehicle = null; SubRoot subRoot = null; Optional <GameObject> opGameObject = NitroxEntity.GetObjectFrom(vehicleModel.Id); if (opGameObject.HasValue) { GameObject gameObject = opGameObject.Value; vehicle = gameObject.GetComponent <Vehicle>(); subRoot = gameObject.GetComponent <SubRoot>(); MultiplayerVehicleControl mvc = null; if (subRoot != null) { mvc = subRoot.gameObject.EnsureComponent <MultiplayerCyclops>(); subRoot.GetComponent <LiveMixin>().health = vehicleModel.Health; } else if (vehicle != null) { SeaMoth seamoth = vehicle as SeaMoth; Exosuit exosuit = vehicle as Exosuit; if (seamoth) { mvc = seamoth.gameObject.EnsureComponent <MultiplayerSeaMoth>(); } else if (exosuit) { mvc = exosuit.gameObject.EnsureComponent <MultiplayerExosuit>(); if (vehicleModel.GetType() == typeof(ExosuitMovementData)) { ExosuitMovementData exoSuitMovement = (ExosuitMovementData)vehicleModel; mvc.SetArmPositions(exoSuitMovement.LeftAimTarget, exoSuitMovement.RightAimTarget); } else { Log.Error($"{nameof(Vehicles)}: Got exosuit vehicle but no ExosuitMovementData"); } } vehicle.GetComponent <LiveMixin>().health = vehicleModel.Health; } if (mvc != null) { mvc.SetPositionVelocityRotation(remotePosition, remoteVelocity, remoteRotation, angularVelocity); mvc.SetThrottle(vehicleModel.AppliedThrottle); mvc.SetSteeringWheel(vehicleModel.SteeringWheelYaw, vehicleModel.SteeringWheelPitch); } } if (player.HasValue) { RemotePlayer playerInstance = player.Value; playerInstance.SetVehicle(vehicle); playerInstance.SetSubRoot(subRoot); playerInstance.SetPilotingChair(subRoot?.GetComponentInChildren <PilotingChair>()); playerInstance.AnimationController.UpdatePlayerAnimations = false; } }
private void SetPlayerGameObjectId(NitroxId id) { NitroxEntity.SetNewId(Player.mainObject, id); Log.Info("Received initial sync Player GameObject Id: " + id); }
// Send ping to other players public static void Postfix(CyclopsSonarButton __instance) { NitroxId id = NitroxEntity.GetId(__instance.subRoot.gameObject); NitroxServiceLocator.LocateService <Cyclops>().BroadcastSonarPing(id); }
public static void Prefix(ExosuitTorpedoArm __instance, bool __result, TorpedoType torpedoType, Transform siloTransform) { if (torpedoType != null) { ExosuitArmAction action = ExosuitArmAction.START_USE_TOOL; if (siloTransform == __instance.siloSecond) { action = ExosuitArmAction.ALT_HIT; } if (siloTransform != __instance.siloFirst && siloTransform != __instance.siloSecond) { Log.Error("Exosuit torpedo arm siloTransform is not first or second silo " + NitroxEntity.GetId(__instance.gameObject)); } NitroxServiceLocator.LocateService <ExosuitModuleEvent>().BroadcastArmAction(TechType.ExosuitTorpedoArmModule, __instance, action, Optional.Of(Player.main.camRoot.GetAimingTransform().forward), Optional.Of(Player.main.camRoot.GetAimingTransform().rotation) ); } }
public void UpdateVehiclePosition(VehicleMovementData vehicleModel, Optional <RemotePlayer> player) { Optional <GameObject> opGameObject = NitroxEntity.GetObjectFrom(vehicleModel.Id); Vehicle vehicle = null; SubRoot subRoot = null; if (opGameObject.HasValue) { Rocket rocket = opGameObject.Value.GetComponent <Rocket>(); vehicle = opGameObject.Value.GetComponent <Vehicle>(); subRoot = opGameObject.Value.GetComponent <SubRoot>(); MultiplayerVehicleControl mvc = null; if (subRoot) { mvc = subRoot.gameObject.EnsureComponent <MultiplayerCyclops>(); } else if (vehicle) { if (vehicle.docked) { Log.Debug($"For vehicle {vehicleModel.Id} position update while docked, will not execute"); return; } switch (vehicle) { case SeaMoth seamoth: { mvc = seamoth.gameObject.EnsureComponent <MultiplayerSeaMoth>(); break; } case Exosuit exosuit: { mvc = exosuit.gameObject.EnsureComponent <MultiplayerExosuit>(); if (vehicleModel is ExosuitMovementData exoSuitMovement) { mvc.SetArmPositions(exoSuitMovement.LeftAimTarget.ToUnity(), exoSuitMovement.RightAimTarget.ToUnity()); } else { Log.Error($"{nameof(Vehicles)}: Got exosuit vehicle but no ExosuitMovementData"); } break; } } } else if (rocket) { rocket.transform.position = vehicleModel.Position.ToUnity(); rocket.transform.rotation = vehicleModel.Rotation.ToUnity(); } if (mvc) { mvc.SetPositionVelocityRotation( vehicleModel.Position.ToUnity(), vehicleModel.Velocity.ToUnity(), vehicleModel.Rotation.ToUnity(), vehicleModel.AngularVelocity.ToUnity() ); mvc.SetThrottle(vehicleModel.AppliedThrottle); mvc.SetSteeringWheel(vehicleModel.SteeringWheelYaw, vehicleModel.SteeringWheelPitch); } } if (player.HasValue) { RemotePlayer playerInstance = player.Value; playerInstance.SetVehicle(vehicle); playerInstance.SetSubRoot(subRoot); playerInstance.SetPilotingChair(subRoot?.GetComponentInChildren <PilotingChair>()); playerInstance.AnimationController.UpdatePlayerAnimations = false; } }
private static void Callback(Rocket rocketInstanceAttachedToConstructor, TechType currentStageTech) { NitroxId rocketId = NitroxEntity.GetId(rocketInstanceAttachedToConstructor.gameObject); NitroxServiceLocator.LocateService <Rockets>().BroadcastRocketStateUpdate(rocketId, currentStageTech); }