Commit b20f107f authored by Colomban Wendling's avatar Colomban Wendling

Move the pawn-related signals from game to board

This gives better consistency by having tile and pawn related signals
next to each other, as well as making the board more self-sufficient
with it getting signals for all changes on it.
parent fe7ff5de
......@@ -688,28 +688,6 @@ on_game_state_changed (OVCCGame *game,
gtk_action_set_sensitive (app->actions.game.rotate_tile_counterclockwise, started);
}
static void
game_pawn_placed_handler (OVCCGame *game,
OVCCPawn *pawn,
OVCCTileObject *obj,
OVCCPosition *pos,
App *app)
{
/* for now just redraw the position */
ovccgtk_board_queue_position_update (OVCCGTK_BOARD (app->board), pos);
}
static void
game_pawn_removed_handler (OVCCGame *game,
OVCCPawn *pawn,
OVCCTileObject *obj,
OVCCPosition *pos,
App *app)
{
/* for now just redraw the position */
ovccgtk_board_queue_position_update (OVCCGTK_BOARD (app->board), pos);
}
static void
client_place_tile_handler (OVCCClientClient *client,
OVCCTile *tile,
......@@ -783,10 +761,6 @@ client_join_ready (GObject *object,
G_CALLBACK (on_current_player_changed), app);
g_signal_connect (app->game, "notify::state",
G_CALLBACK (on_game_state_changed), app);
g_signal_connect (app->game, "pawn-placed",
G_CALLBACK (game_pawn_placed_handler), app);
g_signal_connect (app->game, "pawn-removed",
G_CALLBACK (game_pawn_removed_handler), app);
ovccgtk_player_list_set_game (OVCCGTK_PLAYER_LIST (app->player_list),
app->game);
......
......@@ -59,6 +59,8 @@ struct _OVCCGtkBoardPrivate
{
OVCCBoard *board;
gulong sig_board_add_tile;
gulong sig_board_pawn_added;
gulong sig_board_pawn_removed;
gint margin;
cairo_t *cr;
......@@ -984,6 +986,26 @@ on_board_tile_added (OVCCBoard *board,
gtk_widget_queue_resize (GTK_WIDGET (data));
}
static void
on_board_pawn_added (OVCCBoard *board,
OVCCPawn *pawn,
OVCCTileObject *obj,
OVCCPosition *pos,
gpointer self)
{
ovccgtk_board_queue_position_update (self, pos);
}
static void
on_board_pawn_removed (OVCCBoard *board,
OVCCPawn *pawn,
OVCCTileObject *obj,
OVCCPosition *pos,
gpointer self)
{
ovccgtk_board_queue_position_update (self, pos);
}
GtkWidget *
ovccgtk_board_new (OVCCBoard *board)
{
......@@ -1083,12 +1105,22 @@ ovccgtk_board_set_board (OVCCGtkBoard *self,
if (self->priv->board) {
g_signal_handler_disconnect (self->priv->board,
self->priv->sig_board_add_tile);
g_signal_handler_disconnect (self->priv->board,
self->priv->sig_board_pawn_added);
g_signal_handler_disconnect (self->priv->board,
self->priv->sig_board_pawn_removed);
g_object_unref (self->priv->board);
}
self->priv->board = g_object_ref (board);
self->priv->sig_board_add_tile =
g_signal_connect (board, "tile-added",
G_CALLBACK (on_board_tile_added), self);
self->priv->sig_board_pawn_added =
g_signal_connect (board, "pawn-added",
G_CALLBACK (on_board_pawn_added), self);
self->priv->sig_board_pawn_removed =
g_signal_connect (board, "pawn-removed",
G_CALLBACK (on_board_pawn_removed), self);
}
OVCCBoard *
......
......@@ -95,10 +95,10 @@ namespace OVCC.Network
send_to_peer (SignalType.TILE_PLACED, "(iiu)", p.x, p.y, t.rotation);
});
handle_ids[SignalType.PAWN_PLACED] =
game_object.pawn_placed.connect ((g, pawn, obj, pos) => {
game_object.board.pawn_added.connect ((board, pawn, obj, pos) => {
uint i = 0;
var tile = g.board.get_tile (pos);
var tile = board.get_tile (pos);
var found = false;
assert (tile != null);
foreach (var o in tile.objects) {
......@@ -205,9 +205,9 @@ namespace OVCC.Network
debug ("Current player '%s' placed pawn kind %u at %d %d, object %p",
game_object.current_player.nick, k, p.x, p.y, o);
stop_forward (game_object, SignalType.PAWN_PLACED);
stop_forward (game_object.board, SignalType.PAWN_PLACED);
game_object.current_player.place_pawn ((PawnKind) k, o, p);
resume_forward (game_object, SignalType.PAWN_PLACED);
resume_forward (game_object.board, SignalType.PAWN_PLACED);
});
signal_received[SignalType.TURN_FINISHED.to_string ()].connect ((msg) => {
debug ("Current player '%s' finished its turn",
......
......@@ -63,6 +63,26 @@ namespace OVCC
public Position last_position { get { return _last_position; } }
public signal void tile_added (Tile t, Position pos);
/**
* Signal emitted when a pawn was added to the board
*
* @param pawn The placed pawn
* @param obj The object on which the pawn was placed
* @param pos The position of the tile containing @p pos
*/
public signal void pawn_added (Pawn pawn,
TileObject obj,
Position pos);
/**
* Signal emitted when a pawn was removed
*
* @param pawn The removed pawn
* @param obj The object on which the pawn was
* @param pos The position of the tile containing the object
*/
public signal void pawn_removed (Pawn pawn,
TileObject obj,
Position pos);
public Board (Tile first)
{
......@@ -241,6 +261,43 @@ namespace OVCC
return true;
}
/**
* Tries to place a pawn on the given TileObject
*
* To succeed the object must be free (ie. no other pawn on the same
* "object"). By "object" it is meant the given TileObject AND all of its
* neighbours, doing recursion on the tiles
*
* @param pawn The concerned pawn
* @param object The TileObject where to place the pawn
* @param pos The position where the object's tile is located on the board
* @return true if the pawn was correctly placed, false otherwise
*/
public bool add_pawn (Pawn pawn, TileObject object, Position pos)
{
var ok = add_pawn_check (pawn.kind, object, pos);
if (ok) {
pawn.place (object, pos);
pawn_added (pawn, object, pos);
}
return ok;
}
/**
* Removes a pawn
*
* Removes a pawn from the board.
*
* @param pawn The pawn
*/
public void remove_pawn (Pawn pawn)
{
var o = pawn.location;
var pos = pawn.position;
pawn.player.retrieve_pawn (pawn);
pawn_removed (pawn, o, pos);
}
/**
* Calculates the list of connected objects starting from the given one
*
......
......@@ -79,26 +79,6 @@ namespace OVCC
* A signal to be emitted when a player finished its turn
*/
public signal void turn_finished ();
/**
* Signal emitted when a pawn was placed
*
* @param pawn The placed pawn
* @param obj The object on which the pawn was placed
* @param pos The position of the tile containing @p pos
*/
public signal void pawn_placed (Pawn pawn,
TileObject obj,
Position pos);
/**
* Signal emitted when a pawn was removed
*
* @param pawn The removed pawn
* @param obj The object on which the pawn was
* @param pos The position of the tile containing the object
*/
public signal void pawn_removed (Pawn pawn,
TileObject obj,
Position pos);
public Game (TileSet ts, Stack? s = null)
......@@ -354,7 +334,7 @@ namespace OVCC
table.insert (o.occupant.player, power);
/* removing pawn now is efficient and not wrong */
if (components.nth_data(0).otype != TileObjectType.FIELD) {
remove_pawn (o.occupant);
board.remove_pawn (o.occupant);
}
}
}
......@@ -410,19 +390,10 @@ namespace OVCC
} else {
object.occupant.player.score += 9;
}
remove_pawn (object.occupant);
board.remove_pawn (object.occupant);
}
}
/* small util to remove a pawn and send a cool signal */
private void remove_pawn (Pawn pawn)
{
var o = pawn.location;
var pos = pawn.position;
pawn.player.retrieve_pawn (pawn);
pawn_removed (pawn, o, pos);
}
/**
* Tries to place a pawn on the given TileObject
*
......@@ -442,12 +413,7 @@ namespace OVCC
return false;
}
var ok = board.add_pawn_check (pawn.kind, object, pos);
if (ok) {
pawn.place (object, pos);
pawn_placed (pawn, object, pos);
}
return ok;
return board.add_pawn (pawn, object, pos);
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment