Death is here
This commit is contained in:
@@ -11,7 +11,7 @@ public class CameraFollower : MonoBehaviour
|
||||
void Start()
|
||||
{
|
||||
if(target==null){return;}
|
||||
SetAutoOffset();
|
||||
if(autoOffset){SetAutoOffset();}
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
@@ -23,7 +23,7 @@ public class CameraFollower : MonoBehaviour
|
||||
|
||||
public void SetTarget(Transform Target){
|
||||
target = Target;
|
||||
SetAutoOffset();
|
||||
if(autoOffset){SetAutoOffset();}
|
||||
}
|
||||
|
||||
void SetAutoOffset(){
|
||||
|
||||
24
Assets/Game/Scripts/Minigame/MinigameManager.cs
Normal file
24
Assets/Game/Scripts/Minigame/MinigameManager.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Mirror;
|
||||
|
||||
public class MinigameManager : NetworkBehaviour
|
||||
{
|
||||
|
||||
public void SetRespawn(GameObject player){
|
||||
StartCoroutine(setRespawn(player));
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
player.GetComponent<SpaceshipController>().Respawn(RespawnPoint);
|
||||
}else{
|
||||
yield return new WaitForSeconds(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Game/Scripts/Minigame/MinigameManager.cs.meta
Normal file
11
Assets/Game/Scripts/Minigame/MinigameManager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c35843cf3f2e5e0fc842ea02800896a7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,80 +1,80 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Mirror;
|
||||
|
||||
public class NetworkTrail : NetworkBehaviour
|
||||
{
|
||||
public TrailRenderer trail;
|
||||
public LineRenderer line;
|
||||
public bool enableValidation = true;
|
||||
public int maxDetourPoints = 1;
|
||||
public float DetourThreshold = 1;
|
||||
public int Detours;
|
||||
|
||||
public Vector3[] positions;
|
||||
|
||||
void Start()
|
||||
{
|
||||
//trail.gameObject.SetActive(isServer);
|
||||
line.gameObject.SetActive(!isServer);
|
||||
line.transform.parent=null;
|
||||
line.transform.position =Vector3.zero;
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
if(isServer){
|
||||
positions = new Vector3[trail.positionCount];
|
||||
trail.GetPositions(positions);
|
||||
RpcUpdatePositions(positions);
|
||||
}else{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[ClientRpc]
|
||||
void RpcUpdatePositions(Vector3[] Positions){
|
||||
if(!enableValidation){return;}
|
||||
positions = Positions;
|
||||
line.positionCount = positions.Length;
|
||||
line.SetPositions(positions);
|
||||
|
||||
//Validate With Trail
|
||||
Vector3[] localPositions = new Vector3[trail.positionCount];
|
||||
trail.GetPositions(localPositions);
|
||||
Detours = 0;
|
||||
for(int i=0; i < Positions.Length; i++){
|
||||
bool pointValidated = false;
|
||||
for(int j=1; j < 3; j++){
|
||||
try{
|
||||
if(isCloseEnough(Positions[i], localPositions[i+j])){
|
||||
pointValidated = true;
|
||||
break;
|
||||
}
|
||||
if(isCloseEnough(Positions[i], localPositions[i-j])){
|
||||
pointValidated = true;
|
||||
break;
|
||||
}
|
||||
|
||||
}catch{
|
||||
//No local position, Hence, no validation
|
||||
}
|
||||
}
|
||||
if(!pointValidated){
|
||||
Detours++;
|
||||
}
|
||||
}
|
||||
|
||||
if(Detours > maxDetourPoints){
|
||||
//Too much detours, Set back to servers data
|
||||
trail.SetPositions(Positions);
|
||||
}
|
||||
}
|
||||
|
||||
bool isCloseEnough(Vector3 a, Vector3 b){
|
||||
return Vector3.Distance(a,b) < DetourThreshold;
|
||||
}
|
||||
}
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Mirror;
|
||||
|
||||
public class NetworkTrail : NetworkBehaviour
|
||||
{
|
||||
public TrailRenderer trail;
|
||||
public LineRenderer line;
|
||||
public bool enableValidation = true;
|
||||
public int maxDetourPoints = 1;
|
||||
public float DetourThreshold = 1;
|
||||
public int Detours;
|
||||
|
||||
public Vector3[] positions;
|
||||
|
||||
void Start()
|
||||
{
|
||||
//trail.gameObject.SetActive(isServer);
|
||||
line.gameObject.SetActive(!isServer);
|
||||
line.transform.parent=null;
|
||||
line.transform.position =Vector3.zero;
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
if(isServer){
|
||||
positions = new Vector3[trail.positionCount];
|
||||
trail.GetPositions(positions);
|
||||
RpcUpdatePositions(positions);
|
||||
}else{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[ClientRpc]
|
||||
void RpcUpdatePositions(Vector3[] Positions){
|
||||
if(!enableValidation){return;}
|
||||
positions = Positions;
|
||||
line.positionCount = positions.Length;
|
||||
line.SetPositions(positions);
|
||||
|
||||
//Validate With Trail
|
||||
Vector3[] localPositions = new Vector3[trail.positionCount];
|
||||
trail.GetPositions(localPositions);
|
||||
Detours = 0;
|
||||
for(int i=0; i < Positions.Length; i++){
|
||||
bool pointValidated = false;
|
||||
for(int j=1; j < 3; j++){
|
||||
try{
|
||||
if(isCloseEnough(Positions[i], localPositions[i+j])){
|
||||
pointValidated = true;
|
||||
break;
|
||||
}
|
||||
if(isCloseEnough(Positions[i], localPositions[i-j])){
|
||||
pointValidated = true;
|
||||
break;
|
||||
}
|
||||
|
||||
}catch{
|
||||
//No local position, Hence, no validation
|
||||
}
|
||||
}
|
||||
if(!pointValidated){
|
||||
Detours++;
|
||||
}
|
||||
}
|
||||
|
||||
if(Detours > maxDetourPoints){
|
||||
//Too much detours, Set back to servers data
|
||||
trail.SetPositions(Positions);
|
||||
}
|
||||
}
|
||||
|
||||
bool isCloseEnough(Vector3 a, Vector3 b){
|
||||
return Vector3.Distance(a,b) < DetourThreshold;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ using UnityEngine;
|
||||
public class SceneDataHolder : MonoBehaviour
|
||||
{
|
||||
public Transform trailCollidersParent;
|
||||
public GameObject deadScreen;
|
||||
void Awake()
|
||||
{
|
||||
SceneData.holder = this;
|
||||
@@ -13,5 +14,6 @@ public class SceneDataHolder : MonoBehaviour
|
||||
|
||||
|
||||
public static class SceneData{
|
||||
public static GameObject localPlayer;
|
||||
public static SceneDataHolder holder;
|
||||
}
|
||||
@@ -2,9 +2,17 @@ using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Mirror;
|
||||
using System.Linq;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class SpaceshipController : NetworkBehaviour
|
||||
{
|
||||
[SyncVar(hook=nameof(OnPnameChanged))]
|
||||
public string pname;
|
||||
[SyncVar(hook=nameof(OnScoresChanged))]
|
||||
public int Scores;
|
||||
[SyncVar]
|
||||
public bool dead;
|
||||
public Text pnameTxt;
|
||||
public Transform body;
|
||||
public TrailMgr trailMgr;
|
||||
public float movingSpeed = 0.1f;
|
||||
@@ -21,6 +29,18 @@ public class SpaceshipController : NetworkBehaviour
|
||||
|
||||
public bool showDebugHUD = false;
|
||||
|
||||
[Command]
|
||||
void CmdSetPname(string value){
|
||||
pname = value;
|
||||
}
|
||||
void OnPnameChanged(string oldName, string newName){
|
||||
pnameTxt.text = newName;
|
||||
}
|
||||
|
||||
|
||||
void OnScoresChanged(int oldScores, int newScores){
|
||||
Debug.Log($"Add scores { newScores - oldScores}, (total: {newScores})");
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
@@ -28,6 +48,15 @@ public class SpaceshipController : NetworkBehaviour
|
||||
{
|
||||
if (joystick == null) { joystick = FindObjectOfType<Joystick>(); }
|
||||
FindObjectOfType<CameraFollower>().SetTarget(transform);
|
||||
string myName = PlayerPrefs.GetString("username");
|
||||
SceneData.localPlayer = gameObject;
|
||||
if(isServer){pname=myName;}else{
|
||||
CmdSetPname(myName);
|
||||
}
|
||||
|
||||
pnameTxt.text = myName;
|
||||
pnameTxt.gameObject.SetActive(false);
|
||||
|
||||
}
|
||||
if (isServer)
|
||||
{
|
||||
@@ -42,6 +71,8 @@ public class SpaceshipController : NetworkBehaviour
|
||||
int lastClientUpdateTime = 0;
|
||||
void FixedUpdate()
|
||||
{
|
||||
if(dead){return;}
|
||||
|
||||
if (isLocalPlayer)
|
||||
{
|
||||
joyInput = joystick.input;
|
||||
@@ -68,11 +99,15 @@ public class SpaceshipController : NetworkBehaviour
|
||||
{
|
||||
Vector3 newPosition = body.position + Detour;
|
||||
Quaternion newRotation = body.rotation * RotationDetour;
|
||||
|
||||
body.position = Vector3.Lerp(body.position, newPosition, (Mathf.Abs(Detour.magnitude) > 0.2f) ? DetourCorrectionFactor * 2 * Detour.magnitude : DetourCorrectionFactor);
|
||||
if(Detour.magnitude > 0.5f){
|
||||
trailMgr.trail.emitting =false;
|
||||
}
|
||||
body.position = Vector3.Lerp(body.position, newPosition, (Mathf.Abs(Detour.magnitude) > 0.2f) ? DetourCorrectionFactor * 2 * Detour.magnitude : DetourCorrectionFactor);
|
||||
Detour = newPosition - body.position;
|
||||
body.rotation = Quaternion.Lerp(body.rotation, newRotation, DetourCorrectionFactor * ((joystick.touchDown) ? 0.1f : 1));
|
||||
RotationDetour = Quaternion.Inverse(transform.rotation) * newRotation;
|
||||
|
||||
trailMgr.trail.emitting=true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -213,6 +248,7 @@ public class SpaceshipController : NetworkBehaviour
|
||||
}
|
||||
int distanceSinceSent = (int)Vector3.Distance(newPosition, position);
|
||||
|
||||
#region trailSyncOld
|
||||
// Vector3[] newTrailPositions = new Vector3[trailPositions.Length];
|
||||
// for(int i=0; i < trailPositions.Length; i++){
|
||||
// if(i > trailPositions.Length - distanceSinceSent-1){
|
||||
@@ -229,7 +265,7 @@ public class SpaceshipController : NetworkBehaviour
|
||||
// }
|
||||
// }
|
||||
// trailMgr.trail.SetPositions(newTrailPositions);
|
||||
|
||||
#endregion
|
||||
// Vector3 newPosition = position + new Vector3(0, movingSpeed * );
|
||||
|
||||
// Vector3 newPosition = position;
|
||||
@@ -279,6 +315,69 @@ public class SpaceshipController : NetworkBehaviour
|
||||
trailMgr = GetComponent<TrailMgr>();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void TrailCollided(RaycastHit2D hit){
|
||||
if(!isServer){
|
||||
// Debug.Log("This cannot run on client, That's illegal!"); // <-- What this log says
|
||||
return;
|
||||
}
|
||||
|
||||
SpaceshipController deadPlayer = hit.collider.GetComponent<SpaceshipController>();
|
||||
if(deadPlayer!=null && !deadPlayer.dead){ // <-- okay we killed someone
|
||||
deadPlayer.Die(pname);
|
||||
Debug.Log($"{pname} killed {deadPlayer.pname}");
|
||||
|
||||
Scores+= 10; //TODO: Need to change Scores on kills?
|
||||
}
|
||||
}
|
||||
|
||||
public void Die(string killer){
|
||||
Debug.Log("You got killed by " + killer);
|
||||
|
||||
//Handle Respawning
|
||||
dead=true;
|
||||
RpcDie(killer);
|
||||
FindObjectOfType<MinigameManager>().SetRespawn(gameObject);
|
||||
}
|
||||
|
||||
[ClientRpc]
|
||||
public void RpcDie(string killer){
|
||||
Debug.Log($"{killer} killed {pname} : isItMe? -> {isLocalPlayer}");
|
||||
gameObject.SetActive(false);
|
||||
if(isLocalPlayer){
|
||||
//TODO: Death message goes here
|
||||
SceneData.holder.deadScreen.SetActive(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void Respawn(Vector3 respawnPoint){
|
||||
dead=false;
|
||||
trailMgr.trail.emitting =false;
|
||||
trailMgr.trail.Clear();
|
||||
RpcRespawn(respawnPoint);
|
||||
transform.position = respawnPoint;
|
||||
trailMgr.trail.emitting=true;
|
||||
gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
[ClientRpc]
|
||||
public void RpcRespawn(Vector3 respawnPoint){
|
||||
|
||||
GetComponent<NetworkTrail>().enableValidation=false;
|
||||
trailMgr.trail.Clear();
|
||||
trailMgr.positions = new Vector3[0];
|
||||
trailMgr.trail.emitting = false;
|
||||
transform.position = respawnPoint;
|
||||
trailMgr.trail.emitting=true;
|
||||
GetComponent<NetworkTrail>().enableValidation=true;
|
||||
gameObject.SetActive(true);
|
||||
|
||||
if(isLocalPlayer){
|
||||
SceneData.holder.deadScreen.SetActive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class TrailCollider : MonoBehaviour
|
||||
{
|
||||
public Color gizmoColor = Color.red;
|
||||
public TrailMgr trailMgr;
|
||||
public float radius;
|
||||
void Update(){
|
||||
RaycastHit2D hit = Physics2D.CircleCast(transform.position, radius, Vector2.up);
|
||||
if(hit.collider!=null){
|
||||
if(hit.transform.root == trailMgr.transform){return;} // <-- avoid eating myself
|
||||
trailMgr.OnColliderHit(hit);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDrawGizmos() {
|
||||
Gizmos.color = gizmoColor;
|
||||
Gizmos.DrawWireSphere(transform.position,radius);
|
||||
}
|
||||
}
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class TrailCollider : MonoBehaviour
|
||||
{
|
||||
public Color gizmoColor = Color.red;
|
||||
public TrailMgr trailMgr;
|
||||
public float radius;
|
||||
void Update(){
|
||||
RaycastHit2D hit = Physics2D.CircleCast(transform.position, radius, Vector2.up);
|
||||
if(hit.collider!=null){
|
||||
if(hit.transform.root == trailMgr.transform){return;} // <-- avoid eating myself
|
||||
trailMgr.OnColliderHit(hit);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDrawGizmos() {
|
||||
Gizmos.color = gizmoColor;
|
||||
Gizmos.DrawWireSphere(transform.position,radius);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,47 +1,53 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class TrailMgr : MonoBehaviour
|
||||
{
|
||||
public TrailRenderer trail;
|
||||
public Vector3[] positions;
|
||||
public Transform trailPoolParent;
|
||||
public GameObject trailColliderObj;
|
||||
public List<GameObject> trailsPool;
|
||||
|
||||
void Start(){
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update(){
|
||||
positions = new Vector3[trail.positionCount];
|
||||
int length = trail.GetPositions(positions);
|
||||
if(length > trailsPool.Count){
|
||||
//must create new trails
|
||||
int missingCount = length - trailsPool.Count;
|
||||
for(int i =0; i < missingCount; i++){
|
||||
GameObject newTrail = Instantiate(trailColliderObj, trailPoolParent);
|
||||
// Debug.Log("Spawned new trail obj " + newTrail.name);
|
||||
newTrail.GetComponent<TrailCollider>().trailMgr = this;
|
||||
trailsPool.Add(newTrail);
|
||||
}
|
||||
}
|
||||
for(int i =0; i < trailsPool.Count; i++){
|
||||
if(i < length){
|
||||
trailsPool[i].SetActive(true);
|
||||
trailsPool[i].transform.position = positions[i];
|
||||
}else{
|
||||
trailsPool[i].SetActive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void OnColliderHit(RaycastHit2D hit){
|
||||
return;
|
||||
Debug.Log($"{hit.collider.name} got hit by my trail");
|
||||
Destroy(hit.transform.gameObject);
|
||||
}
|
||||
}
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class TrailMgr : MonoBehaviour
|
||||
{
|
||||
public SpaceshipController controller;
|
||||
public TrailRenderer trail;
|
||||
|
||||
public Vector3[] positions;
|
||||
public Transform trailPoolParent;
|
||||
public GameObject trailColliderObj;
|
||||
public List<GameObject> trailsPool;
|
||||
|
||||
void Start(){
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update(){
|
||||
positions = new Vector3[trail.positionCount];
|
||||
int length = trail.GetPositions(positions);
|
||||
if(length > trailsPool.Count){
|
||||
//must create new trails
|
||||
int missingCount = length - trailsPool.Count;
|
||||
for(int i =0; i < missingCount; i++){
|
||||
GameObject newTrail = Instantiate(trailColliderObj, trailPoolParent);
|
||||
// Debug.Log("Spawned new trail obj " + newTrail.name);
|
||||
newTrail.GetComponent<TrailCollider>().trailMgr = this;
|
||||
trailsPool.Add(newTrail);
|
||||
}
|
||||
}
|
||||
for(int i =0; i < trailsPool.Count; i++){
|
||||
if(i < length){
|
||||
trailsPool[i].SetActive(true);
|
||||
trailsPool[i].transform.position = positions[i];
|
||||
}else{
|
||||
trailsPool[i].SetActive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void OnColliderHit(RaycastHit2D hit){
|
||||
controller.TrailCollided(hit);
|
||||
}
|
||||
|
||||
void OnValidate(){
|
||||
if(controller==null){
|
||||
controller = GetComponent<SpaceshipController>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user