Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: simplify the codebase #40

Merged
merged 1 commit into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Assets/Audio/Events/FMODEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ private void Update3DPosition() {
}

public void SetParameter(FMODParameter parameter, float val) {
if (!IsInitialized || parameter.IsGlobal)
if (!IsInitialized || parameter.IsGlobal) {
return;
}

if (_parameterIdLookup.TryGetValue(parameter, out var instance)) {
Instance.setParameterByID(instance, val);
Expand All @@ -85,6 +86,7 @@ public void Play() {
public void Release() {
if (IsInitialized) {
Instance.release();
IsInitialized = false;
}
}
}
Expand Down
1 change: 0 additions & 1 deletion Assets/DevTools/CSV/CsvParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public static IReadOnlyList<Row> Parse(string text) {
var cells = new List<string>();

var isInsideField = false;
var previousEmpty = false;
_builder.Clear();
_rows.Clear();

Expand Down
22 changes: 12 additions & 10 deletions Assets/DevTools/GoogleSheetLoaderDev.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Aarthificial.Typewriter.References;
using DevTools.CSV;
using Framework;
using Input;
using Interactions;
using Player;
using System.Collections;
Expand All @@ -19,9 +18,7 @@ public partial class GoogleSheetLoader {
private static string _customDocumentId;
private static string _customSheetName;

[SerializeField] [Inject] private PlayerChannel _players;
[SerializeField] [Inject] private InputChannel _input;
[SerializeField] [Inject] private StoryMode _story;
[SerializeField] [Inject] private StoryState _story;
[SerializeField] private DatabaseTable _table;
[SerializeField] private string _documentId;
[SerializeField] private string _sheetName;
Expand All @@ -32,6 +29,11 @@ public partial class GoogleSheetLoader {
private bool _onlyFacts;
private bool _hideEmpty = true;
private string _searchText = "";
private PlayerManager _players;

private void Awake() {
_players = GetComponent<PlayerManager>();
}

private void OnEnable() {
if (string.IsNullOrEmpty(_customDocumentId)) {
Expand All @@ -58,8 +60,8 @@ private void Open() {
}

_isOpen = true;
_previousMap = _input.CurrentMap;
_input.SetMap("None");
_previousMap = App.Input.CurrentMap;
App.Input.SetMap("None");
Time.timeScale = 0;
}

Expand All @@ -69,7 +71,7 @@ private void Close() {
}

_isOpen = false;
_input.SetMap(_previousMap);
App.Input.SetMap(_previousMap);
Time.timeScale = 1;
}

Expand All @@ -78,8 +80,8 @@ private void OnGUI() {
return;
}

var isInConversation = _players.Manager.DialogueState.IsActive
&& _players.Manager.DialogueState.Context != null;
var isInConversation = _players.DialogueState.IsActive
&& _players.DialogueState.Context != null;

var size = new Vector2(640, 160);
var rect = new Rect(
Expand Down Expand Up @@ -162,7 +164,7 @@ private void BlackboardGUI(Rect rect) {

_scrollPosition = GUILayout.BeginScrollView(_scrollPosition);

var context = _players.Manager.DialogueState.Context;
var context = _players.DialogueState.Context;
BlackboardEntriesGUI(context, InteractionContext.GlobalScope);
BlackboardEntriesGUI(context, InteractionContext.InteractionScope);
BlackboardEntriesGUI(context, InteractionContext.ContextScope);
Expand Down
12 changes: 6 additions & 6 deletions Assets/Environment/LookAtCamera.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using UnityEngine;
using Utils;
using View.Overlay;

namespace Environment {
[DefaultExecutionOrder(1000)]
public class LookAtCamera : MonoBehaviour {
[Inject] [SerializeField] private OverlayChannel _overlay;
private Camera _mainCamera;

private void Awake() {
_mainCamera = OverlayManager.Camera;
}

private void LateUpdate() {
if (_overlay.IsReady) {
transform.rotation =
_overlay.CameraManager.MainCamera.transform.rotation;
}
transform.rotation = _mainCamera.transform.rotation;
}
}
}
2 changes: 1 addition & 1 deletion Assets/Environment/Shaders/BoxSDF.shader
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Shader "GUI/BoxSDF"
{
UNITY_SETUP_INSTANCE_ID(input);
Varyings output;
output.worldPosition = float4(TransformWorldToObject(input.vertex), 0.0);
output.worldPosition = float4(TransformWorldToObject(input.vertex.xyz), 0.0);
output.vertex = TransformObjectToHClip(input.vertex);

output.texcoord = input.texcoord;
Expand Down
2 changes: 1 addition & 1 deletion Assets/Environment/Shaders/InteractionGizmo.shader
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ Shader "GUI/InteractionGizmo"
const float scale = 0.5 + lerp(1, 0.58333333333333, _State.x);
const float4 positionOS = input.positionOS * scale;
output.positionOS = positionOS;
output.positionCS = TransformObjectToHClip(positionOS);
output.positionCS = TransformObjectToHClip(positionOS.xyz);

output.uv = (input.uv - 0.5) * scale + 0.5;
output.color = input.color;
Expand Down
45 changes: 32 additions & 13 deletions Assets/Framework/App.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,51 @@
using Audio;
using FMODUnity;
using Input;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace Framework {
// TODO Move to the editor namespace maybe?
#if UNITY_EDITOR
public static class App {
public static GameManager Game;
public static InputManager Input;
public static AudioManager Audio;
public static InputActions Actions;

[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static void Bootstrap() {
if (ShouldBootstrap()) {
SceneManager.LoadScene(0, LoadSceneMode.Additive);
private static void BetterBootstrap() {
#if UNITY_EDITOR
if (ShouldSkip()) {
return;
}
#endif

// Force FMOD initialization to avoid a framerate drop when the first
// sound is played.
_ = RuntimeManager.StudioSystem;

var app = Object.Instantiate(Resources.Load<GameObject>("App"));
Game = app.GetComponent<GameManager>();
Input = app.GetComponent<InputManager>();
Audio = app.GetComponent<AudioManager>();
Actions = Input.Actions;

Game.DrivenAwake();

Object.DontDestroyOnLoad(app);
}

private static bool ShouldBootstrap() {
#if UNITY_EDITOR
private static bool ShouldSkip() {
var activeScene = SceneManager.GetActiveScene();
if (activeScene.buildIndex == 0) {
return false;
}

var path = activeScene.path;
foreach (var scene in UnityEditor.EditorBuildSettings.scenes) {
if (scene.path == path) {
return true;
return false;
}
}

return false;
return true;
}
}
#endif
}
}
85 changes: 24 additions & 61 deletions Assets/Framework/GameManager.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
using System.Collections;
using Audio;
using FMODUnity;
using System;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.SceneManagement;
using Utils;

namespace Framework {
public class GameManager : MonoBehaviour {
[Inject] public MenuMode MenuMode;
[Inject] public StoryMode StoryMode;
public AudioManager Audio;
[NonSerialized] public MenuState Menu;
[NonSerialized] public StoryState Story;

private GameMode _currentMode;
private GameMode _switchingTo;
private GameState _currentState;

private bool ISSwitching => !ReferenceEquals(_switchingTo, null);
public void DrivenAwake() {
Menu = GetComponent<MenuState>();
Story = GetComponent<StoryState>();

public void RequestMode(GameMode mode) {
if (_switchingTo == mode || _currentMode == mode) {
#if UNITY_EDITOR
if (SceneManager.GetActiveScene().buildIndex == 0) {
SwitchState(Menu);
} else {
SwitchState(Story);
}
#else
SwitchState(Menu);
#endif
}

public void SwitchState(GameState state) {
if (_currentState == state) {
return;
}

StartCoroutine(SwitchMode(mode));
_currentState?.OnExit();
_currentState = state;
_currentState?.OnEnter();
}

public void Quit() {
Expand All @@ -32,38 +42,11 @@ public void Quit() {
Application.Quit();
}

private void Awake() {
MenuMode.Setup(this);
StoryMode.Setup(this);

// Force FMOD initialization to avoid a framerate drop when the first
// sound is played.
_ = RuntimeManager.StudioSystem;

#if UNITY_EDITOR
switch (SceneManager.GetActiveScene().buildIndex) {
case 0:
RequestMode(MenuMode);
break;
case 1:
_currentMode = MenuMode;
_currentMode.OnEditorStart();
break;
default:
_currentMode = StoryMode;
_currentMode.OnEditorStart();
break;
}
#else
RequestMode(MenuMode);
#endif
}

#if UNITY_EDITOR || DEVELOPMENT_BUILD
private void Update() {
if (Keyboard.current.rKey.wasPressedThisFrame
&& Keyboard.current.ctrlKey.isPressed) {
StoryMode.Reload();
Story.Reload();
}

if (Keyboard.current.fKey.wasPressedThisFrame
Expand All @@ -72,25 +55,5 @@ private void Update() {
}
}
#endif

private IEnumerator SwitchMode(GameMode mode) {
if (_switchingTo == mode || _currentMode == mode) {
yield break;
}

if (ISSwitching) {
yield return new WaitUntil(() => !ISSwitching);
}

_switchingTo = mode;
if (!ReferenceEquals(_currentMode, null)) {
yield return _currentMode.OnEnd();
}

_currentMode = mode;
yield return _currentMode.OnStart();

_switchingTo = null;
}
}
}
27 changes: 0 additions & 27 deletions Assets/Framework/GameMode.cs

This file was deleted.

11 changes: 11 additions & 0 deletions Assets/Framework/GameState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Player.States;

namespace Framework {
public class GameState : BaseState {
protected GameManager Manager;

protected virtual void Awake() {
Manager = GetComponent<GameManager>();
}
}
}
File renamed without changes.
15 changes: 0 additions & 15 deletions Assets/Framework/MenuMode.asset

This file was deleted.

Loading
Loading