Skip to content

Commit

Permalink
v1.13.0 (#669)
Browse files Browse the repository at this point in the history
## Major features
- New API methods to: Add ship logs from xml, add dialogue from xml,
create a new planet from json string. All of these methods work directly
with strings or XElements and don't load files on their own.

## Improvements
- Pitch anglerfish sounds based on scale. Resolves #160.

## Bug fixes
- Fixes ship being broken when spawning on the center of the universe.
Resolves #648
- Fix Stranger cloak breaking on second loop onwards. Resolves #673.
  • Loading branch information
xen-42 committed Jul 27, 2023
2 parents 8399142 + c5a44c6 commit 07f6748
Show file tree
Hide file tree
Showing 12 changed files with 358 additions and 114 deletions.
37 changes: 10 additions & 27 deletions NewHorizons/Builder/General/SpawnPointBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ namespace NewHorizons.Builder.General
public static class SpawnPointBuilder
{
private static bool suitUpQueued = false;
public static SpawnPoint ShipSpawn { get; private set; }
public static Vector3 ShipSpawnOffset { get; private set; }

public static SpawnPoint Make(GameObject planetGO, SpawnModule module, OWRigidbody owRigidBody)
{
SpawnPoint playerSpawn = null;
Expand All @@ -35,41 +38,21 @@ public static SpawnPoint Make(GameObject planetGO, SpawnModule module, OWRigidbo

if (module.shipSpawn != null)
{
GameObject spawnGO = GeneralPropBuilder.MakeNew("ShipSpawnPoint", planetGO, null, module.shipSpawn);
var spawnGO = GeneralPropBuilder.MakeNew("ShipSpawnPoint", planetGO, null, module.shipSpawn);
spawnGO.SetActive(false);
spawnGO.layer = Layer.PlayerSafetyCollider;

var shipSpawn = spawnGO.AddComponent<SpawnPoint>();
shipSpawn._isShipSpawn = true;
shipSpawn._attachedBody = owRigidBody;
shipSpawn._spawnLocation = SpawnLocation.None;
ShipSpawn = spawnGO.AddComponent<SpawnPoint>();
ShipSpawn._isShipSpawn = true;
ShipSpawn._attachedBody = owRigidBody;
ShipSpawn._spawnLocation = SpawnLocation.None;

// #601 we need to actually set the right trigger volumes here
shipSpawn._triggerVolumes = new OWTriggerVolume[0];
ShipSpawn._triggerVolumes = new OWTriggerVolume[0];

// TODO: this should happen elsewhere
var ship = SearchUtilities.Find("Ship_Body");
if (ship != null)
{
ship.transform.position = spawnGO.transform.position;
ship.transform.rotation = spawnGO.transform.rotation;
ShipSpawnOffset = module.shipSpawn.offset ?? (module.shipSpawn.alignRadial.GetValueOrDefault() ? Vector3.up * 4 : Vector3.zero);

// Move it up a bit more when aligning to surface
if (module.shipSpawn.offset != null)
{
ship.transform.position += spawnGO.transform.TransformDirection(module.shipSpawn.offset);
}
else if (module.shipSpawn.alignRadial.GetValueOrDefault())
{
ship.transform.position += ship.transform.up * 4f;
}

ship.GetRequiredComponent<MatchInitialMotion>().SetBodyToMatch(owRigidBody);
}
spawnGO.SetActive(true);

// Ship doesn't get activated sometimes
Delay.RunWhen(() => Main.IsSystemReady, () => ship.gameObject.SetActive(true));
}

if ((Main.Instance.IsWarpingFromVessel || (!Main.Instance.IsWarpingFromShip && (module.playerSpawn?.startWithSuit ?? false))) && !suitUpQueued)
Expand Down
17 changes: 12 additions & 5 deletions NewHorizons/Builder/Props/DialogueBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using OWML.Common;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using UnityEngine;

namespace NewHorizons.Builder.Props
Expand All @@ -15,6 +16,14 @@ public static class DialogueBuilder
{
// Returns the character dialogue tree and remote dialogue trigger, if applicable.
public static (CharacterDialogueTree, RemoteDialogueTrigger) Make(GameObject go, Sector sector, DialogueInfo info, IModBehaviour mod)
{
var xml = File.ReadAllText(Path.Combine(mod.ModHelper.Manifest.ModFolderPath, info.xmlFile));
var dialogueName = Path.GetFileNameWithoutExtension(info.xmlFile);
return Make(go, sector, info, xml, dialogueName);
}

// Create dialogue directly from xml string instead of loading it from a file
public static (CharacterDialogueTree, RemoteDialogueTrigger) Make(GameObject go, Sector sector, DialogueInfo info, string xml, string dialogueName)
{
NHLogger.LogVerbose($"[DIALOGUE] Created a new character dialogue [{info.rename}] on [{info.parentPath}]");

Expand All @@ -26,7 +35,7 @@ public static (CharacterDialogueTree, RemoteDialogueTrigger) Make(GameObject go,
return (null, null);
}

var dialogue = MakeConversationZone(go, sector, info, mod.ModHelper);
var dialogue = MakeConversationZone(go, sector, info, xml, dialogueName);

RemoteDialogueTrigger remoteTrigger = null;
if (info.remoteTrigger != null)
Expand Down Expand Up @@ -76,7 +85,7 @@ private static RemoteDialogueTrigger MakeRemoteDialogueTrigger(GameObject planet
return remoteDialogueTrigger;
}

private static CharacterDialogueTree MakeConversationZone(GameObject planetGO, Sector sector, DialogueInfo info, IModHelper mod)
private static CharacterDialogueTree MakeConversationZone(GameObject planetGO, Sector sector, DialogueInfo info, string xml, string dialogueName)
{
var conversationZone = GeneralPropBuilder.MakeNew("ConversationZone", planetGO, sector, info, defaultParentPath: info.pathToAnimController);

Expand All @@ -100,13 +109,11 @@ private static CharacterDialogueTree MakeConversationZone(GameObject planetGO, S

var dialogueTree = conversationZone.AddComponent<NHCharacterDialogueTree>();

var xml = File.ReadAllText(Path.Combine(mod.Manifest.ModFolderPath, info.xmlFile));
var text = new TextAsset(xml)
{
// Text assets need a name to be used with VoiceMod
name = Path.GetFileNameWithoutExtension(info.xmlFile)
name = dialogueName
};

dialogueTree.SetTextXml(text);
AddTranslation(xml);

Expand Down
15 changes: 10 additions & 5 deletions NewHorizons/Builder/ShipLog/RumorModeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,20 @@ public static void AddBodyToShipLog(ShipLogManager manager, NewHorizonsBody body
{
string systemName = body.Config.starSystem;
XElement astroBodyFile = XElement.Load(Path.Combine(body.Mod.ModHelper.Manifest.ModFolderPath, body.Config.ShipLog.xmlFile));
XElement astroBodyId = astroBodyFile.Element("ID");
AddShipLogXML(manager, astroBodyFile, body);
}

public static void AddShipLogXML(ShipLogManager manager, XElement xml, NewHorizonsBody body)
{
XElement astroBodyId = xml.Element("ID");
if (astroBodyId == null)
{
NHLogger.LogError("Failed to load ship logs for " + systemName + "!");
NHLogger.LogError("Failed to load ship logs for " + body.Config.name + "!");
}
else
{
var entryIDs = new List<string>();
foreach (XElement entryElement in astroBodyFile.DescendantsAndSelf("Entry"))
foreach (XElement entryElement in xml.DescendantsAndSelf("Entry"))
{
XElement curiosityName = entryElement.Element("Curiosity");
XElement id = entryElement.Element("ID");
Expand Down Expand Up @@ -98,8 +103,8 @@ public static void AddBodyToShipLog(ShipLogManager manager, NewHorizonsBody body
}
AddTranslation(entryElement);
}
TextAsset newAsset = new TextAsset(astroBodyFile.ToString());
List<TextAsset> newBodies = new List<TextAsset>(manager._shipLogXmlAssets) { newAsset };
var newAsset = new TextAsset(xml.ToString());
var newBodies = new List<TextAsset>(manager._shipLogXmlAssets) { newAsset };
manager._shipLogXmlAssets = newBodies.ToArray();
ShipLogHandler.AddConfig(astroBodyId.Value, entryIDs, body);
}
Expand Down
15 changes: 11 additions & 4 deletions NewHorizons/Components/EOTE/CloakLocatorController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using NewHorizons.Components.Stars;
using NewHorizons.Handlers;
using NewHorizons.Utility.OWML;
using System.Linq;
using UnityEngine;

namespace NewHorizons.Components.EOTE
Expand All @@ -12,11 +13,17 @@ internal class CloakLocatorController : MonoBehaviour

public void Start()
{
// Enable and disable all cloaks, else Stranger state is weird at the start
foreach (var cloak in CloakHandler.Cloaks)
if (CloakHandler.Cloaks.Any())
{
SetCurrentCloak(cloak);
cloak.enabled = false;
// Enable and disable all cloaks, else Stranger state is weird at the start
foreach (var cloak in CloakHandler.Cloaks)
{
SetCurrentCloak(cloak);
cloak.enabled = false;
}

// Make sure a cloak is enabled
SetCurrentCloak(CloakHandler.Cloaks.First());
}
}

Expand Down
2 changes: 2 additions & 0 deletions NewHorizons/Components/Ship/ShipWarpController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ public void FinishWarpIn()

GlobalMessenger.FireEvent("EnterShip");
PlayerState.OnEnterShip();

PlayerSpawnHandler.SpawnShip();
}
}
}
5 changes: 5 additions & 0 deletions NewHorizons/External/SerializableData/MColor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public MColor(int r, int g, int b, int a = 255)
this.a = a;
}

public static MColor FromColor(Color color)
{
return new MColor((int)(color.r * 255), (int)(color.g * 255), (int)(color.b * 255), (int)(color.a * 255));
}

/// <summary>
/// The red component of this colour from 0-255, higher values will make the colour glow if applicable.
/// </summary>
Expand Down
57 changes: 48 additions & 9 deletions NewHorizons/Handlers/PlayerSpawnHandler.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using NewHorizons.Builder.General;
using NewHorizons.Utility;
using NewHorizons.Utility.OWML;
using System.Collections;
Expand Down Expand Up @@ -45,33 +46,71 @@ public static void OnSystemReady(bool shouldWarpInFromShip, bool shouldWarpInFro
}
}

public static void SpawnShip()
{
var ship = SearchUtilities.Find("Ship_Body");
if (ship != null)
{
ship.SetActive(true);

var pos = SpawnPointBuilder.ShipSpawn.transform.position;

// Move it up a bit more when aligning to surface
if (SpawnPointBuilder.ShipSpawnOffset != null)
{
pos += SpawnPointBuilder.ShipSpawn.transform.TransformDirection(SpawnPointBuilder.ShipSpawnOffset);
}

SpawnBody(ship.GetAttachedOWRigidbody(), SpawnPointBuilder.ShipSpawn, pos);
}
}

private static IEnumerator SpawnCoroutine(int length)
{
for(int i = 0; i < length; i++)
{
FixVelocity();
FixPlayerVelocity();
yield return new WaitForEndOfFrame();
}

InvulnerabilityHandler.MakeInvulnerable(false);

if (!Main.Instance.IsWarpingFromShip)
{
if (SpawnPointBuilder.ShipSpawn != null)
{
NHLogger.Log("Spawning player ship");
SpawnShip();
}
else
{
NHLogger.Log("System has no ship spawn. Deactivating it.");
SearchUtilities.Find("Ship_Body")?.SetActive(false);
}
}
}

private static void FixVelocity()
private static void FixPlayerVelocity()
{
var playerBody = SearchUtilities.Find("Player_Body").GetAttachedOWRigidbody();
var spawn = GetDefaultSpawn();
var resources = playerBody.GetComponent<PlayerResources>();

playerBody.WarpToPositionRotation(spawn.transform.position, spawn.transform.rotation);
SpawnBody(playerBody, GetDefaultSpawn());

resources._currentHealth = 100f;
}

public static void SpawnBody(OWRigidbody body, SpawnPoint spawn, Vector3? positionOverride = null)
{
var pos = positionOverride ?? spawn.transform.position;

body.WarpToPositionRotation(pos, spawn.transform.rotation);

var spawnVelocity = spawn._attachedBody.GetVelocity();
var spawnAngularVelocity = spawn._attachedBody.GetPointTangentialVelocity(playerBody.transform.position);
var spawnAngularVelocity = spawn._attachedBody.GetPointTangentialVelocity(pos);
var velocity = spawnVelocity + spawnAngularVelocity;

playerBody.SetVelocity(velocity);
NHLogger.LogVerbose($"Player spawn velocity {velocity} Player velocity {playerBody.GetVelocity()} spawn body {spawnVelocity} spawn angular vel {spawnAngularVelocity}");

resources._currentHealth = 100f;
body.SetVelocity(velocity);
}

private static Vector3 CalculateMatchVelocity(OWRigidbody owRigidbody, OWRigidbody bodyToMatch, bool ignoreAngularVelocity)
Expand Down
Loading

0 comments on commit 07f6748

Please sign in to comment.