Skip to content

Commit

Permalink
Optimización y correción
Browse files Browse the repository at this point in the history
  • Loading branch information
pabloneighbour committed Oct 3, 2023
1 parent bc041ee commit 83f89b5
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 100 deletions.
3 changes: 0 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const createWindow = () => {
width: 400,
height: 600,
resizable: false,
icon: './page/source/7hp3.png',
fullscreenable: false
})
window.setMenuBarVisibility(false)
Expand All @@ -14,12 +13,10 @@ const createWindow = () => {

window.on('blur', () => {
window.setOpacity(0.5)
console.log('blur')
})

window.on('focus', () => {
window.setOpacity(1)
console.log('focus')
})

window.loadFile('./page/index.html')
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "arty-calculator",
"version": "1.0.0",
"version": "1.0.1",
"description": "Calculadora de arty",
"main": "main.js",
"scripts": {
Expand Down
94 changes: 0 additions & 94 deletions page/calculadora.js

This file was deleted.

2 changes: 1 addition & 1 deletion page/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</div>

<div class="cordsInput-conteiner">
<label>Coordenadas de la artilleria</label>
<label>Coordenadas de la artillería</label>
<input type="number" class="cordsInput inputElement" id="distArty" min="0" placeholder="Distancia">
<input type="number" class="cordsInput inputElement" id="azmArty" min="0" max="359.9" placeholder="Azimuth">
</div>
Expand Down
95 changes: 94 additions & 1 deletion page/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,103 @@
// Arty Type handler
const getOffset = (gun, wind) => {
const gunTypes = ['120mm', '150mm', 'rkt']
return (gunTypes.includes(gun) ? 25 * wind : gun === '120mm-gb' ? 10 * wind : gun === '300mm' ? 50 * wind : 0)
}

const getVector = (dist, azm) => {
return [(dist * Math.cos(azm * Math.PI / 180)), (dist * Math.sin(azm * Math.PI / 180))]
}

const getPolar = (v) => {
const dist = Math.hypot(v[0], v[1])
const azm = Math.atan2(v[1], v[0]) * 180 / Math.PI
return [dist, (azm < 0 ? azm + 360 : azm)]
}

const getWindVector = (wind, azm, gun) => {
const sth = getOffset(gun, wind)
const vWind = getVector(sth, azm)
return vWind
}

const getFireVector = (gunPos, objPos, wind) => {
return [objPos[0] - gunPos[0] - wind[0], objPos[1] - gunPos[1] - wind[1]]
}

const calCords = (gunType, gunPos, objPos, wind) => {
const gunCords = getVector(gunPos[0], gunPos[1])
const objCords = getVector(objPos[0], objPos[1])
const windCords = getWindVector(wind[0], wind[1], gunType)
const fireVector = getPolar(getFireVector(gunCords, objCords, windCords))
return fireVector
}

const inRange = () => {
const distElement = document.getElementById('distFire')
const azmElement = document.getElementById('azmFire')

// Remove outRange class from both elements
distElement.classList.remove('outRange')
azmElement.classList.remove('outRange')

// Get distance and gun type
const dist = Number(distElement.innerText)
const gunType = document.querySelector('.on')?.getAttribute('name') ?? ''

// Check if distance is in range for gun type
const range = {
'120mm': [100, 250],
'150mm': [200, 350],
'300mm': [400, 1000],
'120mm-gb': [50, 100],
rkt: [225, 350]
}

if (!range[gunType] || !dist || dist < range[gunType][0] || dist > range[gunType][1]) {
// Distance is out of range
distElement.classList.add('outRange')
azmElement.classList.add('outRange')
}
}

const getCords = () => {
const gunType = document.querySelector('.on')?.getAttribute('name') ?? ''
if (gunType === '') return
const gunPos = [
Number(document.getElementById('distArty').value),
Number(document.getElementById('azmArty').value)
]
const objPos = [
Number(document.getElementById('distObj').value),
Number(document.getElementById('azmObj').value)
]
const wind = [
Number(document.getElementById('windSth').value),
Number(document.getElementById('azmWind').value)
]

const fireCords = calCords(gunType, gunPos, objPos, wind)

document.getElementById('distFire').textContent = fireCords[0].toFixed(1)
document.getElementById('azmFire').textContent = fireCords[1].toFixed(1)

inRange()
}

// number input handler
const inputNumberElements = document.querySelectorAll('.inputElement')
inputNumberElements.forEach((inputNumber) => {
inputNumber.addEventListener('input', () => getCords())
})

// Arty type
const artyTypeButtons = document.querySelectorAll('.artyType')
artyTypeButtons.forEach((artyType) => {
artyType.addEventListener('click', () => {
artyTypeButtons.forEach((artyTypeOff) => {
artyTypeOff.classList.remove('on')
})
artyType.classList.add('on')
getCords()
})
})

Expand All @@ -15,6 +107,7 @@ const windSthImg = document.querySelector('.windGif')
windSth.addEventListener('input', (event) => {
const valor = event.target.value
windSthImg.src = `./source/w${valor}.gif`
getCords()
})

// Copy Button
Expand Down

0 comments on commit 83f89b5

Please sign in to comment.