-
Notifications
You must be signed in to change notification settings - Fork 0
/
pawn.cpp
executable file
·150 lines (133 loc) · 3.84 KB
/
pawn.cpp
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "pawn.h"
#include "const.h"
#include "base/func_tool.h"
#include <SDL/SDL_gfxPrimitives.h>
#include <cmath>
Piece* Piece::selected(NULL);
Piece* Piece::moving(NULL);
SDL_Rect Piece::board_top_left;
int** Piece::board(NULL);
int* Piece::game_turn(NULL);
int Piece::diameter(32);
int Piece::instance(0);
int Piece::move_cnt[2] = { 0 };
Piece::Piece(int _x, int _y, bool color) : r_x(_x), r_y(_y), state(IDLE), x_vel(0), y_vel(0), acceleration(-0.015), moved(false) {
instance++;
rect.w = rect.h = diameter;
image = createSurface(diameter, diameter);
shadow = createSurface(diameter, diameter);
light = createSurface(diameter, diameter);
if (!(image and shadow and light)) {
std::cerr << "Erreur lors de la création d'un pion!";
exit(EXIT_FAILURE);
}
filledCircleColor(light, 0.5*diameter, 0.5*diameter, 0.5*diameter,0x5a74a946);
filledCircleColor(shadow, 0.5*diameter, 0.5*diameter, 0.5*diameter, PAWN_SHADOW);
filledCircleColor(image, 0.5*diameter, 0.5*diameter, 0.5*diameter, color?WHITE_PAWN:BLACK_PAWN);
zoom = createSurface(diameter+2, diameter+2);
filledCircleColor(zoom, 0.5*zoom->w, 0.5*zoom->w, 0.5*zoom->w, color?WHITE_PAWN:BLACK_PAWN);
circleColor(image, 0.5*diameter, 0.5*diameter, 0.5*diameter, 0xeeeeee55);
}
Piece::~Piece() {
instance--;
move_cnt[0] = move_cnt[1] = 0;
SDL_FreeSurface(shadow);
SDL_FreeSurface(light);
SDL_FreeSurface(zoom);
zoom = shadow = light = NULL;
}
float dist(int x1, int y1, int x2, int y2)
{ return std::sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)); }
void Piece::update() {
if (state == MOVING) {
SDL_Rect r(get_SDL_coord(d_x, d_y));
/* can't get higher precision than this */
if (dist(std::ceil(x), std::ceil(y), r.x, r.y) > 2) {
x += x_vel;
y += y_vel;
x_vel += acceleration*sgn(x_vel);
y_vel += acceleration*sgn(y_vel);
} else {
x_vel = y_vel = 0;
r_x = d_x;
r_y = d_y;
state = IDLE;
moving = NULL;
}
} else {
SDL_Rect r(get_SDL_coord(r_x, r_y));
x = r.x;
y = r.y;
}
}
SDL_Rect Piece::get_SDL_coord(int rx, int ry) {
SDL_Rect ret = { Sint16(ry*0.5*board_top_left.w - 0.5*diameter + board_top_left.x),
Sint16(rx*0.5*board_top_left.h - 0.5*diameter + board_top_left.y) };
return ret;
}
void Piece::draw(SDL_Surface* screen) {
SDL_Rect pos = { Sint16(x+5), Sint16(y+5) };
SDL_BlitSurface(shadow, NULL, screen, &pos);
SDL_Surface* tmp(image);
if (state == MOVING)
image = zoom;
GameObject::draw(screen);
image = tmp;
if (state == SELECTED) {
pos.x = Sint16(x);
pos.y = Sint16(y);
SDL_BlitSurface(light, NULL, screen, &pos);
}
}
void Piece::bump(const std::string& flag) {
if (flag == "unselect")
state = IDLE;
else {
state = SELECTED;
selected = this;
}
}
void Piece::unselect() {
selected = NULL;
}
void Piece::move(int xDest, int yDest) {
if (state != MOVING) {
if (valid(r_x, r_y, xDest, yDest)) {
(*game_turn) = !(*game_turn);
if (!moved) {
moved = true;
move_cnt[board[r_x][r_y]]++;
}
state = MOVING;
moving = this;
std::swap(board[r_x][r_y], board[xDest][yDest]);
d_x = xDest;
d_y = yDest;
x_vel = 2*sgn(d_y - r_y);
y_vel = 2*sgn(d_x - r_x);
}
else
state = IDLE;
}
}
bool Piece::valid(int x0, int y0, int xDest, int yDest) {
if (xDest < 0 or yDest < 0 or xDest >= 3 or yDest >= 3 or (x0 == xDest and y0 == yDest))
return false;
if (board[xDest][yDest] == black or board[xDest][yDest] == white)
return false;
float d(dist(x0, y0, xDest, yDest));
if (1>d or d>std::sqrt(2))
return false;
/* diagonal move */
if (sgn(xDest - x0) and sgn(yDest - y0))
return x0%2 == y0%2;
return true;
}
int Piece::get_x() const
{ return r_x; }
int Piece::get_y() const
{ return r_y; }
bool Piece::ready()
{ return instance >= 15; }
bool Piece::allMoved(int color)
{ return ((color>=2)?false:(move_cnt[color] == 3)); }