added pawn mouseenter event
This commit is contained in:
parent
efdd150fd9
commit
3058c87fb6
@ -51,6 +51,7 @@ router.get('/', (req,res)=>{
|
|||||||
if(req.session.name){
|
if(req.session.name){
|
||||||
res.send({
|
res.send({
|
||||||
name: req.session.name,
|
name: req.session.name,
|
||||||
|
color: req.session.color,
|
||||||
playerId: req.session.playerId,
|
playerId: req.session.playerId,
|
||||||
roomId: req.session.roomId,
|
roomId: req.session.roomId,
|
||||||
})
|
})
|
||||||
|
|||||||
@ -39,6 +39,7 @@ router.post('/add', function (req, res) {
|
|||||||
.then(function(){
|
.then(function(){
|
||||||
req.session.roomId = newRoom._id;
|
req.session.roomId = newRoom._id;
|
||||||
req.session.playerId = newRoom.players[0]._id;
|
req.session.playerId = newRoom.players[0]._id;
|
||||||
|
req.session.color = newRoom.players[0].color;
|
||||||
req.session.name = req.body.name;
|
req.session.name = req.body.name;
|
||||||
res.status(200).send(req.session.playerId);
|
res.status(200).send(req.session.playerId);
|
||||||
})
|
})
|
||||||
@ -67,6 +68,7 @@ router.post('/add', function (req, res) {
|
|||||||
req.session.roomId = results._id;
|
req.session.roomId = results._id;
|
||||||
req.session.playerId = updateObj.players[updateObj.players.length-1]._id;
|
req.session.playerId = updateObj.players[updateObj.players.length-1]._id;
|
||||||
req.session.name = req.body.name;
|
req.session.name = req.body.name;
|
||||||
|
req.sessions.color = colors[players.length - 1];
|
||||||
res.status(200).send(req.session.playerId);
|
res.status(200).send(req.session.playerId);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -9,8 +9,9 @@ import NameInput from './components/NameInput';
|
|||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
|
|
||||||
const [id, setId] = useState('')
|
const [id, setId] = useState('');
|
||||||
const [redirect, setRedirect] = useState()
|
const [color, setColor] = useState('');
|
||||||
|
const [redirect, setRedirect] = useState();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
axios.get('http://localhost:3000/player', {
|
axios.get('http://localhost:3000/player', {
|
||||||
@ -19,6 +20,7 @@ function App() {
|
|||||||
})
|
})
|
||||||
.then( response => {
|
.then( response => {
|
||||||
setId(response.data.playerId);
|
setId(response.data.playerId);
|
||||||
|
setColor(response.data.color);
|
||||||
response.data.roomId!=null ? setRedirect(true) : setRedirect(false);
|
response.data.roomId!=null ? setRedirect(true) : setRedirect(false);
|
||||||
});
|
});
|
||||||
},[id])
|
},[id])
|
||||||
@ -38,6 +40,7 @@ function App() {
|
|||||||
})
|
})
|
||||||
.then(response => {
|
.then(response => {
|
||||||
setId(response.data.playerId);
|
setId(response.data.playerId);
|
||||||
|
setColor(response.data.color);
|
||||||
setRedirect(true)
|
setRedirect(true)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -54,7 +57,7 @@ function App() {
|
|||||||
</Route>
|
</Route>
|
||||||
<Route path="/game">
|
<Route path="/game">
|
||||||
<Beforeunload onBeforeunload={handleExit}>
|
<Beforeunload onBeforeunload={handleExit}>
|
||||||
<Gameboard id={id}/>
|
<Gameboard id={id} color={color}/>
|
||||||
</Beforeunload>
|
</Beforeunload>
|
||||||
</Route>
|
</Route>
|
||||||
</Switch>
|
</Switch>
|
||||||
|
|||||||
@ -4,7 +4,7 @@ import Map from './game-board-components/Map'
|
|||||||
import Dice from './game-board-components/Dice'
|
import Dice from './game-board-components/Dice'
|
||||||
import Navbar from './Navbar'
|
import Navbar from './Navbar'
|
||||||
|
|
||||||
const Gameboard = ({id}) => {
|
const Gameboard = ({id, color}) => {
|
||||||
const [pawns, setPawns] = useState([]);
|
const [pawns, setPawns] = useState([]);
|
||||||
const [players, setPlayers] = useState([]);
|
const [players, setPlayers] = useState([]);
|
||||||
const [nowMoving, setNowMoving] = useState(false);
|
const [nowMoving, setNowMoving] = useState(false);
|
||||||
@ -38,7 +38,7 @@ const Gameboard = ({id}) => {
|
|||||||
<>
|
<>
|
||||||
<Navbar players={players} started={started}/>
|
<Navbar players={players} started={started}/>
|
||||||
{nowMoving ? <Dice nowMoving={nowMoving}/> : null}
|
{nowMoving ? <Dice nowMoving={nowMoving}/> : null}
|
||||||
<Map pawns={pawns}/>
|
<Map pawns={pawns} nowMoving={nowMoving} color={color}/>
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -6,6 +6,7 @@ import three from '../../images/dice/3.png';
|
|||||||
import four from '../../images/dice/4.png';
|
import four from '../../images/dice/4.png';
|
||||||
import five from '../../images/dice/5.png';
|
import five from '../../images/dice/5.png';
|
||||||
import six from '../../images/dice/6.png';
|
import six from '../../images/dice/6.png';
|
||||||
|
|
||||||
const Dice = () => {
|
const Dice = () => {
|
||||||
const [rolledNumber, setRolledNumber] = useState()
|
const [rolledNumber, setRolledNumber] = useState()
|
||||||
const [images] = useState([one, two, three, four, five, six]);
|
const [images] = useState([one, two, three, four, five, six]);
|
||||||
|
|||||||
@ -1,35 +1,55 @@
|
|||||||
import React, { useEffect, useState, useRef } from 'react';
|
import React, { useEffect, useRef } from 'react';
|
||||||
import positions from './positions';
|
import positions from './positions';
|
||||||
import './Map.css';
|
import './Map.css';
|
||||||
|
|
||||||
const Map = ({ pawns }) => {
|
const Map = ({ pawns, nowMoving, color }) => {
|
||||||
//let pawnsOnMap = [];
|
const paintPawn = (context, x, y, color, id) =>{
|
||||||
let paths = [];
|
|
||||||
const paintPawn = (context, x, y, color) =>{
|
|
||||||
const circle = new Path2D();
|
const circle = new Path2D();
|
||||||
circle.arc(x, y, 12, 0, 2 * Math.PI);
|
circle.arc(x, y, 12, 0, 2 * Math.PI);
|
||||||
context.strokeStyle ='black';
|
context.strokeStyle ='black';
|
||||||
context.stroke(circle);
|
context.stroke(circle);
|
||||||
context.fillStyle = color
|
context.fillStyle = color;
|
||||||
context.fill(circle);
|
context.fill(circle);
|
||||||
paths.push(circle);
|
const currentPawnIndex = pawns.findIndex( pawn => pawn._id === id);
|
||||||
// pawnsOnMap.push(circle);
|
pawns[currentPawnIndex].circle = circle;
|
||||||
}
|
}
|
||||||
|
|
||||||
const canvasRef = useRef(null)
|
const canvasRef = useRef(null);
|
||||||
|
|
||||||
const handleCanvasClick = event => {
|
const handleCanvasClick = event => {
|
||||||
|
if(!nowMoving){
|
||||||
const canvas = canvasRef.current
|
const canvas = canvasRef.current
|
||||||
const context = canvas.getContext('2d')
|
const context = canvas.getContext('2d')
|
||||||
var rect = canvas.getBoundingClientRect(), // get abs. position of canvas
|
const rect = canvas.getBoundingClientRect(),
|
||||||
x = event.clientX - rect.left, // adjust mouse-position
|
x = event.clientX - rect.left,
|
||||||
y = event.clientY - rect.top;
|
y = event.clientY - rect.top;
|
||||||
paths.forEach((path) => {
|
for(const pawn of pawns){
|
||||||
if (context.isPointInPath(path, x, y)) {
|
if (context.isPointInPath(pawn.circle, x, y)) {
|
||||||
alert("clicked")
|
alert(pawn._id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const handleMouseMove = event => {
|
||||||
|
if(!nowMoving){
|
||||||
|
const canvas = canvasRef.current
|
||||||
|
const context = canvas.getContext('2d')
|
||||||
|
const rect = canvas.getBoundingClientRect(),
|
||||||
|
x = event.clientX - rect.left,
|
||||||
|
y = event.clientY - rect.top;
|
||||||
|
document.querySelector("body").style.cursor = "default";
|
||||||
|
for (const pawn of pawns){
|
||||||
|
if(pawn.circle){
|
||||||
|
// Checks if current mouse location in above canvas pawn and if this pawn is same color as player
|
||||||
|
if (context.isPointInPath(pawn.circle, x, y) && pawn.color === color) {
|
||||||
|
document.querySelector("body").style.cursor = "pointer";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
|
||||||
}
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const canvas = canvasRef.current
|
const canvas = canvasRef.current
|
||||||
const context = canvas.getContext('2d')
|
const context = canvas.getContext('2d')
|
||||||
@ -38,12 +58,19 @@ const Map = ({ pawns }) => {
|
|||||||
image.onload = function() {
|
image.onload = function() {
|
||||||
context.drawImage(image, 0 , 0);
|
context.drawImage(image, 0 , 0);
|
||||||
pawns.forEach( pawn => {
|
pawns.forEach( pawn => {
|
||||||
paintPawn(context, positions[pawn.position].x, positions[pawn.position].y, pawn.color);
|
paintPawn(context, positions[pawn.position].x, positions[pawn.position].y, pawn.color, pawn._id);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}, [pawns]);
|
}, [pawns]);
|
||||||
return(
|
return(
|
||||||
<canvas className="canvas-container" width={480} height={480} ref={canvasRef} onClick={handleCanvasClick}></canvas>
|
<canvas
|
||||||
|
className="canvas-container"
|
||||||
|
width={480}
|
||||||
|
height={480}
|
||||||
|
ref={canvasRef}
|
||||||
|
onClick={handleCanvasClick}
|
||||||
|
onMouseMove={handleMouseMove}
|
||||||
|
/>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
export default Map
|
export default Map
|
||||||
Loading…
Reference in New Issue
Block a user