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:
parent
4e78fad7dc
commit
b2ee8c9263
@ -1,6 +1,8 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import classNames from "classnames";
|
import classNames from "classnames";
|
||||||
|
|
||||||
|
import { selectPiece } from "store/actions";
|
||||||
|
|
||||||
class ChessBoardSquare extends React.Component {
|
class ChessBoardSquare extends React.Component {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props);
|
super(props);
|
||||||
@ -8,19 +10,38 @@ class ChessBoardSquare extends React.Component {
|
|||||||
this.selectSquare = this.selectSquare.bind(this);
|
this.selectSquare = this.selectSquare.bind(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
squareCoords() {
|
||||||
|
return { rank: this.props.rank, file: this.props.file };
|
||||||
|
}
|
||||||
|
|
||||||
selectSquare() {
|
selectSquare() {
|
||||||
|
var { store } = this.props;
|
||||||
console.log(`Clicked: ${this.props.file}${this.props.rank}`);
|
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() {
|
render() {
|
||||||
if (this.props.square == undefined) {
|
if (this.props.piece == undefined) {
|
||||||
var squareClass = "board-square";
|
var squareClass = "board-square";
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
var squareClass = classNames(
|
var squareClass = classNames(
|
||||||
"board-square",
|
"board-square",
|
||||||
this.props.square.type,
|
this.props.piece.type,
|
||||||
this.props.square.colour,
|
this.props.piece.colour,
|
||||||
|
{ "selected": this.isSelectedSquare() }
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
|
import { connect } from "react-redux";
|
||||||
|
|
||||||
import ChessBoardSquare from "components/chess-board-square";
|
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);
|
||||||
|
|||||||
@ -15,6 +15,7 @@ const chessBoardReducer = (state = defaultState, action) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
case "SELECT_PIECE":
|
case "SELECT_PIECE":
|
||||||
|
console.log("Action fired");
|
||||||
return Object.assign({}, state, { selectedSquare: action.coords });
|
return Object.assign({}, state, { selectedSquare: action.coords });
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|||||||
@ -3,14 +3,14 @@ const MOVE_PIECE = "MOVE_PIECE";
|
|||||||
|
|
||||||
export const selectPiece = (coords) => {
|
export const selectPiece = (coords) => {
|
||||||
return {
|
return {
|
||||||
type: selectPiece,
|
type: SELECT_PIECE,
|
||||||
coords: coords
|
coords: coords
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export const movePiece = (from, to) => {
|
export const movePiece = (from, to) => {
|
||||||
return {
|
return {
|
||||||
type: movePiece,
|
type: MOVE_PIECE,
|
||||||
from: from,
|
from: from,
|
||||||
to: to
|
to: to
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
const defaultState = {
|
const defaultState = {
|
||||||
selectedSquare: null,
|
selectedSquare: { rank: "1", file: "a" },
|
||||||
|
|
||||||
board: {
|
board: {
|
||||||
8: {
|
8: {
|
||||||
|
|||||||
@ -8,7 +8,10 @@ $base-font-size: 16px;
|
|||||||
$black-square-color: #824c34;
|
$black-square-color: #824c34;
|
||||||
$white-square-color: #d0bfa1;
|
$white-square-color: #d0bfa1;
|
||||||
|
|
||||||
|
$selected-square-color: #ff0000;
|
||||||
|
|
||||||
$square-outline-color: darken($black-square-color, 20%);
|
$square-outline-color: darken($black-square-color, 20%);
|
||||||
|
$selected-outline-color: red;
|
||||||
|
|
||||||
$colours: black white;
|
$colours: black white;
|
||||||
$pieces: king queen bishop knight rook pawn;
|
$pieces: king queen bishop knight rook pawn;
|
||||||
@ -42,10 +45,20 @@ $pieces: king queen bishop knight rook pawn;
|
|||||||
|
|
||||||
%black-square {
|
%black-square {
|
||||||
background-color: $black-square-color;
|
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 {
|
%white-square {
|
||||||
background-color: $white-square-color;
|
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) {
|
.board-rank:nth-child(odd) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user