Gonna move to server authority

This commit is contained in:
2022-01-31 17:27:38 +05:30
parent f3d21f4ec6
commit 7368968176
1354 changed files with 107808 additions and 80043 deletions

View File

@@ -11,11 +11,12 @@ public class NetPlayer : NetworkBehaviour
[SyncVar]
public bool insideDoor;
public LayerMask friendLayer;
public List<NetPlayer> touchingNeighbours = new List<NetPlayer>();
void Start()
{
DontDestroyOnLoad(gameObject);
if(!isLocalPlayer){
gameObject.layer = LayerMask.NameToLayer("Friend");
gameObject.layer = LayerMask.NameToLayer("Gnd");
//GetComponent<BoxCollider2D>().size = new Vector2(GetComponent<BoxCollider2D>().size.x/2f,GetComponent<BoxCollider2D>().size.y);
foreach(Behaviour localComponent in LocalComponents){
localComponent.enabled=false;
@@ -45,28 +46,47 @@ public class NetPlayer : NetworkBehaviour
transform.position = SceneData.netSceneData.spawnPoint.position;
}
public Transform frndTrans;
[SyncVar]
public Transform parentFrnd;
bool oldFlipVal = false;
Transform oldFriendVal;
float oldFriendX;
void Update()
{
if(!isLocalPlayer){return;}
frndTrans = getOnFriend();
// if(oldFriendVal!=frndTrans){//got on someones head, or got off
// if(oldFriendVal==null && frndTrans!=null){//got on
// oldFriendX = frndTrans.position.x;
// oldFriendVal = frndTrans;
// }else{//got off
// oldFriendX = 0;
// oldFriendVal = null;
// }
// }
Transform _parentFrnd;
// if(oldFriendVal!=null){
// transform.Translate(new Vector2(frndTrans.position.x - oldFriendX,0));
// }
[Command]
void CmdChangeParent(Transform newParent){
transform.parent = newParent;
RpcChangeParent(newParent);
}
[ClientRpc]
void RpcChangeParent(Transform newParent){
transform.parent = newParent;
}
float t=0;
void FixedUpdate()
{
if(transform.parent!=null){
if(t <1){
t+=Time.deltaTime;
}else{
GetComponent<NetworkTransform>().useLocalSpace=true;
}
}else{
GetComponent<NetworkTransform>().useLocalSpace=false;
t=0;
}
if(!isLocalPlayer){return;}
parentFrnd = getOnFriend();
transform.parent = parentFrnd;
if(_parentFrnd != parentFrnd){
if(isServer){
transform.parent = parentFrnd;
RpcChangeParent(parentFrnd);
}else{
CmdChangeParent(parentFrnd);
}
_parentFrnd=parentFrnd;
}
if(oldFlipVal != characterSprite.flipX){
if(isServer){
@@ -76,6 +96,15 @@ public Transform frndTrans;
}
oldFlipVal=characterSprite.flipX;
}
// bool someoneOnTop = false;
// foreach(NetPlayer neighbour in touchingNeighbours){
// if(neighbour.parentFrnd == this){
// someoneOnTop=true;
// break;
// }
// }
// GetComponent<PlayerController>().isSomeoneOnTop =someoneOnTop;
@@ -118,7 +147,30 @@ public Transform frndTrans;
//return (Physics2D.Linecast(transform.position, groundChecker.position, groundLayerMask));
Collider2D col = GetComponentInChildren<Collider2D>();
RaycastHit2D hit = Physics2D.BoxCast(col.bounds.center, new Vector2(col.bounds.size.x - (col.bounds.size.x / 5f), col.bounds.size.y), 0, Vector2.down, 0.1f, friendLayer);
friend = (hit) ? hit.collider.transform : null;
friend = (hit) ? ((hit.collider.transform.GetComponent<NetPlayer>()!=null) ? hit.collider.transform : null) : null;
return friend;
}
void OnCollisionEnter2D(Collision2D col){
NetPlayer obj = col.collider.transform.GetComponent<NetPlayer>();
if(obj!=null){
if(!touchingNeighbours.Contains(obj)){
touchingNeighbours.Add(obj);
}
}
}
void OnCollisionExit2D(Collision2D col){
NetPlayer obj = col.collider.transform.GetComponent<NetPlayer>();
if(obj!=null){
if(touchingNeighbours.Contains(obj)){
touchingNeighbours.Remove(obj);
}
}
}
void UpdatePushBoxes(){
foreach(PushBox box in FindObjectsOfType<PushBox>()){
box.UpdateNeighbourCount();
}
}
}

View File

@@ -40,7 +40,7 @@ public class PlayerController : NetworkBehaviour
void FixedUpdate()
{
isGrounded = getGrounded();
if (_grounded != isGrounded)
{
if (isGrounded)
@@ -76,19 +76,21 @@ public class PlayerController : NetworkBehaviour
float HorizontalAxis = 0;
if (Input.GetKey(InputManager.data().leftInput)) { HorizontalAxis = -1; } else if (Input.GetKey(InputManager.data().rightInput)) { HorizontalAxis = 1; }
// if (GameManager.isPaused) { HorizontalAxis = 0; }
// if (GameManager.isPaused) { HorizontalAxis = 0; }
//Move according to input
//Exit the door
if(enteringDoor && !Input.GetKey(InputManager.data().interactingKey)){
enteringDoor=false;
if (enteringDoor && !Input.GetKey(InputManager.data().interactingKey))
{
enteringDoor = false;
}
if(insideDoor && !enteringDoor && Input.GetKey(InputManager.data().interactingKey)){
if (insideDoor && !enteringDoor && Input.GetKey(InputManager.data().interactingKey))
{
Debug.Log("Exiting door");
transform.position = SceneData.netSceneData.door.transform.position;
insideDoor=false;
enteringDoor=true;
if(GetComponent<NetPlayer>()!=null){GetComponent<NetPlayer>().CallChangeInsideDoor(insideDoor);}
insideDoor = false;
enteringDoor = true;
if (GetComponent<NetPlayer>() != null) { GetComponent<NetPlayer>().CallChangeInsideDoor(insideDoor); }
}
if (listenToInput)
{
@@ -106,13 +108,15 @@ public class PlayerController : NetworkBehaviour
}
//Enter the door
if(inDoor && Input.GetKey(InputManager.data().interactingKey) && !enteringDoor){
if(SceneData.netSceneData.doorExit!=null){
if (inDoor && Input.GetKey(InputManager.data().interactingKey) && !enteringDoor)
{
if (SceneData.netSceneData.doorExit != null)
{
Debug.Log("Entering door");
transform.position = SceneData.netSceneData.doorExit.position;
insideDoor = true;
enteringDoor=true;
if(GetComponent<NetPlayer>()!=null){GetComponent<NetPlayer>().CallChangeInsideDoor(insideDoor);}
enteringDoor = true;
if (GetComponent<NetPlayer>() != null) { GetComponent<NetPlayer>().CallChangeInsideDoor(insideDoor); }
}
}
@@ -136,37 +140,39 @@ public class PlayerController : NetworkBehaviour
//Apply moving input to player
if(GetComponent<NetPlayer>()!=null){
if(GetComponent<NetPlayer>().frndTrans!=null){
rigidbody.velocity = GetComponent<NetPlayer>().frndTrans.GetComponent<Rigidbody2D>().velocity;
}
}
// if (GetComponent<NetPlayer>() != null)
// {
// if (GetComponent<NetPlayer>().parentFrnd != null)
// {
// rigidbody.velocity = GetComponent<NetPlayer>().parentFrnd.GetComponent<Rigidbody2D>().velocity;
// }
// }
rigidbody.transform.Translate(new Vector2(moveSpeed * moveInput, 0));
bool _canJump = canJump();
if ((Input.GetKey(InputManager.data().jumpInput)) && _canJump)
{
jumpT = 0;
jumpReleased = false;
rigidbody.velocity = new Vector2(rigidbody.velocity.x, 0);
if(jumpSFX!=null)AudioSingleton.getSFXSource().PlayOneShot(jumpSFX);
}
bool _canJump = canJump();
if ((Input.GetKey(InputManager.data().jumpInput)) && _canJump)
{
jumpT = 0;
jumpReleased = false;
rigidbody.velocity = new Vector2(rigidbody.velocity.x, 0);
if (jumpSFX != null) AudioSingleton.getSFXSource().PlayOneShot(jumpSFX);
}
//Blocks continous jump button
if (!Input.GetKey(InputManager.data().jumpInput)) { jumpReleased = true; }
//Blocks continous jump button
if (!Input.GetKey(InputManager.data().jumpInput)) { jumpReleased = true; }
//Apply Jump to player
if (jumpT < jumpDuration)
{
jumpT += Time.deltaTime;
float progress = (jumpDuration - jumpT) / jumpDuration;
//|| jumpT < jumpDuration/2f
if ((Input.GetKey(InputManager.data().jumpInput)) || b) { rigidbody.AddForce(new Vector2(0, jumpForce * progress)); }
}
else { b = false; }
//Apply Jump to player
if (jumpT < jumpDuration)
{
jumpT += Time.deltaTime;
float progress = (jumpDuration - jumpT) / jumpDuration;
//|| jumpT < jumpDuration/2f
if ((Input.GetKey(InputManager.data().jumpInput)) || b) { rigidbody.AddForce(new Vector2(0, jumpForce * progress)); }
}
else { b = false; }
_isSwimming = inWater;
}
@@ -176,7 +182,7 @@ public class PlayerController : NetworkBehaviour
public bool waterBoost;
public bool canJump()
{
return jumpT >= jumpDuration && isGrounded && listenToInput && jumpReleased && !isSwimming;//&& !GameManager.isPaused
return jumpT >= jumpDuration && isGrounded && listenToInput && jumpReleased && !isSwimming && transform.childCount <= 0;//&& !GameManager.isPaused
}
public bool canBotJump()
@@ -209,15 +215,22 @@ public class PlayerController : NetworkBehaviour
{
// Debug.Log("Hey look im flying");
}
public float groundCheckerDist = 0.1f;
public float groundCheckerHeighMultipler = 1f;
public bool getGrounded()
{
//return (Physics2D.Linecast(transform.position, groundChecker.position, groundLayerMask));
Collider2D col = GetComponentInChildren<Collider2D>();
return (Physics2D.BoxCast(col.bounds.center, new Vector2(col.bounds.size.x - (col.bounds.size.x / 5f), col.bounds.size.y), 0, Vector2.down, 0.1f, groundLayerMask));
return (Physics2D.BoxCast(col.bounds.center, new Vector2(col.bounds.size.x - (col.bounds.size.x / 5f), col.bounds.size.y * groundCheckerHeighMultipler), 0, Vector2.down, groundCheckerDist, groundLayerMask));
}
void OnDrawGizmos()
{
Collider2D col = GetComponentInChildren<Collider2D>();
Gizmos.color = Color.red;
Gizmos.DrawWireCube(col.bounds.center - new Vector3(0, groundCheckerDist), new Vector2(col.bounds.size.x - (col.bounds.size.x / 5f), col.bounds.size.y * groundCheckerHeighMultipler));
}
// public bool getInWater(){Collider2D col = GetComponentInChildren<Collider2D>();
// return (Physics2D.BoxCast(col.bounds.center, new Vector2(col.bounds.size.x - (col.bounds.size.x / 5f), col.bounds.size.y), 0, Vector2.down, 0.1f, waterLayerMask));
// }
@@ -231,14 +244,17 @@ public class PlayerController : NetworkBehaviour
// }
public static class AudioSingleton{
public static class AudioSingleton
{
private static AudioSource music;
private static AudioSource sfx;
public static AudioMixer mixer = Resources.Load("MasterMixer") as AudioMixer;
public static AudioSource getMusicSource(){
if(music == null){
public static AudioSource getMusicSource()
{
if (music == null)
{
GameObject go = new GameObject("Music Audio Source");
go.AddComponent<AudioSource>();
music = go.GetComponent<AudioSource>();
@@ -247,13 +263,15 @@ public static class AudioSingleton{
return music;
}
public static AudioSource getSFXSource(){
public static AudioSource getSFXSource()
{
//AudioMixer mixer = Resources.Load("MasterMixer") as AudioMixer;
if(sfx == null){
if (sfx == null)
{
GameObject go = new GameObject("SFX Audio Source");
go.AddComponent<AudioSource>();
sfx= go.GetComponent<AudioSource>();
Debug.Log("sfx : " + (sfx==null).ToString() + ", mixer: " + (mixer == null).ToString() );
sfx = go.GetComponent<AudioSource>();
Debug.Log("sfx : " + (sfx == null).ToString() + ", mixer: " + (mixer == null).ToString());
sfx.outputAudioMixerGroup = mixer.FindMatchingGroups("SFX")[0];
}

85
Assets/Scripts/PushBox.cs Normal file
View File

@@ -0,0 +1,85 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Mirror;
public class PushBox : NetworkBehaviour
{
public int playersRequired;
public Text numberTxt;
public List<NetPlayer> DTP;
public List<NetPlayer> Neighbours;
public List<NetPlayer> targets;
public List<NetPlayer> scannedList;
void Start()
{
UpdateText();
}
void OnCollisionEnter2D(Collision2D col)
{
NetPlayer player = col.collider.GetComponent<NetPlayer>();
if (player != null)
{
if (!DTP.Contains(player))
{
DTP.Add(player);
UpdateNeighbourCount();
}
}
}
void OnCollisionExit2D(Collision2D col)
{
NetPlayer player = col.collider.GetComponent<NetPlayer>();
if (player != null)
{
if (DTP.Contains(player))
{
DTP.Remove(player);
UpdateNeighbourCount();
}
}
}
public void UpdateNeighbourCount()
{
targets = new List<NetPlayer>();
Neighbours = new List<NetPlayer>();
scannedList= new List<NetPlayer>();
targets.AddRange(DTP);
int failCount = 0;
while(targets.Count > 0 && failCount < 50){
failCount++;
Neighbours.Add(targets[0]);
scannedList.Add(targets[0]);
foreach(NetPlayer neighbour in targets[0].touchingNeighbours){
if(!scannedList.Contains(neighbour)){
targets.Add(neighbour);
}
}
scannedList.Add(targets[0]);
targets.RemoveAt(0);
}
if(failCount >= 50){
Debug.LogError("Fail switch triggered");
}
GetComponent<Rigidbody2D>().simulated=((playersRequired - Neighbours.Count) > 0);
UpdateText();
}
void UpdateText()
{
numberTxt.text = (playersRequired - Neighbours.Count).ToString();
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 95601679d599a9fd8a2fe7721e81b132
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: