summaryrefslogtreecommitdiff
path: root/src/engine/visual
diff options
context:
space:
mode:
authorniliara-edu <nil.jimeno@estudiant.fjaverianas.com>2024-12-12 13:51:59 +0100
committerniliara-edu <nil.jimeno@estudiant.fjaverianas.com>2024-12-12 13:51:59 +0100
commiteb460fbb161e76052b7bdd2c57afc65e2e99bc2e (patch)
tree2d8bd718d099a3c9202ac63fb9146754b07e4ef4 /src/engine/visual
parentfa377bcc6d4793dcd79ae5c07a43a1193aa2613a (diff)
place pieces
Diffstat (limited to 'src/engine/visual')
-rw-r--r--src/engine/visual/placeOnBoard.js61
-rw-r--r--src/engine/visual/vector2.js6
2 files changed, 67 insertions, 0 deletions
diff --git a/src/engine/visual/placeOnBoard.js b/src/engine/visual/placeOnBoard.js
new file mode 100644
index 0000000..cee2969
--- /dev/null
+++ b/src/engine/visual/placeOnBoard.js
@@ -0,0 +1,61 @@
+import { board } from "../../board.js"
+
+const HEAD_TOP_VW = 20
+const HEAD_TOP_VH = 20
+
+const MEDIA_LISTENER = window.matchMedia("(max-width: 600px)")
+
+MEDIA_LISTENER.addEventListener("change", function() {
+ Array.from(board.stones).forEach(x => {
+ Array.from(x).filter((x) => x !== undefined).map(y => {
+ y.update(MEDIA_LISTENER)
+ })
+ })
+
+ Array.from(board.links).forEach(x => {
+ Array.from(x).filter((x) => x !== undefined).map(y => {
+ y.update(MEDIA_LISTENER)
+ })
+ })
+});
+
+const PLACE_VALUES = {
+ stone: {
+ size: 54,
+ size_mob: 72,
+ },
+ link: {
+ size: 69,
+ size_mob: 92,
+ }
+}
+
+
+export function placeOnBoard({
+ position = new Vector2(0, 0),
+ board_size = 9,
+ use_real_size = false,
+}) {
+ let response = {}
+ if (MEDIA_LISTENER.matches) {
+ let totalSize = (
+ use_real_size ?
+ PLACE_VALUES.link.size_mob : PLACE_VALUES.stone.size_mob
+ )
+ let size = totalSize / board_size
+ response["left"] = `calc(50% + ${position.x * (80 / (board_size - 1)) - size / 2 - 40}vw)`
+ response["top"] = `calc(${30 + 5 + position.y * (80 / (board_size - 1)) - size / 2 - HEAD_TOP_VW}vw + ${HEAD_TOP_VH}vh)`
+ response["size"] = `${size}vw`
+ } else {
+ let totalSize = (
+ use_real_size ?
+ PLACE_VALUES.link.size : PLACE_VALUES.stone.size
+ )
+ let size = totalSize / board_size
+ response["left"] = `calc(50% - 30vh + ${position.x * (60 / (board_size - 1)) - totalSize / board_size / 2}vh)`
+ response["top"] = `${14 + 5 + position.y * (60 / (board_size - 1)) - size / 2}vh`
+ response["size"] = `${size}vh`
+ }
+
+ return response
+}
diff --git a/src/engine/visual/vector2.js b/src/engine/visual/vector2.js
new file mode 100644
index 0000000..a3a1bc2
--- /dev/null
+++ b/src/engine/visual/vector2.js
@@ -0,0 +1,6 @@
+export class Vector2 {
+ constructor(x, y) {
+ this.x = x
+ this.y = y
+ }
+}