Files
basic1/games/monopoly/PropertyModalGame.h
T
2026-01-31 22:23:49 -05:00

196 lines
8.7 KiB
C++

// PropertyModalGame.h
#pragma once
#include "../../lib/game.h"
#include "../../display/low_level_render.h"
#include "../../display/low_level_gui.h"
#include "input_manager.h"
#include "monopoly_board.h"
#include "player.h"
#include "MonopolyBoardRenderer.h"
class PropertyModalGame : public Game {
const BoardTile* property;
bool dismissed;
bool is_owned;
const char* owner_name;
int owner_id; // -1 if not owned
bool can_afford;
int selected_choice; // 0: Buy, 1: Cancel
bool buy_requested;
bool rent_requested;
Player* players;
int players_count;
public:
PropertyModalGame(uint16_t width, uint16_t height, LowLevelRenderer* renderer, LowLevelGUI* gui, InputManager* input_manager, const BoardTile* prop, bool owned, const char* owner, int o_id, bool affordable, Player* p_list = nullptr, int p_count = 0)
: Game(width, height, renderer, gui, input_manager), property(prop), dismissed(false), is_owned(owned), owner_name(owner), owner_id(o_id), can_afford(affordable), selected_choice(0), buy_requested(false), rent_requested(false), players(p_list), players_count(p_count) {
if (is_owned || !can_afford) selected_choice = 1;
}
void init() override { dismissed = false; buy_requested = false; rent_requested = false; selected_choice = 0; }
Type get_type() const override { return Type::MONOPOLY_PROPERTY; }
bool update(const InputEvent& event) override {
// If rent is owed, any button pays it (it's forced)
if (is_owned && owner_id != -1) {
if (event.type == INPUT_BUTTON_0 || event.type == INPUT_BUTTON_1) {
rent_requested = true;
dismissed = true;
return true;
}
return false;
}
// Otherwise (Buy/Pass), use A to cycle and B to select
if (event.type == INPUT_BUTTON_0) { // BUTTON A -> Change selection
if (!is_owned && can_afford) {
selected_choice = (selected_choice + 1) % 2;
return true;
}
}
if (event.type == INPUT_BUTTON_1) { // BUTTON B -> Select action
if (!is_owned && can_afford) {
if (selected_choice == 0) buy_requested = true;
dismissed = true;
return true;
} else {
// Only option was Pass
dismissed = true;
return true;
}
}
return false;
}
void draw() override {
renderer->clear_buffer();
int win_w = 160;
int win_h = 160;
int win_x = (width - win_w) / 2;
int win_y = (height - win_h) / 2;
char buf[128];
if (players && players_count > 0) {
// Find current player position (who is interacting with the modal)
// In MonopolyGame, p is players[current_player_idx]
// We don't have the idx here directly, but we can highlight the property itself
int property_idx = -1;
for(int i=0; i<40; i++) if(&MONOPOLY_BOARD[i] == property) property_idx = i;
MonopolyBoardRenderer::draw_board_perimeter(renderer, width, height, players, players_count, property_idx);
}
// Window background (White box)
renderer->draw_filled_rectangle(win_x, win_y, win_w, win_h, false, 0); // Clear background
renderer->draw_rectangle(win_x, win_y, win_w, win_h, true, 2);
renderer->draw_rectangle(win_x + 3, win_y + 3, win_w - 6, win_h - 6, true, 1);
// Header Title Bar
renderer->draw_filled_rectangle(win_x + 4, win_y + 4, win_w - 8, 30, true, 1);
renderer->set_text_color(false); // White text
snprintf(buf, sizeof(buf), "%s", property->name);
renderer->draw_string_scaled(win_x + (win_w - (int)strlen(buf) * 6) / 2, win_y + 8, buf, 1);
renderer->set_text_color(true);
// Subtitle (Type)
const char* type_str = "PROPERTY";
if (property->type == TILE_RAILROAD) type_str = "RAILROAD";
else if (property->type == TILE_UTILITY) type_str = "UTILITY";
snprintf(buf, sizeof(buf), "%s", type_str);
renderer->draw_string_scaled(win_x + (win_w - (int)strlen(buf) * 6) / 2, win_y + 40, buf, 1);
// Info box center
int info_y = win_y + 60;
// Price
snprintf(buf, sizeof(buf), "PRICE: $%d", property->cost);
renderer->draw_string_scaled(win_x + 15, info_y, buf, 1);
info_y += 15;
// Rent
if (property->type == TILE_PROPERTY) {
snprintf(buf, sizeof(buf), "RENT: $%d", property->rent[0]);
} else if (property->type == TILE_UTILITY) {
snprintf(buf, sizeof(buf), "RENT: 4x DICE");
} else if (property->type == TILE_RAILROAD) {
snprintf(buf, sizeof(buf), "RENT: $25");
}
renderer->draw_string_scaled(win_x + 15, info_y, buf, 1);
info_y += 15;
// Owner
if (is_owned && owner_name) {
snprintf(buf, sizeof(buf), "OWNER: %s", owner_name);
} else {
snprintf(buf, sizeof(buf), "OWNER: %s", is_owned ? "PLAYER" : "BANK");
}
renderer->draw_string_scaled(win_x + 15, info_y, buf, 1);
// Action Buttons
int btn_y = win_y + win_h - 60;
int btn_w = win_w - 30;
int btn_h = 25;
if (is_owned && owner_id != -1) {
// Option: Pay Rent (A or B)
renderer->draw_filled_rectangle(win_x + 15, btn_y, btn_w, btn_h, true, 1);
renderer->set_text_color(false);
int rent = 0;
if (property->type == TILE_PROPERTY) {
rent = property->rent[0];
} else if (property->type == TILE_RAILROAD) {
// Calculate Railroad rent based on owner's count
int rr_count = 0;
if (owner_id != -1 && owner_id < players_count) {
for (int i = 0; i < players[owner_id].property_count; ++i) {
// Find the property in the global board to check type (or just trust the index)
int prop_idx = players[owner_id].properties_owned[i];
if (MONOPOLY_BOARD[prop_idx].type == TILE_RAILROAD) {
rr_count++;
}
}
}
if (rr_count == 1) rent = 25;
else if (rr_count == 2) rent = 50;
else if (rr_count == 3) rent = 100;
else if (rr_count == 4) rent = 200;
else rent = 25; // Fallback
} else if (property->type == TILE_UTILITY) {
// Simplified 40 or use a more complex check
rent = 40;
}
snprintf(buf, sizeof(buf), ">PAY RENT ($%d)", rent);
renderer->draw_string_scaled(win_x + (win_w - (int)strlen(buf) * 6) / 2, btn_y + 8, buf, 1);
renderer->set_text_color(true);
} else if (!is_owned && can_afford) {
// Choice: Buy (A) or Pass (B). Changed to: A cycles, B selects.
// Buy Button
if (selected_choice == 0) renderer->draw_filled_rectangle(win_x + 15, btn_y, btn_w, btn_h, true, 1);
else renderer->draw_rectangle(win_x + 15, btn_y, btn_w, btn_h, true, 1);
if (selected_choice == 0) renderer->set_text_color(false);
snprintf(buf, sizeof(buf), "%sBUY ($%d)", (selected_choice == 0 ? "> " : " "), property->cost);
renderer->draw_string_scaled(win_x + 20, btn_y + 8, buf, 1);
renderer->set_text_color(true);
btn_y += 30;
// Pass Button
if (selected_choice == 1) renderer->draw_filled_rectangle(win_x + 15, btn_y, btn_w, btn_h, true, 1);
if (selected_choice == 1) renderer->set_text_color(false);
snprintf(buf, sizeof(buf), "%sPASS", (selected_choice == 1 ? "> " : " "));
renderer->draw_string_scaled(win_x + 20, btn_y + 8, buf, 1);
renderer->set_text_color(true);
// Helpful hint
renderer->draw_string_scaled(win_x + 15, win_y + win_h - 15, "A:Next B:Sel", 1);
} else {
// Only one option: PASS (B) (e.g. if owned by self or can't afford)
renderer->draw_filled_rectangle(win_x + 15, btn_y, btn_w, btn_h, true, 1);
renderer->set_text_color(false);
renderer->draw_string_scaled(win_x + 25, btn_y + 8, ">B PASS", 1);
renderer->set_text_color(true);
}
}
bool is_dismissed() const { return dismissed; }
bool wants_to_buy() const { return buy_requested; }
bool wants_to_pay_rent() const { return rent_requested; }
int get_owner_id() const { return owner_id; }
};