179 lines
4.3 KiB
C#
179 lines
4.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Mirror;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
|
|
public class PlayerController : NetworkBehaviour
|
|
{
|
|
public PredictedRigidbody2D rb;
|
|
[SyncVar]
|
|
public float yVelocity;
|
|
|
|
[SyncVar(hook =nameof(OnPlayerTypeChanged))]
|
|
public string playerTypeString;
|
|
public float gravity= -9f;
|
|
public float movingSpeed = 1f;
|
|
public float jumpForce = 1f;
|
|
public float jumpInterval = 0.1f;
|
|
float jumpTimer =0;
|
|
public Transform rotateVisual;
|
|
public float rotateIntensity = 10f;
|
|
|
|
public bool queueJump = false;
|
|
|
|
|
|
public PlayerType playerType;
|
|
public GameObject masterVisual;
|
|
public GameObject clientVisual;
|
|
public static PlayerController localPlayer;
|
|
void OnValidate()
|
|
{
|
|
if(rb == null){
|
|
rb = GetComponent<PredictedRigidbody2D>();
|
|
}
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
if(isLocalPlayer){
|
|
SetPlayerType(GameData.isMaster ? PlayerType.Master : PlayerType.Client);
|
|
localPlayer = this;
|
|
}
|
|
}
|
|
|
|
#region SetPlayerType
|
|
void SetPlayerType(PlayerType type){
|
|
if(isServer){
|
|
setPlayerType(type);
|
|
RpcSetPlayerType(type);
|
|
}else{
|
|
CmdSetPlayerType(type);
|
|
}
|
|
}
|
|
|
|
[ClientRpc]
|
|
void RpcSetPlayerType(PlayerType type){
|
|
setPlayerType(type);
|
|
}
|
|
|
|
[Command]
|
|
void CmdSetPlayerType(PlayerType type){
|
|
playerTypeString = type.ToString();
|
|
setPlayerType(type);
|
|
RpcSetPlayerType(type);
|
|
}
|
|
|
|
void setPlayerType(PlayerType type){
|
|
playerType=type;
|
|
if(type == PlayerType.Master){
|
|
masterVisual.SetActive(true);
|
|
clientVisual.SetActive(false);
|
|
}else{
|
|
masterVisual.SetActive(false);
|
|
clientVisual.SetActive(true);
|
|
}
|
|
}
|
|
|
|
|
|
void OnPlayerTypeChanged(string oldVal, string newVal){
|
|
playerType = (PlayerType)System.Enum.Parse(typeof(PlayerType), newVal);
|
|
setPlayerType(playerType);
|
|
}
|
|
|
|
|
|
#endregion
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
if(!NetGameManager.instance.playerSpawned){
|
|
rb.predictedRigidbody.constraints = RigidbodyConstraints2D.FreezeAll;
|
|
return;
|
|
}else{
|
|
rb.predictedRigidbody.constraints = RigidbodyConstraints2D.None;
|
|
}
|
|
|
|
HandleRotation();
|
|
|
|
if(isLocalPlayer){
|
|
HandleKeyboardInput();
|
|
HandleMouseInput();
|
|
}
|
|
|
|
if(isServer || isLocalPlayer){
|
|
HandleJump();
|
|
HandleMovement();
|
|
}
|
|
}
|
|
|
|
#region input
|
|
void HandleKeyboardInput(){
|
|
if(Input.GetKeyDown(KeyCode.Space)){
|
|
OnJumpClicked();
|
|
}
|
|
}
|
|
|
|
void HandleMouseInput(){
|
|
if(Input.GetMouseButtonDown(0)){
|
|
OnJumpClicked();
|
|
}
|
|
}
|
|
|
|
public void OnJumpClicked(){
|
|
if(isServer){
|
|
queueJump = true;
|
|
}else{
|
|
CmdOnJumpClicked();
|
|
queueJump=true;
|
|
}
|
|
}
|
|
|
|
[Command]
|
|
void CmdOnJumpClicked(){
|
|
queueJump = true;
|
|
}
|
|
#endregion
|
|
|
|
#region Movement
|
|
void HandleMovement(){
|
|
// yVelocity += gravity * Time.deltaTime;
|
|
// transform.position += new Vector3(0, yVelocity * Time.deltaTime, 0);
|
|
}
|
|
|
|
void HandleRotation(){
|
|
rotateVisual.rotation = Quaternion.Euler(0, 0, rb.predictedRigidbody.velocity.y * rotateIntensity);
|
|
}
|
|
|
|
void HandleJump(){
|
|
if(jumpTimer < jumpInterval){
|
|
jumpTimer += Time.deltaTime;
|
|
return;
|
|
}
|
|
if(queueJump){
|
|
if(jumpTimer >= jumpInterval){
|
|
jumpTimer = 0;
|
|
queueJump = false;
|
|
Jump();
|
|
}
|
|
}
|
|
}
|
|
|
|
void Jump(){
|
|
// yVelocity = jumpForce;
|
|
Debug.Log("Jumped at " + NetworkTime.time);
|
|
rb.predictedRigidbody.velocity = new Vector2(rb.predictedRigidbody.velocity.x, jumpForce);
|
|
}
|
|
#endregion
|
|
|
|
void OnTriggerEnter2D(Collider2D other)
|
|
{
|
|
if(!isServer){
|
|
return;
|
|
}
|
|
if(other.gameObject.CompareTag("Pipe")){
|
|
Debug.Log("Collided with pipe");
|
|
NetGameManager.instance.GameOver(playerType == PlayerType.Master ? PlayerType.Client : PlayerType.Master);
|
|
}
|
|
}
|
|
}
|