mirror of
https://github.com/danbee/invaders
synced 2025-03-04 08:39:08 +00:00
Save high score to cookie.
This commit is contained in:
parent
c3d22cf3f0
commit
5138ebdb60
@ -6,11 +6,11 @@
|
||||
<!-- Always force latest IE rendering engine or request Chrome Frame -->
|
||||
<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
|
||||
|
||||
<!-- Use title if it's in the page YAML frontmatter -->
|
||||
<title>NB Invaders</title>
|
||||
|
||||
<link href="stylesheets/all.css" rel="stylesheet" type="text/css" />
|
||||
<script src="javascripts/lib/phaser.js" type="text/javascript"></script>
|
||||
<script src="javascripts/lib/cookies.min.js" type="text/javascript"></script>
|
||||
</head>
|
||||
|
||||
<body class="index">
|
||||
|
||||
@ -14,6 +14,9 @@ var bulletTime = 0,
|
||||
score = 0,
|
||||
highScore = 0;
|
||||
|
||||
var style = { font: "32px silkscreen", fill: "#666666", align: "center" },
|
||||
boldStyle = { font: "bold 32px silkscreen", fill: "#ffffff", align: "center" };
|
||||
|
||||
function create () {
|
||||
game.physics.startSystem(Phaser.Physics.ARCADE);
|
||||
|
||||
@ -61,7 +64,6 @@ function create () {
|
||||
explosions.forEach(setupExplosion, this);
|
||||
|
||||
// Text bits
|
||||
var style = { font: "32px silkscreen", fill: "#666666", align: "right" };
|
||||
livesText = game.add.text(game.world.bounds.width - 16, 16, "LIVES: " + lives, style);
|
||||
livesText.anchor.set(1, 0);
|
||||
|
||||
@ -71,11 +73,14 @@ function create () {
|
||||
highScoreText = game.add.text(16, 16, '', style);
|
||||
highScoreText.anchor.set(0, 0);
|
||||
|
||||
getHighScore();
|
||||
|
||||
updateScore();
|
||||
|
||||
// Setup controls
|
||||
cursors = game.input.keyboard.createCursorKeys();
|
||||
fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
|
||||
restartButton = game.input.keyboard.addKey(Phaser.Keyboard.S);
|
||||
}
|
||||
|
||||
function setupExplosion (explosion) {
|
||||
@ -90,6 +95,11 @@ function update () {
|
||||
fireBullet();
|
||||
}
|
||||
|
||||
// Restart?
|
||||
if (restartButton.isDown && lives == 0) {
|
||||
restartGame();
|
||||
}
|
||||
|
||||
// Handle aliens dropping bombs
|
||||
handleBombs();
|
||||
|
||||
@ -170,6 +180,13 @@ function updateLivesText () {
|
||||
livesText.text = "LIVES: " + lives;
|
||||
}
|
||||
|
||||
function getHighScore () {
|
||||
savedHighScore = Cookies.get('highScore');
|
||||
if (savedHighScore != undefined) {
|
||||
highScore = savedHighScore;
|
||||
}
|
||||
}
|
||||
|
||||
function updateScore () {
|
||||
if (score > highScore) {
|
||||
highScore = score;
|
||||
@ -192,10 +209,28 @@ function newWave () {
|
||||
}, 1000);
|
||||
}
|
||||
|
||||
function restartGame () {
|
||||
gameOverText.destroy();
|
||||
restartText.destroy();
|
||||
|
||||
lives = 3
|
||||
score = 0
|
||||
updateScore();
|
||||
updateLivesText();
|
||||
|
||||
respawnPlayer();
|
||||
newWave();
|
||||
}
|
||||
|
||||
function gameOver () {
|
||||
var style = { font: "bold 32px silkscreen", fill: "#ffffff", align: "center" };
|
||||
livesText = game.add.text(game.world.centerX, game.world.centerY, "GAME OVER", style);
|
||||
livesText.anchor.set(0.5, 0.5);
|
||||
setTimeout(function() {
|
||||
gameOverText = game.add.text(game.world.centerX, game.world.centerY, "GAME OVER", boldStyle);
|
||||
gameOverText.anchor.set(0.5, 0.5);
|
||||
restartText = game.add.text(game.world.centerX, game.world.height - 16, "PRESS 'S' TO RESTART", style);
|
||||
restartText.anchor.set(0.5, 1);
|
||||
|
||||
Cookies.set('highScore', highScore, { expires: '2078-12-31' });
|
||||
}, 1000);
|
||||
}
|
||||
|
||||
function createAliens () {
|
||||
|
||||
2
javascripts/lib/cookies.min.js
vendored
Normal file
2
javascripts/lib/cookies.min.js
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
/*! Cookies.js - 0.4.0; Copyright (c) 2014, Scott Hamper; http://www.opensource.org/licenses/MIT */
|
||||
(function(e){"use strict";var b=function(a,d,c){return 1===arguments.length?b.get(a):b.set(a,d,c)};b._document=document;b._navigator=navigator;b.defaults={path:"/"};b.get=function(a){b._cachedDocumentCookie!==b._document.cookie&&b._renewCache();return b._cache[a]};b.set=function(a,d,c){c=b._getExtendedOptions(c);c.expires=b._getExpiresDate(d===e?-1:c.expires);b._document.cookie=b._generateCookieString(a,d,c);return b};b.expire=function(a,d){return b.set(a,e,d)};b._getExtendedOptions=function(a){return{path:a&& a.path||b.defaults.path,domain:a&&a.domain||b.defaults.domain,expires:a&&a.expires||b.defaults.expires,secure:a&&a.secure!==e?a.secure:b.defaults.secure}};b._isValidDate=function(a){return"[object Date]"===Object.prototype.toString.call(a)&&!isNaN(a.getTime())};b._getExpiresDate=function(a,d){d=d||new Date;switch(typeof a){case "number":a=new Date(d.getTime()+1E3*a);break;case "string":a=new Date(a)}if(a&&!b._isValidDate(a))throw Error("`expires` parameter cannot be converted to a valid Date instance"); return a};b._generateCookieString=function(a,b,c){a=a.replace(/[^#$&+\^`|]/g,encodeURIComponent);a=a.replace(/\(/g,"%28").replace(/\)/g,"%29");b=(b+"").replace(/[^!#$&-+\--:<-\[\]-~]/g,encodeURIComponent);c=c||{};a=a+"="+b+(c.path?";path="+c.path:"");a+=c.domain?";domain="+c.domain:"";a+=c.expires?";expires="+c.expires.toUTCString():"";return a+=c.secure?";secure":""};b._getCookieObjectFromString=function(a){var d={};a=a?a.split("; "):[];for(var c=0;c<a.length;c++){var f=b._getKeyValuePairFromCookieString(a[c]); d[f.key]===e&&(d[f.key]=f.value)}return d};b._getKeyValuePairFromCookieString=function(a){var b=a.indexOf("="),b=0>b?a.length:b;return{key:decodeURIComponent(a.substr(0,b)),value:decodeURIComponent(a.substr(b+1))}};b._renewCache=function(){b._cache=b._getCookieObjectFromString(b._document.cookie);b._cachedDocumentCookie=b._document.cookie};b._areEnabled=function(){var a="1"===b.set("cookies.js",1).get("cookies.js");b.expire("cookies.js");return a};b.enabled=b._areEnabled();"function"===typeof define&& define.amd?define(function(){return b}):"undefined"!==typeof exports?("undefined"!==typeof module&&module.exports&&(exports=module.exports=b),exports.Cookies=b):window.Cookies=b})();
|
||||
Loading…
Reference in New Issue
Block a user