ball spawning and velocity

main
Benjamin Kraft 1 year ago
parent 918cc41428
commit 32eaec7c21
  1. 4
      Assets/Scripts/Dimensions.cs
  2. 16
      Assets/Scripts/Game/Ball.cs
  3. 5
      Assets/Scripts/Game/DeathGate.cs
  4. 61
      Assets/Scripts/Game/GameManager.cs
  5. 10
      Assets/Scripts/Game/Player.cs
  6. 12
      Assets/UI Toolkit/player_panel.uss

@ -41,6 +41,7 @@ public class Dimensions : MonoBehaviour {
private void Update() { private void Update() {
float height = mainCamera.orthographicSize * 2; float height = mainCamera.orthographicSize * 2;
float allWidth = mainCamera.aspect * height;
float panelHeight = height * panelHeightPercentage / 100f; float panelHeight = height * panelHeightPercentage / 100f;
float playGroundHeight = height - 2 * panelHeight; float playGroundHeight = height - 2 * panelHeight;
float playGroundWidth = playGroundHeight * playGroundAspect; float playGroundWidth = playGroundHeight * playGroundAspect;
@ -83,8 +84,7 @@ public class Dimensions : MonoBehaviour {
playerPanelTop.transform.position = new Vector2(0, (height + Height) / 4); playerPanelTop.transform.position = new Vector2(0, (height + Height) / 4);
playerPanelBottom.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); playerPanelTop.size = playerPanelBottom.size = new Vector2(allWidth, panelHeight);
background.size = new Vector2(allWidth, playGroundHeight); background.size = new Vector2(allWidth, playGroundHeight);

@ -1,4 +1,5 @@
using System; using System;
using System.Collections;
using Unity.Netcode; using Unity.Netcode;
using UnityEngine; using UnityEngine;
using UnityEngine.Serialization; using UnityEngine.Serialization;
@ -9,9 +10,12 @@ namespace Game {
public Rigidbody2D Rb { get; private set; } public Rigidbody2D Rb { get; private set; }
public Player LastContactPlayer { get; private set; } public Player LastContactPlayer { get; private set; }
public bool IsPermanent { get; set; }
private CircleCollider2D Collider { get; set; } private CircleCollider2D Collider { get; set; }
private const float SpeedGain = 1.025f;
public float Radius { public float Radius {
get => transform.localScale.x * Collider.radius; get => transform.localScale.x * Collider.radius;
set => transform.localScale = new Vector3(1, 1, 1) * value * 2; set => transform.localScale = new Vector3(1, 1, 1) * value * 2;
@ -19,7 +23,12 @@ namespace Game {
private void OnCollisionEnter2D(Collision2D other) { private void OnCollisionEnter2D(Collision2D other) {
if (other.gameObject.TryGetComponent(out Player player)) if (other.gameObject.TryGetComponent(out Player player))
LastContactPlayer = player; PlayerContact(player);
}
private void PlayerContact(Player player) {
LastContactPlayer = player;
Rb.velocity *= SpeedGain;
} }
private void Awake() { private void Awake() {
@ -27,10 +36,5 @@ namespace Game {
Collider = GetComponent<CircleCollider2D>(); Collider = GetComponent<CircleCollider2D>();
} }
private void Start() {
Rb.velocity = new Vector2(0, 25);
Rb.position = new Vector2(Random.Range(-2, 2), 0);
}
} }
} }

@ -25,8 +25,9 @@ namespace Game {
Ball ball = other.GetComponent<Ball>(); Ball ball = other.GetComponent<Ball>();
GameManager.Singleton.RemoveBall(ball); GameManager.Singleton.RemoveBall(ball);
GameManager.Singleton.SpawnBall(); if (ball.IsPermanent)
GameManager.Singleton.SpawnBall(thisPlayer, true);
} }
} }

@ -31,44 +31,10 @@ namespace Game {
public Player Player1 { get; private set; } public Player Player1 { get; private set; }
public Player Player2 { 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() { private void Awake() {
Settings.Type = Type.Hybrid; Settings.Type = Type.Hybrid;
Settings.AIDifficulty = Difficulty.Hard; Settings.AIDifficulty = Difficulty.Hard;
var ball = Instantiate(ballPrefab).GetComponent<Ball>();
Balls.Add(ball);
ball.Radius = 0.5f;
var p1Obj = Instantiate(playerPrefab); var p1Obj = Instantiate(playerPrefab);
var p2Obj = Instantiate(playerPrefab); var p2Obj = Instantiate(playerPrefab);
@ -99,8 +65,8 @@ namespace Game {
Player1 = p1; Player1 = p1;
Player2 = p2; Player2 = p2;
Tests(); SpawnBall(Player1, true);
} }
private IEnumerator Start() { private IEnumerator Start() {
@ -117,15 +83,28 @@ namespace Game {
mod.Properties = properties; mod.Properties = properties;
} }
public void SpawnBall() { public void SpawnBall(Player towards, bool isPermanent) {
Vector2 position = Vector2.zero; const float startSpeed = 15;
Vector2 velocity = Vector2.up; Vector2 position = new Vector2(0, -towards.transform.position.y * 0.5f);
var ball = Instantiate(ballPrefab, position, Quaternion.identity).GetComponent<Ball>(); var ball = Instantiate(ballPrefab, position, Quaternion.identity).GetComponent<Ball>();
var rb = ball.Rb; ball.Rb.velocity = RandomDirectionTowards(towards) * startSpeed;
rb.velocity = velocity; ball.IsPermanent = isPermanent;
ball.Radius = 0.5f;
Balls.Add(ball); 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) { public void RemoveBall(Ball ball) {
Balls.Remove(ball); Balls.Remove(ball);

@ -120,10 +120,12 @@ namespace Game {
private void FixedUpdate() { private void FixedUpdate() {
if (!isThisClient) if (!isThisClient)
return; return;
var keyboard = Keyboard.current; if (Application.isEditor) {
//goingLeft = keyboard.aKey.isPressed; var keyboard = Keyboard.current;
//goingRight = keyboard.dKey.isPressed; goingLeft = keyboard.aKey.isPressed;
goingRight = keyboard.dKey.isPressed;
}
TryLinearMove(Time.fixedDeltaTime); TryLinearMove(Time.fixedDeltaTime);
} }

@ -19,7 +19,7 @@
margin-right: 10px; margin-right: 10px;
margin-top: 10px; margin-top: 10px;
margin-bottom: 10px; margin-bottom: 10px;
background-color: rgba(91, 91, 91, 255); background-color: rgb(91, 91, 91);
flex-grow: 1; flex-grow: 1;
} }
@ -29,7 +29,7 @@
} }
#score { #score {
font-size: 50px; font-size: 70px;
-unity-font-style: bold; -unity-font-style: bold;
-unity-text-align: middle-center; -unity-text-align: middle-center;
margin-left: 5px; margin-left: 5px;
@ -49,8 +49,8 @@
} }
.go:hover { .go:hover {
border-left-color: rgba(64, 72, 255, 255); border-left-color: rgb(64, 72, 255);
border-right-color: rgba(64, 72, 255, 255); border-right-color: rgb(64, 72, 255);
border-top-color: rgba(64, 72, 255, 255); border-top-color: rgb(64, 72, 255);
border-bottom-color: rgba(64, 72, 255, 255); border-bottom-color: rgb(64, 72, 255);
} }

Loading…
Cancel
Save