master
Ondřej Hruška 5 years ago
parent 6f10b87577
commit c4e86ef1a4
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 81
      script.js

@ -1,6 +1,6 @@
!(function() {
!(function () {
class Svg {
class Svg {
/**
* Build a node from XML
*
@ -38,70 +38,70 @@ class Svg {
static fromXMLg(code, wrapperOpts = null) {
return Svg.fromXML(code, 'g', wrapperOpts);
}
}
}
/* --------- Shared Constants --------- */
const BOARD_SIZE = 121;
/* --------- Shared Constants --------- */
const BOARD_SIZE = 121;
const METAL_SEQ = ['lead', 'tin', 'iron', 'copper', 'silver', 'gold'];
const METAL_SEQ = ['lead', 'tin', 'iron', 'copper', 'silver', 'gold'];
const SYMBOLS_METALS = [
const SYMBOLS_METALS = [
'mercury', 'lead', 'tin', 'iron', 'copper', 'silver', 'gold'
];
];
const SYMBOLS_ALL = [
const SYMBOLS_ALL = [
'salt', 'air', 'fire', 'water', 'earth', 'mercury', 'lead',
'tin', 'iron', 'copper', 'silver', 'gold', 'mors', 'vitae'
];
];
/**
/**
* Convert grid coordinates to gameGrid array index
*
* @param {Number} x
* @param {Number} y
* @returns {Number}
*/
function xyToGridIndex(x, y) {
function xyToGridIndex(x, y) {
return y * 11 + x
}
}
/**
/**
* Convert grid index to X, Y
*
* @param {Number} index
* @returns {{x: Number, y: Number}}
*/
function gridIndexToXy(index) {
function gridIndexToXy(index) {
return {
x: index % 11,
y: Math.floor(index / 11)
};
}
}
/**
/**
* Get if a coordinate is outside the game board.
*
* @param x
* @param y
* @return {boolean|boolean}
*/
function isXyOutside(x, y) {
function isXyOutside(x, y) {
return x < 0
|| x > 10
|| y < 0
|| y > 10
|| (y <= 5 && x > 5 + y)
|| (y > 5 && x < y - 5);
}
}
/**
/**
* Game board
*
* Orb grid coordinates:
* x - grid X coordinate (counted from the left edge of a triangle starting in the top left corner)
* y - grid Y coordinate (row)
*/
class Board {
class Board {
constructor() {
this.$bg = document.getElementById('boardbg');
this.$orbs = document.getElementById('orbs');
@ -601,14 +601,14 @@ class Board {
getOrbByIndex(n) {
return this.grid[n];
}
}
}
/**
/**
* Random number generator
*
* Uses Mullbery32 from https://stackoverflow.com/a/47593316/2180189
*/
class Rng {
class Rng {
/**
* Construct with a given or random seed
*
@ -651,9 +651,9 @@ class Rng {
nextInt(max) {
return Math.floor((max + 1) * this.next());
}
}
}
class SettingsStorage {
class SettingsStorage {
constructor() {
// this object is never overwritten, references are stable
this.settings = {
@ -695,9 +695,9 @@ class SettingsStorage {
save() {
localStorage.setItem('sigmar_settings', JSON.stringify(this.settings));
}
}
}
class Game {
class Game {
/**
* Init the game
*/
@ -944,7 +944,7 @@ class Game {
const allo = template.includes(i);
allowedTable.push(allo);
let { x, y } = gridIndexToXy(i);
let {x, y} = gridIndexToXy(i);
if (!allo && !isXyOutside(x, y)) {
outsideTemplate.push(i);
}
@ -973,7 +973,7 @@ class Game {
this.board.grid[n] = null;
};
const findAvailableIndexWithNeighbours = (count, except=null) => {
const findAvailableIndexWithNeighbours = (count, except = null) => {
let candidates = [];
for (let i = 0; i < template.length; i++) {
const n = template[i];
@ -994,7 +994,7 @@ class Game {
}
};
const findAvailableIndex = (except=null) => {
const findAvailableIndex = (except = null) => {
for (let i = 6; i >= 0; i--) {
const n = findAvailableIndexWithNeighbours(i, except);
if (n !== false) return n;
@ -1277,19 +1277,16 @@ class Game {
this.board.removeOrbByIndex(n);
this.selectedOrb = null;
wantRefresh = true;
}
else if (this.selectedOrb === null) {
} else if (this.selectedOrb === null) {
// first selection
this.selectedOrb = { n, orb };
this.selectedOrb = {n, orb};
orb.node.classList.add('selected');
}
else {
} else {
if (this.selectedOrb.n === n) {
// orb clicked twice
orb.node.classList.remove('selected');
this.selectedOrb = null;
}
else {
} else {
// second orb in a pair
const otherSymbol = this.selectedOrb.orb.symbol;
@ -1308,7 +1305,7 @@ class Game {
} else {
// Bad selection, select it as the first orb.
this.selectedOrb.orb.node.classList.remove('selected');
this.selectedOrb = { n, orb };
this.selectedOrb = {n, orb};
orb.node.classList.add('selected');
}
}
@ -1395,10 +1392,10 @@ class Game {
this.nextMetal = METAL_SEQ[METAL_SEQ.indexOf(this.nextMetal) + 1];
console.debug(`Next metal unlocked: ${this.nextMetal}`);
}
}
}
/* Start */
/* Start */
window.game = new Game();
window.game = new Game();
})();

Loading…
Cancel
Save