From 8a0c403d2bc9230a9f2d6c72facaf1af8f1829f8 Mon Sep 17 00:00:00 2001 From: Benjamin Kraft Date: Tue, 18 Apr 2023 10:26:30 +0200 Subject: [PATCH] game balance values out sourced to scriptable object --- Assets/Prefabs/BalanceValues.asset | 59 +++++++++++++++++++++++ Assets/Prefabs/BalanceValues.asset.meta | 8 +++ Assets/Scenes/Game.unity | 34 +------------ Assets/Scripts/Game/AIPlayer.cs | 6 +-- Assets/Scripts/Game/BalanceValues.cs | 37 ++++++++++++++ Assets/Scripts/Game/BalanceValues.cs.meta | 3 ++ Assets/Scripts/Game/Ball.cs | 2 +- Assets/Scripts/Game/GameManager.cs | 39 +++++++-------- Assets/Scripts/Game/Player.cs | 14 +++--- 9 files changed, 136 insertions(+), 66 deletions(-) create mode 100644 Assets/Prefabs/BalanceValues.asset create mode 100644 Assets/Prefabs/BalanceValues.asset.meta create mode 100644 Assets/Scripts/Game/BalanceValues.cs create mode 100644 Assets/Scripts/Game/BalanceValues.cs.meta diff --git a/Assets/Prefabs/BalanceValues.asset b/Assets/Prefabs/BalanceValues.asset new file mode 100644 index 0000000..5764828 --- /dev/null +++ b/Assets/Prefabs/BalanceValues.asset @@ -0,0 +1,59 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: b6828f2f0908479394eb20d0c0beb8c0, type: 3} + m_Name: BalanceValues + m_EditorClassIdentifier: + spawnRates: + - spawnable: 0 + values: + - spawnRate: 1 + baseSeconds: 20 + - spawnRate: 2 + baseSeconds: 10 + - spawnRate: 3 + baseSeconds: 5 + - spawnable: 1 + values: + - spawnRate: 1 + baseSeconds: 30 + - spawnRate: 2 + baseSeconds: 20 + - spawnRate: 3 + baseSeconds: 10 + - spawnable: 2 + values: + - spawnRate: 1 + baseSeconds: 10 + - spawnRate: 2 + baseSeconds: 7 + - spawnRate: 3 + baseSeconds: 3 + - spawnable: 3 + values: + - spawnRate: 1 + baseSeconds: 40 + - spawnRate: 2 + baseSeconds: 30 + - spawnRate: 3 + baseSeconds: 15 + spawnArea: {x: 0.9, y: 0.5} + playerSpeed: 15 + playerSpeedMultiplier: 1.5 + playerWidth: 6 + playerWidthMultiplier: 1.5 + playerBorder: 0.85 + playerBorderSummand: 0.15 + aiFutureSecondsBase: 1 + aiFutureSecondsPerDifficulty: 0.75 + aiSmartIdleMinDifficulty: 2 + ballSpeed: 15 + ballSpeedMultiplier: 1.025 diff --git a/Assets/Prefabs/BalanceValues.asset.meta b/Assets/Prefabs/BalanceValues.asset.meta new file mode 100644 index 0000000..7925340 --- /dev/null +++ b/Assets/Prefabs/BalanceValues.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: a557c60a64523e194996165dd8bbf6a4 +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 11400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/Game.unity b/Assets/Scenes/Game.unity index e0287e1..b1cc55d 100644 --- a/Assets/Scenes/Game.unity +++ b/Assets/Scenes/Game.unity @@ -1017,39 +1017,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 52712ba5190f247fcab14f6d688872ba, type: 3} m_Name: m_EditorClassIdentifier: - spawnRates: - - spawnable: 0 - values: - - spawnRate: 1 - baseSeconds: 20 - - spawnRate: 2 - baseSeconds: 10 - - spawnRate: 3 - baseSeconds: 5 - - spawnable: 1 - values: - - spawnRate: 1 - baseSeconds: 30 - - spawnRate: 2 - baseSeconds: 20 - - spawnRate: 3 - baseSeconds: 10 - - spawnable: 2 - values: - - spawnRate: 1 - baseSeconds: 10 - - spawnRate: 2 - baseSeconds: 7 - - spawnRate: 3 - baseSeconds: 3 - - spawnable: 3 - values: - - spawnRate: 1 - baseSeconds: 40 - - spawnRate: 2 - baseSeconds: 30 - - spawnRate: 3 - baseSeconds: 15 + balanceValues: {fileID: 11400000, guid: a557c60a64523e194996165dd8bbf6a4, type: 2} ballPrefab: {fileID: 7041796185663956342, guid: d67d5f2db5f0b3d9d8298731f423a9b6, type: 3} playerPrefab: {fileID: 5402279313309450415, guid: 32b3bb87d3699d314ace59ddb1674875, type: 3} borderZonePrefab: {fileID: 4827788429173683660, guid: 50e7c76b45ba22fb18c0012e23b4b34c, type: 3} diff --git a/Assets/Scripts/Game/AIPlayer.cs b/Assets/Scripts/Game/AIPlayer.cs index 46637e6..72be96f 100644 --- a/Assets/Scripts/Game/AIPlayer.cs +++ b/Assets/Scripts/Game/AIPlayer.cs @@ -5,7 +5,7 @@ using UnityEngine.UIElements; namespace Game { public class AIPlayer : Player { - private const float SmoothTime = 0.2f; + private const float SmoothTime = 0.1f; private float currentSmoothV; @@ -16,9 +16,9 @@ namespace Game { public Difficulty Difficulty { get; set; } - private float FutureSeconds => (float) Difficulty * 0.5f; + private float FutureSeconds => GameManager.BalanceValues.aiFutureSecondsBase + (float) Difficulty * GameManager.BalanceValues.aiFutureSecondsPerDifficulty; - private float IdlePosition => Difficulty >= Difficulty.Medium ? 0 : X; + private float IdlePosition => Difficulty >= GameManager.BalanceValues.aiSmartIdleMinDifficulty ? 0 : X; private float DistortAmount => 1 - 1 / ((float) Difficulty + 1); diff --git a/Assets/Scripts/Game/BalanceValues.cs b/Assets/Scripts/Game/BalanceValues.cs new file mode 100644 index 0000000..bae503f --- /dev/null +++ b/Assets/Scripts/Game/BalanceValues.cs @@ -0,0 +1,37 @@ +using System; +using System.Runtime.Remoting.Messaging; +using UnityEngine; + +namespace Game { + [CreateAssetMenu(fileName = "Data", menuName = "ScriptableObjects/BalanceValues", order = 0)] + public class BalanceValues : ScriptableObject { + [Serializable] + public struct SpawnRatesValue { + public SpawnRate spawnRate; + public float baseSeconds; + } + [Serializable] + public struct SpawnableSpawnRates { + public Spawnable spawnable; + public SpawnRatesValue[] values; + } + + [Header("Spawning")] + public SpawnableSpawnRates[] spawnRates; + public Vector2 spawnArea; + [Header("Player")] + public float playerSpeed; + public float playerSpeedMultiplier; + public float playerWidth; + public float playerWidthMultiplier; + public float playerBorder; + public float playerBorderSummand; + [Header("AI")] + public float aiFutureSecondsBase; + public float aiFutureSecondsPerDifficulty; + public Difficulty aiSmartIdleMinDifficulty; + [Header("Ball")] + public float ballSpeed; + public float ballSpeedMultiplier; + } +} diff --git a/Assets/Scripts/Game/BalanceValues.cs.meta b/Assets/Scripts/Game/BalanceValues.cs.meta new file mode 100644 index 0000000..6d13624 --- /dev/null +++ b/Assets/Scripts/Game/BalanceValues.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: b6828f2f0908479394eb20d0c0beb8c0 +timeCreated: 1681802462 \ No newline at end of file diff --git a/Assets/Scripts/Game/Ball.cs b/Assets/Scripts/Game/Ball.cs index 6222420..31e7343 100644 --- a/Assets/Scripts/Game/Ball.cs +++ b/Assets/Scripts/Game/Ball.cs @@ -4,7 +4,7 @@ using UnityEngine; namespace Game { public class Ball : NetworkBehaviour { - private const float SpeedGain = 1.025f; + private float SpeedGain { get; } = GameManager.BalanceValues.ballSpeedMultiplier; public Rigidbody2D Rb { get; private set; } diff --git a/Assets/Scripts/Game/GameManager.cs b/Assets/Scripts/Game/GameManager.cs index aeb3d8c..1a84152 100644 --- a/Assets/Scripts/Game/GameManager.cs +++ b/Assets/Scripts/Game/GameManager.cs @@ -11,18 +11,7 @@ using Random = UnityEngine.Random; namespace Game { public class GameManager : NetworkBehaviour { - [Serializable] - public struct SpawnRatesValue { - public SpawnRate spawnRate; - public float baseSeconds; - } - [Serializable] - public struct SpawnableSpawnRates { - public Spawnable spawnable; - public SpawnRatesValue[] values; - } - - public SpawnableSpawnRates[] spawnRates; + public BalanceValues balanceValues; public Object ballPrefab; public Object playerPrefab; @@ -34,6 +23,8 @@ namespace Game { public static GameManager Singleton { get; private set; } + public static BalanceValues BalanceValues => Singleton.balanceValues; + public List Balls { get; } = new(); public Player Player1 { get; private set; } @@ -41,12 +32,12 @@ namespace Game { public Player Player2 { get; private set; } private void Awake() { + Singleton = this; + SetupPlayers(); } private void OnEnable() { - Singleton = this; - // Move this to settings Application.targetFrameRate = 144; QualitySettings.vSyncCount = 0; @@ -56,7 +47,7 @@ namespace Game { SpawnRate required = RoomSettings.SpawnRates[spawnable]; if (required == SpawnRate.Never) yield break; - float baseSeconds = spawnRates.First(s => s.spawnable == spawnable).values.First(v => v.spawnRate == required).baseSeconds; + float baseSeconds = balanceValues.spawnRates.First(s => s.spawnable == spawnable).values.First(v => v.spawnRate == required).baseSeconds; while (Application.isPlaying) { yield return new WaitForSeconds(baseSeconds); switch (spawnable) { @@ -126,25 +117,29 @@ namespace Game { p1.borderZonePrefab = p2.borderZonePrefab = borderZonePrefab; } + private Vector2 GetSpawnPosition() { + Vector2 area = balanceValues.spawnArea * Dimensions.Singleton.playGroundSize * 0.5f; + float x = Random.Range(-area.x, area.x); + float y = Random.Range(-area.y, area.y); + return new Vector2(x, y); + } + private void SpawnNewBall(bool permanent) { - var pos = new Vector2(Random.Range(0, Dimensions.Singleton.PlaySize.x) - Dimensions.Singleton.PlaySize.x / 2, Random.Range(-5, 5)); - Instantiate(newBallPrefab, pos, Quaternion.identity).GetComponent().IsPermanent = permanent; + Instantiate(newBallPrefab, GetSpawnPosition(), Quaternion.identity).GetComponent().IsPermanent = permanent; } private void SpawnWormhole() { - var pos = new Vector2(Random.Range(0, Dimensions.Singleton.PlaySize.x) - Dimensions.Singleton.PlaySize.x / 2, Random.Range(-5, 5)); - Instantiate(wormholePrefab, pos, Quaternion.identity); + Instantiate(wormholePrefab, GetSpawnPosition(), Quaternion.identity); } private void SpawnModification() { - var pos = new Vector2(Random.Range(0, Dimensions.Singleton.PlaySize.x) - Dimensions.Singleton.PlaySize.x / 2, Random.Range(-2, 2)); var properties = modifications[Random.Range(0, modifications.Length)]; - var mod = Instantiate(modificationPrefab, pos, Quaternion.identity).GetComponent(); + var mod = Instantiate(modificationPrefab, GetSpawnPosition(), Quaternion.identity).GetComponent(); mod.Properties = properties; } public void SpawnBall(Player towards, bool isPermanent) { - const float startSpeed = 15; + float startSpeed = balanceValues.ballSpeed; var position = new Vector2(0, -towards.transform.position.y * 0.5f); var ball = Instantiate(ballPrefab, position, Quaternion.identity).GetComponent(); ball.Rb.velocity = RandomDirectionTowards(towards) * startSpeed; diff --git a/Assets/Scripts/Game/Player.cs b/Assets/Scripts/Game/Player.cs index 0dcad0e..1986433 100644 --- a/Assets/Scripts/Game/Player.cs +++ b/Assets/Scripts/Game/Player.cs @@ -17,15 +17,15 @@ namespace Game { private Transform borderZoneLeft, borderZoneRight; - private const float BaseSpeed = 15; - private const float SpeedMultiplier = 1.5f; + private float BaseSpeed { get; } = GameManager.BalanceValues.playerSpeed; + private float SpeedMultiplier { get; } = GameManager.Singleton.balanceValues.playerSpeedMultiplier; // Unit distance from zero - private readonly float baseBorder = Dimensions.Singleton.PlaySize.x / 2 * 0.85f; - private readonly float borderSummand = Dimensions.Singleton.PlaySize.x / 2 * 0.15f; + private float BaseBorder { get; } = Dimensions.Singleton.PlaySize.x / 2 * GameManager.BalanceValues.playerBorder; + private float BorderSummand { get; } = Dimensions.Singleton.PlaySize.x / 2 * GameManager.BalanceValues.playerBorderSummand; - private const float BaseWidth = 6; - private const float WidthMultiplier = 1.5f; + private float BaseWidth { get; } = GameManager.BalanceValues.playerWidth; + private float WidthMultiplier { get; } = GameManager.BalanceValues.playerWidthMultiplier; protected bool goingLeftLinear, goingRightLinear; @@ -56,7 +56,7 @@ namespace Game { } protected float Border { get { - float value = GetModified(ModEffect.Border, baseBorder, borderSummand, (f, f1) => f + f1, f => -f); + float value = GetModified(ModEffect.Border, BaseBorder, BorderSummand, (f, f1) => f + f1, f => -f); return Mathf.Clamp(value, Width / 2, Dimensions.Singleton.PlaySize.x / 2); } }