1
0
mirror of https://github.com/danbee/chess synced 2025-03-04 08:39:06 +00:00

Can select a piece

This commit is contained in:
Daniel Barber 2016-12-08 14:35:33 +00:00
parent 4e78fad7dc
commit b2ee8c9263
6 changed files with 47 additions and 7 deletions

View File

@ -1,6 +1,8 @@
import React from "react";
import classNames from "classnames";
import { selectPiece } from "store/actions";
class ChessBoardSquare extends React.Component {
constructor(props) {
super(props);
@ -8,19 +10,38 @@ class ChessBoardSquare extends React.Component {
this.selectSquare = this.selectSquare.bind(this);
}
squareCoords() {
return { rank: this.props.rank, file: this.props.file };
}
selectSquare() {
var { store } = this.props;
console.log(`Clicked: ${this.props.file}${this.props.rank}`);
if (this.props.piece != undefined) {
store.dispatch(selectPiece(this.squareCoords()));
}
console.log(store.getState().selectedSquare);
};
isSelectedSquare() {
var { store } = this.props;
return this.squareCoords().rank == store.getState().selectedSquare.rank
&& this.squareCoords().file == store.getState().selectedSquare.file;
}
render() {
if (this.props.square == undefined) {
if (this.props.piece == undefined) {
var squareClass = "board-square";
}
else {
var squareClass = classNames(
"board-square",
this.props.square.type,
this.props.square.colour,
this.props.piece.type,
this.props.piece.colour,
{ "selected": this.isSelectedSquare() }
)
}

View File

@ -1,4 +1,5 @@
import React from "react";
import { connect } from "react-redux";
import ChessBoardSquare from "components/chess-board-square";
@ -40,4 +41,8 @@ class ChessBoard extends React.Component {
}
}
export default ChessBoard;
function mapStateToProps(state) {
return { selectedSquare: state.selectedSquare }
}
export default connect(mapStateToProps)(ChessBoard);

View File

@ -15,6 +15,7 @@ const chessBoardReducer = (state = defaultState, action) => {
});
case "SELECT_PIECE":
console.log("Action fired");
return Object.assign({}, state, { selectedSquare: action.coords });
default:

View File

@ -3,14 +3,14 @@ const MOVE_PIECE = "MOVE_PIECE";
export const selectPiece = (coords) => {
return {
type: selectPiece,
type: SELECT_PIECE,
coords: coords
};
}
export const movePiece = (from, to) => {
return {
type: movePiece,
type: MOVE_PIECE,
from: from,
to: to
};

View File

@ -1,5 +1,5 @@
const defaultState = {
selectedSquare: null,
selectedSquare: { rank: "1", file: "a" },
board: {
8: {

View File

@ -8,7 +8,10 @@ $base-font-size: 16px;
$black-square-color: #824c34;
$white-square-color: #d0bfa1;
$selected-square-color: #ff0000;
$square-outline-color: darken($black-square-color, 20%);
$selected-outline-color: red;
$colours: black white;
$pieces: king queen bishop knight rook pawn;
@ -42,10 +45,20 @@ $pieces: king queen bishop knight rook pawn;
%black-square {
background-color: $black-square-color;
&.selected {
background-color: mix($black-square-color, $selected-square-color, 60%);
border: 0.2vw solid $selected-outline-color;
}
}
%white-square {
background-color: $white-square-color;
&.selected {
background-color: mix($white-square-color, $selected-square-color, 60%);
border: 0.2vw solid $selected-outline-color;
}
}
.board-rank:nth-child(odd) {