Skip to content

Commit

Permalink
Select the hosts card game when joining
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmfinol committed Feb 25, 2018
1 parent 491182b commit 9b5cb58
Show file tree
Hide file tree
Showing 17 changed files with 94 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ GameObject:
- component: {fileID: 114797580129434228}
- component: {fileID: 114416027808059046}
m_Layer: 0
m_Name: NetPlayer
m_Name: CGSNetPlayer
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
Expand Down Expand Up @@ -52,6 +52,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 6a88bd459dba0be449622f9c07ed8e64, type: 3}
m_Name:
m_EditorClassIdentifier:
Points: 0
--- !u!114 &114797580129434228
MonoBehaviour:
m_ObjectHideFlags: 1
Expand Down
6 changes: 3 additions & 3 deletions Assets/Scenes/DeckEditor.unity
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: -0.000045558307, y: 0}
m_AnchoredPosition: {x: 0.00005602201, y: 0}
m_SizeDelta: {x: 0, y: 50}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &179835150
Expand Down Expand Up @@ -722,7 +722,7 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 0.000046195815, y: 0}
m_AnchoredPosition: {x: -0.000012394135, y: 0}
m_SizeDelta: {x: 0, y: 50}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &196520020
Expand Down Expand Up @@ -2598,7 +2598,7 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: -0.000060282364, y: 0}
m_AnchoredPosition: {x: -0.000031949905, y: 0}
m_SizeDelta: {x: 0, y: 50}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &836546758
Expand Down
8 changes: 4 additions & 4 deletions Assets/Scenes/PlayMode.unity
Original file line number Diff line number Diff line change
Expand Up @@ -2906,7 +2906,7 @@ MonoBehaviour:
m_OnlineScene:
m_SpawnPrefabs: []
m_CustomConfig: 1
m_MaxConnections: 4
m_MaxConnections: 5
m_ConnectionConfig:
m_PacketSize: 1440
m_FragmentSize: 500
Expand Down Expand Up @@ -3019,8 +3019,8 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 100}
m_AnchoredPosition: {x: 5, y: 0}
m_SizeDelta: {x: -10, y: 100}
m_Pivot: {x: 0.5, y: 0}
--- !u!114 &1284800461
MonoBehaviour:
Expand Down Expand Up @@ -3054,7 +3054,7 @@ MonoBehaviour:
m_HorizontalOverflow: 0
m_VerticalOverflow: 0
m_LineSpacing: 1
m_Text:
m_Text: <multiplayer-data>
--- !u!222 &1284800462
CanvasRenderer:
m_ObjectHideFlags: 0
Expand Down
31 changes: 14 additions & 17 deletions Assets/Scripts/CardGameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class CardGameManager : MonoBehaviour
public const int PixelsPerInch = 100;

public static string GamesFilePathBase => Application.persistentDataPath + "/games";
public static string CurrentGameName { get; private set; } = Set.DefaultCode;
public static CardGame Current { get; private set; } = new CardGame();

public static bool IsMultiplayer { get; set; }
public static bool IsQuitting { get; private set; }
Expand Down Expand Up @@ -59,7 +59,8 @@ void Awake()
string gameName = gameDirectory.Substring(GamesFilePathBase.Length + 1);
AllCardGames [gameName] = new CardGame(gameName, string.Empty);
}
CurrentGameName = PlayerPrefs.GetString(PlayerPrefGameName, FirstGameName);
CardGame currentGame;
Current = AllCardGames.TryGetValue(PlayerPrefs.GetString(PlayerPrefGameName, FirstGameName), out currentGame) ? currentGame : new CardGame();

Application.logMessageReceived += HandleLog;
SceneManager.sceneLoaded += OnSceneLoaded;
Expand All @@ -85,19 +86,20 @@ public IEnumerator DownloadCardGame(string gameUrl)
{
//Messenger.Show("Game download has started");
CardGame newGame = new CardGame(Set.DefaultCode, gameUrl) {AutoUpdate = true};
Current = newGame;
yield return newGame.Download();
if (string.IsNullOrEmpty(newGame.Error)) {
AllCardGames [newGame.Name] = newGame;
SelectCardGame(newGame.Name);
} else
if (string.IsNullOrEmpty(newGame.Error))
AllCardGames[newGame.Name] = newGame;
else
Debug.LogError(GameLoadErrorMessage + newGame.Error);
SelectCardGame(newGame.Name);
//Messenger.Show("Game download has finished");
}

public void SelectCardGame(string gameName, string gameUrl)
{
if (string.IsNullOrEmpty(gameName) || !AllCardGames.ContainsKey(gameName)) {
DownloadCardGame(gameUrl);
StartCoroutine(DownloadCardGame(gameUrl));
return;
}
SelectCardGame(gameName);
Expand All @@ -107,12 +109,13 @@ public void SelectCardGame(string gameName)
{
if (string.IsNullOrEmpty(gameName) || !AllCardGames.ContainsKey(gameName)) {
Debug.LogError(InvalidGameSelectionMessage);
CurrentGameName = Set.DefaultCode;
Selector.Show();
return;
}

CurrentGameName = gameName;
CardGame currentGame;
Current = AllCardGames.TryGetValue(gameName, out currentGame) ? currentGame : new CardGame();

DoGameSceneActions();
}

Expand All @@ -133,7 +136,7 @@ public void DoGameSceneActions()
if (!string.IsNullOrEmpty(Current.Error))
Debug.LogError(GameLoadErrorMessage + Current.Error);
else
PlayerPrefs.SetString(PlayerPrefGameName, CurrentGameName);
PlayerPrefs.SetString(PlayerPrefGameName, Current.Name);

if (BackgroundImage != null)
BackgroundImage.sprite = Current.BackgroundImageSprite;
Expand All @@ -150,7 +153,7 @@ public void DeleteGame()
{
try {
Directory.Delete(Current.FilePathBase, true);
AllCardGames.Remove(CurrentGameName);
AllCardGames.Remove(Current.Name);
SelectCardGame(AllCardGames.Keys.First());
Selector.Show();
} catch (Exception ex) {
Expand Down Expand Up @@ -182,12 +185,6 @@ public static CardGameManager Instance {
}
}

public static CardGame Current {
get { CardGame currentGame;
return Instance.AllCardGames.TryGetValue(CurrentGameName, out currentGame) ? currentGame : new CardGame();
}
}

public GameSelectionMenu Selector {
get {
if (_selector != null) return _selector;
Expand Down
20 changes: 13 additions & 7 deletions Assets/Scripts/CardModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public class CardModel : NetworkBehaviour, IPointerDownHandler, IPointerUpHandle
public static readonly Vector2 OutlineHighlightDistance = new Vector2(10, 10);

public bool IsOnline => NetworkManager.singleton != null && NetworkManager.singleton.isNetworkActive
&& transform.parent == ((LocalNetManager)NetworkManager.singleton).playController.playAreaContent;
&& transform.parent == ((CGSNetManager)NetworkManager.singleton).playController.playAreaContent;
public bool IsProcessingSecondaryDragAction => PointerPositions.Count > 1
|| (CurrentPointerEventData != null && CurrentPointerEventData.button == PointerEventData.InputButton.Right);
|| (CurrentPointerEventData != null && CurrentPointerEventData.button == PointerEventData.InputButton.Right && CurrentPointerEventData.button == PointerEventData.InputButton.Middle);
public CardStack ParentCardStack => transform.parent.GetComponent<CardStack>();

public bool DoesCloneOnDrag { get; set; }
Expand Down Expand Up @@ -78,7 +78,8 @@ void Start()

public void OnPointerDown(PointerEventData eventData)
{
DidSelectOnDown = eventData.button != PointerEventData.InputButton.Right && CardInfoViewer.Instance.SelectedCardModel != this && CardInfoViewer.Instance.WasVisible;
DidSelectOnDown = eventData.button != PointerEventData.InputButton.Right && eventData.button != PointerEventData.InputButton.Middle
&& CardInfoViewer.Instance.SelectedCardModel != this && CardInfoViewer.Instance.WasVisible;
if (DidSelectOnDown)
EventSystem.current.SetSelectedGameObject(gameObject, eventData);

Expand All @@ -90,7 +91,9 @@ public void OnPointerDown(PointerEventData eventData)

public void OnPointerUp(PointerEventData eventData)
{
if (CurrentPointerEventData != null && CurrentPointerEventData.pointerId == eventData.pointerId && eventData.button != PointerEventData.InputButton.Right && !eventData.dragging && !DraggedClones.ContainsKey(eventData.pointerId)) {
if (CurrentPointerEventData != null && CurrentPointerEventData.pointerId == eventData.pointerId && !eventData.dragging
&& eventData.button != PointerEventData.InputButton.Right && eventData.button != PointerEventData.InputButton.Middle
&& !DraggedClones.ContainsKey(eventData.pointerId)) {
if (!DidSelectOnDown && EventSystem.current.currentSelectedGameObject == gameObject && DoubleClickAction != null)
DoubleClickAction(this);
else if (PlaceHolder == null)
Expand Down Expand Up @@ -180,7 +183,10 @@ public void OnEndDrag(PointerEventData eventData)
if (cardModel.SecondaryDragAction != null && cardModel.IsProcessingSecondaryDragAction)
cardModel.SecondaryDragAction();

Vector2 removedOffset = (Vector2)cardModel.transform.position - eventData.position - cardModel.PointerDragOffsets[eventData.pointerId];
Vector2 removedOffset = Vector2.zero;
Vector2 pointerDragOffset;
if (cardModel.PointerDragOffsets.TryGetValue(eventData.pointerId, out pointerDragOffset))
removedOffset = (Vector2)cardModel.transform.position - eventData.position - pointerDragOffset;
cardModel.PointerPositions.Remove(eventData.pointerId);
cardModel.PointerDragOffsets.Remove(eventData.pointerId);
Vector2 otherOffset;
Expand Down Expand Up @@ -215,7 +221,7 @@ public static CardModel GetPointerDrag(PointerEventData eventData)
public void UpdatePosition()
{
#if (!UNITY_ANDROID && !UNITY_IOS) || UNITY_EDITOR
if (Input.GetMouseButton(1) || Input.GetMouseButtonUp(1))
if (Input.GetMouseButton(1) || Input.GetMouseButtonUp(1) || Input.GetMouseButton(2) || Input.GetMouseButtonUp(2))
return;
#endif
bool isOnline = IsOnline;
Expand Down Expand Up @@ -292,7 +298,7 @@ void CmdUnspawnCard()
{
RpcUnspawn();
NetworkServer.UnSpawn(gameObject);
LocalNetManager.Instance.UnSpawnCard(gameObject);
CGSNetManager.Instance.UnSpawnCard(gameObject);
}

[ClientRpc]
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/MainMenu/GameSelectionMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void Show()
{
gameObject.SetActive(true);
transform.SetAsLastSibling();
Rebuild(CardGameManager.Instance.AllCardGames.Keys.ToList(), SelectGame, CardGameManager.CurrentGameName);
Rebuild(CardGameManager.Instance.AllCardGames.Keys.ToList(), SelectGame, CardGameManager.Current.Name);
}

public void SelectGame(bool isOn, string gameName)
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/MainMenu/MainMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void Update()
public void UpdateCurrentGameText()
{
if (currentGameText != null)
currentGameText.text = CardGameManager.CurrentGameName;
currentGameText.text = CardGameManager.Current.Name;
}

public void GoToMainMenu()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using UnityEngine;
using UnityEngine.Networking;

public class LocalNetManager : NetworkManager
public class CGSNetManager : NetworkManager
{
public static LocalNetManager Instance => (LocalNetManager)singleton;
public NetPlayer LocalPlayer { get; set; }
public static CGSNetManager Instance => (CGSNetManager)singleton;
public CGSNetPlayer LocalPlayer { get; set; }
public PlayMode playController;
public GameObject cardModelPrefab;

Expand Down Expand Up @@ -38,14 +38,14 @@ public override void OnStartHost()
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
{
base.OnServerAddPlayer(conn, playerControllerId);
//Debug.Log("CGSNet: Host adds player: " + playerControllerId);
playController.netText.text = NetworkServer.connections.Count.ToString();
}

public override void OnStartClient(NetworkClient netClient)
{
base.OnStartClient(netClient);
ClientScene.RegisterSpawnHandler(cardModelPrefab.GetComponent<NetworkIdentity>().assetId, SpawnCard, UnSpawnCard);
playController.netText.text = "<multiplayer data>";
playController.netText.text = netClient.serverIp;
}

public GameObject SpawnCard(Vector3 position, NetworkHash128 assetId)
Expand All @@ -66,28 +66,16 @@ public void UnSpawnCard(GameObject spawned)
Destroy(spawned);
}

public override void OnStopHost()
{
base.OnStopHost();
Debug.Log("Host Stopped");
}

public override void OnServerError(NetworkConnection conn, int errorCode)
{
//base.OnServerError(conn, errorCode);
Debug.Log("Server error:" + errorCode);
}

public override void OnStopServer()
{
base.OnStopServer();
Debug.Log("Server Stopped");
}

public override void OnServerDisconnect(NetworkConnection conn)
{
//base.OnServerDisconnect(conn);
Debug.Log("Server disconnected");
Debug.Log("Player disconnected");
}

public override void OnClientError(NetworkConnection conn, int errorCode)
Expand All @@ -96,12 +84,6 @@ public override void OnClientError(NetworkConnection conn, int errorCode)
Debug.Log("Client error:" + errorCode);
}

public override void OnStopClient()
{
base.OnStopClient();
Debug.Log("Client Stopped");
}

public override void OnClientDisconnect(NetworkConnection conn)
{
//base.OnClientDisconnect(conn);
Expand Down
Loading

0 comments on commit 9b5cb58

Please sign in to comment.