forked from FCCVienna/JavaScript-AI-Battle-Game
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.js
More file actions
146 lines (132 loc) · 3.49 KB
/
Board.js
File metadata and controls
146 lines (132 loc) · 3.49 KB
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
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { flatten, times } from 'lodash/fp';
import { tileTypes } from '../constants';
import { addMove, removeMove } from '../modules/humanMoves';
import './Board.css';
function Board({ board: { tiles }, humanMoves, addMove, removeMove }) {
const addMoveCreator = tile => (from, to) =>
addMove(from, to, tile.unitCount);
return (
<div className="board">
{flatten(
tiles.map((row, x) =>
row.map((tile, y) =>
<Tile
key={`${x},${y}`}
{...tile}
humanPlayer={humanMoves.player}
addMove={addMoveCreator(tile)}
removeMove={removeMove}
moves={humanMoves.moves}
/>
)
)
)}
</div>
);
}
class Tile extends Component {
render() {
const { x, y, type, player, unitCount, unitType, humanPlayer, addMove, removeMove, moves } = this.props;
return (
<div
className="boardTile"
style={{
background: player && (unitCount > 0 || type !== tileTypes.NEUTRAL) ? player.color : 'white',
}}
>
{tileTypeIcons[type]}
<div className='coordinates'>
{x},{y}
</div>
{unitCount > 0 &&
<div className='unitDisplay'>
{/*<div className='unitType'>*/}
{/*{unitType}*/}
{/*</div>*/}
<div className='unitCount'>
{unitCount}
</div>
</div>}
{player &&
humanPlayer === player &&
// Move buttons
<div>
{times(
i => {
const move = getMove(x, y, i, moves);
return shouldDrawArrow(x, y, i, moves)
? <div
className={`moveButton${move ? ' chosen' : ''}`}
style={{
top : '50%',
left : '50%',
transform: `translate(${Math.cos(Math.PI * 2 * (i / 4)) * 60}px, ${Math.sin(Math.PI * 2 * (i / 4)) *
60}px) rotate(${360 * (i + 1) / 4}deg)`,
}}
onClick={() => move
? removeMove(move)
: addMove({ x, y }, getTargetTile(x, y, i))
}
/>
: null
},
4
)}
</div>}
</div>
);
}
}
function shouldDrawArrow(x, y, i, moves) {
return getMatchingMove(x, y, moves)
? getMove(x, y, i, moves)
: (x > 0 && x < 4 && y > 0 && y < 4) ||
(i === 0 && x < 4) ||
(i === 1 && y < 4) ||
(i === 2 && x > 0) ||
(i === 3 && y > 0);
}
function getMove(x, y, i, moves) {
const matchingMove = getMatchingMove(x, y, moves);
if (!matchingMove) return false;
const targetTile = getTargetTile(x, y, i);
return targetTile.x === matchingMove.to.x && targetTile.y === matchingMove.to.y && matchingMove;
}
function getMatchingMove(x, y, moves) {
return moves.find(({ from }) => from.x === x && from.y === y);
}
function getTargetTile(x, y, i) {
const targetX = x + { 0: 1, 1: 0, 2: -1, 3: 0 }[i];
const targetY = y + { 0: 0, 1: 1, 2: 0, 3: -1 }[i];
return { x: targetX, y: targetY };
}
const tileTypeIcons = {
[tileTypes.NEUTRAL]: null,
[tileTypes.MINOR_SPAWN]: (
<svg height="60" width="60" className="minorSpawn tileIcon">
<polygon points="30,5 55,50 5,50" />
</svg>
),
[tileTypes.CAPTURE_POINT]: (
<svg height="60" width="60" className="capturePoint tileIcon">
<polygon points="5,5 55,5 55,55 5,55 " />
</svg>
),
[tileTypes.MAJOR_SPAWN]: (
<svg width="50" height="50" className="majorSpawn tileIcon">
<path d="m25,1 6,17h18l-14,11 5,17-15-10-15,10 5-17-14-11h18z" />
</svg>
),
};
export default connect(
state => ({
board: state.board,
humanMoves: state.humanMoves,
}),
{
addMove,
removeMove
}
)(Board);