From 32eaec7c212ad9ab261876e4797acfcd914fe902 Mon Sep 17 00:00:00 2001 From: Benjamin Kraft Date: Thu, 13 Apr 2023 16:47:39 +0200 Subject: [PATCH] ball spawning and velocity --- Assets/Scripts/Dimensions.cs | 4 +- Assets/Scripts/Game/Ball.cs | 16 +++++--- Assets/Scripts/Game/DeathGate.cs | 5 ++- Assets/Scripts/Game/GameManager.cs | 61 ++++++++++-------------------- Assets/Scripts/Game/Player.cs | 10 +++-- Assets/UI Toolkit/player_panel.uss | 12 +++--- 6 files changed, 47 insertions(+), 61 deletions(-) diff --git a/Assets/Scripts/Dimensions.cs b/Assets/Scripts/Dimensions.cs index 93746f4..a0e34d0 100644 --- a/Assets/Scripts/Dimensions.cs +++ b/Assets/Scripts/Dimensions.cs @@ -41,6 +41,7 @@ public class Dimensions : MonoBehaviour { private void Update() { float height = mainCamera.orthographicSize * 2; + float allWidth = mainCamera.aspect * height; float panelHeight = height * panelHeightPercentage / 100f; float playGroundHeight = height - 2 * panelHeight; float playGroundWidth = playGroundHeight * playGroundAspect; @@ -83,8 +84,7 @@ public class Dimensions : MonoBehaviour { playerPanelTop.transform.position = new Vector2(0, (height + Height) / 4); playerPanelBottom.transform.position = new Vector2(0, -(height + Height) / 4); - - float allWidth = mainCamera.aspect * height; + playerPanelTop.size = playerPanelBottom.size = new Vector2(allWidth, panelHeight); background.size = new Vector2(allWidth, playGroundHeight); diff --git a/Assets/Scripts/Game/Ball.cs b/Assets/Scripts/Game/Ball.cs index 187429c..099a317 100644 --- a/Assets/Scripts/Game/Ball.cs +++ b/Assets/Scripts/Game/Ball.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using Unity.Netcode; using UnityEngine; using UnityEngine.Serialization; @@ -9,9 +10,12 @@ namespace Game { public Rigidbody2D Rb { get; private set; } public Player LastContactPlayer { get; private set; } + public bool IsPermanent { get; set; } private CircleCollider2D Collider { get; set; } + private const float SpeedGain = 1.025f; + public float Radius { get => transform.localScale.x * Collider.radius; set => transform.localScale = new Vector3(1, 1, 1) * value * 2; @@ -19,7 +23,12 @@ namespace Game { private void OnCollisionEnter2D(Collision2D other) { if (other.gameObject.TryGetComponent(out Player player)) - LastContactPlayer = player; + PlayerContact(player); + } + + private void PlayerContact(Player player) { + LastContactPlayer = player; + Rb.velocity *= SpeedGain; } private void Awake() { @@ -27,10 +36,5 @@ namespace Game { Collider = GetComponent(); } - private void Start() { - Rb.velocity = new Vector2(0, 25); - Rb.position = new Vector2(Random.Range(-2, 2), 0); - } - } } diff --git a/Assets/Scripts/Game/DeathGate.cs b/Assets/Scripts/Game/DeathGate.cs index ef39c62..cbb894b 100644 --- a/Assets/Scripts/Game/DeathGate.cs +++ b/Assets/Scripts/Game/DeathGate.cs @@ -25,8 +25,9 @@ namespace Game { Ball ball = other.GetComponent(); GameManager.Singleton.RemoveBall(ball); - - GameManager.Singleton.SpawnBall(); + + if (ball.IsPermanent) + GameManager.Singleton.SpawnBall(thisPlayer, true); } } diff --git a/Assets/Scripts/Game/GameManager.cs b/Assets/Scripts/Game/GameManager.cs index 89cf81c..4e381fc 100644 --- a/Assets/Scripts/Game/GameManager.cs +++ b/Assets/Scripts/Game/GameManager.cs @@ -31,44 +31,10 @@ namespace Game { public Player Player1 { get; private set; } public Player Player2 { get; private set; } - private static void Tests() { - var v1 = new Vector2(1, 3); - var v2 = new Vector2(3, 2); - var v3 = new Vector2(4, 4); - var v4 = new Vector2(1, 1); - var v5 = new Vector2(4, 1); - var v6 = new Vector2(2, 5); - var v7 = new Vector2(2, 3); - var v8 = new Vector2(-1, 4); - var v9 = new Vector2(-2, 1); - var v10 = new Vector2(3, -1); - Vector2 p, rs; - Assert.IsTrue(AIPlayer.Intersect(v8, v4, v1, v9, out p, out rs)); - Assert.IsTrue(AIPlayer.Intersect(v1, v2, v4, v7, out p, out rs)); - Assert.IsTrue(AIPlayer.Intersect(v10, v6, v9, v2, out p, out rs)); - Assert.IsTrue(AIPlayer.Intersect(v9, v5, v8, v10, out p, out rs)); - Assert.IsFalse(AIPlayer.Intersect(v8, v4, v6, v5, out p, out rs)); - Assert.IsFalse(AIPlayer.Intersect(v3, v5, v6, v8, out p, out rs)); - Assert.IsFalse(AIPlayer.Intersect(v10, v4, v8, v9, out p, out rs)); - Assert.IsFalse(AIPlayer.Intersect(v1, v7, v3, v2, out p, out rs)); - - var v11 = new Vector2(0, 0); - var v12 = new Vector2(0, 5); - var v13 = new Vector2(-2, 2); - var v14 = new Vector2(2, 2); - Assert.IsTrue(AIPlayer.Intersect(v11, v12, v13, v14, out p, out rs)); - Assert.AreApproximatelyEqual(rs.x, 0.4f); - Assert.AreApproximatelyEqual(rs.y, 0.5f); - } - private void Awake() { Settings.Type = Type.Hybrid; Settings.AIDifficulty = Difficulty.Hard; - var ball = Instantiate(ballPrefab).GetComponent(); - Balls.Add(ball); - ball.Radius = 0.5f; - var p1Obj = Instantiate(playerPrefab); var p2Obj = Instantiate(playerPrefab); @@ -99,8 +65,8 @@ namespace Game { Player1 = p1; Player2 = p2; - - Tests(); + + SpawnBall(Player1, true); } private IEnumerator Start() { @@ -117,15 +83,28 @@ namespace Game { mod.Properties = properties; } - public void SpawnBall() { - Vector2 position = Vector2.zero; - Vector2 velocity = Vector2.up; + public void SpawnBall(Player towards, bool isPermanent) { + const float startSpeed = 15; + Vector2 position = new Vector2(0, -towards.transform.position.y * 0.5f); var ball = Instantiate(ballPrefab, position, Quaternion.identity).GetComponent(); - var rb = ball.Rb; - rb.velocity = velocity; + ball.Rb.velocity = RandomDirectionTowards(towards) * startSpeed; + ball.IsPermanent = isPermanent; + ball.Radius = 0.5f; Balls.Add(ball); } + + private static Vector2 RandomDirectionTowards(Player player) { + const float maxAngle = 45; + float radians = Random.Range(-maxAngle, maxAngle) * Mathf.PI / 180; + float x = Mathf.Sin(radians); + float y = Mathf.Cos(radians) * player.Side switch { + Side.Top => 1, + Side.Bottom => -1, + _ => throw new ArgumentOutOfRangeException() + }; + return new Vector2(x, y); + } public void RemoveBall(Ball ball) { Balls.Remove(ball); diff --git a/Assets/Scripts/Game/Player.cs b/Assets/Scripts/Game/Player.cs index dfa7331..a4082ed 100644 --- a/Assets/Scripts/Game/Player.cs +++ b/Assets/Scripts/Game/Player.cs @@ -120,10 +120,12 @@ namespace Game { private void FixedUpdate() { if (!isThisClient) return; - - var keyboard = Keyboard.current; - //goingLeft = keyboard.aKey.isPressed; - //goingRight = keyboard.dKey.isPressed; + + if (Application.isEditor) { + var keyboard = Keyboard.current; + goingLeft = keyboard.aKey.isPressed; + goingRight = keyboard.dKey.isPressed; + } TryLinearMove(Time.fixedDeltaTime); } diff --git a/Assets/UI Toolkit/player_panel.uss b/Assets/UI Toolkit/player_panel.uss index ecbc187..1da1227 100644 --- a/Assets/UI Toolkit/player_panel.uss +++ b/Assets/UI Toolkit/player_panel.uss @@ -19,7 +19,7 @@ margin-right: 10px; margin-top: 10px; margin-bottom: 10px; - background-color: rgba(91, 91, 91, 255); + background-color: rgb(91, 91, 91); flex-grow: 1; } @@ -29,7 +29,7 @@ } #score { - font-size: 50px; + font-size: 70px; -unity-font-style: bold; -unity-text-align: middle-center; margin-left: 5px; @@ -49,8 +49,8 @@ } .go:hover { - border-left-color: rgba(64, 72, 255, 255); - border-right-color: rgba(64, 72, 255, 255); - border-top-color: rgba(64, 72, 255, 255); - border-bottom-color: rgba(64, 72, 255, 255); + border-left-color: rgb(64, 72, 255); + border-right-color: rgb(64, 72, 255); + border-top-color: rgb(64, 72, 255); + border-bottom-color: rgb(64, 72, 255); }