Play zone + leftover spawning
This commit is contained in:
@@ -5,93 +5,164 @@ using Mirror;
|
||||
|
||||
public class MinigameManager : NetworkBehaviour
|
||||
{
|
||||
public static MinigameManager instance;
|
||||
public float mapRadius;
|
||||
public int maxMoons, maxStars = 100;
|
||||
public Transform pickupItemsParent;
|
||||
public List<PickupItem> ActiveMoons = new List<PickupItem>();
|
||||
public List<PickupItem> ActiveStars = new List<PickupItem>();
|
||||
private List<PickupItem> ActiveMoons = new List<PickupItem>();
|
||||
private List<PickupItem> ActiveStars = new List<PickupItem>();
|
||||
|
||||
List<PickupItem> MoonPool = new List<PickupItem>();
|
||||
List<PickupItem> StarsPool = new List<PickupItem>();
|
||||
public GameObject moon;
|
||||
public GameObject star;
|
||||
|
||||
private void Awake() {
|
||||
SceneData.GameManager=this;
|
||||
private void Awake()
|
||||
{
|
||||
SceneData.GameManager = this;
|
||||
instance = this;
|
||||
}
|
||||
|
||||
void Update(){
|
||||
void Update()
|
||||
{
|
||||
|
||||
if(!isServer){return;}
|
||||
if (!isServer) { return; }
|
||||
|
||||
HandlePickupSpawn();
|
||||
KillOutOfBoundsPlayers();
|
||||
|
||||
}
|
||||
|
||||
void KillOutOfBoundsPlayers()
|
||||
{
|
||||
SpaceshipController[] players = FindObjectsOfType<SpaceshipController>();
|
||||
foreach (SpaceshipController player in players)
|
||||
{
|
||||
if (Vector3.Distance(player.transform.position, Vector3.zero) > mapRadius)
|
||||
{
|
||||
//Out of bounds. Kill him
|
||||
player.Die("Playzone");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HandlePickupSpawn()
|
||||
{
|
||||
int moonsNeed = maxMoons - ActiveMoons.Count;
|
||||
int starsNeed = maxStars - ActiveStars.Count;
|
||||
|
||||
if(moonsNeed > 0){ // <-- We need more moons!
|
||||
for(int i =0; i < moonsNeed; i++){
|
||||
if(MoonPool.Count > 0){ // <-- Got some in the pool, no need to spawn new
|
||||
PickupItem pickedMoon = MoonPool[0];
|
||||
pickedMoon.Reposition(getRandomPositionOnMap());
|
||||
|
||||
ActiveMoons.Add(pickedMoon);
|
||||
MoonPool.RemoveAt(0);
|
||||
}else{
|
||||
GameObject newMoon = Instantiate(moon,pickupItemsParent);
|
||||
NetworkServer.Spawn(newMoon);
|
||||
newMoon.GetComponent<PickupItem>().Reposition(getRandomPositionOnMap());
|
||||
ActiveMoons.Add(newMoon.GetComponent<PickupItem>());
|
||||
}
|
||||
}
|
||||
if (moonsNeed > 0)
|
||||
{ // <-- We need more moons!
|
||||
SpawnMoons(moonsNeed);
|
||||
}
|
||||
|
||||
if(starsNeed > 0){ // <-- We need more moons!
|
||||
for(int i =0; i < starsNeed; i++){
|
||||
if(StarsPool.Count > 0){ // <-- Got some in the pool, no need to spawn new
|
||||
PickupItem pickedStar = StarsPool[0];
|
||||
pickedStar.Reposition(getRandomPositionOnMap());
|
||||
|
||||
ActiveStars.Add(pickedStar);
|
||||
StarsPool.RemoveAt(0);
|
||||
}else{
|
||||
GameObject newStar = Instantiate(star,pickupItemsParent);
|
||||
NetworkServer.Spawn(newStar);
|
||||
newStar.GetComponent<PickupItem>().Reposition(getRandomPositionOnMap());
|
||||
ActiveStars.Add(newStar.GetComponent<PickupItem>());
|
||||
}
|
||||
}
|
||||
}
|
||||
if (starsNeed > 0)
|
||||
{ // <-- We need more moons!
|
||||
SpawnStars(starsNeed);
|
||||
}
|
||||
}
|
||||
|
||||
public void DeactivatePickupItem(PickupItem item){
|
||||
if(item.type == PickupItem.PickupType.Moon){
|
||||
public void SpawnLeftoverPickups(Vector3 position, int amount)
|
||||
{
|
||||
SpawnStars(amount, focusedPosition: position);
|
||||
}
|
||||
|
||||
void SpawnStars(int amount, Vector3? focusedPosition = null)
|
||||
{
|
||||
for (int i = 0; i < amount; i++)
|
||||
{
|
||||
Vector3 newPosition = (focusedPosition == null) ? getRandomPositionOnMap() : getRandomPointInCirlce((Vector3)focusedPosition, 10);
|
||||
if (StarsPool.Count > 0)
|
||||
{ // <-- Got some in the pool, no need to spawn new
|
||||
PickupItem pickedStar = StarsPool[0];
|
||||
pickedStar.Reposition(newPosition);
|
||||
|
||||
ActiveStars.Add(pickedStar);
|
||||
StarsPool.RemoveAt(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
GameObject newStar = Instantiate(star, pickupItemsParent);
|
||||
NetworkServer.Spawn(newStar);
|
||||
newStar.GetComponent<PickupItem>().Reposition(newPosition);
|
||||
ActiveStars.Add(newStar.GetComponent<PickupItem>());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SpawnMoons(int amount)
|
||||
{
|
||||
for (int i = 0; i < amount; i++)
|
||||
{
|
||||
if (MoonPool.Count > 0)
|
||||
{ // <-- Got some in the pool, no need to spawn new
|
||||
PickupItem pickedMoon = MoonPool[0];
|
||||
pickedMoon.Reposition(getRandomPositionOnMap());
|
||||
|
||||
ActiveMoons.Add(pickedMoon);
|
||||
MoonPool.RemoveAt(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
GameObject newMoon = Instantiate(moon, pickupItemsParent);
|
||||
NetworkServer.Spawn(newMoon);
|
||||
newMoon.GetComponent<PickupItem>().Reposition(getRandomPositionOnMap());
|
||||
ActiveMoons.Add(newMoon.GetComponent<PickupItem>());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void DeactivatePickupItem(PickupItem item)
|
||||
{
|
||||
if (item.type == PickupItem.PickupType.Moon)
|
||||
{
|
||||
ActiveMoons.Remove(item);
|
||||
MoonPool.Add(item);
|
||||
}else if(item.type == PickupItem.PickupType.Star){
|
||||
}
|
||||
else if (item.type == PickupItem.PickupType.Star)
|
||||
{
|
||||
ActiveStars.Remove(item);
|
||||
StarsPool.Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetRespawn(GameObject player){
|
||||
public void SetRespawn(GameObject player)
|
||||
{
|
||||
StartCoroutine(setRespawn(player));
|
||||
}
|
||||
|
||||
IEnumerator setRespawn(GameObject player){
|
||||
if(isServer){
|
||||
IEnumerator setRespawn(GameObject player)
|
||||
{
|
||||
if (isServer)
|
||||
{
|
||||
player.SetActive(false);
|
||||
yield return new WaitForSeconds(3);
|
||||
Vector3 RespawnPoint = NetworkManager.startPositions[Random.Range(0, NetworkManager.startPositions.Count-1)].position;
|
||||
|
||||
Vector3 RespawnPoint = NetworkManager.startPositions[Random.Range(0, NetworkManager.startPositions.Count - 1)].position;
|
||||
|
||||
player.GetComponent<SpaceshipController>().Respawn(RespawnPoint);
|
||||
}else{
|
||||
}
|
||||
else
|
||||
{
|
||||
yield return new WaitForSeconds(1);
|
||||
}
|
||||
}
|
||||
|
||||
Vector3 getRandomPositionOnMap(){
|
||||
return new Vector3(Random.Range(-mapRadius, mapRadius), Random.Range(-mapRadius,mapRadius));
|
||||
Vector3 getRandomPositionOnMap()
|
||||
{
|
||||
return getRandomPointInCirlce(Vector3.zero, mapRadius);
|
||||
}
|
||||
|
||||
void OnDrawGizmos() {
|
||||
Gizmos.DrawWireSphere(transform.position, mapRadius);
|
||||
Vector3 getRandomPointInCirlce(Vector3 center, float radius)
|
||||
{
|
||||
float r = radius * Mathf.Sqrt(Random.Range(0f, 1f));
|
||||
float theta = Random.Range(0f, 1f) * 2 * Mathf.PI;
|
||||
float x = center.x + r * Mathf.Cos(theta);
|
||||
float y = center.y + r * Mathf.Sin(theta);
|
||||
return new Vector3(x, y);
|
||||
}
|
||||
|
||||
void OnDrawGizmos()
|
||||
{
|
||||
Gizmos.DrawWireSphere(transform.position, mapRadius);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,91 +1,91 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Mirror;
|
||||
public class PickupItem : NetworkBehaviour
|
||||
{
|
||||
public PickupType type;
|
||||
public float radius=1;
|
||||
public Color gizmoColor = Color.green;
|
||||
public bool active = true;
|
||||
|
||||
|
||||
void Update()
|
||||
{
|
||||
if(!isServer){return;}
|
||||
if(!active){return;}
|
||||
|
||||
Collider2D hit = Physics2D.OverlapCircle(transform.position, radius);
|
||||
if(hit!=null && hit.GetComponent<SpaceshipController>()!=null){
|
||||
Debug.Log(hit.GetComponent<SpaceshipController>().pname +$" collected me at {transform.position}");
|
||||
hit.GetComponent<SpaceshipController>().CollectPickup(type);
|
||||
active=false;
|
||||
Deactivate();
|
||||
}
|
||||
}
|
||||
|
||||
public void Reposition(Vector3 newPosition){
|
||||
if(isServer){
|
||||
reposition(newPosition);
|
||||
|
||||
RpcReposition(newPosition);
|
||||
}else{
|
||||
CmdReposition(newPosition);
|
||||
}
|
||||
}
|
||||
|
||||
[Command]
|
||||
void CmdReposition(Vector3 newPosition){
|
||||
reposition(newPosition);
|
||||
|
||||
RpcReposition(newPosition);
|
||||
}
|
||||
|
||||
[ClientRpc]
|
||||
void RpcReposition(Vector3 newPosition){
|
||||
reposition(newPosition);
|
||||
}
|
||||
|
||||
void reposition(Vector3 newPosition){
|
||||
active=true;
|
||||
transform.position = newPosition;
|
||||
gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
public void Deactivate(){
|
||||
if(isServer){
|
||||
deactivate();
|
||||
RpcDeactivate();
|
||||
}else{
|
||||
CmdDeactivate();
|
||||
}
|
||||
}
|
||||
|
||||
void deactivate(){
|
||||
active=false;
|
||||
gameObject.SetActive(false);
|
||||
SceneData.GameManager.DeactivatePickupItem(this);
|
||||
}
|
||||
|
||||
[Command]
|
||||
void CmdDeactivate(){
|
||||
deactivate();
|
||||
RpcDeactivate();
|
||||
}
|
||||
|
||||
[ClientRpc]
|
||||
void RpcDeactivate(){
|
||||
deactivate();
|
||||
}
|
||||
|
||||
private void OnDrawGizmos() {
|
||||
Gizmos.color = gizmoColor;
|
||||
Gizmos.DrawWireSphere(transform.position,radius);
|
||||
}
|
||||
|
||||
public enum PickupType{
|
||||
Star,
|
||||
Moon
|
||||
}
|
||||
}
|
||||
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Mirror;
|
||||
public class PickupItem : NetworkBehaviour
|
||||
{
|
||||
public PickupType type;
|
||||
public float radius=1;
|
||||
public Color gizmoColor = Color.green;
|
||||
public bool active = true;
|
||||
|
||||
|
||||
void Update()
|
||||
{
|
||||
if(!isServer){return;}
|
||||
if(!active){return;}
|
||||
|
||||
Collider2D hit = Physics2D.OverlapCircle(transform.position, radius);
|
||||
if(hit!=null && hit.GetComponent<SpaceshipController>()!=null){
|
||||
Debug.Log(hit.GetComponent<SpaceshipController>().pname +$" collected me at {transform.position}");
|
||||
hit.GetComponent<SpaceshipController>().CollectPickup(type);
|
||||
active=false;
|
||||
Deactivate();
|
||||
}
|
||||
}
|
||||
|
||||
public void Reposition(Vector3 newPosition){
|
||||
if(isServer){
|
||||
reposition(newPosition);
|
||||
|
||||
RpcReposition(newPosition);
|
||||
}else{
|
||||
CmdReposition(newPosition);
|
||||
}
|
||||
}
|
||||
|
||||
[Command]
|
||||
void CmdReposition(Vector3 newPosition){
|
||||
reposition(newPosition);
|
||||
|
||||
RpcReposition(newPosition);
|
||||
}
|
||||
|
||||
[ClientRpc]
|
||||
void RpcReposition(Vector3 newPosition){
|
||||
reposition(newPosition);
|
||||
}
|
||||
|
||||
void reposition(Vector3 newPosition){
|
||||
active=true;
|
||||
transform.position = newPosition;
|
||||
gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
public void Deactivate(){
|
||||
if(isServer){
|
||||
deactivate();
|
||||
RpcDeactivate();
|
||||
}else{
|
||||
CmdDeactivate();
|
||||
}
|
||||
}
|
||||
|
||||
void deactivate(){
|
||||
active=false;
|
||||
gameObject.SetActive(false);
|
||||
SceneData.GameManager.DeactivatePickupItem(this);
|
||||
}
|
||||
|
||||
[Command]
|
||||
void CmdDeactivate(){
|
||||
deactivate();
|
||||
RpcDeactivate();
|
||||
}
|
||||
|
||||
[ClientRpc]
|
||||
void RpcDeactivate(){
|
||||
deactivate();
|
||||
}
|
||||
|
||||
private void OnDrawGizmos() {
|
||||
Gizmos.color = gizmoColor;
|
||||
Gizmos.DrawWireSphere(transform.position,radius);
|
||||
}
|
||||
|
||||
public enum PickupType{
|
||||
Star,
|
||||
Moon
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
44
Assets/Game/Scripts/Minigame/PositionUnitTest.cs
Normal file
44
Assets/Game/Scripts/Minigame/PositionUnitTest.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
using Mirror;
|
||||
|
||||
public class PositionUnitTest : NetworkBehaviour
|
||||
{
|
||||
Logger logger = null;
|
||||
SpaceshipController _controller;
|
||||
void Start(){
|
||||
_controller = GetComponent<SpaceshipController>();
|
||||
}
|
||||
void FixedUpdate()
|
||||
{
|
||||
if(logger == null){
|
||||
if(_controller.pname.Length>0){
|
||||
logger= new Logger(_controller.pname);
|
||||
}
|
||||
return;
|
||||
}
|
||||
logger.Log($"time: {NetworkTime.time}, pos: {transform.position}, rot: {transform.rotation}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class Logger{
|
||||
private string name;
|
||||
public string Name=> name;
|
||||
public string FileLocation => $"{Application.dataPath}/{name}.log";
|
||||
|
||||
public Logger(string _name){
|
||||
name=_name;
|
||||
if(File.Exists(FileLocation)){
|
||||
File.Delete(FileLocation);
|
||||
}
|
||||
File.WriteAllText(FileLocation, $"[{System.DateTime.Now}]Log Created\n");
|
||||
Debug.Log($"Logger initiated at {FileLocation}");
|
||||
}
|
||||
|
||||
public void Log(string newData){
|
||||
File.AppendAllText(FileLocation, $"{newData}\n");
|
||||
}
|
||||
}
|
||||
11
Assets/Game/Scripts/Minigame/PositionUnitTest.cs.meta
Normal file
11
Assets/Game/Scripts/Minigame/PositionUnitTest.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6f6b772eb5e369b4297e0ac00baf35c1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -38,6 +38,8 @@ public class SpaceshipController : NetworkBehaviour
|
||||
|
||||
public bool showDebugHUD = false;
|
||||
|
||||
public float distanceFromCenter= 0;
|
||||
|
||||
[Command]
|
||||
void CmdSetPname(string value){
|
||||
pname = value;
|
||||
@@ -118,6 +120,7 @@ public class SpaceshipController : NetworkBehaviour
|
||||
int lastClientUpdateTime = 0;
|
||||
void FixedUpdate()
|
||||
{
|
||||
distanceFromCenter = Vector3.Distance(transform.position, Vector3.zero);
|
||||
pnameTxt.rectTransform.rotation = Quaternion.Euler(Vector3.zero);
|
||||
|
||||
//Update size of trail and spaceship
|
||||
@@ -399,8 +402,7 @@ public class SpaceshipController : NetworkBehaviour
|
||||
if(deadPlayer!=null && !deadPlayer.dead){ // <-- okay we killed someone | KILLCODE
|
||||
deadPlayer.Die(pname);
|
||||
Debug.Log($"{pname} killed {deadPlayer.pname}");
|
||||
OnKill();
|
||||
|
||||
OnKill();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -436,16 +438,19 @@ public class SpaceshipController : NetworkBehaviour
|
||||
}
|
||||
|
||||
public void Die(string killer){
|
||||
|
||||
MinigameManager.instance.SpawnLeftoverPickups(transform.position, Scores);
|
||||
Debug.Log($"Sending death signal to {pname} by {killer}");
|
||||
|
||||
//Handle Respawning
|
||||
OnScaleChanged(scaleMultiplier,1);
|
||||
scaleMultiplier=1;
|
||||
dead=true;
|
||||
Scores=0;
|
||||
trailTime = 1;
|
||||
trailMgr.trail.time = trailTime;
|
||||
RpcDie(killer);
|
||||
FindObjectOfType<MinigameManager>().SetRespawn(gameObject);
|
||||
MinigameManager.instance.SetRespawn(gameObject);
|
||||
}
|
||||
|
||||
[ClientRpc]
|
||||
|
||||
Reference in New Issue
Block a user