summaryrefslogtreecommitdiff
path: root/helper.cpp
blob: c16cb3a6cf61f0cd3dcb741e72e749aad01c5b60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "helper.h"
#include <iostream>

std::shared_ptr<std::vector<sf::Texture>>
loadTextures(const char* path)
{
  int sprite_width = SPRITE_SIZE, sprite_height = SPRITE_SIZE;
  sf::Image textureAtlas;
  if (!textureAtlas.loadFromFile(path)) {
    std::cerr << "Unable to load textures from file: " << path << std::endl;
    throw std::runtime_error("Unable to load spritesheet");
  }

  textureAtlas.createMaskFromColor(GLOBAL_MASK);
  auto imageSize = textureAtlas.getSize();

  auto textures = std::make_shared<std::vector<sf::Texture>>();

  for (int y = 0; y < imageSize.y; y += sprite_height) {
    for (int x = 0; x < imageSize.x; x += sprite_width) {
      sf::Texture t;
      t.loadFromImage(textureAtlas,
                      sf::IntRect(x, y, sprite_width, sprite_height));
      textures->push_back(t);
    }
  }
  return textures;
}

sf::Font loadFont()
{
  sf::Font font;
  if (!font.loadFromFile("./res/DejaVuSansMono.ttf"))
  {
    std::cerr << "Unable to load font" << std::endl;
    throw std::runtime_error("Unable to load font");
  }
  return font;
}

std::shared_ptr<struct piece_t>
createPiece(int id, sf::Texture texture)
{
  std::shared_ptr<sf::Sprite> s = std::make_shared<sf::Sprite>();
  s->setTexture(texture);

  auto p = std::make_shared<struct piece_t>();
  p->id = id;
  p->sprite = s;

  return p;
}

std::shared_ptr<struct player_t>
createPlayer(sf::Texture texture)
{
  std::shared_ptr<struct player_t> player = std::make_shared<struct player_t>();
  player->score = 0;
  player->pieces =
    std::make_shared<std::vector<std::shared_ptr<struct piece_t>>>();
  for (int i = 0; i < NUM_PIECES; i++) {
    player->pieces->push_back(createPiece(i + 1, texture));
  }

  return player;
}

bool
clickedPiece(sf::Vector2i mousePosition, struct piece_t* piece)
{
  return piece->sprite->getGlobalBounds().contains(mousePosition.x,
                                                   mousePosition.y);
}

bool
canMovePiece(
  struct piece_t* piece,
  int roll,
  std::shared_ptr<std::vector<std::shared_ptr<struct piece_t>>> myPieces,
  std::shared_ptr<std::vector<std::shared_ptr<struct piece_t>>> enemyPieces)
{
  int next = piece->position + roll;

  // rolled passed the exit
  if (next > EXIT_SPACE) {
    return false;
  }

  // colliding with another piece
  for (std::shared_ptr<struct piece_t> p : (*myPieces)) {
    // cannot move onto your own piece
    if (p->id != piece->id && p->position == next) {
      return false;
    }
  }

  // can't attack in safe square
  for (std::shared_ptr<struct piece_t> p : (*enemyPieces)) {
    // cannot move onto a protected enemy piece
    if (next == SAFE_SPACE && p->position == SAFE_SPACE) {
      return false;
    }
  }

  return true;
}