Skip to content

Commit

Permalink
polish, fix some checks
Browse files Browse the repository at this point in the history
  • Loading branch information
ediardi committed Jun 19, 2024
1 parent 7de6266 commit ee960ce
Show file tree
Hide file tree
Showing 15 changed files with 65 additions and 52 deletions.
2 changes: 1 addition & 1 deletion CapturedNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CapturedNode: public Node {
public:
explicit CapturedNode(Node& node,Capturer& capturer1): Node(node), capturer(capturer1){};

bool is_occupied_by(Capturer &cap) override{
bool is_occupied_by(Capturer &) override{
return false;
}
Capturer& get_capturer()
Expand Down
8 changes: 7 additions & 1 deletion Capturer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ bool Capturer::move(Node &node, std::vector<Node *> &graph) {
graph[node.get_index_in_graph()] = node.new_state_on_move(*this,node);
graph[node.get_index_in_graph()]->update_color();
current_pos=node.get_index_in_graph();
Drawables::move_pawn(getId(), *graph[node.get_index_in_graph()]);
return true;
}
catch(AlreadyOccupiedError &err){
Expand All @@ -22,7 +23,7 @@ bool Capturer::move(Node &node, std::vector<Node *> &graph) {
}
}

std::vector<int>& neighbours= node.get_neighbours();
const std::vector<int>& neighbours= node.get_neighbours();
int valid= -1;
for(auto index:neighbours)
{
Expand All @@ -38,6 +39,7 @@ bool Capturer::move(Node &node, std::vector<Node *> &graph) {
graph[node.get_index_in_graph()] = node.new_state_on_move(*this,node);
graph[node.get_index_in_graph()]->update_color();
current_pos=node.get_index_in_graph();
Drawables::move_pawn(getId(), *graph[node.get_index_in_graph()]);
Node* temp=graph[valid];
graph[valid] = temp->new_state_on_removed(*this,*temp);
graph[valid]->update_color();
Expand Down Expand Up @@ -66,3 +68,7 @@ const sf::Color &Capturer::getCapturedCol() const {
const sf::Color &Capturer::getOccupiedCol() const {
return occupied_col;
}

int Capturer::getId() const {
return id;
}
16 changes: 13 additions & 3 deletions Capturer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,32 @@

#include <vector>
#include <SFML/Graphics.hpp>
#include "Drawables.h"

class Node;

class Capturer {
static inline int id_c = 0;
int id;
public:
[[nodiscard]] int getId() const;

private:
int current_pos;
sf::Color captured_col,occupied_col;
public:
const sf::Color &getCapturedCol() const;
[[nodiscard]] const sf::Color &getCapturedCol() const;

const sf::Color &getOccupiedCol() const;
[[nodiscard]] const sf::Color &getOccupiedCol() const;

public:
[[nodiscard]] int getCurrentPos() const;

public:
explicit Capturer(sf::Color cap_col,sf::Color occ_col):current_pos(-1),captured_col(cap_col),occupied_col(occ_col){}
explicit Capturer(sf::Color cap_col,sf::Color occ_col):id(id_c),current_pos(-1),captured_col(cap_col),occupied_col(occ_col){
id_c++;
Drawables::add(sf::Vertex(),occupied_col);
}
bool move(Node& node, std::vector<Node *> &graph);
};

Expand Down
25 changes: 22 additions & 3 deletions Drawables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@
//

#include "Drawables.h"
#include "Node.h"


void Drawables::add(sf::Vertex x) {
points.append(x);
void Drawables::add(sf::Vertex x,sf::Color col,float radius) {
sf::CircleShape circle(radius,30);
circle.setOrigin(radius,radius);
circle.setPosition(x.position.x,x.position.y);
circle.setOutlineColor(col);
circle.setFillColor(col);
circle.setOutlineThickness(1);
int new_index=(int)pawns.size();
pawns[new_index]=circle;
}

int Drawables::add_node(Point origin, float radius=10) {
Expand All @@ -30,7 +38,6 @@ void Drawables::draw(sf::RenderTarget &target, sf::RenderStates states) const {
// to do: delete (placed here to avoid unused warning)
states.texture=states.texture;
//
target.draw(points);

target.draw(lines);

Expand All @@ -39,6 +46,14 @@ void Drawables::draw(sf::RenderTarget &target, sf::RenderStates states) const {
auto circle =item.second;
target.draw(circle);
}

for(const auto &item:pawns)
{
auto circle =item.second;
target.draw(circle);
}

target.draw(points);
}

void Drawables::change_circle_color(const int index, const sf::Color new_color) {
Expand All @@ -52,6 +67,10 @@ void Drawables::add_line(Line l) {
lines.append(y);
}

void Drawables::move_pawn(int index, const Node& x) {
pawns[index].setPosition( x.to_vertex().position );
}


/*sf::Color Drawables::get_circle_color(const int index) {
return circles[index].getOutlineColor();
Expand Down
6 changes: 5 additions & 1 deletion Drawables.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@
#include "Point.h"
#include "Line.h"

class Node;


class Drawables : public sf::Drawable{
private:
static sf::VertexArray points;
static sf::VertexArray lines;
static std::unordered_map<int, sf::CircleShape> circles;
static std::unordered_map<int, sf::CircleShape> pawns;
public:
static void add(sf::Vertex x);
static void add(sf::Vertex x,sf::Color col,float radius = 5);
static void move_pawn(int index, const Node& x);
static int add_node(Point origin, float radius);
static void add_line(Line l);
//static void change_circle_color(sf::CircleShape& reference,sf::Color color);
Expand Down
5 changes: 2 additions & 3 deletions GameStateManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ bool GameStateManager::has_ended() {
return false;
}

/*
void GameStateManager::evaluate() {
state=showing_result;
}


}*/

9 changes: 5 additions & 4 deletions GameStateManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
class GameStateManager {
std::ifstream fin;
MapGenerator& map;
int n{},index,points=0, frames_to_show_results=0;
int n{},index,points=0;
//int frames_to_show_results=0;
const int results_max_frames = 45;
void next_level();
void evaluate();
bool replay_level=false;
//void next_level();
//void evaluate();
//bool replay_level=false;
enum State{
awaiting_point,
showing_result,
Expand Down
26 changes: 0 additions & 26 deletions Line.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,6 @@ Point Line::endpoint() { return b;}

Line::Line(Point a1, Point b1) : a(a1),b(b1) {}

bool Line::intersects(Line line) const{
// credit goes to https://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect
// Returns 1 if the lines intersect, otherwise 0. In addition, if the lines
// p0= a
// p1 =b
// p2 =line.a
// p3 =line.b
{
float s1_x, s1_y, s2_x, s2_y;
s1_x = b.getx() - a.getx(); s1_y = b.gety() - a.gety();
s2_x = line.b.getx() - line.a.getx(); s2_y = line.b.gety() - line.a.gety();

float s, t;
s = (-s1_y * (a.getx() - line.a.getx()) + s1_x * (a.gety() - line.a.gety())) / (-s2_x * s1_y + s1_x * s2_y);
t = ( s2_x * (a.gety() - line.a.gety()) - s2_y * (a.getx() - line.a.getx())) / (-s2_x * s1_y + s1_x * s2_y);

if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
{
// Collision detected
return true;
}

return false; // No collision
}
}

float Line::get_length() const{
float length_x= b.getx() - a.getx();
float length_y= b.gety() - a.gety();
Expand Down
1 change: 0 additions & 1 deletion Line.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class Line {
Point startpoint();
Point endpoint();
Line(Point a1,Point b1);
[[nodiscard]] bool intersects(Line line) const;
[[nodiscard]] float get_length() const;
friend std::ostream& operator<<(std::ostream& os, const Line& line);
};
Expand Down
2 changes: 1 addition & 1 deletion Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ Node *Node::new_state_on_move(Capturer &cap, Node &node) {
return new OccupiedNode(node, cap);
}

Node *Node::new_state_on_removed(Capturer &cap, Node &node) {
Node *Node::new_state_on_removed(Capturer &, Node &) {
throw MovedFromEmptyTile("This tile has no piece to move");
}
2 changes: 1 addition & 1 deletion Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Node : public Point, public CapturableEntity{
Drawables::change_circle_color(index,new_color);
}

bool is_occupied_by(Capturer &cap) override{
bool is_occupied_by(Capturer &) override{
return false;
}

Expand Down
6 changes: 3 additions & 3 deletions OccupiedNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ class OccupiedNode: public CapturedNode {
}
return false;
}
Node * new_state_on_move(Capturer &cap, Node &node) override{
Node * new_state_on_move(Capturer &, Node &) override{
throw AlreadyOccupiedError("Tile already occupied");
}
Node * new_state_on_removed(Capturer &cap, Node &node) override{
Node * new_state_on_removed(Capturer &, Node &node) override{
auto upnode=dynamic_cast<CapturedNode*>(&node);
auto this_node= new CapturedNode(*upnode);
return this_node;
}
void update_color() override{
Drawables::change_circle_color(index,occupier.getOccupiedCol());
Drawables::change_circle_color(index,get_capturer().getCapturedCol());
}
};

Expand Down
4 changes: 2 additions & 2 deletions Point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ Point::Point(int x,int y)
this->x= static_cast<float>(x);
this->y= static_cast<float>(y);
}
void Point::setx(float new_x){this->x=new_x;}
void Point::sety(float new_y){this->y=new_y;}
//void Point::setx(float new_x){this->x=new_x;}
//void Point::sety(float new_y){this->y=new_y;}
float Point::getx() const{return x;}
float Point::gety() const{return y;}
sf::Vertex Point::to_vertex() const
Expand Down
4 changes: 2 additions & 2 deletions Point.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class Point {
Point();
Point(int x,int y);
Point(float x,float y);
void setx(float new_x);
void sety(float new_y);
//void setx(float new_x);
//void sety(float new_y);
[[nodiscard]] float getx() const;
[[nodiscard]] float gety() const;
[[nodiscard]] sf::Vertex to_vertex() const;
Expand Down
1 change: 1 addition & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
sf::VertexArray Drawables::points;
sf::VertexArray Drawables::lines(sf::Lines,0);
std::unordered_map<int,sf::CircleShape> Drawables::circles;
std::unordered_map<int,sf::CircleShape> Drawables::pawns;


int main() {
Expand Down

0 comments on commit ee960ce

Please sign in to comment.