diff --git a/assets/i18n/EN.json b/assets/i18n/EN.json index c8d98ce..5475712 100644 --- a/assets/i18n/EN.json +++ b/assets/i18n/EN.json @@ -13,6 +13,7 @@ "GAME OVER": "GAME OVER", "SCORE": "SCORE", + "SCOREBOARD": "SCOREBOARD", "CPM": "CPM", "MODE": "MODE", diff --git a/assets/i18n/RU.json b/assets/i18n/RU.json index ef11540..6ed8ca6 100644 --- a/assets/i18n/RU.json +++ b/assets/i18n/RU.json @@ -13,6 +13,7 @@ "GAME OVER": "ИГРА ОКОНЧЕНА", "SCORE": "СЧЕТ", + "SCOREBOARD": "ТАБЛО", "CPM": "СВМ", "MODE": "РЕЖИМ", diff --git a/cs.js b/cs.js index c65cd61..d2f8fbe 100644 --- a/cs.js +++ b/cs.js @@ -51,6 +51,9 @@ CSGame.setStateFromMenu = function () { this.MENU_ITEM = 0; return this.setState(this.STATES.OPTIONS_STATE); } + case 2: { + return this.setState(this.STATES.SCOREBOARD_STATE); + } } }; // Restart diff --git a/helpers/draw-menu.helper.js b/helpers/draw-menu.helper.js index 806b488..fe71027 100644 --- a/helpers/draw-menu.helper.js +++ b/helpers/draw-menu.helper.js @@ -1,9 +1,8 @@ export function drawMenu(menu, selectedId) { this.ctx.save(); - this.ctx.font = "italic " + this.ctx.font; menu.forEach((item, id) => { this.ctx.beginPath(); - this.ctx.fillText(`${selectedId === id ? ">" : ""} ${item} ${selectedId === id ? "<" : ""}`, this.widthCenter, this.heightCenter - (16 * 1.4 * 1 - 8 * 1.4 * menu.length % 2) + 16 * 1.4 * id); + this.ctx.fillText(`${selectedId === id ? ">" : ""} ${item} ${selectedId === id ? "<" : ""}`, this.widthCenter, this.heightCenter - (16 * 1.4 * menu.length / 2) + 16 * 1.4 * id + 8 * 1.4); }) this.ctx.closePath(); this.ctx.restore(); diff --git a/helpers/index.js b/helpers/index.js index e841b22..715c4f5 100644 --- a/helpers/index.js +++ b/helpers/index.js @@ -1,3 +1,4 @@ export * from "./nop.helper.js"; export * from "./draw-menu.helper.js"; export * from "./settings.helper.js"; +export * from "./random-with-except.helper.js" diff --git a/helpers/random-with-except.helper.js b/helpers/random-with-except.helper.js new file mode 100644 index 0000000..7627adf --- /dev/null +++ b/helpers/random-with-except.helper.js @@ -0,0 +1,15 @@ +export function randomCharWithExcept(chars, except) { + let result = chars[Math.floor(Math.random() * chars.length)]; + if (result === except) { + result = randomCharWithExcept(chars, except); + } + return result; +} + +export function randomWithExcept(to, except) { + let result = Math.floor(Math.random() * to + 1); + if (result === except) { + result = randomCharWithExcept(to, except); + } + return result; +} diff --git a/helpers/settings.helper.js b/helpers/settings.helper.js index 021b29b..3a72aa2 100644 --- a/helpers/settings.helper.js +++ b/helpers/settings.helper.js @@ -21,6 +21,23 @@ export const SETTINGS = { obj.SCORE = 0; break; } + case 'SCOREBOARD': { + obj.SCOREBOARD = [ + [ + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0] + ], + [ + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0] + ], + [ + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0] + ] + ]; + break; + } case 'LANG': { obj.LANG = 0; break; diff --git a/index.html b/index.html index e9353cc..2f09175 100644 --- a/index.html +++ b/index.html @@ -6,6 +6,6 @@ Chars Score - + \ No newline at end of file diff --git a/modules/game.module.js b/modules/game.module.js index 98bb897..bbcf51b 100644 --- a/modules/game.module.js +++ b/modules/game.module.js @@ -34,7 +34,6 @@ export class G { document.body.style.margin = "0"; document.body.style.height = "100%"; document.body.style.width = "100%"; - document.documentElement.style.cursor = "none"; document.documentElement.style.padding = "0"; document.documentElement.style.margin = "0"; document.documentElement.style.height = "100%"; diff --git a/package.json b/package.json index ac74d40..17f595f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "chars-score", - "version": "1.3.2", + "version": "1.3.3", "description": "Chars shooter game", "scripts": { }, diff --git a/states/game-over.state.js b/states/game-over.state.js index 00fb175..1359ea7 100644 --- a/states/game-over.state.js +++ b/states/game-over.state.js @@ -1,11 +1,8 @@ -import { drawMenu, SETTINGS } from "./../helpers/index.js"; +import { drawMenu } from "./../helpers/index.js"; export const GAME_OVER_STATE = { update: function () { const i18n = (v) => this.i18n(v); - if(this.SCORE > SETTINGS.get("SCORE")) { - SETTINGS.set("SCORE", this.SCORE) - } // BACKSPACE if (this.CODES["8"]) { this.CODES["8"] = false; diff --git a/states/game.state.js b/states/game.state.js index 22568e1..5577cb9 100644 --- a/states/game.state.js +++ b/states/game.state.js @@ -1,15 +1,29 @@ -import { SETTINGS } from "./../helpers/index.js"; +import { SETTINGS, randomCharWithExcept, randomWithExcept } from "./../helpers/index.js"; // Add Char function addChar() { + let c = randomCharWithExcept(this.LANGS_CHARS); + let x = randomWithExcept(40); + + if (this.CHARS.length) { + const last = this.CHARS[this.CHARS.length - 1]; + + if (last.c === c) { + c = randomCharWithExcept(this.LANGS_CHARS, c); + } + + if (last.x === (x * 9.6 - 4.8)) { + x = randomWithExcept(40, x); + } + } // CHAR const char = { - // 0 - 40 col - x: Math.floor(Math.random() * 40 + 1) * 9.6 - 4.8, + // CHAR + c: c, + // COL + x: x * 9.6 - 4.8, // TOP y: 0, - // CHAR - c: this.LANGS_CHARS[Math.floor(Math.random() * this.LANGS_CHARS.length)], // TIME t: performance.now() }; @@ -41,7 +55,13 @@ function updateChars() { char.y = char.y + speed; } // Condition for Game Over - if (char.y >= this.height) { + if (char.y - 5 >= this.height) { + // Scoreboard update + const scoreboard = SETTINGS.get("SCOREBOARD"); + scoreboard[this.MODE][this.LANG].push(this.SCORE); + scoreboard[this.MODE][this.LANG].sort().reverse().pop(); + SETTINGS.set("SCOREBOARD", scoreboard); + return this.setState(this.STATES.GAME_OVER_STATE); }; // Draw Char diff --git a/states/index.js b/states/index.js index 9bb1644..5dbb8b4 100644 --- a/states/index.js +++ b/states/index.js @@ -2,3 +2,4 @@ export * from "./menu.state.js"; export * from "./game.state.js"; export * from "./options.state.js"; export * from "./game-over.state.js"; +export * from "./scoreboard.state.js"; diff --git a/states/menu.state.js b/states/menu.state.js index d29065c..e565b36 100644 --- a/states/menu.state.js +++ b/states/menu.state.js @@ -1,7 +1,7 @@ import { drawMenu } from "./../helpers/index.js"; export const MENU_STATE = { - menu: ["START", "OPTIONS"], + menu: ["START", "OPTIONS", "SCOREBOARD"], update: function () { const i18n = (v) => this.i18n(v); // DOWN diff --git a/states/scoreboard.state.js b/states/scoreboard.state.js new file mode 100644 index 0000000..8076f75 --- /dev/null +++ b/states/scoreboard.state.js @@ -0,0 +1,36 @@ +import { drawMenu, SETTINGS } from "./../helpers/index.js"; + +export const SCOREBOARD_STATE = { + update: function() { + // ENTER + if (this.CODES["13"]) { + this.CODES["13"] = false; + this.MENU_ITEM = 2; + return this.setState(this.STATES.MENU_STATE); + } + // BACKSPACE + if (this.CODES["8"]) { + this.CODES["8"] = false; + this.MENU_ITEM = 2; + return this.setState(this.STATES.MENU_STATE); + } + // ESC + if (this.CODES["27"]) { + this.CODES["27"] = false; + this.MENU_ITEM = 2; + return this.setState(this.STATES.MENU_STATE); + } + + const scoreboard = SETTINGS.get('SCOREBOARD')[this.MODE][this.LANG]; + + drawMenu.call(this, [ + `RANK SCORE`, + `====================`, + ` 1st ${new Array(7 - scoreboard[0].toString().length).join(0) + scoreboard[0]}`, + ` 2nd ${new Array(7 - scoreboard[1].toString().length).join(0) + scoreboard[1]}`, + ` 3rd ${new Array(7 - scoreboard[2].toString().length).join(0) + scoreboard[2]}`, + ` 4th ${new Array(7 - scoreboard[3].toString().length).join(0) + scoreboard[3]}`, + ` 5th ${new Array(7 - scoreboard[4].toString().length).join(0) + scoreboard[4]}` + ]); + } +} \ No newline at end of file