add other snap mode
This commit is contained in:
parent
42aaf10679
commit
2a12900614
11 changed files with 164 additions and 27 deletions
|
|
@ -16,6 +16,7 @@ import {
|
|||
PuzzleData,
|
||||
PuzzleDataChange,
|
||||
ScoreMode,
|
||||
SnapMode,
|
||||
Timestamp
|
||||
} from './Types'
|
||||
import Util from './Util'
|
||||
|
|
@ -172,6 +173,10 @@ function getScoreMode(gameId: string): ScoreMode {
|
|||
return GAMES[gameId].scoreMode || ScoreMode.FINAL
|
||||
}
|
||||
|
||||
function getSnapMode(gameId: string): SnapMode {
|
||||
return GAMES[gameId].snapMode || SnapMode.NORMAL
|
||||
}
|
||||
|
||||
function isFinished(gameId: string): boolean {
|
||||
return getFinishedPiecesCount(gameId) === getPieceCount(gameId)
|
||||
}
|
||||
|
|
@ -237,6 +242,16 @@ const getPieceGroup = (gameId: string, tileIdx: number): number => {
|
|||
return tile.group
|
||||
}
|
||||
|
||||
const isCornerPiece = (gameId: string, tileIdx: number): boolean => {
|
||||
const info = GAMES[gameId].puzzle.info
|
||||
return (
|
||||
tileIdx === 0 // top left corner
|
||||
|| tileIdx === (info.tilesX - 1) // top right corner
|
||||
|| tileIdx === (info.tiles - info.tilesX) // bottom left corner
|
||||
|| tileIdx === (info.tiles - 1) // bottom right corner
|
||||
)
|
||||
}
|
||||
|
||||
const getFinalPiecePos = (gameId: string, tileIdx: number): Point => {
|
||||
const info = GAMES[gameId].puzzle.info
|
||||
const boardPos = {
|
||||
|
|
@ -406,6 +421,14 @@ const movePiecesDiff = (
|
|||
}
|
||||
}
|
||||
|
||||
const isFinishedPiece = (gameId: string, pieceIdx: number): boolean => {
|
||||
return getPieceOwner(gameId, pieceIdx) === -1
|
||||
}
|
||||
|
||||
const getPieceOwner = (gameId: string, pieceIdx: number): string|number => {
|
||||
return getPiece(gameId, pieceIdx).owner
|
||||
}
|
||||
|
||||
const finishPieces = (gameId: string, pieceIdxs: Array<number>): void => {
|
||||
for (const pieceIdx of pieceIdxs) {
|
||||
changePiece(gameId, pieceIdx, { owner: -1, z: 1 })
|
||||
|
|
@ -721,7 +744,26 @@ function handleInput(
|
|||
// Check if the tile was dropped near the final location
|
||||
const tilePos = getPiecePos(gameId, pieceIdx)
|
||||
const finalPos = getFinalPiecePos(gameId, pieceIdx)
|
||||
if (Geometry.pointDistance(finalPos, tilePos) < puzzle.info.snapDistance) {
|
||||
|
||||
let canSnapToFinal = false
|
||||
console.log(getSnapMode(gameId))
|
||||
if (getSnapMode(gameId) === SnapMode.REAL) {
|
||||
// only can snap to final if any of the grouped pieces are
|
||||
// corner pieces
|
||||
for (const pieceIdxTmp of pieceIdxs) {
|
||||
if (isCornerPiece(gameId, pieceIdxTmp)) {
|
||||
canSnapToFinal = true
|
||||
break
|
||||
}
|
||||
}
|
||||
} else {
|
||||
canSnapToFinal = true
|
||||
}
|
||||
|
||||
if (
|
||||
canSnapToFinal
|
||||
&& Geometry.pointDistance(finalPos, tilePos) < puzzle.info.snapDistance
|
||||
) {
|
||||
const diff = Geometry.pointSub(finalPos, tilePos)
|
||||
// Snap the tile to the final destination
|
||||
movePiecesDiff(gameId, pieceIdxs, diff)
|
||||
|
|
@ -774,8 +816,12 @@ function handleInput(
|
|||
movePiecesDiff(gameId, pieceIdxs, diff)
|
||||
groupTiles(gameId, tileIdx, otherTileIdx)
|
||||
pieceIdxs = getGroupedPieceIdxs(gameId, tileIdx)
|
||||
const zIndex = getMaxZIndexByPieceIdxs(gameId, pieceIdxs)
|
||||
setPiecesZIndex(gameId, pieceIdxs, zIndex)
|
||||
if (isFinishedPiece(gameId, otherTileIdx)) {
|
||||
finishPieces(gameId, pieceIdxs)
|
||||
} else {
|
||||
const zIndex = getMaxZIndexByPieceIdxs(gameId, pieceIdxs)
|
||||
setPiecesZIndex(gameId, pieceIdxs, zIndex)
|
||||
}
|
||||
_pieceChanges(pieceIdxs)
|
||||
return true
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,6 +49,8 @@ export type EncodedGame = FixedLengthArray<[
|
|||
Array<EncodedPlayer>,
|
||||
Record<string, EvtInfo>,
|
||||
ScoreMode,
|
||||
ShapeMode,
|
||||
SnapMode,
|
||||
]>
|
||||
|
||||
export interface ReplayData {
|
||||
|
|
@ -75,6 +77,7 @@ export interface Game {
|
|||
evtInfos: Record<string, EvtInfo>
|
||||
scoreMode?: ScoreMode
|
||||
shapeMode?: ShapeMode
|
||||
snapMode?: SnapMode
|
||||
rng: GameRng
|
||||
}
|
||||
|
||||
|
|
@ -93,6 +96,7 @@ export interface GameSettings {
|
|||
image: Image
|
||||
scoreMode: ScoreMode
|
||||
shapeMode: ShapeMode
|
||||
snapMode: SnapMode
|
||||
}
|
||||
|
||||
export interface Puzzle {
|
||||
|
|
@ -207,3 +211,8 @@ export enum ShapeMode {
|
|||
ANY = 1,
|
||||
FLAT = 2,
|
||||
}
|
||||
|
||||
export enum SnapMode {
|
||||
NORMAL = 0,
|
||||
REAL = 1,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,9 @@ import {
|
|||
PieceShape,
|
||||
Player,
|
||||
PuzzleInfo,
|
||||
ScoreMode
|
||||
ScoreMode,
|
||||
ShapeMode,
|
||||
SnapMode
|
||||
} from './Types'
|
||||
import { Point } from './Geometry'
|
||||
import { Rng } from './Rng'
|
||||
|
|
@ -129,6 +131,8 @@ function encodeGame(data: Game): EncodedGame {
|
|||
data.players,
|
||||
data.evtInfos,
|
||||
data.scoreMode || ScoreMode.FINAL,
|
||||
data.shapeMode || ShapeMode.ANY,
|
||||
data.snapMode || SnapMode.NORMAL,
|
||||
]
|
||||
}
|
||||
|
||||
|
|
@ -143,6 +147,8 @@ function decodeGame(data: EncodedGame): Game {
|
|||
players: data[4],
|
||||
evtInfos: data[5],
|
||||
scoreMode: data[6],
|
||||
shapeMode: data[7],
|
||||
snapMode: data[8],
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue