// [Client with Server-Only section] public void CreateGameGridObjects() { GameObject gridParent = new GameObject("MainGrid"); for (int x = 0; x < config.GameGridWidth; x++) { for (int y = 0; y < config.GameGridHeight; y++) { float xOff = x * cardOffX; float yOff = y * cardOffY; GameObject cardObj = Instantiate(matchController.landTilePrefab, new Vector3(xOff, yOff, 50), Quaternion.identity); // cardObj.GetComponent<Animator>().Play("Flip"); cardObj.name = (CardUtility.CreateCardObjectName("Tile", x, y)); cardObj.transform.SetParent(gridParent.transform); cardObj.transform.rotation = new Quaternion(0, 180, 0, 0); // 0, 180, 0, 0 knownOwnersGrid[x, y] = new CardData(); if (hasAuthority) { masterGrid[x, y].CardObject = cardObj; } } } }
// [Client/Server] public void CreateMarketGridObjects() { InitMarketGrid(); GameObject marketGridParent = new GameObject("MarketGrid"); for (int x = 0; x < Mathf.CeilToInt(((float)ResourceInfo.resources.Count - 1) / (float)config.GameGridHeight); x++) { for (int y = 0; y < config.GameGridHeight; y++) { if (masterMarketGrid[x, y] != null) { float xOff = ((config.GameGridWidth + 1) * cardOffX) + x * cardOffX; float yOff = y * cardOffY; GameObject cardObj = (GameObject)Instantiate(matchController.marketCardPrefab, new Vector3(xOff, yOff, 50), Quaternion.identity); cardObj.name = (CardUtility.CreateCardObjectName("Market", x, y)); cardObj.transform.SetParent(marketGridParent.transform); masterMarketGrid[x, y].CardObject = cardObj; // Physical representation } } } }
private void CreateNewCardObject(int index, string cardStr) { if (cardStr != null) { float xOff = index * 11 + (((config.GameGridWidth - config.PlayerHandSize) / 2f) * 11); float yOff = -10; Vector3 finalPosition = new Vector3(xOff, yOff, 40); // If old card exists, destroy it GameObject oldCardObj = GameObject.Find(CardUtility.CreateCardObjectName("GameCard", this.id, index)); if (oldCardObj != null) { Debug.Log(debugTag + "Trying to destroy " + oldCardObj.name); Destroy(oldCardObj); } // Create new card // NOTE: In the future when there is an actual deck model, have it originate from that. GameObject cardObj = (GameObject)Instantiate(matchController.gameCardPrefab, new Vector3(0, -40, 40), Quaternion.identity); cardObj.GetComponent <CardViewController>().Card = JsonUtility.FromJson <Card>(cardStr); cardObj.transform.SetParent(GameObject.Find("PlayerHand").transform); StartCoroutine(CardAnimations.MoveCardCoroutine(cardObj, finalPosition, 0.1f)); cardObj.name = (CardUtility.CreateCardObjectName("GameCard", this.id, index)); } }
private void TargetLoadHandCard(string hand, int index) { this.hand.Add(JsonUtility.FromJson <Card>(hand)); string cardString = CardUtility.CreateCardObjectName("GameCard", this.id, index); // Debug.Log("Looking for " + cardString); GameObject.Find(cardString).GetComponent <CardViewController>().Card = this.hand[index]; }
// [Client/Server] public void CreatePlayerHandObjects(int playerNum) { GameObject playerHandParent = new GameObject("PlayerHand"); for (int i = 0; i < config.PlayerHandSize; i++) { float xOff = i * 11 + (((config.GameGridWidth - config.PlayerHandSize) / 2f) * 11); float yOff = -10; GameObject cardObj = (GameObject)Instantiate(matchController.gameCardPrefab, new Vector3(xOff, yOff, 40), Quaternion.identity); cardObj.name = (CardUtility.CreateCardObjectName("GameCard", playerNum, i)); cardObj.transform.SetParent(playerHandParent.transform); } }
} // BuyingPhasePrimaryClick() private void PlayingPhasePrimaryClick(string type, int x, int y) { GameObject oldSelection; switch (type) { case "Tile": case "Market": if (selection >= 0 && matchData.Turn == this.ownerId) { Debug.Log(debugTag.head + "Trying to play card " + selection + " on " + objectHit.transform.parent.name); playIndex = selection; CmdPlayCard(selection, objectHit.transform.parent.name); } Debug.Log(debugTag + "Trying to find " + CardUtility.CreateCardObjectName("GameCard", this.ownerId, selection)); oldSelection = GameObject.Find(CardUtility.CreateCardObjectName("GameCard", this.ownerId, selection)); // oldSelection.transform.parent.position = new Vector3(oldSelection.transform.parent.position.x, oldSelection.transform.parent.position.y, objectHit.transform.parent.position.z); oldSelection.GetComponentsInChildren <Renderer>()[0].material.color = ColorPalette.tintCard; oldSelection.GetComponentsInChildren <Renderer>()[1].material.color = ColorPalette.tintCard; selection = -1; break; case "GameCard": // If the object clicked was already selected, deselect it if (selection == y) { selection = -1; // objectHit.transform.parent.position = new Vector3(objectHit.transform.parent.position.x, objectHit.transform.parent.position.y, objectHit.transform.parent.position.z); objectHit.GetComponentsInChildren <Renderer>()[0].material.color = ColorPalette.GetNewlandsColor("Card", 500, true); objectHit.GetComponentsInChildren <Renderer>()[1].material.color = ColorPalette.GetNewlandsColor("Card", 500, true); } else if (selection >= 0) { Debug.Log(debugTag + "Trying to find " + CardUtility.CreateCardObjectName("GameCard", this.ownerId, selection)); oldSelection = GameObject.Find(CardUtility.CreateCardObjectName("GameCard", this.ownerId, selection)); // oldSelection.transform.parent.position = new Vector3(oldSelection.transform.parent.position.x, oldSelection.transform.parent.position.y, objectHit.transform.parent.position.z); oldSelection.GetComponentsInChildren <Renderer>()[0].material.color = ColorPalette.GetNewlandsColor("Card", 500, true); oldSelection.GetComponentsInChildren <Renderer>()[1].material.color = ColorPalette.GetNewlandsColor("Card", 500, true); selection = y; // objectHit.transform.parent.position = new Vector3(objectHit.transform.parent.position.x, objectHit.transform.parent.position.y, (objectHit.transform.parent.position.z -2f)); objectHit.GetComponentsInChildren <Renderer>()[0].material.color = ColorPalette.GetNewlandsColor("Cyan", 300, true); objectHit.GetComponentsInChildren <Renderer>()[1].material.color = ColorPalette.GetNewlandsColor("Cyan", 300, true); } else { selection = y; // objectHit.transform.parent.position = new Vector3(objectHit.transform.parent.position.x, objectHit.transform.parent.position.y, (objectHit.transform.parent.position.z -2f)); objectHit.GetComponentsInChildren <Renderer>()[0].material.color = ColorPalette.GetNewlandsColor("Cyan", 300, true); objectHit.GetComponentsInChildren <Renderer>()[1].material.color = ColorPalette.GetNewlandsColor("Cyan", 300, true); } break; case "Discard": if (selection >= 0 && matchData.Turn == this.ownerId) { Debug.Log(debugTag.head + "Trying to play card " + selection + " on " + objectHit.name); playIndex = selection; CmdPlayCard(selection, objectHit.name); } Debug.Log(debugTag + "Trying to find " + CardUtility.CreateCardObjectName("GameCard", this.ownerId, selection)); oldSelection = GameObject.Find(CardUtility.CreateCardObjectName("GameCard", this.ownerId, selection)); oldSelection.GetComponentsInChildren <Renderer>()[0].material.color = ColorPalette.tintCard; oldSelection.GetComponentsInChildren <Renderer>()[1].material.color = ColorPalette.tintCard; selection = -1; break; default: break; } }
} // UpdateKnownRounds() private void HandleTurnEvent() { GameObject cardObj; //Handle an event during Phase 2 on your turn this.turnEventStr = matchDataBroadcaster.TurnEventStr; TurnEvent turnEvent = JsonUtility.FromJson <TurnEvent>(this.turnEventStr); Card turnCard = JsonUtility.FromJson <Card>(turnEvent.card); CardData targetCard = JsonUtility.FromJson <CardData>(turnEvent.card); Debug.Log(debugTag + "TurnEvent: " + turnEvent); Debug.Log(debugTag + "Target Category: " + targetCard.Category); // Check if the message should be addressed by this player // if (turnEvent.phase != phase || turnEvent.playerId != this.id) // { // return; // } // Operations ========================================================== switch (turnEvent.operation) { case "Play": // Debug.Log(debugTag + "Trying to destroy " + cardObj.name); // Destroy(cardObj); // Debug.Log(debugTag + "Moving " + cardObj.name); // If the card is meant to be stacked if (!turnCard.DiscardFlag) { if (!hasAuthority) { gridController.AddCardToStack(turnEvent.targetX, turnEvent.targetY, targetCard.Category, turnCard); } // Debug.Log(debugTag + "Running Shift Row Check on " + targetCard.Category + ", " + turnEvent.targetX+ ", " + turnEvent.targetY); if (gridController.ShiftRowCheck(targetCard.Category, turnEvent.targetX, turnEvent.targetY)) { gridController.IncrementStackSize(turnEvent.targetY, targetCard.Category); } Debug.Log(debugTag + "Trying to find " + CardUtility.CreateCardObjectName(targetCard.Category, turnEvent.targetX, turnEvent.targetY)); GameObject targetObject = GameObject.Find(CardUtility.CreateCardObjectName(targetCard.Category, turnEvent.targetX, turnEvent.targetY)); CardData tile = gridController.GetClientTile(targetCard.Category, turnEvent.targetX, turnEvent.targetY); // Debug.Log(debugTag.head + "Target Category: " + targetCard.Category); // Debug.Log(debugTag.head + targetObject.name + " Stack Size: " + tile.CardStack.Count); Vector3 gap = targetObject.transform.position - new Vector3(targetObject.transform.position.x, targetObject.transform.position.y - (gridController.shiftUnit * (tile.CardStack.Count)), (targetObject.transform.position.z) + (gridController.cardThickness * (tile.CardStack.Count))); if (turnEvent.playerId == this.id) { cardObj = GameObject.Find(CardUtility.CreateCardObjectName(turnEvent.cardType, turnEvent.x, turnEvent.y)); Debug.Log(debugTag + "Trying to move " + cardObj.name + " under " + targetObject.name); // gridController.GetTile(targetCard.x, targetCard.y) cardObj.transform.name = CardUtility.CreateCardObjectName("Stacked", turnEvent.x, tile.CardStack.Count - 1); cardObj.transform.SetParent(targetObject.transform); StartCoroutine(CardAnimations.MoveCardCoroutine(cardObj, targetObject, gap, .1f)); CreateNewCardObject(turnEvent.y, turnEvent.topCard); } else { GameObject otherPlayersCard = (GameObject)Instantiate(matchController.gameCardPrefab, new Vector3(0, 60, 40), Quaternion.identity); otherPlayersCard.GetComponent <CardViewController>().Card = JsonUtility.FromJson <CardData>(turnEvent.playedCard); otherPlayersCard.transform.name = CardUtility.CreateCardObjectName("Stacked", turnEvent.x, tile.CardStack.Count - 1); otherPlayersCard.transform.SetParent(targetObject.transform); StartCoroutine(CardAnimations.MoveCardCoroutine(otherPlayersCard, targetObject, gap, .1f)); } } else // Cards that don't get stacked (like discards) { if (turnEvent.playerId == this.id) { cardObj = GameObject.Find(CardUtility.CreateCardObjectName(turnEvent.cardType, turnEvent.x, turnEvent.y)); Destroy(cardObj); CreateNewCardObject(turnEvent.y, turnEvent.topCard); } } // cardObj.transform.Translate(new Vector3(targetObject.transform.position.x, // targetObject.transform.position.y // - (gridController.shiftUnit * targetCard.CardStack.Count), // (targetObject.transform.position.z) // + (gridController.cardThickness * targetCard.CardStack.Count))); if (turnEvent.topCard != "empty") { // this.hand[turnEvent.y] = JsonUtility.FromJson<CardData>(turnEvent.topCard); // CreateNewCardObject(turnEvent.y, turnEvent.topCard); } else { Debug.Log(debugTag + "GameCard deck must be empty!"); } // TODO: Add code to refresh a market card's footer value if a card was played on it. break; case "Buy": // Add bought Tile to local knowledge base gridController.KnownOwnersGrid[turnEvent.x, turnEvent.y].OwnerId = turnEvent.playerId; this.knownOwnersList[turnEvent.playerId - 1].Add(new Coordinate2(turnEvent.x, turnEvent.y)); // Grab the Tile GameObject that was bought Debug.Log(debugTag + CardUtility.CreateCardObjectName(turnEvent.cardType, turnEvent.x, turnEvent.y)); cardObj = GameObject.Find(CardUtility.CreateCardObjectName(turnEvent.cardType, turnEvent.x, turnEvent.y)); CardData boughtCard = JsonUtility.FromJson <CardData>(turnEvent.card); boughtCard.CardObject = cardObj; gridController.KnownOwnersGrid[turnEvent.x, turnEvent.y] = boughtCard; gridController.KnownOwnersGrid[turnEvent.x, turnEvent.y].OwnerId = turnEvent.playerId; CardAnimations.FlipCard(turnEvent.cardType, turnEvent.x, turnEvent.y); cardObj.GetComponent <CardViewController>().Card = JsonUtility.FromJson <Card>(turnEvent.card); // Depending on the player who bought the tile, change the Tile's color. cardObj.GetComponentsInChildren <Renderer>()[0].material.color = ColorPalette.GetDefaultPlayerColor(turnEvent.playerId, 500, true); cardObj.GetComponentsInChildren <Renderer>()[1].material.color = ColorPalette.GetDefaultPlayerColor(turnEvent.playerId, 500, true); break; } }
} // Update() private IEnumerator InitPlayer(string address) { // Grab Components yield return(StartCoroutine(GrabComponentsCoroutine())); // Determine of the main grid has been created yet GameObject testCard = GameObject.Find(CardUtility.CreateCardObjectName("Tile", 0, 0)); while (testCard == null) { testCard = GameObject.Find(CardUtility.CreateCardObjectName("Tile", 0, 0)); yield return(null); } // Get ID from MatchManager CmdInitId(address); while (this.id == -1) { yield return(null); } // Fetch username from PlayerDataController and send it serverside to MatchController InitUsername(); // Send our ID to the HudController, then force an update from it. this.hudController.GetComponent <HudController>().ThisPlayerId = this.id; this.hudController.UpdateHud(); // Grab the config from the broadcaster this.config = JsonUtility.FromJson <MatchConfig>(matchDataBroadcaster.MatchConfigStr); while (this.config == null) { yield return(null); } // Create hand card objects Debug.Log(debugTag + "Creating hand card objects for Player " + this.id); gridController.CreatePlayerHandObjects(this.id); // CmdGetHand(this.id); // Debug.Log(debug + "Hand size: " + this.hand.Count); // gridMan.CreateHandObjects(this.id, this.hand); this.knownOwnersList = new List <Coordinate2> [config.MaxPlayerCount]; // this.knownOwnersGrid = new int[config.GameGridWidth, config.GameGridHeight]; for (int i = 0; i < config.MaxPlayerCount; i++) { this.knownOwnersList[i] = new List <Coordinate2>(); } InitLocalResourcePrices(); this.matchData = JsonUtility.FromJson <MatchData>(matchDataBroadcaster.MatchDataStr); this.turnEventStr = matchDataBroadcaster.TurnEventStr; // InitLocalMarketGrid(); UpdateKnownInfo(); while (this.id == -1) { yield return(null); } for (int i = 0; i < config.PlayerHandSize; i++) { CmdGetHandCard(this.id, i); } // If this is Player 1 and it's the first Turn of the first Round if (this.id == 1 && this.matchData.Turn == 1 && this.matchData.Round == 1 && this.matchData.Phase == 1) { CardAnimations.HighlightCards(GetUnownedCards(), this.id); } this.initialzed = true; }