Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to slightly miss a guess, if hitting an already guessed hood #311 #313

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions public/js/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -1041,8 +1041,7 @@ function createMap() {
.attr('name', function(d) { return sanitizeName(d.properties.name) })
.on('click', function(d) {
var el = d3.event.target || d3.event.toElement

onNeighborhoodClick(el)
onNeighborhoodClick(el, d3.event)
})
.on('mousedown', function(d) {
setTouchActive(false)
Expand Down Expand Up @@ -1225,11 +1224,43 @@ function updateGuessedAndInactiveStates() {
}
}

function onNeighborhoodClick(el) {
function onNeighborhoodClick(el, ev) {
if (!mapClickable || el.getAttribute('inactive')) {
return
}

if (ev && el.classList.contains('guessed')) {
const expectedEl = getHighlightableNeighborhoodEl(neighborhoodToBeGuessedNext)
const { clientX, clientY } = ev
const maxDist = 10
const expectedRect = expectedEl ? expectedEl.getBoundingClientRect() : {}
const leftDist = clientX - expectedRect.left
const rightDist = clientX - expectedRect.right
const topDist = clientY - expectedRect.top
const bottomDist = clientY - expectedRect.bottom
const betweenTopBottom = Math.sign(topDist) !== Math.sign(bottomDist)
const betweenLeftRight = Math.sign(leftDist) !== Math.sign(rightDist)
let isCloseClick = false
if (Math.abs(leftDist) < maxDist && betweenTopBottom) {
isCloseClick = true
}
else if (Math.abs(rightDist) < maxDist && betweenTopBottom) {
isCloseClick = true
}
else if (Math.abs(topDist) < maxDist && betweenLeftRight) {
isCloseClick = true
}
else if (Math.abs(bottomDist) < maxDist && betweenLeftRight) {
isCloseClick = true
}
else if (betweenTopBottom && betweenLeftRight && [leftDist, rightDist, topDist, bottomDist].find(dist => Math.abs(dist) < maxDist * 3)) {
isCloseClick = true
}
if (isCloseClick) {
return
}
}

setMapClickable(false)

if (el.getAttribute('composited')) {
Expand Down Expand Up @@ -1462,7 +1493,7 @@ function gameOver() {
stopTimer()

setMapClickable(false)
var els = document.querySelectorAll('#map .guessed')
var els = document.querySelectorAll('#map .guessed:not(.guessed-animation)')

// TODO constants
var timer = 300
Expand Down