ball spawning and velocity

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

@ -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;
@ -84,7 +85,6 @@ 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);

@ -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<CircleCollider2D>();
}
private void Start() {
Rb.velocity = new Vector2(0, 25);
Rb.position = new Vector2(Random.Range(-2, 2), 0);
}
}
}

@ -26,7 +26,8 @@ namespace Game {
Ball ball = other.GetComponent<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 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<Ball>();
Balls.Add(ball);
ball.Radius = 0.5f;
var p1Obj = Instantiate(playerPrefab);
var p2Obj = Instantiate(playerPrefab);
@ -100,7 +66,7 @@ namespace Game {
Player1 = p1;
Player2 = p2;
Tests();
SpawnBall(Player1, true);
}
private IEnumerator Start() {
@ -117,16 +83,29 @@ 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<Ball>();
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);
Destroy(ball.gameObject);

@ -121,9 +121,11 @@ namespace Game {
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);
}

@ -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);
}

Loading…
Cancel
Save