Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating game.js to ES6 #151

Open
wants to merge 1 commit into
base: gh-pages
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 37 additions & 37 deletions js/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var game = {
},

setHandlers: function() {
$('#next').on('click', function() {
$('#next').on('click', () => {
$('#code').focus();

if ($(this).hasClass('disabled')) {
Expand All @@ -58,7 +58,7 @@ var game = {
}, 2000);
});

$('#code').on('keydown', function(e) {
$('#code').on('keydown', (e) => {
if (e.keyCode === 13) {

if (e.ctrlKey || e.metaKey) {
Expand All @@ -85,15 +85,15 @@ var game = {
}
}
}).on('input', game.debounce(game.check, 500))
.on('input', function() {
.on('input', () => {
game.changed = true;
});

$('#editor').on('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function() {
$('#editor').on('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', () => {
$(this).removeClass();
});

$('#labelReset').on('click', function() {
$('#labelReset').on('click', () => {
var warningReset = messages.warningReset[game.language] || messages.warningReset.en;
var r = confirm(warningReset);

Expand All @@ -107,16 +107,16 @@ var game = {
}
});

$('#labelSettings').on('click', function() {
$('#labelSettings').on('click', () => {
$('#levelsWrapper').hide();
$('#settings .tooltip').toggle();
})

$('#language').on('change', function() {
$('#language').on('change', () => {
window.location.hash = $(this).val();
});

$('#difficulty').on('change', function() {
$('#difficulty').on('change', () => {
game.difficulty = $('input:checked', '#difficulty').val();

// setting height will prevent a slight jump when the animation starts
Expand All @@ -131,7 +131,7 @@ var game = {
}
});

$('#colorblind').on('change', function() {
$('#colorblind').on('change', () => {
game.colorblind = $('input:checked', '#colorblind').val();

if (game.colorblind == 'true') {
Expand All @@ -141,21 +141,21 @@ var game = {
}
});

$('body').on('click', function() {
$('body').on('click', () => {
$('.tooltip').hide();
});

$('.tooltip, .toggle, #level-indicator').on('click', function(e) {
$('.tooltip, .toggle, #level-indicator').on('click', (e) => {
e.stopPropagation();
});

$(window).on('beforeunload', function() {
$(window).on('beforeunload', () => {
game.saveAnswer();
localStorage.setItem('level', game.level);
localStorage.setItem('answers', JSON.stringify(game.answers));
localStorage.setItem('solved', JSON.stringify(game.solved));
localStorage.setItem('colorblind', JSON.stringify(game.colorblind));
}).on('hashchange', function() {
}).on('hashchange', () => {
game.language = window.location.hash.substring(1) || 'en';
game.translate();

Expand All @@ -171,14 +171,14 @@ var game = {
});
},

prev: function() {
prev: () => {
this.level--;

var levelData = levels[this.level];
this.loadLevel(levelData);
},

next: function() {
next: () => {
if (this.difficulty === "hard") {
this.level = Math.floor(Math.random()* levels.length)
} else {
Expand All @@ -189,7 +189,7 @@ var game = {
this.loadLevel(levelData);
},

loadMenu: function() {
loadMenu: () => {
levels.forEach(function(level, i) {
var levelMarker = $('<span/>').addClass('level-marker').attr({'data-level': i, 'title': level.name}).text(i+1);

Expand All @@ -200,20 +200,20 @@ var game = {
levelMarker.appendTo('#levels');
});

$('.level-marker').on('click', function() {
$('.level-marker').on('click', () => {
game.saveAnswer();

var level = $(this).attr('data-level');
game.level = parseInt(level, 10);
game.loadLevel(levels[level]);
});

$('#level-indicator').on('click', function() {
$('#level-indicator').on('click', () => {
$('#settings .tooltip').hide();
$('#levelsWrapper').toggle();
});

$('.arrow.left').on('click', function() {
$('.arrow.left').on('click', () => {
if ($(this).hasClass('disabled')) {
return;
}
Expand All @@ -222,7 +222,7 @@ var game = {
game.prev();
});

$('.arrow.right').on('click', function() {
$('.arrow.right').on('click', () => {
if ($(this).hasClass('disabled')) {
return;
}
Expand All @@ -232,7 +232,7 @@ var game = {
});
},

loadLevel: function(level) {
loadLevel: (level) => {
$('#editor').show();
$('#share').hide();
$('#background, #pond').removeClass('wrap').attr('style', '').empty();
Expand Down Expand Up @@ -302,14 +302,14 @@ var game = {
game.check();
},

loadDocs: function() {
$('#instructions code').each(function() {
loadDocs: () => {
$('#instructions code').each(() => {
var code = $(this);
var text = code.text();

if (text in docs) {
code.addClass('help');
code.on('mouseenter', function(e) {
code.on('mouseenter', (e) => {
if ($('#instructions .tooltip').length === 0) {
var html = docs[text][game.language] || docs[text].en;
var tooltipX = code.offset().left;
Expand All @@ -323,23 +323,23 @@ var game = {
});
},

applyStyles: function() {
applyStyles: () => {
var level = levels[game.level];
var code = $('#code').val();
var selector = level.selector || '';
$('#pond ' + selector).attr('style', code);
game.saveAnswer();
},

check: function() {
check: () => {
game.applyStyles();

var level = levels[game.level];
var lilypads = {};
var frogs = {};
var correct = true;

$('.frog').each(function() {
$('.frog').each( () => {
var position = $(this).position();
position.top = Math.floor(position.top);
position.left = Math.floor(position.left);
Expand All @@ -349,7 +349,7 @@ var game = {
frogs[key] = val;
});

$('.lilypad').each(function() {
$('.lilypad').each( () => {
var position = $(this).position();
position.top = Math.floor(position.top);
position.left = Math.floor(position.left);
Expand Down Expand Up @@ -388,16 +388,16 @@ var game = {
}
},

saveAnswer: function() {
saveAnswer: () => {
var level = levels[this.level];
game.answers[level.name] = $('#code').val();
},

tryagain: function() {
tryagain: () => {
$('#editor').addClass('animated shake');
},

win: function() {
win: () => {
var solution = $('#code').val();

this.loadLevel(levelWin);
Expand All @@ -408,14 +408,14 @@ var game = {
$('.frog .bg').removeClass('pulse').addClass('bounce');
},

transform: function() {
transform: () => {
var scale = 1 + ((Math.random() / 5) - 0.2);
var rotate = 360 * Math.random();

return {'transform': 'scale(' + scale + ') rotate(' + rotate + 'deg)'};
},

translate: function() {
translate: () => {
document.title = messages.title[game.language] || messages.title.en;
$('html').attr('lang', game.language);

Expand All @@ -424,7 +424,7 @@ var game = {
$('#instructions').html(instructions);
game.loadDocs();

$('.translate').each(function() {
$('.translate').each( () => {
var label = $(this).attr('id');
if (messages[label]) {
var text = messages[label][game.language] || messages[label].en;
Expand All @@ -434,11 +434,11 @@ var game = {
});
},

debounce: function(func, wait, immediate) {
debounce: (func, wait, immediate) => {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
var later = () => {
timeout = null;
if (!immediate) func.apply(context, args);
};
Expand All @@ -450,6 +450,6 @@ var game = {
}
};

$(document).ready(function() {
$(document).ready(() => {
game.start();
});