Skip to content

Commit

Permalink
Merge pull request #22 from denisnarush/develop
Browse files Browse the repository at this point in the history
Master < [#20], [#21], version
  • Loading branch information
denisnarush committed May 5, 2020
2 parents b7de613 + 1ef4f4c commit 605a2c9
Show file tree
Hide file tree
Showing 15 changed files with 106 additions and 16 deletions.
1 change: 1 addition & 0 deletions assets/i18n/EN.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

"GAME OVER": "GAME OVER",
"SCORE": "SCORE",
"SCOREBOARD": "SCOREBOARD",

"CPM": "CPM",
"MODE": "MODE",
Expand Down
1 change: 1 addition & 0 deletions assets/i18n/RU.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

"GAME OVER": "ИГРА ОКОНЧЕНА",
"SCORE": "СЧЕТ",
"SCOREBOARD": "ТАБЛО",

"CPM": "СВМ",
"MODE": "РЕЖИМ",
Expand Down
3 changes: 3 additions & 0 deletions cs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions helpers/draw-menu.helper.js
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
1 change: 1 addition & 0 deletions helpers/index.js
Original file line number Diff line number Diff line change
@@ -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"
15 changes: 15 additions & 0 deletions helpers/random-with-except.helper.js
Original file line number Diff line number Diff line change
@@ -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;
}
17 changes: 17 additions & 0 deletions helpers/settings.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
<title>Chars Score</title>
</head>
<body>
<script src="./cs.js?v=1.3.2" type="module"></script>
<script src="./cs.js?v=1.3.3" type="module"></script>
</body>
</html>
1 change: 0 additions & 1 deletion modules/game.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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%";
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": "chars-score",
"version": "1.3.2",
"version": "1.3.3",
"description": "Chars shooter game",
"scripts": {
},
Expand Down
5 changes: 1 addition & 4 deletions states/game-over.state.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
32 changes: 26 additions & 6 deletions states/game.state.js
Original file line number Diff line number Diff line change
@@ -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()
};
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions states/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
2 changes: 1 addition & 1 deletion states/menu.state.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
36 changes: 36 additions & 0 deletions states/scoreboard.state.js
Original file line number Diff line number Diff line change
@@ -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]}`
]);
}
}

0 comments on commit 605a2c9

Please sign in to comment.