Skip to content

Commit

Permalink
Merge pull request #140 from Gothic-UnZENity-Project/feature/game-con…
Browse files Browse the repository at this point in the history
…figuration-spawn-to-world

add(GameConfiguration.PreselectWorldToSpawn): Define world to spawn player into.
  • Loading branch information
JaXt0r authored Sep 29, 2024
2 parents e69f8f5 + d6ae996 commit a1c14b7
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ MonoBehaviour:
EnableMainMenu: 1
LoadFromSaveSlot: 0
SaveSlotToLoad: 0
PreselectWorldToSpawn: 0
SpawnAtWaypoint:
EnableVOBs: 1
SpawnVOBTypes:
Value:
Expand All @@ -49,7 +51,6 @@ MonoBehaviour:
ShowFreePoints: 0
ShowWayPoints: 0
ShowWayEdges: 0
SpawnAtWaypoint:
EnableGameMusic: 1
EnableGameSounds: 1
SunLightColor: {r: 0.69, g: 0.69, b: 0.69, a: 1}
Expand Down
43 changes: 40 additions & 3 deletions Assets/UnZENity-Core/Scripts/GameConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using GUZ.Core.World;
using MyBox;
using UnityEngine;
Expand All @@ -17,11 +18,44 @@ public class MeshCullingGroup
public float CullingDistance;
}

public enum WorldToSpawn
{
None,
// G1
G1World,
G1OldMine,
G1FreeMine,
G1OrcGraveyard,
G1OrcTempel,
// G2
G2Newworld,
G2OldWorld,
G2AddonWorld,
G2DragonIsland,
}


[CreateAssetMenu(fileName = "NewGameConfiguration", menuName = "UnZENity/ScriptableObjects/GameConfiguration", order = 1)]
public class GameConfiguration : ScriptableObject
{

[NonSerialized]
public static Dictionary<WorldToSpawn, string> WorldMappings = new()
{
{ WorldToSpawn.None, "NO MAPPING AVAILABLE. LOAD WORLD AS STATED IN NEW GAME/SAVE GAME!" },
// G1
{ WorldToSpawn.G1World, "world.zen" },
{ WorldToSpawn.G1OldMine, "oldmine.zen" },
{ WorldToSpawn.G1FreeMine, "freemine.zen" },
{ WorldToSpawn.G1OrcGraveyard, "orcgraveyard.zen" },
{ WorldToSpawn.G1OrcTempel, "orctempel.zen" },
// G2
{ WorldToSpawn.G2Newworld, "newworld.zen" },
{ WorldToSpawn.G2OldWorld, "oldworld.zen" },
{ WorldToSpawn.G2AddonWorld, "addonworld.zen" },
{ WorldToSpawn.G2DragonIsland, "dragonisland.zen" }
};

/**
* ##########
* ConditionalFieldArrayFilter
Expand Down Expand Up @@ -91,6 +125,12 @@ public class VOBTypesCollection : CollectionWrapper<VirtualObjectType> { }
public int SaveSlotToLoad;
private bool SaveSlotFieldCondition() => !EnableMainMenu && LoadFromSaveSlot;

public WorldToSpawn PreselectWorldToSpawn;

[Tooltip("Covers Free Points and Way Points.")]
public string SpawnAtWaypoint = string.Empty;



/**
* ##########
Expand Down Expand Up @@ -179,9 +219,6 @@ public class VOBTypesCollection : CollectionWrapper<VirtualObjectType> { }
[OverrideLabel("Show Way Point Edge Meshes")]
public bool ShowWayEdges;

[Tooltip("Covers Free Points and Way Points.")]
public string SpawnAtWaypoint = string.Empty;


/**
* ##########
Expand Down
1 change: 1 addition & 0 deletions Assets/UnZENity-Core/Scripts/Globals/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public static class Constants
public const string ClimbableTag = "Climbable";
public const string SpotTag = "PxVob_zCVobSpot";
public const string PlayerTag = "Player";
public const string MainCameraTag = "MainCamera";


public static int MeshPerFrame { get; } = 10;
Expand Down
7 changes: 7 additions & 0 deletions Assets/UnZENity-Core/Scripts/Manager/NpcHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ public static void CacheHero()
if (GameData.GothicVm.GlobalHero == null)
{
var playerGo = GameObject.FindWithTag(Constants.PlayerTag);

// Flat player
if (playerGo == null)
{
playerGo = GameObject.FindWithTag(Constants.MainCameraTag);
}

var playerProperties = playerGo.GetComponent<NpcProperties>();

var heroInstance = GameData.GothicVm.AllocInstance<NpcInstance>(GameGlobals.Settings.IniPlayerInstanceName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void Init()

// Load world.zen
// TODO - In future, we can also fetch name of scene to load from another config setting.
GameManager.I.LoadWorld(Constants.SelectedWorld, -1, SceneManager.GetActiveScene().name);
GameManager.I.LoadWorld(GetWorldNameToSpawn(), -1, SceneManager.GetActiveScene().name);
return;
}

Expand All @@ -34,5 +34,20 @@ public void Init()

GlobalEventDispatcher.MainMenuSceneLoaded.Invoke();
}

private string GetWorldNameToSpawn()
{
var world = GameGlobals.Config.PreselectWorldToSpawn;

if (world == WorldToSpawn.None)
{
// FIXME - Read default from INI file
return Constants.SelectedWorld;
}
else
{
return GameConfiguration.WorldMappings[world];
}
}
}
}
14 changes: 12 additions & 2 deletions Assets/UnZENity-Core/Scripts/Manager/Scenes/WorldSceneManager.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using GUZ.Core.Creator;
Expand Down Expand Up @@ -59,6 +60,17 @@ private async Task LoadWorldContentAsync()
// World fully loaded
ResourceLoader.ReleaseLoadedData();
TeleportPlayerToStart();

// There are many handlers which listen to this event. If any of these fails, we won't get notified without a try-catch.
try
{
GlobalEventDispatcher.WorldSceneLoaded.Invoke();
}
catch (Exception e)
{
Debug.LogError(e);
}

SceneManager.UnloadSceneAsync(Constants.SceneLoading);
}

Expand Down Expand Up @@ -115,8 +127,6 @@ private void TeleportPlayerToStart()

GameContext.InteractionAdapter.TeleportPlayerTo(startPoint.transform.position, startPoint.transform.rotation);
GameContext.InteractionAdapter.UnlockPlayer();

GlobalEventDispatcher.WorldSceneLoaded.Invoke();
}
}
}
9 changes: 7 additions & 2 deletions Assets/UnZENity-Flat/Scripts/FlatInteractionAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ namespace GUZ.Flat
{
public class FlatInteractionAdapter : IInteractionAdapter
{
private GameObject _playerController;


private const string _contextName = "Flat";

public string GetContextName()
Expand All @@ -25,7 +28,9 @@ public GameObject CreatePlayerController(Scene scene, Vector3 position = default
// world scene and removed whenever we change the world.
SceneManager.MoveGameObjectToScene(go, scene);

return go.GetComponentInChildren<Rigidbody>().gameObject;
_playerController = go.GetComponentInChildren<Rigidbody>().gameObject;

return _playerController;
}

public void CreateVRDeviceSimulator()
Expand All @@ -45,7 +50,7 @@ public void UnlockPlayer()

public void TeleportPlayerTo(Vector3 position, Quaternion rotation = default)
{
// Not yet implemented
_playerController.transform.SetLocalPositionAndRotation(position, rotation);
}

public void InitUIInteraction()
Expand Down

0 comments on commit a1c14b7

Please sign in to comment.