Trail improved and pickups complete
This commit is contained in:
@@ -5,6 +5,71 @@ using Mirror;
|
||||
|
||||
public class MinigameManager : NetworkBehaviour
|
||||
{
|
||||
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>();
|
||||
List<PickupItem> MoonPool = new List<PickupItem>();
|
||||
List<PickupItem> StarsPool = new List<PickupItem>();
|
||||
public GameObject moon;
|
||||
public GameObject star;
|
||||
|
||||
private void Awake() {
|
||||
SceneData.GameManager=this;
|
||||
}
|
||||
|
||||
void Update(){
|
||||
|
||||
if(!isServer){return;}
|
||||
|
||||
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(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>());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void DeactivatePickupItem(PickupItem item){
|
||||
if(item.type == PickupItem.PickupType.Moon){
|
||||
ActiveMoons.Remove(item);
|
||||
MoonPool.Add(item);
|
||||
}else if(item.type == PickupItem.PickupType.Star){
|
||||
ActiveStars.Remove(item);
|
||||
StarsPool.Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetRespawn(GameObject player){
|
||||
StartCoroutine(setRespawn(player));
|
||||
@@ -21,4 +86,12 @@ public class MinigameManager : NetworkBehaviour
|
||||
yield return new WaitForSeconds(1);
|
||||
}
|
||||
}
|
||||
|
||||
Vector3 getRandomPositionOnMap(){
|
||||
return new Vector3(Random.Range(-mapRadius, mapRadius), Random.Range(-mapRadius,mapRadius));
|
||||
}
|
||||
|
||||
void OnDrawGizmos() {
|
||||
Gizmos.DrawWireSphere(transform.position, mapRadius);
|
||||
}
|
||||
}
|
||||
|
||||
91
Assets/Game/Scripts/Minigame/PickupItem.cs
Normal file
91
Assets/Game/Scripts/Minigame/PickupItem.cs
Normal file
@@ -0,0 +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
|
||||
}
|
||||
}
|
||||
|
||||
11
Assets/Game/Scripts/Minigame/PickupItem.cs.meta
Normal file
11
Assets/Game/Scripts/Minigame/PickupItem.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: afb8c8ecfb1028546b4ee5775cc5edfe
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -26,6 +26,7 @@ public class SceneDataHolder : MonoBehaviour
|
||||
public static class SceneData{
|
||||
public static GameObject localPlayer;
|
||||
public static SceneDataHolder holder;
|
||||
public static MinigameManager GameManager;
|
||||
public static UnityEvent OnBoostDown = new UnityEvent();
|
||||
public static UnityEvent OnBoostUp = new UnityEvent();
|
||||
}
|
||||
@@ -76,9 +76,6 @@ public class SpaceshipController : NetworkBehaviour
|
||||
}
|
||||
|
||||
void OnScaleChanged(float oldScale, float newScale){
|
||||
transform.localScale = new Vector3(newScale,newScale,newScale);
|
||||
trailMgr.trail.startWidth = trailMgr.trail.endWidth = newScale;
|
||||
|
||||
if(isLocalPlayer){
|
||||
SceneData.holder.boostBtn.gameObject.SetActive(newScale>1);
|
||||
}
|
||||
@@ -122,6 +119,11 @@ public class SpaceshipController : NetworkBehaviour
|
||||
void FixedUpdate()
|
||||
{
|
||||
pnameTxt.rectTransform.rotation = Quaternion.Euler(Vector3.zero);
|
||||
|
||||
//Update size of trail and spaceship
|
||||
transform.localScale = Vector3.Lerp(transform.localScale,new Vector3(scaleMultiplier,scaleMultiplier,scaleMultiplier),0.1f);
|
||||
trailMgr.trail.startWidth = Mathf.Lerp(trailMgr.trail.startWidth,scaleMultiplier,0.1f);
|
||||
|
||||
if(dead){return;}
|
||||
|
||||
if (isLocalPlayer)
|
||||
@@ -305,8 +307,8 @@ public class SpaceshipController : NetworkBehaviour
|
||||
|
||||
if (sentTime < lastRubberBandTime) { Debug.Log("Old rubber band rpc, ignoree..."); return; }
|
||||
//Lag comprehension
|
||||
double delay = timeInMillis - sentTime + 10;
|
||||
int numberOfFrames = (int)((float)(delay) / 20f);
|
||||
double delay = timeInMillis - sentTime;
|
||||
int numberOfFrames = (int)((float)(delay*2) / 20f);
|
||||
Vector3 newPosition = position;
|
||||
Quaternion newRotation = rotation;
|
||||
for (int i = 0; i < numberOfFrames; i++)
|
||||
@@ -386,13 +388,13 @@ public class SpaceshipController : NetworkBehaviour
|
||||
|
||||
|
||||
|
||||
public void TrailCollided(RaycastHit2D hit){
|
||||
public void TrailCollided(Collider2D hit){
|
||||
if(!isServer){
|
||||
// Debug.Log("This cannot run on client, That's illegal!"); // <-- What this log says
|
||||
return;
|
||||
}
|
||||
|
||||
SpaceshipController deadPlayer = hit.collider.GetComponent<SpaceshipController>();
|
||||
SpaceshipController deadPlayer = hit.GetComponent<SpaceshipController>();
|
||||
|
||||
if(deadPlayer!=null && !deadPlayer.dead){ // <-- okay we killed someone | KILLCODE
|
||||
deadPlayer.Die(pname);
|
||||
@@ -406,8 +408,13 @@ public class SpaceshipController : NetworkBehaviour
|
||||
Scores+= 10; //TODO: Need to change Scores on kills?
|
||||
scaleMultiplier+=0.05f;
|
||||
OnScaleChanged(scaleMultiplier,scaleMultiplier);
|
||||
trailTime = trailMgr.trail.time+ trailIncrementRate;
|
||||
IncreaseTrail(trailIncrementRate);
|
||||
}
|
||||
|
||||
void IncreaseTrail(float rate){
|
||||
trailTime = trailMgr.trail.time+ rate;
|
||||
trailMgr.trail.time = trailTime;
|
||||
Debug.Log("Increasing trail of" + pname);
|
||||
}
|
||||
|
||||
[Command]
|
||||
@@ -415,6 +422,19 @@ public class SpaceshipController : NetworkBehaviour
|
||||
OnKill();
|
||||
}
|
||||
|
||||
public void CollectPickup(PickupItem.PickupType type){
|
||||
if(isClient){Debug.Log("Server function ran on client. That's illegal!");} // <-- What this log says
|
||||
switch(type){
|
||||
case PickupItem.PickupType.Moon:
|
||||
IncreaseTrail(trailIncrementRate);
|
||||
break;
|
||||
|
||||
case PickupItem.PickupType.Star:
|
||||
IncreaseTrail(trailIncrementRate/2f);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void Die(string killer){
|
||||
Debug.Log($"Sending death signal to {pname} by {killer}");
|
||||
|
||||
|
||||
@@ -8,8 +8,8 @@ public class TrailCollider : MonoBehaviour
|
||||
public TrailMgr trailMgr;
|
||||
public float radius;
|
||||
void Update(){
|
||||
RaycastHit2D hit = Physics2D.CircleCast(transform.position, radius, Vector2.up);
|
||||
if(hit.collider!=null){
|
||||
Collider2D hit = Physics2D.OverlapCircle(transform.position, radius);
|
||||
if(hit!=null){
|
||||
if(hit.transform.root == trailMgr.transform){return;} // <-- avoid eating myself
|
||||
trailMgr.OnColliderHit(hit);
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ public class TrailMgr : MonoBehaviour
|
||||
|
||||
|
||||
|
||||
public void OnColliderHit(RaycastHit2D hit){
|
||||
public void OnColliderHit(Collider2D hit){
|
||||
controller.TrailCollided(hit);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user