feature/authoritative-server #1
2 changed files with 63 additions and 61 deletions
|
|
@ -139,11 +139,11 @@ function getSurroundingTilesByIdx(gameId, tileIdx) {
|
||||||
|
|
||||||
return [
|
return [
|
||||||
// top
|
// top
|
||||||
(_Y > 0) ? (tileIdx - info.tiles_x) : -1,
|
(_Y > 0) ? (tileIdx - info.tilesX) : -1,
|
||||||
// right
|
// right
|
||||||
(_X < info.tiles_x - 1) ? (tileIdx + 1) : -1,
|
(_X < info.tilesX - 1) ? (tileIdx + 1) : -1,
|
||||||
// bottom
|
// bottom
|
||||||
(_Y < info.tiles_y - 1) ? (tileIdx + info.tiles_x) : -1,
|
(_Y < info.tilesY - 1) ? (tileIdx + info.tilesX) : -1,
|
||||||
// left
|
// left
|
||||||
(_X > 0) ? (tileIdx - 1) : -1,
|
(_X > 0) ? (tileIdx - 1) : -1,
|
||||||
]
|
]
|
||||||
|
|
|
||||||
116
server/Puzzle.js
116
server/Puzzle.js
|
|
@ -6,20 +6,18 @@ import Util from './../common/Util.js'
|
||||||
const TILE_SIZE = 64
|
const TILE_SIZE = 64
|
||||||
|
|
||||||
async function createPuzzle(targetTiles, image) {
|
async function createPuzzle(targetTiles, image) {
|
||||||
const imgPath = './../game' + image
|
const imagePath = './../game' + image
|
||||||
const imgUrl = image
|
const imageUrl = image
|
||||||
|
|
||||||
// load bitmap, to determine the original size of the image
|
// load bitmap, to determine the original size of the image
|
||||||
let dim = sizeOf(imgPath)
|
const dim = sizeOf(imagePath)
|
||||||
|
|
||||||
// determine puzzle information from the bitmap
|
// determine puzzle information from the bitmap
|
||||||
let info = determinePuzzleInfo(dim.width, dim.height, targetTiles)
|
const info = determinePuzzleInfo(dim.width, dim.height, targetTiles)
|
||||||
|
|
||||||
let tiles = new Array(info.tiles)
|
let tiles = new Array(info.tiles)
|
||||||
for (let i = 0; i < tiles.length; i++) {
|
for (let i = 0; i < tiles.length; i++) {
|
||||||
tiles[i] = {
|
tiles[i] = { idx: i }
|
||||||
idx: i,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
const shapes = determinePuzzleTileShapes(info)
|
const shapes = determinePuzzleTileShapes(info)
|
||||||
|
|
||||||
|
|
@ -33,36 +31,39 @@ async function createPuzzle(targetTiles, image) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let tableWidth = info.width * 3
|
const tableWidth = info.width * 3
|
||||||
let tableHeight = info.height * 3
|
const tableHeight = info.height * 3
|
||||||
|
|
||||||
let off = (info.tileSize * 1.5)
|
const off = info.tileSize * 1.5
|
||||||
let last = {x: info.width - (1 * off), y: info.height - (2 * off) }
|
let last = {
|
||||||
let count_x = Math.ceil(info.width / off) + 2
|
x: info.width - (1 * off),
|
||||||
let count_y = Math.ceil(info.height / off) + 2
|
y: info.height - (2 * off),
|
||||||
|
}
|
||||||
|
let countX = Math.ceil(info.width / off) + 2
|
||||||
|
let countY = Math.ceil(info.height / off) + 2
|
||||||
|
|
||||||
let diff_x = off
|
let diffX = off
|
||||||
let diff_y = 0
|
let diffY = 0
|
||||||
let index = 0
|
let index = 0
|
||||||
for (let pos of positions) {
|
for (let pos of positions) {
|
||||||
pos.x = last.x
|
pos.x = last.x
|
||||||
pos.y = last.y
|
pos.y = last.y
|
||||||
last.x+=diff_x
|
last.x+=diffX
|
||||||
last.y+=diff_y
|
last.y+=diffY
|
||||||
index++
|
index++
|
||||||
// did we move horizontally?
|
// did we move horizontally?
|
||||||
if (diff_x !== 0) {
|
if (diffX !== 0) {
|
||||||
if (index === count_x) {
|
if (index === countX) {
|
||||||
diff_y = diff_x
|
diffY = diffX
|
||||||
count_y ++
|
countY++
|
||||||
diff_x = 0
|
diffX = 0
|
||||||
index = 0
|
index = 0
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (index === count_y) {
|
if (index === countY) {
|
||||||
diff_x = -diff_y
|
diffX = -diffY
|
||||||
count_x ++
|
countX++
|
||||||
diff_y = 0
|
diffY = 0
|
||||||
index = 0
|
index = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -91,7 +92,7 @@ async function createPuzzle(targetTiles, image) {
|
||||||
})
|
})
|
||||||
|
|
||||||
// Complete puzzle object
|
// Complete puzzle object
|
||||||
const p = {
|
return {
|
||||||
// tiles array
|
// tiles array
|
||||||
tiles,
|
tiles,
|
||||||
// game data for puzzle, data changes during the game
|
// game data for puzzle, data changes during the game
|
||||||
|
|
@ -109,7 +110,7 @@ async function createPuzzle(targetTiles, image) {
|
||||||
},
|
},
|
||||||
// information that was used to create the puzzle
|
// information that was used to create the puzzle
|
||||||
targetTiles: targetTiles,
|
targetTiles: targetTiles,
|
||||||
imageUrl: imgUrl,
|
imageUrl,
|
||||||
|
|
||||||
width: info.width, // actual puzzle width (same as bitmap.width)
|
width: info.width, // actual puzzle width (same as bitmap.width)
|
||||||
height: info.height, // actual puzzle height (same as bitmap.height)
|
height: info.height, // actual puzzle height (same as bitmap.height)
|
||||||
|
|
@ -122,8 +123,8 @@ async function createPuzzle(targetTiles, image) {
|
||||||
// makes the tile snap to destination
|
// makes the tile snap to destination
|
||||||
snapDistance: info.tileSize / 2,
|
snapDistance: info.tileSize / 2,
|
||||||
tiles: info.tiles, // the final number of tiles in the puzzle
|
tiles: info.tiles, // the final number of tiles in the puzzle
|
||||||
tiles_x: info.tiles_x, // number of tiles each row
|
tilesX: info.tilesX, // number of tiles each row
|
||||||
tiles_y: info.tiles_y, // number of tiles each col
|
tilesY: info.tilesY, // number of tiles each col
|
||||||
coords: info.coords, // map of tile index to its coordinates
|
coords: info.coords, // map of tile index to its coordinates
|
||||||
// ( index => {x, y} )
|
// ( index => {x, y} )
|
||||||
// this is not the physical coordinate, but
|
// this is not the physical coordinate, but
|
||||||
|
|
@ -133,7 +134,6 @@ async function createPuzzle(targetTiles, image) {
|
||||||
shapes: shapes, // tile shapes
|
shapes: shapes, // tile shapes
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
return p
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function determinePuzzleTileShapes(info) {
|
function determinePuzzleTileShapes(info) {
|
||||||
|
|
@ -142,34 +142,38 @@ function determinePuzzleTileShapes(info) {
|
||||||
const shapes = new Array(info.tiles)
|
const shapes = new Array(info.tiles)
|
||||||
for (let i = 0; i < info.tiles; i++) {
|
for (let i = 0; i < info.tiles; i++) {
|
||||||
shapes[i] = {
|
shapes[i] = {
|
||||||
top: info.coords[i].y === 0 ? 0 : shapes[i - info.tiles_x].bottom * -1,
|
top: info.coords[i].y === 0 ? 0 : shapes[i - info.tilesX].bottom * -1,
|
||||||
right: info.coords[i].x === info.tiles_x - 1 ? 0 : Util.choice(tabs),
|
right: info.coords[i].x === info.tilesX - 1 ? 0 : Util.choice(tabs),
|
||||||
left: info.coords[i].x === 0 ? 0 : shapes[i - 1].right * -1,
|
left: info.coords[i].x === 0 ? 0 : shapes[i - 1].right * -1,
|
||||||
bottom: info.coords[i].y === info.tiles_y - 1 ? 0 : Util.choice(tabs),
|
bottom: info.coords[i].y === info.tilesY - 1 ? 0 : Util.choice(tabs),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return shapes
|
return shapes
|
||||||
}
|
}
|
||||||
|
|
||||||
const determinePuzzleInfo = (w, h, targetTiles) => {
|
const determineTilesXY = (w, h, targetTiles) => {
|
||||||
let tileSize = 0
|
const w_ = w < h ? (w * h) : (w * w)
|
||||||
|
const h_ = w < h ? (h * h) : (w * h)
|
||||||
|
let size = 0
|
||||||
let tiles = 0
|
let tiles = 0
|
||||||
do {
|
do {
|
||||||
tileSize++
|
size++
|
||||||
tiles = tilesFit(w, h, tileSize)
|
tiles = Math.floor(w_ / size) * Math.floor(h_ / size)
|
||||||
} while (tiles >= targetTiles)
|
} while (tiles >= targetTiles)
|
||||||
tileSize--
|
size--
|
||||||
|
return {
|
||||||
|
tilesX: Math.round(w_ / size),
|
||||||
|
tilesY: Math.round(h_ / size),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
tiles = tilesFit(w, h, tileSize)
|
const determinePuzzleInfo = (w, h, targetTiles) => {
|
||||||
const tiles_x = Math.round(w / tileSize)
|
const {tilesX, tilesY} = determineTilesXY(w, h, targetTiles)
|
||||||
const tiles_y = Math.round(h / tileSize)
|
const tiles = tilesX * tilesY
|
||||||
tiles = tiles_x * tiles_y
|
const tileSize = TILE_SIZE
|
||||||
|
const width = tilesX * tileSize
|
||||||
// then resize to final TILE_SIZE (which is always the same)
|
const height = tilesY * tileSize
|
||||||
tileSize = TILE_SIZE
|
const coords = buildCoords({ width, height, tileSize, tiles })
|
||||||
const width = tiles_x * tileSize
|
|
||||||
const height = tiles_y * tileSize
|
|
||||||
const coords = coordsByNum({ width, height, tileSize, tiles })
|
|
||||||
|
|
||||||
const tileMarginWidth = tileSize * .5;
|
const tileMarginWidth = tileSize * .5;
|
||||||
const tileDrawSize = Math.round(tileSize + tileMarginWidth * 2)
|
const tileDrawSize = Math.round(tileSize + tileMarginWidth * 2)
|
||||||
|
|
@ -181,20 +185,18 @@ const determinePuzzleInfo = (w, h, targetTiles) => {
|
||||||
tileMarginWidth,
|
tileMarginWidth,
|
||||||
tileDrawSize,
|
tileDrawSize,
|
||||||
tiles,
|
tiles,
|
||||||
tiles_x,
|
tilesX,
|
||||||
tiles_y,
|
tilesY,
|
||||||
coords,
|
coords,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const tilesFit = (w, h, size) => Math.floor(w / size) * Math.floor(h / size)
|
const buildCoords = (puzzleInfo) => {
|
||||||
|
const wTiles = puzzleInfo.width / puzzleInfo.tileSize
|
||||||
const coordsByNum = (puzzleInfo) => {
|
|
||||||
const w_tiles = puzzleInfo.width / puzzleInfo.tileSize
|
|
||||||
const coords = new Array(puzzleInfo.tiles)
|
const coords = new Array(puzzleInfo.tiles)
|
||||||
for (let i = 0; i < puzzleInfo.tiles; i++) {
|
for (let i = 0; i < puzzleInfo.tiles; i++) {
|
||||||
const y = Math.floor(i / w_tiles)
|
const y = Math.floor(i / wTiles)
|
||||||
const x = i % w_tiles
|
const x = i % wTiles
|
||||||
coords[i] = { x, y }
|
coords[i] = { x, y }
|
||||||
}
|
}
|
||||||
return coords
|
return coords
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue