summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorStephen Enders <smenders@gmail.com>2021-01-28 20:47:14 -0500
committerStephen Enders <smenders@gmail.com>2021-01-28 20:47:14 -0500
commit6efca6678e777edacfc0bae4bf9f201d22a73860 (patch)
treeb2259d98445ceb0037cbf069240f1e450ca73e61 /src
parent0999ff296676655718241221da28b6afa9cbb166 (diff)
Allow turn passing on 0 roll
Diffstat (limited to 'src')
-rw-r--r--src/helper.cpp22
-rw-r--r--src/helper.hpp9
-rw-r--r--src/ur.cpp47
3 files changed, 68 insertions, 10 deletions
diff --git a/src/helper.cpp b/src/helper.cpp
index 11374e8..ff5ed61 100644
--- a/src/helper.cpp
+++ b/src/helper.cpp
@@ -228,6 +228,28 @@ createRollSprites(sf::Texture& t1, sf::Texture& t2)
return sprites;
}
+std::shared_ptr<std::vector<sf::Sprite>>
+createPassSprites(std::shared_ptr<std::vector<sf::Texture>> textures)
+{
+ auto sprites = std::make_shared<std::vector<sf::Sprite>>();
+
+ for (int i = 0; i < 3; i++) {
+ sprites->push_back(sf::Sprite((*textures)[PASS_TILES[i]]));
+ }
+ return sprites;
+}
+
+std::shared_ptr<std::vector<sf::Sprite>>
+createStartSprites(std::shared_ptr<std::vector<sf::Texture>> textures)
+{
+ auto sprites = std::make_shared<std::vector<sf::Sprite>>();
+
+ for (int i = 0; i < 3; i++) {
+ sprites->push_back(sf::Sprite((*textures)[START_TILES[i]]));
+ }
+ return sprites;
+}
+
void
makeNum(sf::Sprite* sprite_ptr,
int num,
diff --git a/src/helper.hpp b/src/helper.hpp
index 2ff8b24..41383d1 100644
--- a/src/helper.hpp
+++ b/src/helper.hpp
@@ -31,6 +31,8 @@ static const int DIE_0 = 17;
static const int DIE_1 = 16;
static const int NUMS_TILES[8] = { 8, 9, 10, 11, 12, 13, 14, 15 };
static const int ROLL_TILES[2] = { 20, 21 };
+static const int PASS_TILES[3] = { 24, 25, 26 };
+static const int START_TILES[3] = { 27, 28, 29 };
static const char* TITLE = "Royal Game of Ur";
static const sf::Color GLOBAL_MASK(255, 0, 255, 255);
@@ -39,6 +41,7 @@ enum GameState
{
WAITING,
ROLLING,
+ PASSING,
PLACING,
GAME_OVER
};
@@ -93,6 +96,12 @@ createAllDice(sf::Texture& die0Texture, sf::Texture& die1Texture);
std::shared_ptr<std::vector<sf::Sprite>>
createRollSprites(sf::Texture& t1, sf::Texture& t2);
+std::shared_ptr<std::vector<sf::Sprite>>
+createPassSprites(std::shared_ptr<std::vector<sf::Texture>> textures);
+
+std::shared_ptr<std::vector<sf::Sprite>>
+createStartSprites(std::shared_ptr<std::vector<sf::Texture>> textures);
+
void
makeNum(sf::Sprite* sprite_ptr,
int num,
diff --git a/src/ur.cpp b/src/ur.cpp
index f406623..fac0a5f 100644
--- a/src/ur.cpp
+++ b/src/ur.cpp
@@ -41,9 +41,11 @@ next(int* i, int max)
}
inline void
-next_turn()
+next_turn(std::shared_ptr<std::vector<sf::Sprite>> roll_sprites)
{
+ turn_roll = 0;
turn_pid = turn_pid == P1_ID ? P2_ID : P1_ID;
+ for (auto& s : (*roll_sprites)) s.setColor(sf::Color::White);
change_state(GameState::WAITING);
}
@@ -63,6 +65,7 @@ inline void
render_dice(sf::RenderWindow* window,
std::shared_ptr<std::vector<struct dice_t>> dice,
std::shared_ptr<std::vector<sf::Sprite>> roll_sprites,
+ std::shared_ptr<std::vector<sf::Sprite>> pass_sprites,
std::shared_ptr<std::vector<sf::Texture>> textures,
sf::Sprite* roll_result,
ur::TimedLatch* animation_timer,
@@ -71,7 +74,6 @@ render_dice(sf::RenderWindow* window,
if (animation_timer->is_completed()) {
animation_timer->reset();
- change_state(GameState::PLACING);
int rolls[4] = {
dice_rand.next(), dice_rand.next(), dice_rand.next(), dice_rand.next()
};
@@ -81,8 +83,15 @@ render_dice(sf::RenderWindow* window,
die.show = die.value == rolls[i / 2];
}
// set roll result
- for (int r : rolls)
+ for (int r : rolls) {
turn_roll += r;
+ }
+ makeNum(roll_result, turn_roll, textures);
+ if (turn_roll == 0) {
+ change_state(GameState::PASSING);
+ } else {
+ change_state(GameState::PLACING);
+ }
}
// draw dice
@@ -99,10 +108,16 @@ render_dice(sf::RenderWindow* window,
roll_c = dice_c;
}
- if (state == GameState::PLACING) {
+ if (state == GameState::PLACING || state == GameState::PASSING) {
// draw roll text
- makeNum(roll_result, turn_roll, textures);
window->draw(*roll_result);
+ if (turn_roll == 0) {
+ int psi = SPRITE_COLS / 2 - 1;
+ for (auto& ps : (*pass_sprites)) {
+ ps.setPosition(pos(psi++, SPRITE_ROWS / 2));
+ window->draw(ps);
+ }
+ }
} else if (state == GameState::ROLLING) {
// if completed update dice sprites
if (animation_frame_timer->is_completed()) {
@@ -175,6 +190,12 @@ main()
const std::shared_ptr<std::vector<struct dice_t>> dice =
createAllDice((*textures)[DIE_0], (*textures)[DIE_1]);
+ const std::shared_ptr<std::vector<sf::Sprite>> pass_sprites =
+ createPassSprites(textures);
+
+ const std::shared_ptr<std::vector<sf::Sprite>> start_sprites =
+ createStartSprites(textures);
+
sf::Sprite p1Score;
p1Score.setPosition(pos(0, SPRITE_ROWS - 1));
makeNum(&p1Score, 0, textures);
@@ -261,6 +282,14 @@ main()
break;
}
}
+ } else if (state == GameState::PASSING) {
+ for (auto& s : (*pass_sprites)) {
+ // zoom sprite bounds
+ if (s.getGlobalBounds().contains(mPos)) {
+ next_turn(roll_sprites);
+ break;
+ }
+ }
}
window.setView(window.getDefaultView()); // reset back to main view
} else if (!sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) {
@@ -311,11 +340,7 @@ main()
}
if (in_place) {
- next_turn();
- turn_roll = 0;
- for (auto& s : (*roll_sprites)) {
- s.setColor(sf::Color::White);
- }
+ next_turn(roll_sprites);
} else {
grabbed_piece->sprite.setPosition(grabbed_piece_origin);
}
@@ -337,6 +362,7 @@ main()
float y = mPos.y - (grabbed_piece->sprite.getGlobalBounds().height / 2);
grabbed_piece->sprite.setPosition(x, y);
}
+
// draw pieces (draw own pieces last to ensure "on top")
if (p1_turn()) {
for (auto& p : *(p2->pieces)) {
@@ -357,6 +383,7 @@ main()
render_dice(&window,
dice,
roll_sprites,
+ pass_sprites,
textures,
&roll_result,
&rolling_animation_timer,