Cards no longer try to lay on top of each other

This commit is contained in:
Bill Rossi 2025-02-12 16:45:31 -05:00
parent 892fc32efa
commit 98ba7fb670
3 changed files with 27 additions and 16 deletions

30
card.c
View File

@ -54,24 +54,40 @@ void remove_from_hand(Hand *h, Card *c) {
}
void add_to_hand(Hand *h, Card *c) {
int i = 0;
for (; i < h->count; i++) {
if (h->cards[i] == NULL) break;
int order_guy[48];
int order_guy_count = 0;
for (int i = 0; i < 48; i++) {
order_guy[i] = 0;
}
h->cards[i] = c;
for (int i = 0; i < h->count; i++) {
if (h->cards[i] == NULL) continue;
order_guy[h->cards[i]->order] = 1;
order_guy_count++;
}
int first_open_spot = h->count;
for (int i = 0; i < order_guy_count; i++) {
if (order_guy[i]) continue;
first_open_spot = i;
break;
}
c->order = first_open_spot;
h->cards[h->count] = c;
c->move.position = &c->position;
c->move.origin.x = c->position.x;
c->move.origin.y = c->position.y;
switch (h->display_type) {
case HAND_DISPLAY_ROW:
c->move.destination.x = h->position.x + (i * (CARD_WIDTH + 10));
c->move.destination.x = h->position.x + (first_open_spot * (CARD_WIDTH + 10));
c->move.destination.y = h->position.y;
break;
case HAND_DISPLAY_FIELD:
c->move.destination.x = h->position.x + ((i / 2) * (CARD_WIDTH + 10));
c->move.destination.y = h->position.y + (i % 2 * (CARD_HEIGHT + 10));
c->move.destination.x = h->position.x + ((first_open_spot / 2) * (CARD_WIDTH + 10));
c->move.destination.y = h->position.y + (first_open_spot % 2 * (CARD_HEIGHT + 10));
break;
case HAND_DISPLAY_DECK:
c->move.destination.x = h->position.x;

3
card.h
View File

@ -56,6 +56,7 @@ struct Card {
bool selected;
bool visible;
Move move;
int order;
};
typedef enum HandDisplayType {
@ -75,6 +76,8 @@ void draw_card(Card *c, Texture2D *cards_texture);
bool point_within_card(Card *c, Vector2 v);
void shuffle_hand(Hand *h);
void deal(Hand *from, Hand *to, int count, bool up);
void remove_from_hand(Hand *h, Card *c);
void add_to_hand(Hand *h, Card *c);
bool card_done_moving(Card *c);
Rectangle next_card_position(Hand *h);

10
game.c
View File

@ -60,6 +60,7 @@ void initialize_game(Game *g) {
g->cards[i].move.end_time = 0.;
g->cards[i].move.position = &g->cards[i].position;
g->cards[i].move.destination = (Vector2) { 800, 400 };
g->cards[i].order = i;
g->deck.cards[i] = &g->cards[i];
g->deck.count++;
}
@ -116,10 +117,8 @@ void handle_input(Game *g) {
case GAME_STATE_PLAYER_CHOOSING_TARGET:
if (IsMouseButtonPressed(0)) {
mouse_pos = GetMousePosition();
bool clicked_hand_card = false;
for (int i = 0; i < g->player_hand.count; i++) {
if (point_within_card(g->player_hand.cards[i], mouse_pos)) {
clicked_hand_card = true;
if (g->player_hand.cards[i]->selected) {
g->player_hand.cards[i]->selected = false;
} else {
@ -145,7 +144,6 @@ void handle_input(Game *g) {
if (valid_play(&g->field, selected_card, g->field.cards[i])) {
g->current_play_from_hand = selected_card;
g->current_play_target = g->field.cards[i];
printf("Valid\n");
} else {
printf("Invalid\n");
}
@ -157,7 +155,6 @@ void handle_input(Game *g) {
if (valid_play(&g->field, selected_card, NULL)) {
g->current_play_from_hand = selected_card;
g->current_play_target = NULL;
printf("Valid\n");
} else {
printf("Invalid\n");
}
@ -249,8 +246,6 @@ void run_frame_player_choosing_target(Game *g) {
void run_frame_player_from_deck(Game *g) {
Card *top_card = g->deck.cards[g->deck.count - 1];
if (top_card->visible) {
printf("flipped\n");
// if the card's already been flipped up
Card *target = valid_target(top_card, &g->field);
if (target) {
remove_from_hand(&g->field, target);
@ -263,14 +258,11 @@ void run_frame_player_from_deck(Game *g) {
}
g->state = GAME_STATE_INITIALIZING;
} else {
printf("flip top\n");
// flip up the top card
remove_from_hand(&g->deck, top_card);
add_to_hand(&g->deck, top_card);
top_card->visible = true;
top_card->move.end_time = 0.3;
top_card->move.destination.x = top_card->move.destination.x + 100;
printf("moving to %f %f\n", top_card->move.destination.x, top_card->move.destination.y);
}
}