Hyluss icon

cards

Hyluss | PRO | 10/16/18 11:34:25 PM UTC | 0 ⭐ | 406 👁️ | Never ⏰ | []
C# |

3.11 KB

|

None

|

0 👍

/

0 👎

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
 
namespace Game2
{
    public class Card
    {
        Texture2D currentTexture;
        Texture2D reversTexture;
        Texture2D mainTexture;
        Rectangle rect;
        Rectangle chunk;
        Rectangle? currentChunk=null;
 
 
        int width = 100;
        int height = 100;
 
        public int Type;
 
        bool clicked = false;
        bool unlocked = false;
 
        public Rectangle Rect { get => rect; set => rect = value; }
 
        public Card(ContentManager content, int x, int y, int offsetX, int offsetY, int type)
        {
            Type = type;
            reversTexture = content.Load<Texture2D>("question");
            mainTexture = content.Load<Texture2D>("numbers");
 
            chunk = new Rectangle(y * width, x * height, width, height); //reverse x and y
 
 
            rect = new Rectangle(offsetX, offsetY, width, height);
 
            
 
            currentTexture = reversTexture;
        }
 
        public void Draw(SpriteBatch spriteBatch, int x, int y)
        {
            //spriteBatch.Draw(currentTexture, rect, Color.White);
            spriteBatch.Draw(currentTexture, rect , currentChunk, Color.White);
        }
 
        public void Update(Rectangle cursor)
        {
            if (rect.Intersects(cursor)) //mouse collision
            {
                if (Mouse.GetState().LeftButton == ButtonState.Pressed && clicked==false && unlocked==false)
                {
                    clicked = true;
                    ++Game1.ClickedCounter;
                    ++Game1.ShowedCards;                    
 
                    if (Game1.CurrentType == Type)
                    {
                        Unlock();
                        Game1.pairs++;
                    }
                    else
                    {
                        if (Game1.ShowedCards == 2)
                        {
                            --Game1.ClickedCounter;
                            Hide();
                            Game1.SelectedCard.Hide();
                        }
                        else
                        {
                            Show(); 
                        }
                    }
                }
            }
        }
 
        public void Hide()
        {
            Game1.ShowedCards = 0;
            Game1.CurrentType = -1;            
            currentChunk = null;
            currentTexture = reversTexture;
            clicked = false;
        }
 
        public void Show()
        {
            Game1.SelectedCard = this;
            Game1.CurrentType = Type;
            currentChunk = chunk;
            currentTexture = mainTexture;
        }
 
        public void Unlock()
        {
            unlocked = true;
            currentChunk = chunk;
            currentTexture = mainTexture;
            Game1.CurrentType = -1;
            Game1.SelectedCard = null;
            Game1.ShowedCards = 0;            
 
        }
    }
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎