public void SelectCard(GameCardBehaviour card) { // avoid selection during animation if (!this.m_Animating) { // card is already in the current selection if ((card != this.m_SelectedCard0) && (card != this.m_SelectedCard1)) { // select the first card if (this.m_SelectedCard0 == null) { this.m_SelectedCard0 = card; // show card front face this.TurnCard(card, false); } else // select the second card if ((this.m_SelectedCard1 == null) && (card != this.m_SelectedCard0)) { this.m_SelectedCard1 = card; // show card front face this.TurnCard(card, false); // if the couple does not match simply turn cards backside, else animate and hide them StartCoroutine(this.m_SelectedCard0.AnimalType == this.m_SelectedCard1.AnimalType ? "GoodCouple" : "WrongCouple"); } } } }
public void SelectCard(GameCardBehaviour card) { // avoid selection during animation if (this.m_Animating) return; // card is already in the current selection if (card == null || card == this.m_SelectedCard0 || card == this.m_SelectedCard1) return; // select the first card if (this.m_SelectedCard0 == null) { this.m_SelectedCard0 = card; // show card front face this.TurnCard(card, false); return; } // select the second card if (this.m_SelectedCard1 == null && card != this.m_SelectedCard0) { this.m_SelectedCard1 = card; // show card front face this.TurnCard(card, false); // if the couple does not match simply turn cards backside, else animate and hide them StartCoroutine(this.m_SelectedCard0.AnimalType == this.m_SelectedCard1.AnimalType ? "GoodCouple" : "WrongCouple"); } }
public void HideCard(GameCardBehaviour card) { card.Active = false; // disable renderer card.gameObject.GetComponent <SpriteRenderer>().enabled = false; // disable collider card.GetComponent <BoxCollider2D>().enabled = false; }
public void HideCard(GameCardBehaviour card) { card.Active = false; // disable renderer card.gameObject.GetComponent<SpriteRenderer>().enabled = false; // disable collider card.GetComponent<BoxCollider2D>().enabled = false; }
public void ShowCard(GameCardBehaviour card) { card.Active = true; // enable renderer card.gameObject.GetComponent <SpriteRenderer>().enabled = true; // enable collider card.GetComponent <BoxCollider2D>().enabled = true; }
public override void OnInspectorGUI() { // get the target object GameCardBehaviour card = target as GameCardBehaviour; if (card != null) { this.DrawInspector(card); } }
private void DrawInspector(GameCardBehaviour card) { bool needSpriteUpdate = false; bool active = EditorGUILayout.Toggle(new GUIContent("Active", ""), card.Active); bool backSide = EditorGUILayout.Toggle(new GUIContent("Back side", ""), card.BackSide); CardType animalType = (CardType)EditorGUILayout.EnumPopup("Animal type", card.AnimalType); int index = EditorGUILayout.IntField("Index in deck", card.Index); GameBehaviour game = EditorGUILayout.ObjectField("Game manager", card.Game, typeof(GameBehaviour), true) as GameBehaviour; // update active flag, if needed if (active != card.Active) { card.Active = active; EditorUtility.SetDirty(card); //needUpdate = true; if (card.Game != null) { if (card.Active) card.Game.ShowCard(card); else card.Game.HideCard(card); } } // update back side flag, if needed if (backSide != card.BackSide) { card.BackSide = backSide; EditorUtility.SetDirty(card); needSpriteUpdate = true; } // update animal/card type, if needed if (animalType != card.AnimalType) { card.AnimalType = animalType; EditorUtility.SetDirty(card); needSpriteUpdate = true; } // update index in deck, if needed if (index != card.Index) { card.Index = index; EditorUtility.SetDirty(card); } // update game manager, if needed if (game != card.Game) { card.Game = game; EditorUtility.SetDirty(card); } if (needSpriteUpdate && card.Game != null) card.Game.UpdateCardSprite(card); }
private void DrawInspector(GameCardBehaviour card) { bool needSpriteUpdate = false; bool active = EditorGUILayout.Toggle(new GUIContent("Active", ""), card.Active); bool backSide = EditorGUILayout.Toggle(new GUIContent("Back side", ""), card.BackSide); GameCardType animalType = (GameCardType)EditorGUILayout.EnumPopup("Animal type", card.AnimalType); GameBehaviour game = EditorGUILayout.ObjectField("Game manager", card.Game, typeof(GameBehaviour), true) as GameBehaviour; // update active flag, if needed if (active != card.Active) { card.Active = active; SVGUtils.MarkSceneDirty(); if (card.Game != null) { if (card.Active) { card.Game.ShowCard(card); } else { card.Game.HideCard(card); } } } // update back side flag, if needed if (backSide != card.BackSide) { card.BackSide = backSide; SVGUtils.MarkSceneDirty(); needSpriteUpdate = true; } // update animal/card type, if needed if (animalType != card.AnimalType) { card.AnimalType = animalType; SVGUtils.MarkSceneDirty(); needSpriteUpdate = true; } // update game manager, if needed if (game != card.Game) { card.Game = game; SVGUtils.MarkSceneDirty(); } if (needSpriteUpdate && (card.Game != null)) { card.Game.UpdateCardSprite(card); } }
private void DisposeCards() { if (this.Camera != null) { int[] cardsIndexes; int slotsPerRow, slotsPerColumn; SVGRuntimeSprite data = this.Atlas.GetSpriteByName(GameCardBehaviour.AnimalSpriteName(GameCardType.BackSide)); float cardWidth = data.Sprite.bounds.size.x; float cardHeight = data.Sprite.bounds.size.y; float worldWidth = this.Camera.WorldWidth; float worldHeight = this.Camera.WorldHeight; if (worldWidth <= worldHeight) { // number of card slots in each dimension slotsPerRow = 3; slotsPerColumn = 4; cardsIndexes = CARDS_INDEXES_PORTRAIT; } else { // number of card slots in each dimension slotsPerRow = 4; slotsPerColumn = 3; cardsIndexes = CARDS_INDEXES_LANDSCAPE; } // 5% border float ofsX = worldWidth * 0.05f; float ofsY = worldHeight * 0.05f; float horizSeparator = ((worldWidth - (slotsPerRow * cardWidth) - (2.0f * ofsX)) / (slotsPerRow - 1)); float vertSeparator = ((worldHeight - (slotsPerColumn * cardHeight) - (2.0f * ofsY)) / (slotsPerColumn - 1)); int cardIdx = 0; for (int y = 0; y < slotsPerColumn; ++y) { for (int x = 0; x < slotsPerRow; ++x) { float posX = ofsX + (x * (cardWidth + horizSeparator)) - (worldWidth * 0.5f) + (cardWidth * 0.5f); float posY = ofsY + (y * (cardHeight + vertSeparator)) - (worldHeight * 0.5f) + (cardHeight * 0.5f); this.Cards[cardsIndexes[cardIdx]].transform.position = new Vector3(posX, posY); cardIdx++; } } } }
private void StartNewGame() { GameCardType[] animalCouples = new GameCardType[this.Cards.Length]; // start with a random animal GameCardType currentAnimal = GameCardBehaviour.RandomAnimal(); // generate animal couples for (int i = 0; i < (this.Cards.Length / 2); ++i) { animalCouples[i * 2] = currentAnimal; animalCouples[(i * 2) + 1] = currentAnimal; currentAnimal = GameCardBehaviour.NextAnimal(currentAnimal); } // shuffle couples this.Shuffle(animalCouples); // assign cards for (int i = 0; i < this.Cards.Length; ++i) { this.Cards[i].BackSide = true; this.Cards[i].AnimalType = animalCouples[i]; this.ShowCard(this.Cards[i]); } // select a background if (this.Background != null) { if (this.BackgroundFiles.Length > 0) { // destroy current texture and sprite this.Background.DestroyAll(true); // assign a new SVG file this.Background.SVGFile = this.BackgroundFiles[this.m_BackgroundIndex % this.BackgroundFiles.Length]; this.m_BackgroundIndex++; } else { this.Background.SVGFile = null; } } // no selection this.m_SelectedCard0 = null; this.m_SelectedCard1 = null; }
public Sprite UpdateCardSprite(GameCardBehaviour card) { if (this.Atlas != null) { GameCardType cardType = card.BackSide ? GameCardType.BackSide : card.AnimalType; SVGRuntimeSprite data = this.Atlas.GetSpriteByName(GameCardBehaviour.AnimalSpriteName(cardType)); // get the sprite, given its name if (data != null) { card.gameObject.GetComponent <SpriteRenderer>().sprite = data.Sprite; // keep updated the SVGSpriteLoaderBehaviour component too SVGSpriteLoaderBehaviour loader = card.gameObject.GetComponent <SVGSpriteLoaderBehaviour>(); if (loader != null) { loader.SpriteReference = data.SpriteReference; } return(data.Sprite); } } return(null); }
public void ShowCard(GameCardBehaviour card) { card.Active = true; // enable renderer card.gameObject.GetComponent<SpriteRenderer>().enabled = true; // enable collider card.GetComponent<BoxCollider2D>().enabled = true; }
private void TurnCard(GameCardBehaviour card, bool backSide) { card.BackSide = backSide; this.UpdateCardSprite(card); }
// Reset is called when the user hits the Reset button in the Inspector's context menu or when adding the component the first time. // This function is only called in editor mode. Reset is most commonly used to give good default values in the inspector. void Reset() { this.Camera = null; this.Background = null; this.Atlas = null; this.Cards = null; this.m_SelectedCard0 = null; this.m_SelectedCard1 = null; this.m_Animating = false; }
private IEnumerator WrongCouple() { yield return new WaitForSeconds(1.5f); // show card back face this.TurnCard(this.m_SelectedCard0, true); this.TurnCard(this.m_SelectedCard1, true); this.m_SelectedCard0 = this.m_SelectedCard1 = null; }
private void TurnCard(GameCardBehaviour card, bool backSide) { card.BackSide = backSide; this.UpdateCardSprite(card); }
// Use this for initialization void Start() { this.m_SelectedCard0 = this.m_SelectedCard1 = null; // initialize random generator this.m_Random = new System.Random(System.Environment.TickCount); this.m_Animating = false; this.m_BackgroundIndex = this.m_Random.Next(); // start a new game this.StartNewGame(); if (this.Camera != null) { // register ourself for receiving resize events this.Camera.OnResize += this.OnResize; // now fire a resize event by hand this.Camera.Resize(true); } // show the "shuffle" animation StartCoroutine(this.ShuffleAnimation()); }
private IEnumerator GoodCouple() { this.m_Animating = true; this.m_SelectedCard0.GetComponent<Animation>().PlayQueued("cardRotation"); this.m_SelectedCard1.GetComponent<Animation>().PlayQueued("cardRotation"); yield return new WaitForSeconds(2); this.m_Animating = false; // hide both cards this.HideCard(this.m_SelectedCard0); this.HideCard(this.m_SelectedCard1); this.m_SelectedCard0 = this.m_SelectedCard1 = null; // if we have finished, start a new game! if (this.GameFinished()) { // start a new game: shuffle the cards deck and change the background this.StartNewGame(); // we don't want to update colliders this.UpdateCardsSprites(false); // because we have changed SVG file, we want to be sure that the new background will cover the whole screen this.ResizeBackground((int)SVGAssets.ScreenResolutionWidth, (int)SVGAssets.ScreenResolutionHeight); // show the "shuffle" animation StartCoroutine(this.ShuffleAnimation()); } }
public Sprite UpdateCardSprite(GameCardBehaviour card) { if (this.Atlas != null) { CardType cardType = card.BackSide ? CardType.BackSide : card.AnimalType; SVGRuntimeSprite data = this.Atlas.GetSpriteByName(GameCardBehaviour.AnimalSpriteName(cardType)); // get the sprite, given its name if (data != null) { card.gameObject.GetComponent<SpriteRenderer>().sprite = data.Sprite; // keep updated the SVGSpriteLoaderBehaviour component too SVGSpriteLoaderBehaviour loader = card.gameObject.GetComponent<SVGSpriteLoaderBehaviour>(); if (loader != null) loader.SpriteReference = data.SpriteReference; return data.Sprite; } } return null; }