Files
soccar2d/Assets/Scripts/NetPlayer.cs
T

220 lines
5.9 KiB
C#

using UnityEngine;
using Mirror;
using UnityEngine.EventSystems;
public class NetPlayer : NetworkBehaviour
{
[SyncVar]
public Team myTeam;
// set the team to red if there are no other players, if not set blue
public override void OnStartLocalPlayer()
{
base.OnStartLocalPlayer();
#if UNITY_EDITOR
// Count current NetPlayers in the scene
if(Cupid.RoomPort == -1){
//Without cupid, set the team to red
bool cloneWorkspace = LoginManager.EditorWorkspaceLooksLikeClone();
SetMyTeam(cloneWorkspace ? Team.Red : Team.Blue);
}else{
SetMyTeam(GameManager.MyTeam);
}
#else
SetMyTeam(GameManager.MyTeam);
#endif
TeamSpecificEffects.instance.SetTeamSpecificEffects(myTeam);
}
void SetMyTeam(Team team){
if(isServer){
setMyTeam(team);
}else{
CmdSetTeam(team);
setMyTeam(team);
}
}
void setMyTeam(Team team){
myTeam = team;
if (GameManager.instance != null)
GameManager.instance.OnMatchPlayerTeamAssigned(team);
}
[Command]
void CmdSetTeam(Team team){
setMyTeam(team);
Logger.Log($"Client {netId} set team to {team}");
}
public static NetPlayer localPlayer;
public static Vector2 curPosition;
public Vector2 startPosition;
public float curPuckPullForce;
#region EMOTES
public void SendTextEmote(string txtName){
if(isServer){
RpcSendTextEmote(txtName);
ShowTextEmote(txtName);
}else{
CmdSendTextEmote(txtName);
}
}
[Command]
void CmdSendTextEmote(string txtName){
RpcSendTextEmote(txtName);
ShowTextEmote(txtName);
Logger.Log($"Client {netId} sent text emote {txtName}");
}
[ClientRpc]
void RpcSendTextEmote(string txtName){
ShowTextEmote(txtName);
}
void ShowTextEmote(string txtName){
GameCanvas.instance.ShowTextEmote(txtName, myTeam);
}
public void SendEmojiEmote(int id){
if(isServer){
RpcSendEmojiEmote(id);
ShowEmojiEmote(id);
}else{
CmdSendEmojiEmote(id);
}
}
[Command]
void CmdSendEmojiEmote(int id){
RpcSendEmojiEmote(id);
ShowEmojiEmote(id);
Logger.Log($"Client {netId} sent emoji emote {id}");
}
[ClientRpc]
void RpcSendEmojiEmote(int id){
ShowEmojiEmote(id);
}
void ShowEmojiEmote(int id){
GameCanvas.instance.ShowEmojiEmote(id, myTeam);
}
#endregion
void Start(){
if(isLocalPlayer){
localPlayer = this;
SetupInputPanel();
}
}
void OnDestroy(){
if(localPlayer == this){
localPlayer = null;
}
}
void SetupInputPanel(){
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerDown;
entry.callback.AddListener(OnPointerDown);
GameCanvas.instance.gameInputPanel.triggers.Add(entry);
entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerUp;
entry.callback.AddListener(OnPointerUp);
GameCanvas.instance.gameInputPanel.triggers.Add(entry);
entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.Drag;
entry.callback.AddListener(OnDrag);
GameCanvas.instance.gameInputPanel.triggers.Add(entry);
}
void OnPointerDown(BaseEventData eventData){
if(GameManager.instance.SelectedTeam != myTeam){return;}
if(GameManager.instance.m_isMoving){return;}
PointerEventData pointerEventData = eventData as PointerEventData;
Vector2 position = pointerEventData.position;
Vector3 worldPosition = Camera.main.ScreenToWorldPoint(new Vector3(position.x, position.y, Camera.main.nearClipPlane));
curPosition= position;
Puck closestPuck = GameManager.instance.GetClosestPuck(worldPosition);
GameManager.SelectedPuck = closestPuck;
//Temporarily set the puck until server validates
CmdOnPointerDown(worldPosition);
}
[Command]
void CmdOnPointerDown(Vector2 position){
if(GameManager.instance.SelectedTeam != myTeam){return;}
Puck closestPuck = GameManager.instance.GetClosestPuck(position);
GameManager.SelectedPuck = closestPuck;
RpcOnPointerDown(closestPuck.gameObject);
}
[ClientRpc]
void RpcOnPointerDown(GameObject puckGo){
if(puckGo == null){return;}
if(!isLocalPlayer){return;}
Puck puck = puckGo.GetComponent<Puck>();
if(puck!=GameManager.SelectedPuck){
Debug.LogError("Selected puck changed on client, This cant happen. Resetting to servers choice", gameObject);
}
GameManager.SelectedPuck = puck;
}
void OnDrag(BaseEventData eventData){
PointerEventData pointerEventData = eventData as PointerEventData;
curPosition = pointerEventData.position;
if(GameManager.SelectedPuck!=null){
curPuckPullForce = GameManager.SelectedPuck.lineEnd.magnitude;
}
}
void OnPointerUp(BaseEventData eventData){
if(GameManager.SelectedPuck==null){return;}
if(GameManager.instance.SelectedTeam != myTeam){return;}
PointerEventData pointerEventData = eventData as PointerEventData;
Vector2 position = pointerEventData.position;
Vector2 direction = GameManager.SelectedPuck.lineEnd;
CmdOnPointerUp(direction);
CameraEffects.instance.ReleaseStretchFactor();
}
[Command]
void CmdOnPointerUp(Vector2 direction){
if(GameManager.instance.SelectedTeam != myTeam){return;}
Debug.Log("Pointer up");
GameManager.instance.OnPointerUp(direction);
RpcOnPointerUp();
}
[ClientRpc]
void RpcOnPointerUp(){
}
}