|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using Unity.Netcode;
|
|
|
|
using Unity.VisualScripting;
|
|
|
|
using UnityEngine;
|
|
|
|
using Object = UnityEngine.Object;
|
|
|
|
using Random = UnityEngine.Random;
|
|
|
|
|
|
|
|
namespace Game {
|
|
|
|
public class GameManager : NetworkBehaviour {
|
|
|
|
|
|
|
|
public Object ballPrefab;
|
|
|
|
public Object playerPrefab;
|
|
|
|
public Object modificationPrefab;
|
|
|
|
public ModificationProperties[] modifications;
|
|
|
|
public Object wormholePrefab;
|
|
|
|
public Object newBallPrefab;
|
|
|
|
|
|
|
|
public static GameManager Singleton { get; private set; }
|
|
|
|
|
|
|
|
public List<Ball> Balls { get; } = new();
|
|
|
|
|
|
|
|
public Player Player1 { get; private set; }
|
|
|
|
|
|
|
|
public Player Player2 { get; private set; }
|
|
|
|
|
|
|
|
private void Awake() {
|
|
|
|
SetupPlayers();
|
|
|
|
SpawnBall(Player1, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnEnable() {
|
|
|
|
Singleton = this;
|
|
|
|
|
|
|
|
// Move this to settings
|
|
|
|
Application.targetFrameRate = 144;
|
|
|
|
QualitySettings.vSyncCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private IEnumerator Start() {
|
|
|
|
while (Application.isPlaying) {
|
|
|
|
yield return new WaitForSeconds(1);
|
|
|
|
SpawnModification();
|
|
|
|
yield return new WaitForSeconds(2);
|
|
|
|
SpawnWormhole();
|
|
|
|
yield return new WaitForSeconds(1);
|
|
|
|
SpawnNewBall();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SetupPlayers() {
|
|
|
|
Settings.Type = Type.Hybrid;
|
|
|
|
Settings.AIDifficulty = Difficulty.VeryHard;
|
|
|
|
|
|
|
|
var p1Obj = Instantiate(playerPrefab);
|
|
|
|
var p2Obj = Instantiate(playerPrefab);
|
|
|
|
|
|
|
|
Player p1, p2;
|
|
|
|
switch (Settings.Type) {
|
|
|
|
case Type.AI:
|
|
|
|
p1 = p1Obj.AddComponent<AIPlayer>();
|
|
|
|
p2 = p2Obj.AddComponent<AIPlayer>();
|
|
|
|
((AIPlayer) p1).Difficulty = Settings.AIDifficulty;
|
|
|
|
((AIPlayer) p2).Difficulty = Settings.AIDifficulty;
|
|
|
|
break;
|
|
|
|
case Type.RealOnline:
|
|
|
|
p1 = p1Obj.AddComponent<RealPlayer>();
|
|
|
|
p2 = p2Obj.AddComponent<RealPlayer>();
|
|
|
|
((RealPlayer) p1).isThisClient = true;
|
|
|
|
break;
|
|
|
|
case Type.RealOffline:
|
|
|
|
p1 = p1Obj.AddComponent<RealPlayer>();
|
|
|
|
p2 = p2Obj.AddComponent<RealPlayer>();
|
|
|
|
((RealPlayer) p1).isThisClient = true;
|
|
|
|
((RealPlayer) p2).isThisClient = true;
|
|
|
|
break;
|
|
|
|
case Type.Hybrid:
|
|
|
|
p1 = p1Obj.AddComponent<RealPlayer>();
|
|
|
|
p2 = p2Obj.AddComponent<AIPlayer>();
|
|
|
|
((RealPlayer) p1).isThisClient = true;
|
|
|
|
((AIPlayer) p2).Difficulty = Settings.AIDifficulty;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new ArgumentOutOfRangeException();
|
|
|
|
}
|
|
|
|
|
|
|
|
p1.Side = Side.Bottom;
|
|
|
|
p2.Side = Side.Top;
|
|
|
|
|
|
|
|
Player1 = p1;
|
|
|
|
Player2 = p2;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SpawnNewBall() {
|
|
|
|
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<NewBall>().IsPermanent = Random.value > 0.8f;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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<Modification>();
|
|
|
|
mod.Properties = properties;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SpawnBall(Player towards, bool isPermanent) {
|
|
|
|
const float startSpeed = 15;
|
|
|
|
var position = new Vector2(0, -towards.transform.position.y * 0.5f);
|
|
|
|
var ball = Instantiate(ballPrefab, position, Quaternion.identity).GetComponent<Ball>();
|
|
|
|
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;
|
|
|
|
var radians = Random.Range(-maxAngle, maxAngle) * Mathf.PI / 180;
|
|
|
|
var x = Mathf.Sin(radians);
|
|
|
|
var 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|