Multi card drag stuff and slight bugfixes.

master
sigonasr2 2 years ago
parent dbccc87399
commit fbc95c9548
  1. 121
      Solitaire/main.cpp

@ -118,22 +118,22 @@ class Solitaire : public olc::PixelGameEngine
}break; }break;
case HOME: { case HOME: {
if (faceUpCards.size() > 2) { if (faceUpCards.size() > 2) {
Card& c = faceUpCards[faceUpCards.size() - 1]; Card c = faceUpCards[faceUpCards.size() - 3];
game->RenderCard(pos+vi2d{-2,-2}, c.s, c.number); game->RenderCard(pos+vi2d{-2,-2}, c.s, c.number);
c = faceUpCards[faceUpCards.size() - 2]; c = faceUpCards[faceUpCards.size() - 2];
game->RenderCard(pos + vi2d{ -1,-1 }, c.s, c.number); game->RenderCard(pos + vi2d{ -1,-1 }, c.s, c.number);
c = faceUpCards[faceUpCards.size() - 3]; c = faceUpCards[faceUpCards.size() - 1];
game->RenderCard(pos, c.s, c.number); game->RenderCard(pos, c.s, c.number);
} else } else
if (faceUpCards.size() > 1) { if (faceUpCards.size() > 1) {
Card& c = faceUpCards[faceUpCards.size() - 1]; Card c = faceUpCards[faceUpCards.size() - 2];
game->RenderCard(pos + vi2d{ -1,-1 }, c.s, c.number); game->RenderCard(pos + vi2d{ -1,-1 }, c.s, c.number);
c = faceUpCards[faceUpCards.size() - 2]; c = faceUpCards[faceUpCards.size() - 1];
game->RenderCard(pos, c.s, c.number); game->RenderCard(pos, c.s, c.number);
} }
else else
if (faceUpCards.size() == 1) { if (faceUpCards.size() == 1) {
Card& c = faceUpCards[faceUpCards.size() - 1]; Card c = faceUpCards[faceUpCards.size() - 1];
game->RenderCard(pos, c.s, c.number); game->RenderCard(pos, c.s, c.number);
} }
}break; }break;
@ -180,6 +180,38 @@ class Solitaire : public olc::PixelGameEngine
} }
return false; return false;
} }
void ResetDraggedCard() {
for (int i = 0; i < previousCardInteraction.size(); i++) {
previousCardInteraction[i]->number = draggedCard[i].number;
previousCardInteraction[i]->s = draggedCard[i].s;
}
draggedCard.clear();
previousCardInteraction.clear();
}
void DeleteDraggedCard() {
switch (previousCardSlotInteraction->type) {
case ACTIVE_PLAYDECK: {
//Erases top-most card.
previousCardSlotInteraction->faceUpCards.erase(previousCardSlotInteraction->faceUpCards.end() - 1);
}break;
case HOME: {
//Erases top-most card.
previousCardSlotInteraction->faceUpCards.erase(previousCardSlotInteraction->faceUpCards.end() - 1);
}break;
case FIELD: {
//Erases top-most card.
for (int i = 0; i < draggedCard.size(); i++) {
previousCardSlotInteraction->faceUpCards.erase(previousCardSlotInteraction->faceUpCards.end() - 1);
}
}break;
}
previousCardSlotInteraction = NULL;
draggedCard.clear();
previousCardInteraction.clear();
}
void UpdateCardSlots() { void UpdateCardSlots() {
if (GetMouse(0).bPressed&&MouseOver(PlayDeck)) { if (GetMouse(0).bPressed&&MouseOver(PlayDeck)) {
if (PlayDeck.faceDownCards.size() > 0) { if (PlayDeck.faceDownCards.size() > 0) {
@ -202,8 +234,9 @@ class Solitaire : public olc::PixelGameEngine
if (GetMouse(0).bPressed && MouseOver(ActivePlayDeck)) { if (GetMouse(0).bPressed && MouseOver(ActivePlayDeck)) {
if (ActivePlayDeck.faceUpCards.size() > 0) { if (ActivePlayDeck.faceUpCards.size() > 0) {
Card& card = ActivePlayDeck.faceUpCards[ActivePlayDeck.faceUpCards.size() - 1]; Card& card = ActivePlayDeck.faceUpCards[ActivePlayDeck.faceUpCards.size() - 1];
draggedCard = { card.s,card.number }; draggedCard.push_back({ card.s,card.number });
previousCardInteraction = &ActivePlayDeck.faceUpCards[ActivePlayDeck.faceUpCards.size() - 1]; previousCardInteraction.push_back(& ActivePlayDeck.faceUpCards[ActivePlayDeck.faceUpCards.size() - 1]);
previousCardSlotInteraction = &ActivePlayDeck;
ActivePlayDeck.faceUpCards[ActivePlayDeck.faceUpCards.size() - 1].number = -1; ActivePlayDeck.faceUpCards[ActivePlayDeck.faceUpCards.size() - 1].number = -1;
} }
} }
@ -211,8 +244,9 @@ class Solitaire : public olc::PixelGameEngine
if (GetMouse(0).bPressed && MouseOver(Home[i])) { if (GetMouse(0).bPressed && MouseOver(Home[i])) {
if (Home[i].faceUpCards.size() > 0) { if (Home[i].faceUpCards.size() > 0) {
Card& card = Home[i].faceUpCards[Home[i].faceUpCards.size() - 1]; Card& card = Home[i].faceUpCards[Home[i].faceUpCards.size() - 1];
draggedCard = { card.s,card.number }; draggedCard.push_back({ card.s,card.number });
previousCardInteraction = &Home[i].faceUpCards[Home[i].faceUpCards.size() - 1]; previousCardInteraction.push_back(& Home[i].faceUpCards[Home[i].faceUpCards.size() - 1]);
previousCardSlotInteraction = &Home[i];
Home[i].faceUpCards[Home[i].faceUpCards.size() - 1].number = -1; Home[i].faceUpCards[Home[i].faceUpCards.size() - 1].number = -1;
} }
} }
@ -220,10 +254,28 @@ class Solitaire : public olc::PixelGameEngine
for (int i = 0; i < Field.size(); i++) { for (int i = 0; i < Field.size(); i++) {
if (GetMouse(0).bPressed && MouseOver(Field[i])) { if (GetMouse(0).bPressed && MouseOver(Field[i])) {
if (Field[i].faceUpCards.size() > 0) { if (Field[i].faceUpCards.size() > 0) {
Card& card = Field[i].faceUpCards[Field[i].faceUpCards.size() - 1]; if (GetMouseY() < Field[i].pos.y + Field[i].faceDownCards.size() * Field[i].cardSpacing) {
draggedCard = { card.s,card.number }; //Grabbing everything because we clicked above the stack.
previousCardInteraction = &Field[i].faceUpCards[Field[i].faceUpCards.size() - 1]; for (int j=0;j<Field[i].faceUpCards.size();j++){
Field[i].faceUpCards[Field[i].faceUpCards.size() - 1].number = -1; Card& card = Field[i].faceUpCards[j];
draggedCard.push_back({ card.s,card.number });
previousCardInteraction.push_back(&Field[i].faceUpCards[j]);
previousCardSlotInteraction = &Field[i];
Field[i].faceUpCards[j].number = -1;
}
}
else
{
int highestY = Field[i].pos.y + Field[i].faceDownCards.size() * Field[i].cardSpacing;
int cardSlotsDown = std::min((int)Field[i].faceUpCards.size()-1,(GetMouseY() - highestY) / Field[i].cardSpacing);
for (int j = cardSlotsDown; j < Field[i].faceUpCards.size(); j++) {
Card& card = Field[i].faceUpCards[j];
draggedCard.push_back({ card.s,card.number });
previousCardInteraction.push_back(&Field[i].faceUpCards[j]);
previousCardSlotInteraction = &Field[i];
Field[i].faceUpCards[j].number = -1;
}
}
} else } else
if (Field[i].faceDownCards.size() > 0) { if (Field[i].faceDownCards.size() > 0) {
Card card = Field[i].faceDownCards[Field[i].faceDownCards.size() - 1]; Card card = Field[i].faceDownCards[Field[i].faceDownCards.size() - 1];
@ -234,10 +286,32 @@ class Solitaire : public olc::PixelGameEngine
} }
} }
if (GetMouse(0).bReleased && draggedCard.number !=-1) { if (GetMouse(0).bReleased && draggedCard.size()>0) {
previousCardInteraction->number = draggedCard.number; for (int i = 0; i < Home.size(); i++) {
previousCardInteraction->s = draggedCard.s; if (MouseOver(Home[i])) {
draggedCard.number = -1; if (draggedCard.size()==1&&draggedCard[0].number == 1 && Home[i].faceUpCards.size() == 0) {
//If the slot has no cards in it and it's an Ace, accept.
Home[i].faceUpCards.push_back(draggedCard[0]);
DeleteDraggedCard();
goto draggedCardSuccess;
} else
if (Home[i].faceUpCards.size() > 0 && draggedCard.size() == 1 && draggedCard[0].number == Home[i].faceUpCards[Home[i].faceUpCards.size() - 1].number + 1 &&
draggedCard[0].s == Home[i].faceUpCards[Home[i].faceUpCards.size() - 1].s) {
//If the card we are holding is one number higher than the top card on the slot & the suit is matching.
Home[i].faceUpCards.push_back(draggedCard[0]);
DeleteDraggedCard();
goto draggedCardSuccess;
}
}
}
for (int i = 0; i < Field.size(); i++) {
if (MouseOver(Field[i])) {
}
}
ResetDraggedCard();
draggedCardSuccess:
int a;
} }
} }
@ -255,8 +329,9 @@ class Solitaire : public olc::PixelGameEngine
CardSlot ActivePlayDeck{ {56,8},ACTIVE_PLAYDECK }; CardSlot ActivePlayDeck{ {56,8},ACTIVE_PLAYDECK };
std::array<CardSlot, 4> Home; std::array<CardSlot, 4> Home;
std::array<CardSlot, 7> Field; std::array<CardSlot, 7> Field;
Card draggedCard = {HEART,-1}; std::vector<Card> draggedCard = {};
Card*previousCardInteraction=NULL; std::vector<Card*>previousCardInteraction = {};
CardSlot*previousCardSlotInteraction=NULL;
Card DrawCard() { Card DrawCard() {
std::uniform_int_distribution<int> distribution(0, cardDeck.size()-1); std::uniform_int_distribution<int> distribution(0, cardDeck.size()-1);
@ -279,6 +354,10 @@ class Solitaire : public olc::PixelGameEngine
diamond_spr.Load("assets/diamond.png"); diamond_spr.Load("assets/diamond.png");
heart_spr.Load("assets/heart.png"); heart_spr.Load("assets/heart.png");
spade_spr.Load("assets/spade.png"); spade_spr.Load("assets/spade.png");
Home[0].faceUpCards.push_back({ SPADE,1 });
Home[0].faceUpCards.push_back({ SPADE,2 });
Field[3].faceUpCards.push_back({ SPADE,3 });
Field[3].faceUpCards.push_back({ SPADE,4 });
for (int i = 0; i < Field.size(); i++) { for (int i = 0; i < Field.size(); i++) {
for (int j = 0; j < i; j++) { for (int j = 0; j < i; j++) {
Field[i].faceDownCards.push_back(DrawCard()); Field[i].faceDownCards.push_back(DrawCard());
@ -316,7 +395,9 @@ class Solitaire : public olc::PixelGameEngine
for (CardSlot& c : Field) { for (CardSlot& c : Field) {
c.RenderCards(); c.RenderCards();
} }
RenderCard(GetMousePos() - CARD_SIZE/2, draggedCard.s, draggedCard.number); for (int i = 0; i < draggedCard.size(); i++) {
RenderCard(GetMousePos() - CARD_SIZE / 2 + vi2d{0,i*16}, draggedCard[i].s, draggedCard[i].number);
}
return true; return true;
} }
}; };

Loading…
Cancel
Save