monopoly board
This commit is contained in:
@@ -19,6 +19,8 @@ extern "C" {
|
||||
#include <time.h>
|
||||
#include "DiceModalGame.h"
|
||||
#include "PropertyModalGame.h"
|
||||
#include "BoardModalGame.h"
|
||||
#include "MonopolyBoardRenderer.h"
|
||||
|
||||
|
||||
// --- Constructor ---
|
||||
@@ -56,11 +58,26 @@ bool MonopolyGame::update(const InputEvent& event) {
|
||||
if (active_modal) {
|
||||
bool modal_redraw = active_modal->update(event);
|
||||
if (modal_redraw) needs_redraw = true;
|
||||
// If modal is dismissed, delete and return control
|
||||
// Use dynamic_cast to check for modal type and dismissal
|
||||
|
||||
// Check for specific modal types to handle their results
|
||||
auto dice_modal = dynamic_cast<DiceModalGame*>(active_modal);
|
||||
auto prop_modal = dynamic_cast<PropertyModalGame*>(active_modal);
|
||||
if ((dice_modal && dice_modal->is_dismissed()) || (prop_modal && prop_modal->is_dismissed())) {
|
||||
auto board_modal = dynamic_cast<BoardModalGame*>(active_modal);
|
||||
|
||||
if (dice_modal && dice_modal->is_dismissed()) {
|
||||
delete active_modal;
|
||||
active_modal = nullptr;
|
||||
needs_redraw = true;
|
||||
} else if (prop_modal && prop_modal->is_dismissed()) {
|
||||
if (prop_modal->wants_to_buy()) {
|
||||
const BoardTile* tile = &MONOPOLY_BOARD[p->position];
|
||||
p->balance -= tile->cost;
|
||||
p->properties_owned[p->property_count++] = p->position;
|
||||
}
|
||||
delete active_modal;
|
||||
active_modal = nullptr;
|
||||
needs_redraw = true;
|
||||
} else if (board_modal && board_modal->is_dismissed()) {
|
||||
delete active_modal;
|
||||
active_modal = nullptr;
|
||||
needs_redraw = true;
|
||||
@@ -75,56 +92,44 @@ bool MonopolyGame::update(const InputEvent& event) {
|
||||
break;
|
||||
case INPUT_BUTTON_1: // Select option
|
||||
switch (selected_action) {
|
||||
case 0: // Roll
|
||||
if (!has_rolled && !p->is_in_jail) {
|
||||
int dice1 = (rand() % 6) + 1;
|
||||
int dice2 = (rand() % 6) + 1;
|
||||
int total = dice1 + dice2;
|
||||
int old_pos = p->position;
|
||||
p->position = (p->position + total) % BOARD_SIZE;
|
||||
if (p->position < old_pos) p->balance += 200;
|
||||
has_rolled = true;
|
||||
needs_redraw = true;
|
||||
// Store dice values and show dice modal
|
||||
last_dice1 = dice1;
|
||||
last_dice2 = dice2;
|
||||
if (active_modal) delete active_modal;
|
||||
active_modal = new DiceModalGame(width, height, renderer, gui, input_manager, dice1, dice2);
|
||||
// Show property modal if landed on property/railroad/utility
|
||||
const BoardTile* landed = &MONOPOLY_BOARD[p->position];
|
||||
if (landed->type == TILE_PROPERTY || landed->type == TILE_RAILROAD || landed->type == TILE_UTILITY) {
|
||||
modal_property_index = p->position;
|
||||
// Queue property modal after dice modal is dismissed
|
||||
}
|
||||
// TODO: Handle doubles, jail, landing effects
|
||||
}
|
||||
break;
|
||||
case 1: // Buy
|
||||
if (has_rolled) {
|
||||
const BoardTile* tile = &MONOPOLY_BOARD[p->position];
|
||||
if ((tile->type == TILE_PROPERTY || tile->type == TILE_RAILROAD || tile->type == TILE_UTILITY) && p->balance >= tile->cost) {
|
||||
bool owned = false;
|
||||
for (int i = 0; i < p->property_count; ++i) {
|
||||
if (p->properties_owned[i] == p->position) owned = true;
|
||||
}
|
||||
if (!owned) {
|
||||
p->balance -= tile->cost;
|
||||
p->properties_owned[p->property_count++] = p->position;
|
||||
needs_redraw = true;
|
||||
case 0: // Context Action
|
||||
if (!has_rolled) {
|
||||
// Roll Dice
|
||||
if (!p->is_in_jail) {
|
||||
int dice1 = (rand() % 6) + 1;
|
||||
int dice2 = (rand() % 6) + 1;
|
||||
int total = dice1 + dice2;
|
||||
int old_pos = p->position;
|
||||
p->position = (p->position + total) % BOARD_SIZE;
|
||||
if (p->position < old_pos) p->balance += 200;
|
||||
has_rolled = true;
|
||||
needs_redraw = true;
|
||||
// Store dice values and show dice modal
|
||||
last_dice1 = dice1;
|
||||
last_dice2 = dice2;
|
||||
if (active_modal) delete active_modal;
|
||||
active_modal = new DiceModalGame(width, height, renderer, gui, input_manager, dice1, dice2, &MONOPOLY_BOARD[old_pos], &MONOPOLY_BOARD[p->position], players, players_count);
|
||||
// Show property modal if landed on property/railroad/utility
|
||||
const BoardTile* landed = &MONOPOLY_BOARD[p->position];
|
||||
if (landed->type == TILE_PROPERTY || landed->type == TILE_RAILROAD || landed->type == TILE_UTILITY) {
|
||||
modal_property_index = p->position;
|
||||
}
|
||||
}
|
||||
// TODO: Check for ownership by other players
|
||||
}
|
||||
break;
|
||||
case 2: // End Turn
|
||||
if (has_rolled) {
|
||||
} else {
|
||||
// End Turn
|
||||
current_player_idx = (current_player_idx + 1) % players_count;
|
||||
has_rolled = false;
|
||||
double_rolls = 0;
|
||||
just_sent_to_jail = false;
|
||||
selected_action = 0; // Reset selection for next player
|
||||
needs_redraw = true;
|
||||
}
|
||||
break;
|
||||
case 1: // View Board
|
||||
if (active_modal) delete active_modal;
|
||||
active_modal = new BoardModalGame(width, height, renderer, gui, input_manager, players, players_count);
|
||||
needs_redraw = true;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@@ -132,7 +137,22 @@ bool MonopolyGame::update(const InputEvent& event) {
|
||||
}
|
||||
// If dice modal was just dismissed and a property modal is queued, show it
|
||||
if (!active_modal && modal_property_index >= 0) {
|
||||
active_modal = new PropertyModalGame(width, height, renderer, gui, input_manager, &MONOPOLY_BOARD[modal_property_index]);
|
||||
// Evaluate ownership and affordability
|
||||
bool is_owned = false;
|
||||
const char* owner_name = nullptr;
|
||||
for (int i = 0; i < players_count; ++i) {
|
||||
for (int j = 0; j < players[i].property_count; ++j) {
|
||||
if (players[i].properties_owned[j] == modal_property_index) {
|
||||
is_owned = true;
|
||||
owner_name = players[i].name;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (is_owned) break;
|
||||
}
|
||||
bool can_afford = (p->balance >= MONOPOLY_BOARD[modal_property_index].cost);
|
||||
|
||||
active_modal = new PropertyModalGame(width, height, renderer, gui, input_manager, &MONOPOLY_BOARD[modal_property_index], is_owned, owner_name, can_afford, players, players_count);
|
||||
modal_property_index = -1;
|
||||
needs_redraw = true;
|
||||
}
|
||||
@@ -146,63 +166,50 @@ void MonopolyGame::draw() {
|
||||
active_modal->draw();
|
||||
return;
|
||||
}
|
||||
|
||||
renderer->clear_buffer();
|
||||
|
||||
// --- Draw Board Perimeter ---
|
||||
MonopolyBoardRenderer::draw_board_perimeter(renderer, width, height, players, players_count);
|
||||
|
||||
// --- Inner Dashboard (Center Area) ---
|
||||
int cw = width / 7;
|
||||
int ch = height / 7;
|
||||
int ix = cw + 2, iy = ch + 2;
|
||||
int iw = width - 2 * cw - 4, ih = height - 2 * ch - 4;
|
||||
|
||||
Player* p = &players[current_player_idx];
|
||||
const BoardTile* tile = &MONOPOLY_BOARD[p->position];
|
||||
|
||||
// Title
|
||||
renderer->draw_string_scaled(10, 10, "Monopoly", 3);
|
||||
|
||||
// --- Player Stats (Right Side) ---
|
||||
int stats_x = width - 200;
|
||||
int y = 40 + 15 * current_player_idx;
|
||||
// Stats Window in center
|
||||
char buf[128];
|
||||
// make a window showing player stats
|
||||
LowLevelWindow* stats_win = gui->draw_new_window(stats_x - 10, y - 10 , 190, 120, p->name);
|
||||
y += 20;
|
||||
renderer->draw_string_scaled(stats_x, y, "Location:", 1);
|
||||
renderer->draw_string_scaled(stats_x, y + 10, tile->name, 2);
|
||||
y += 30;
|
||||
// Money
|
||||
snprintf(buf, sizeof(buf), "$%d", p->balance);
|
||||
renderer->draw_string_scaled(stats_x, y, "Money:", 1);
|
||||
renderer->draw_string_scaled(stats_x, y + 10, buf, 2);
|
||||
y += 30;
|
||||
// Properties
|
||||
int prop_count = 0;
|
||||
for (int i = 0; i < p->property_count; ++i) {
|
||||
int prop_idx = p->properties_owned[i];
|
||||
if (prop_idx >= 0 && MONOPOLY_BOARD[prop_idx].type == TILE_PROPERTY) prop_count++;
|
||||
}
|
||||
snprintf(buf, sizeof(buf), "Properties: %d", prop_count);
|
||||
renderer->draw_string_scaled(stats_x, y, buf, 1);
|
||||
y += 10;
|
||||
// Monopoly count
|
||||
int monopoly_count = 0;
|
||||
// For each group, check if player owns all properties in group
|
||||
for (int group = 1; group <= 8; ++group) {
|
||||
int group_total = 0, group_owned = 0;
|
||||
for (int i = 0; i < BOARD_SIZE; ++i) {
|
||||
if (MONOPOLY_BOARD[i].type == TILE_PROPERTY && MONOPOLY_BOARD[i].group[0] == group) {
|
||||
group_total++;
|
||||
for (int j = 0; j < p->property_count; ++j) {
|
||||
if (p->properties_owned[j] == i) group_owned++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (group_total > 0 && group_total == group_owned) monopoly_count++;
|
||||
}
|
||||
snprintf(buf, sizeof(buf), "Monopolies: %d", monopoly_count);
|
||||
renderer->draw_string_scaled(stats_x, y, buf, 1);
|
||||
renderer->draw_string_scaled(ix + 5, iy + 5, "Monopoly", 2);
|
||||
|
||||
int content_y = iy + 25;
|
||||
snprintf(buf, sizeof(buf), "TURN: %s", p->name);
|
||||
renderer->draw_string_scaled(ix + 5, content_y, buf, 1);
|
||||
content_y += 12;
|
||||
|
||||
snprintf(buf, sizeof(buf), "BAL: $%d", p->balance);
|
||||
renderer->draw_string_scaled(ix + 5, content_y, buf, 1);
|
||||
content_y += 12;
|
||||
|
||||
snprintf(buf, sizeof(buf), "POS: %s", tile->name);
|
||||
renderer->draw_string_scaled(ix + 5, content_y, buf, 1);
|
||||
content_y += 15;
|
||||
|
||||
// Draw action menu
|
||||
const char* actions[ACTION_COUNT];
|
||||
if (!has_rolled) {
|
||||
actions[0] = "Roll Dice";
|
||||
} else {
|
||||
actions[0] = "End Turn";
|
||||
}
|
||||
actions[1] = "View Board";
|
||||
|
||||
// Draw action menu (highlight selected)
|
||||
const char* actions[ACTION_COUNT] = {"Roll Dice", "Buy Property", "End Turn"};
|
||||
for (int i = 0; i < ACTION_COUNT; ++i) {
|
||||
int y = height - 80 + i * 20;
|
||||
snprintf(buf, sizeof(buf), "%s%s", (i == selected_action) ? "> " : " ", actions[i]);
|
||||
renderer->draw_string_scaled(10, y, buf, 2);
|
||||
renderer->draw_string_scaled(ix + 5, content_y, buf, 1);
|
||||
content_y += 12;
|
||||
}
|
||||
|
||||
// TODO: Draw board, all players, property ownership, jail, chance, etc.
|
||||
// TODO: Add win/lose/game over conditions
|
||||
// TODO: Add touch support, more UI, etc.
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user