Commit c8722292 authored by Matthew Odle's avatar Matthew Odle

move leaderboard and main menu into their own lib files

parent 6e93e5ea
......@@ -2,8 +2,10 @@ var init = {
run : function() {
console.log('init the things, yo');
knobsAndLevers.init();
leaderboard.init();
templates.init();
initials.init();
mainMenu.init();
menus.init();
game.init();
metrics.init();
......
......@@ -39,6 +39,7 @@ var knobsAndLevers = {
height : 800,
gridDivisor : 25,
},
targetLeaderboard : 'centipedeLeaderboard',
centipede : {
baseSpeed : 10,
maxNumber : 10,
......
var leaderboard = {
// TODO leaderboard functions only work on chrome
// either disable the leaderboard menu option when not in chrome
// or figure out firefox storage usage
saveScore : function(initials, score) {
console.log('saving score');
try {
let currentLeaderboard = this.readLeaderboard(this.targetLeaderboard);
let entry = {initials : initials, score : score, when : Date.now()};
if (currentLeaderboard) {
currentLeaderboard.push(entry);
} else {
currentLeaderboard = [entry];
};
localStorage.setItem(this.targetLeaderboard, JSON.stringify(currentLeaderboard));
} catch(e) {
console.log('could not save leaderboard to localStorage', e);
};
},
readLeaderboard : function() {
try {
return JSON.parse(localStorage.getItem(this.targetLeaderboard));
} catch(e) {
console.log('could not load leaderBoard from localStorage', e);
};
},
clearLeaderboard : function() {
try {
localStorage.removeItem(this.targetLeaderboard);
console.log(this.targetLeaderboard, 'removed from local storage');
} catch(e) {
console.log(this.targetLeaderboard, 'not found in localStorage', e);
};
},
init : function() {
this.targetLeaderboard = knobsAndLevers.targetLeaderboard;
},
};
......@@ -101,38 +101,4 @@ var main = {
players.manage();
collisions.check();
},
// TODO leaderboard functions only work on chrome
// either disable the leaderboard menu option when not in chrome
// or figure out firefox storage usage
saveScore : function(initials) {
console.log('saving score');
try {
let currentLeaderboard = this.readLeaderboard();
let score = {initials : initials, score : metrics.lastScore, when : Date.now()};
if (currentLeaderboard) {
currentLeaderboard.push(score);
} else {
currentLeaderboard = [score];
};
localStorage.setItem('centipedeLeaderboard', JSON.stringify(currentLeaderboard));
} catch(e) {
console.log('could not save leaderboard to localStorage', e);
};
},
readLeaderboard : function() {
try {
return JSON.parse(localStorage.getItem('centipedeLeaderboard'));
} catch(e) {
console.log('could not load leaderBoard from localStorage', e);
};
},
clearLeaderboard : function() {
let key = 'centipedeLeaderboard';
try {
localStorage.removeItem(key);
console.log(key, 'removed from local storage');
} catch(e) {
console.log(key, 'not found in localStorage', e);
};
},
};
......@@ -72,7 +72,7 @@ var initials = {
xAdjust : 175,
yAdjust : -125,
submit : function() {
main.saveScore(menus.screens.initials.getEntries());
leaderboard.saveScore(menus.screens.initials.getEntries(), metrics.lastScore);
menus.display('main');
},
action : function() {
......@@ -92,19 +92,15 @@ var initials = {
this.entries.right.options = this.options.slice();
},
text : {
entries : [
{
name : 'enterInitials',
text : 'Enter your initials',
fontSize : '25px',
},
{
name : 'currentScore',
text : '',
xAdjust : 85,
yAdjust : 50,
fontSize : '20px',
},
],
enterInitials : {
text : 'Enter your initials',
fontSize : '25px',
},
currentScore : {
text : '',
xAdjust : 85,
yAdjust : 50,
fontSize : '20px',
},
},
};
mainMenu = {
order : [
'play',
'instructions',
'settings',
'cheats',
],
entries : {
play : {
text : 'PLAY',
action : function() {
menus.display('playerSelect');
},
},
instructions : {
text : 'INSTRUCTIONS',
action : function() {
menus.display('instructions');
},
},
settings : {
text : 'SETTINGS',
action : function() {
menus.display('settings');
},
},
cheats : {
text : 'CHEATS',
action : function() {
menus.display('cheats');
},
},
},
text : {
entries : [],
},
init : function() {
this.setLeaderboardTexts();
},
refresh : function() {
this.setLeaderboardTexts();
},
setLeaderboardTexts : function() {
let leaderboards = leaderboard.readLeaderboard('centipedeLeaderboard');
if (!leaderboards) {
return;
};
this.text.entries = [];
let entriesSoFar = 0;
let text = '';
supporting.fieldToCompare = 'score';
leaderboards.sort(supporting.compare).forEach((entry, index) => {
entriesSoFar = this.text.entries.length;
text = entry.initials + ': ' + entry.score;
if (index < 10) {
this.text.entries.push(this.buildEntry(text, entriesSoFar));
};
});
},
buildEntry : function(text, count) {
return {
text : text,
xAdjust : 175,
};
},
};
menus = {
leaderboards : undefined,
currentSelection : undefined,
init : function() {
Object.assign(this, menusProps);
this.screens.initials = initials;
this.screens.main = mainMenu;
this.selectionMarker = Object.assign({}, templates.marker);
console.log('menus initialized');
},
......@@ -31,8 +31,7 @@ menus = {
if (this.show.initials) {
this.manageInitials();
} else if (this.show.main) {
this.leaderboards = main.readLeaderboard();
this.setLeaderboardTexts();
this.screens.main.refresh();
} else if (this.show.playerActivate) {
this.setGamepadText();
};
......@@ -63,7 +62,7 @@ menus = {
return;
};
this.screens.initials.text.entries[1].text = 'your score ' + metrics.lastScore;
this.screens.initials.text.currentScore.text = 'your score ' + metrics.lastScore;
this.shiftListOrder(this.currentSelection.entry.options);
let order = this.currentSelection.entry.options.slice();
......@@ -209,39 +208,6 @@ menus = {
};
};
},
addInitials : function(initial) {
let theText = this.screens.initials.text.entries[2].text;
if (theText.length < 3) {
theText += initial;
} else {
main.saveScore(theText);
this.show.main = true;
this.reset();
};
this.screens.initials.text.entries[2].text = theText;
},
setLeaderboardTexts : function() {
if (!this.leaderboards) {
return;
};
this.screens.main.text.entries = [];
let entriesSoFar = 0;
let text = '';
supporting.fieldToCompare = 'score';
this.leaderboards.sort(supporting.compare).forEach((entry, index) => {
entriesSoFar = this.screens.main.text.entries.length;
text = entry.initials + ': ' + entry.score;
if (index < 10) {
this.screens.main.text.entries.push(this.buildEntry(text, entriesSoFar));
};
});
},
buildEntry : function(text, count) {
return {
text : text,
xAdjust : 175,
};
},
setGamepadText : function() {
let gamepadsEnabled = controls.gamepad.enabledGamepadIndices.size;
let playerActivateEntries = this.screens.playerActivate.text.entries;
......
......@@ -135,43 +135,6 @@ var menusProps = {
],
},
},
main : {
order : [
'play',
'instructions',
'settings',
'cheats',
],
entries : {
play : {
text : 'PLAY',
action : function() {
menus.display('playerSelect');
},
},
instructions : {
text : 'INSTRUCTIONS',
action : function() {
menus.display('instructions');
},
},
settings : {
text : 'SETTINGS',
action : function() {
menus.display('settings');
},
},
cheats : {
text : 'CHEATS',
action : function() {
menus.display('cheats');
},
},
},
text : {
entries : [],
},
},
playerActivate : {
order : [
'check',
......
......@@ -37,9 +37,11 @@
<script src="app/scripts/game.js"></script>
<script src="app/scripts/hud.js"></script>
<script src="app/scripts/knobs-and-levers.js"></script>
<script src="app/scripts/leaderboard.js"></script>
<script src="app/scripts/main.js"></script>
<script src="app/scripts/menus/properties/menus-props.js"></script>
<script src="app/scripts/menus/initials.js"></script>
<script src="app/scripts/menus/main-menu.js"></script>
<script src="app/scripts/menus/menus.js"></script>
<script src="app/scripts/sound.js"></script>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment