Allow player to choose target when playing from deck

This commit is contained in:
Bill Rossi 2025-02-22 10:14:38 -05:00
parent 2651157b3a
commit c76efc3e31
4 changed files with 83 additions and 15 deletions

84
game.c
View File

@ -217,6 +217,23 @@ void handle_input(Game *g) {
}
}
break;
case GAME_STATE_CHOOSING_TARGET_FROM_DECK:
if (IsMouseButtonPressed(0)) {
mouse_pos = GetMousePosition();
for (int i = 0; i < g->field.count; i++) {
if (point_within_card(g->field.cards[i], mouse_pos)) {
Card *selected_card = g->deck.cards[g->deck.count - 1];
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];
} else {
printf("Invalid\n");
}
break;
}
}
}
break;
default:
break;
}
@ -381,7 +398,7 @@ void run_frame_choosing_from_hand(Game *g) {
run_frame_player_choosing_from_hand(g);
} else {
run_frame_ai_playing(g);
g->state = GAME_STATE_PLAYING_FROM_DECK;
g->state = GAME_STATE_SHOWING_CARD_FROM_DECK;
}
}
@ -425,29 +442,60 @@ void run_frame_choosing_target(Game *g) {
add_to_hand(&g->field, g->current_play_from_hand);
g->current_play_from_hand = NULL;
}
g->state = GAME_STATE_PLAYING_FROM_DECK;
g->state = GAME_STATE_SHOWING_CARD_FROM_DECK;
}
}
void run_frame_showing_card_from_deck(Game *g) {
Card *top_card = g->deck.cards[g->deck.count - 1];
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;
g->state = GAME_STATE_PLAYING_FROM_DECK;
}
void run_frame_playing_from_deck(Game *g) {
Hand *to_hand = &current_player(g)->scored;
Card *top_card = g->deck.cards[g->deck.count - 1];
if (top_card->visible) {
Card *target = valid_target(top_card, &g->field);
if (target) {
capture_card_from_field(g, top_card, target, &g->deck, to_hand);
} else {
remove_from_hand(&g->deck, top_card);
add_to_hand(&g->field, top_card);
}
Card *targets[4];
int target_count = 0;
valid_targets(top_card, &g->field, &targets[0], &target_count);
Card *target = NULL;
if (target_count == 1) {
capture_card_from_field(g, top_card, targets[0], &g->deck, to_hand);
g->state = GAME_STATE_CHECKING_FOR_NEW_DEKIYAKU;
} else if(target_count == 0) {
remove_from_hand(&g->deck, top_card);
add_to_hand(&g->field, top_card);
g->state = GAME_STATE_CHECKING_FOR_NEW_DEKIYAKU;
} else {
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;
g->state = GAME_STATE_CHOOSING_TARGET_FROM_DECK;
return;
}
}
void run_frame_choosing_target_from_deck(Game *g) {
Hand *to_hand = &current_player(g)->scored;
Card *top_card = g->deck.cards[g->deck.count - 1];
Card *targets[4];
int target_count = 0;
valid_targets(top_card, &g->field, &targets[0], &target_count);
Card *target = NULL;
if (is_player_turn(g)) {
if (g->current_play_target) {
capture_card_from_field(g, top_card, g->current_play_target, &g->deck, to_hand);
g->state = GAME_STATE_CHECKING_FOR_NEW_DEKIYAKU;
}
} else {
// TODO: better AI
capture_card_from_field(g, top_card, targets[0], &g->deck, to_hand);
g->state = GAME_STATE_CHECKING_FOR_NEW_DEKIYAKU;
}
}
@ -589,9 +637,15 @@ void run_frame(Game *g) {
case GAME_STATE_CHOOSING_TARGET:
run_frame_choosing_target(g);
break;
case GAME_STATE_SHOWING_CARD_FROM_DECK:
run_frame_showing_card_from_deck(g);
break;
case GAME_STATE_PLAYING_FROM_DECK:
run_frame_playing_from_deck(g);
break;
case GAME_STATE_CHOOSING_TARGET_FROM_DECK:
run_frame_choosing_target_from_deck(g);
break;
case GAME_STATE_CHECKING_FOR_NEW_DEKIYAKU:
run_frame_checking_for_new_dekiyaku(g);
break;

2
game.h
View File

@ -22,7 +22,9 @@ typedef enum GameState {
GAME_STATE_CHECKING_FOR_CANCEL,
GAME_STATE_CHOOSING_FROM_HAND,
GAME_STATE_CHOOSING_TARGET,
GAME_STATE_SHOWING_CARD_FROM_DECK,
GAME_STATE_PLAYING_FROM_DECK,
GAME_STATE_CHOOSING_TARGET_FROM_DECK,
GAME_STATE_CHECKING_FOR_NEW_DEKIYAKU,
GAME_STATE_SELECTING_DEKIYAKU_ACTION,
GAME_STATE_CALCULATING_SCORES,

11
play.c
View File

@ -1,4 +1,5 @@
#include <stddef.h>
#include <stdio.h>
#include <stdbool.h>
#include "play.h"
@ -25,6 +26,16 @@ bool valid_play(Hand *field, Card *played, Card *target) {
}
}
void valid_targets(Card *active, Hand *field, Card **targets, int *target_count) {
*target_count = 0;
for (int i = 0; i < field->count; i++) {
if (field->cards[i]->month == active->month) {
targets[(*target_count)++] = field->cards[i];
}
}
targets[*target_count] = NULL; // Sentinel
}
Card *valid_target(Card *active, Hand *field) {
for (int i = 0; i < field->count; i++) {
if (field->cards[i]->month == active->month) {

1
play.h
View File

@ -13,6 +13,7 @@ struct Play {
};
bool valid_play(Hand *field, Card *played, Card *target);
void valid_targets(Card *active, Hand *field, Card **targets, int *target_count);
Card *valid_target(Card *active, Hand *field);
Play ai_play(Hand *hand, Hand *field);