1
0

Tile, pip, and border ordering is working!

Signed-off-by: James Ketrenos <james_eikona@ketrenos.com>
This commit is contained in:
James Ketrenos 2022-02-02 21:01:45 -08:00
parent 7af8d02802
commit 6cf6ab9612
2 changed files with 87 additions and 111 deletions

View File

@ -1,4 +1,4 @@
import React, { useCallback, useRef, useState, useEffect } from "react"; import React, { useState, useEffect } from "react";
import "./Board.css"; import "./Board.css";
const base = process.env.PUBLIC_URL; const base = process.env.PUBLIC_URL;
@ -15,23 +15,19 @@ const Board = ({ game }) => {
let row = 0, rowCount = 0; let row = 0, rowCount = 0;
let y = center.y - rows.length * 0.5 * 67, let y = center.y - rows.length * 0.5 * 67,
x = center.x - (rows[row] - 1) * 0.5 * 77.5; x = center.x - (rows[row] - 1) * 0.5 * 77.5;
let divs = [], count = 19; return game.pipOrder.map(order => {
for (let i = 0; i < count; i++) { const pip = game.pips[order];
let code = (i === (count - 1)) const div = <div
? 'robber' key={`pip-${order}`}
: String.fromCharCode(65 + i); className="Pip"
divs.push( style={{
<div top: `${y}px`,
key={`pip-${(i).toString(16)}`} left: `${x}px`,
className="Pip" backgroundImage: `url(${assetsPath}/gfx/pip-numbers.png)`,
style={{ backgroundPositionX: `${ 100. * (order % 6) / 5.}%`,
top: `${y}px`, backgroundPositionY: `${ 100 * Math.floor(order / 6) / 5. }%`
left: `${x}px`, }}
backgroundImage: `url(${assetsPath}/gfx/pip-numbers.png)`, />;
backgroundPositionX: `${ 100. * (i % 6) / 5.}%`,
backgroundPositionY: `${ 100 * Math.floor(i / 6) / 5. }%`
}}/>
);
if (++rowCount === rows[row]) { if (++rowCount === rows[row]) {
row++; row++;
@ -41,98 +37,68 @@ const Board = ({ game }) => {
} else { } else {
x += 77.5; x += 77.5;
} }
}
return divs; return div;
});
}; };
const generateTiles = () => { const generateTiles = () => {
let row = 0, rowCount = 0; let row = 0, rowCount = 0;
let y = center.y - rows.length * 0.5 * 67, let y = center.y - rows.length * 0.5 * 67,
x = center.x - (rows[row] - 1) * 0.5 * 77.5; x = center.x - (rows[row] - 1) * 0.5 * 77.5;
return game.tileOrder.map(order => {
const tile = game.tiles[order];
let div = <div
key={`${tile.type}-${tile.card}`}
className="Tile"
style={{
top: `${y}px`,
left: `${x}px`,
backgroundImage: `url(${assetsPath}/gfx/tiles-${tile.type}.png)`,
backgroundPositionY: `-${tile.card*77.5}px`
}}
/>;
return [ "wood", "wheat", "stone", "sheep", "brick", "robber" ].map((type) => { if (++rowCount === rows[row]) {
let count; row++;
switch (type) { rowCount = 0;
case "robber": y += 67;
count = 1; x = center.x - (rows[row] - 1) * 0.5 * 77.5;
break; } else {
case "brick": x += 77.5;
count = 3;
break;
case "wood":
count = 4;
break;
case "sheep":
count = 4;
break;
case "stone":
count = 3;
break;
case "wheat":
count = 4;
break;
default:
console.error(`Invalid type: ${type}`);
break;
}
let divs = [];
for (let i = 0; i < count; i++) {
divs.push(
<div
key={`${type}-${(i).toString(16)}`}
className="Tile"
style={{
top: `${y}px`,
left: `${x}px`,
backgroundImage: `url(${assetsPath}/gfx/tiles-${type}.png)`,
backgroundPositionY: `-${i*77.5}px`
}}/>
);
if (++rowCount === rows[row]) {
row++;
rowCount = 0;
y += 67;
x = center.x - (rows[row] - 1) * 0.5 * 77.5;
} else {
x += 77.5;
}
} }
return divs; return div;
}); });
}; };
const generateBorders = () => { const generateBorders = () => {
const divs = [], const radius = 77.5 * 2,
radius = 77.5 * 2; sides = 6;
const sides = 6; let index = 0;
for (let side = 0; side < sides; side++) { return game.borderOrder.map(order => {
// const border = game.borders[order];
const side = order;
let x = center.x + Math.sin(Math.PI - side / sides * 2. * Math.PI) * radius, let x = center.x + Math.sin(Math.PI - side / sides * 2. * Math.PI) * radius,
y = -33.5 + center.y + Math.cos(Math.PI - side / sides * 2. * Math.PI) * radius; y = -33.5 + center.y + Math.cos(Math.PI - side / sides * 2. * Math.PI) * radius;
let prev = (side == 0) ? 6 : side; let prev = (side === 0) ? 6 : side;
const file = `borders-${side+1}.${prev}.png`; const file = `borders-${side+1}.${prev}.png`;
divs.push(<div index++;
key={`border-${side.toString(16)}`} return <div
key={`border-${index}}`}
className="Border" className="Border"
style={{ style={{
top: `${y}px`, top: `${y}px`,
left: `${x}px`, left: `${x}px`,
transform: `rotate(${side*(360/sides)}deg) translate(89px, 0) scale(-1, -1)`, transform: `rotate(${side*(360/sides)}deg) translate(89px, 0) scale(-1, -1)`,
backgroundImage: `url(${assetsPath}/gfx/${file} )` backgroundImage: `url(${assetsPath}/gfx/${file} )`
}}>{side}</div>); }}
} />;
return divs; });
}; };
const updateBorders = () => {
for (let i = 0; i < 6; i++) {
}
}
useEffect(() => { useEffect(() => {
if (game && game.signature != signature) { if (game && game.signature !== signature) {
console.log(`Generate for ${game.signature}`); console.log(`Generate for ${game.signature}`);
setPips(generatePips); setPips(generatePips);
setBorders(generateBorders); setBorders(generateBorders);
@ -143,7 +109,17 @@ const Board = ({ game }) => {
console.log(`No game in Board`); console.log(`No game in Board`);
} }
} }
}, [ setPips, setBorders, setTiles, setSignature, game]); }, [
setPips,
setBorders,
setTiles,
setSignature,
generatePips,
generateBorders,
generateTiles,
game,
signature
]);
return ( return (
<div className="Board" style={{backgroundImage: `url(${assetsPath}/gfx/tabletop.png)`}}> <div className="Board" style={{backgroundImage: `url(${assetsPath}/gfx/tabletop.png)`}}>

View File

@ -36,25 +36,25 @@ function shuffle(array) {
const assetData = { const assetData = {
tiles: [ tiles: [
"wood", { type: "robber", card: 0 },
"wood", { type: "wood", card: 0 },
"wood", { type: "wood", card: 1 },
"wood", { type: "wood", card: 2 },
"wheat", { type: "wood", card: 3 },
"wheat", { type: "wheat", card: 0 },
"wheat", { type: "wheat", card: 1 },
"wheat", { type: "wheat", card: 2 },
"stone", { type: "wheat", card: 3 },
"stone", { type: "stone", card: 0 },
"stone", { type: "stone", card: 1 },
"sheep", { type: "stone", card: 2 },
"sheep", { type: "sheep", card: 0 },
"sheep", { type: "sheep", card: 1 },
"sheep", { type: "sheep", card: 2 },
"brick", { type: "sheep", card: 3 },
"brick", { type: "brick", card: 0 },
"brick", { type: "brick", card: 1 },
"robber" { type: "brick", card: 2 }
], ],
pips: [ pips: [
{ roll: 7, pips: 0 }, { roll: 7, pips: 0 },
@ -681,11 +681,11 @@ const shuffleBoard = (game) => {
for (let i = 0, p = 0; i < sequence.length; i++) { for (let i = 0, p = 0; i < sequence.length; i++) {
const target = sequence[i]; const target = sequence[i];
/* If the target tile is the desert (18), then set the /* If the target tile is the desert (18), then set the
* pip value to the robber (0) otherwise set * pip value to the robber (18) otherwise set
* the target pip value to the currently incremeneting * the target pip value to the currently incremeneting
* pip value. */ * pip value. */
if (game.tileOrder[target] === 18) { if (game.tiles[game.tileOrder[target]].type === 'robber') {
game.pipOrder[target] = 0; game.pipOrder[target] = 18;
} else { } else {
game.pipOrder[target] = p++; game.pipOrder[target] = p++;
} }