mp fundementals and turn timer
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
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();
|
||||
// Count current NetPlayers in the scene
|
||||
NetPlayer[] players = FindObjectsOfType<NetPlayer>();
|
||||
if (players.Length <= 1)
|
||||
{
|
||||
myTeam = Team.Red;
|
||||
CmdSetTeam(Team.Red);
|
||||
}
|
||||
else
|
||||
{
|
||||
myTeam = Team.Blue;
|
||||
CmdSetTeam(Team.Blue);
|
||||
}
|
||||
}
|
||||
|
||||
[Command]
|
||||
void CmdSetTeam(Team team){
|
||||
myTeam = team;
|
||||
}
|
||||
|
||||
|
||||
public static NetPlayer localPlayer;
|
||||
public static Vector2 curPosition;
|
||||
public Vector2 startPosition;
|
||||
public float curPuckPullForce;
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
[Command]
|
||||
void CmdOnPointerUp(Vector2 direction){
|
||||
if(GameManager.instance.SelectedTeam != myTeam){return;}
|
||||
|
||||
Debug.Log("Pointer up");
|
||||
|
||||
GameManager.instance.OnPointerUp(direction);
|
||||
|
||||
RpcOnPointerUp();
|
||||
}
|
||||
|
||||
[ClientRpc]
|
||||
void RpcOnPointerUp(){
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user