From ca2356eb3f3c88745d5ecc646383f49ff301be90 Mon Sep 17 00:00:00 2001 From: Carson Katri Date: Sat, 12 Jun 2021 19:50:29 -0400 Subject: Add scoreboard UI --- Assets/Fonts.meta | 8 ++ Assets/Fonts/joystix monospace.ttf | Bin 0 -> 47604 bytes Assets/Fonts/joystix monospace.ttf.meta | 21 +++++ Assets/Scenes/Main.unity | 112 +++++++++++++++++++++++---- Assets/Scripts/Controllers/Ball.cs | 8 +- Assets/Scripts/Controllers/GameController.cs | 25 ++++++ 6 files changed, 157 insertions(+), 17 deletions(-) create mode 100644 Assets/Fonts.meta create mode 100644 Assets/Fonts/joystix monospace.ttf create mode 100644 Assets/Fonts/joystix monospace.ttf.meta diff --git a/Assets/Fonts.meta b/Assets/Fonts.meta new file mode 100644 index 0000000..306a7a7 --- /dev/null +++ b/Assets/Fonts.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: e7bf0aa98a3854e19ac18f7c3ab9fddf +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Fonts/joystix monospace.ttf b/Assets/Fonts/joystix monospace.ttf new file mode 100644 index 0000000..5fd36a5 Binary files /dev/null and b/Assets/Fonts/joystix monospace.ttf differ diff --git a/Assets/Fonts/joystix monospace.ttf.meta b/Assets/Fonts/joystix monospace.ttf.meta new file mode 100644 index 0000000..59093ad --- /dev/null +++ b/Assets/Fonts/joystix monospace.ttf.meta @@ -0,0 +1,21 @@ +fileFormatVersion: 2 +guid: b6747d7b13b2c4b9cb022acf49f3277b +TrueTypeFontImporter: + externalObjects: {} + serializedVersion: 4 + fontSize: 16 + forceTextureCase: -2 + characterSpacing: 0 + characterPadding: 1 + includeFontData: 1 + fontNames: + - Joystix + fallbackFontReferences: [] + customCharacters: + fontRenderingMode: 0 + ascentCalculationMode: 1 + useLegacyBoundsCalculation: 0 + shouldRoundAdvanceValue: 1 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/Main.unity b/Assets/Scenes/Main.unity index cd82e65..a2bab54 100644 --- a/Assets/Scenes/Main.unity +++ b/Assets/Scenes/Main.unity @@ -181,7 +181,7 @@ MonoBehaviour: m_PersistentCalls: m_Calls: [] m_FontData: - m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0} + m_Font: {fileID: 12800000, guid: b6747d7b13b2c4b9cb022acf49f3277b, type: 3} m_FontSize: 24 m_FontStyle: 1 m_BestFit: 0 @@ -444,7 +444,7 @@ MonoBehaviour: m_PersistentCalls: m_Calls: [] m_FontData: - m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0} + m_Font: {fileID: 12800000, guid: b6747d7b13b2c4b9cb022acf49f3277b, type: 3} m_FontSize: 14 m_FontStyle: 1 m_BestFit: 0 @@ -523,7 +523,7 @@ MonoBehaviour: m_PersistentCalls: m_Calls: [] m_FontData: - m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0} + m_Font: {fileID: 12800000, guid: b6747d7b13b2c4b9cb022acf49f3277b, type: 3} m_FontSize: 24 m_FontStyle: 1 m_BestFit: 0 @@ -825,8 +825,8 @@ MonoBehaviour: m_PersistentCalls: m_Calls: [] m_FontData: - m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0} - m_FontSize: 15 + m_Font: {fileID: 12800000, guid: b6747d7b13b2c4b9cb022acf49f3277b, type: 3} + m_FontSize: 12 m_FontStyle: 1 m_BestFit: 0 m_MinSize: 0 @@ -1290,8 +1290,8 @@ MonoBehaviour: m_OnCullStateChanged: m_PersistentCalls: m_Calls: [] - m_Sprite: {fileID: 10907, guid: 0000000000000000f000000000000000, type: 0} - m_Type: 1 + m_Sprite: {fileID: 7482667652216324306, guid: 48e93eef0688c4a259cb0eddcd8661f7, type: 3} + m_Type: 0 m_PreserveAspect: 0 m_FillCenter: 1 m_FillMethod: 4 @@ -1361,6 +1361,8 @@ MonoBehaviour: PlayerHoop: {fileID: 614591307} EnemyHoop: {fileID: 1354192742} dribbleSound: {fileID: 1174357921} + playerScoreText: {fileID: 140689492} + enemyScoreText: {fileID: 447684163} --- !u!82 &1174357921 AudioSource: m_ObjectHideFlags: 0 @@ -1520,7 +1522,7 @@ MonoBehaviour: m_PersistentCalls: m_Calls: [] m_FontData: - m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0} + m_Font: {fileID: 12800000, guid: b6747d7b13b2c4b9cb022acf49f3277b, type: 3} m_FontSize: 14 m_FontStyle: 1 m_BestFit: 0 @@ -1600,8 +1602,8 @@ MonoBehaviour: m_OnCullStateChanged: m_PersistentCalls: m_Calls: [] - m_Sprite: {fileID: 10907, guid: 0000000000000000f000000000000000, type: 0} - m_Type: 1 + m_Sprite: {fileID: 7482667652216324306, guid: 48e93eef0688c4a259cb0eddcd8661f7, type: 3} + m_Type: 0 m_PreserveAspect: 0 m_FillCenter: 1 m_FillMethod: 4 @@ -1887,8 +1889,8 @@ MonoBehaviour: m_PersistentCalls: m_Calls: [] m_FontData: - m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0} - m_FontSize: 15 + m_Font: {fileID: 12800000, guid: b6747d7b13b2c4b9cb022acf49f3277b, type: 3} + m_FontSize: 12 m_FontStyle: 1 m_BestFit: 0 m_MinSize: 0 @@ -1941,6 +1943,88 @@ Transform: m_Father: {fileID: 0} m_RootOrder: 3 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1 &1576856532 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1576856534} + - component: {fileID: 1576856533} + m_Layer: 0 + m_Name: Square + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!212 &1576856533 +SpriteRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1576856532} + m_Enabled: 1 + m_CastShadows: 0 + m_ReceiveShadows: 0 + m_DynamicOccludee: 1 + m_MotionVectors: 1 + m_LightProbeUsage: 1 + m_ReflectionProbeUsage: 1 + m_RayTracingMode: 0 + m_RayTraceProcedural: 0 + m_RenderingLayerMask: 1 + m_RendererPriority: 0 + m_Materials: + - {fileID: 10754, guid: 0000000000000000f000000000000000, type: 0} + m_StaticBatchInfo: + firstSubMesh: 0 + subMeshCount: 0 + m_StaticBatchRoot: {fileID: 0} + m_ProbeAnchor: {fileID: 0} + m_LightProbeVolumeOverride: {fileID: 0} + m_ScaleInLightmap: 1 + m_ReceiveGI: 1 + m_PreserveUVs: 0 + m_IgnoreNormalsForChartDetection: 0 + m_ImportantGI: 0 + m_StitchLightmapSeams: 1 + m_SelectedEditorRenderState: 0 + m_MinimumChartSize: 4 + m_AutoUVMaxDistance: 0.5 + m_AutoUVMaxAngle: 89 + m_LightmapParameters: {fileID: 0} + m_SortingLayerID: 0 + m_SortingLayer: 0 + m_SortingOrder: 0 + m_Sprite: {fileID: 7482667652216324306, guid: 48e93eef0688c4a259cb0eddcd8661f7, type: 3} + m_Color: {r: 1, g: 1, b: 1, a: 1} + m_FlipX: 0 + m_FlipY: 0 + m_DrawMode: 0 + m_Size: {x: 1, y: 1} + m_AdaptiveModeThreshold: 0.5 + m_SpriteTileMode: 0 + m_WasSpriteAssigned: 1 + m_MaskInteraction: 0 + m_SpriteSortPoint: 0 +--- !u!4 &1576856534 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1576856532} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 761.59344, y: 167.65875, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 5 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &1742973248 GameObject: m_ObjectHideFlags: 0 @@ -2102,8 +2186,8 @@ MonoBehaviour: m_OnCullStateChanged: m_PersistentCalls: m_Calls: [] - m_Sprite: {fileID: 10907, guid: 0000000000000000f000000000000000, type: 0} - m_Type: 1 + m_Sprite: {fileID: 7482667652216324306, guid: 48e93eef0688c4a259cb0eddcd8661f7, type: 3} + m_Type: 0 m_PreserveAspect: 0 m_FillCenter: 1 m_FillMethod: 4 diff --git a/Assets/Scripts/Controllers/Ball.cs b/Assets/Scripts/Controllers/Ball.cs index 45e607b..d9c85c2 100644 --- a/Assets/Scripts/Controllers/Ball.cs +++ b/Assets/Scripts/Controllers/Ball.cs @@ -11,6 +11,9 @@ namespace Controllers internal GameController controller; [SerializeField] private float shotForce; + + public Rigidbody2D Rigidbody => _rigidbody != null ? _rigidbody : _rigidbody = GetComponent(); + private Rigidbody2D _rigidbody; private void OnCollisionEnter2D(Collision2D other) { @@ -20,9 +23,8 @@ namespace Controllers public void Shoot(Vector3 target) { transform.right = (target - transform.position); - var rb = GetComponent(); - rb.velocity = Vector2.zero; - rb.AddForce((transform.right + (transform.up * 0.5f)) * shotForce); + Rigidbody.velocity = Vector2.zero; + Rigidbody.AddForce((transform.right + (transform.up * 0.5f)) * shotForce); } } } diff --git a/Assets/Scripts/Controllers/GameController.cs b/Assets/Scripts/Controllers/GameController.cs index e1af95c..3260fb2 100644 --- a/Assets/Scripts/Controllers/GameController.cs +++ b/Assets/Scripts/Controllers/GameController.cs @@ -1,5 +1,6 @@ using System; using UnityEngine; +using UnityEngine.UI; namespace Controllers { @@ -19,11 +20,17 @@ namespace Controllers /// [SerializeField] public Ball ball; + [Header("Hoops")] [SerializeField] public Hoop PlayerHoop; [SerializeField] public Hoop EnemyHoop; + [Header("SFX")] [SerializeField] public AudioSource dribbleSound; + [Header("UI")] + [SerializeField] private Text playerScoreText; + [SerializeField] private Text enemyScoreText; + private void Awake() { player = new Player { isEnemy = false, controller = this }; @@ -33,6 +40,17 @@ namespace Controllers EnemyHoop.controller = this; BallAnimation = ball.GetComponentInChildren(); } + + private void Start() + { + UpdateUI(); + } + + private void UpdateUI() + { + playerScoreText.text = $"{player.score}"; + enemyScoreText.text = $"{enemy.score}"; + } public struct Player { @@ -59,6 +77,8 @@ namespace Controllers { score += 2; } + + controller.UpdateUI(); } private State dribble => isEnemy ? State.EnemyDribble : State.PlayerDribble; @@ -95,6 +115,9 @@ namespace Controllers controller.BallAnimation.enabled = false; controller.dribbleSound.Play(); Move(handPosition); + + controller.ball.Rigidbody.bodyType = RigidbodyType2D.Kinematic; + return true; } @@ -109,6 +132,7 @@ namespace Controllers controller.BallAnimation.enabled = true; controller.dribbleSound.Stop(); controller.state = shoot; + controller.ball.Rigidbody.bodyType = RigidbodyType2D.Dynamic; controller.ball.Shoot(hoop.transform.position); lastShotPosition = playerTransform.position; return true; @@ -119,6 +143,7 @@ namespace Controllers { BallAnimation.enabled = true; dribbleSound.Stop(); + ball.Rigidbody.bodyType = RigidbodyType2D.Dynamic; state = State.Idle; } -- cgit v1.2.3-56-ge451