Amazing-Python-Scripts
110 строк · 2.7 Кб
1(function(){2window.ScoreModel = function(options){3var self = this;4
5self._points = 0;6self._multiplier = 1;7self._maxMultiplier = 20;8self._enemyChain = self._threshold = 10;9self._totalEnemies = 0;10
11self._difficultyLevel = 0;12self._difficultyChain = 0;13self.increaseDifficulty = options.increaseDifficulty;14
15self._powerupLevel = 0;16self._powerupChain = 0;17self.increasePowerup = options.increasePowerup;18
19self._previousHighScore = 0;20
21self._killChain = 0;22
23// Stats24
25self._bulletsFired = 0;26self._enemiesKilled = 0;27self._enemiesEscaped = 0;28self._longestKillChain = 0;29
30// Load highscore from local storage31var storedData = localStorage.getItem('gunship');32if(storedData){33self._previousHighScore = (JSON.parse(storedData)).score;34}35
36// Save the current score to storage if it's higher than the37// previous high score38self.save = function(){39if(self.score() > self._previousHighScore){40localStorage.setItem('gunship', JSON.stringify({41date: (new Date()).getTime(),42score: self.score()43}));44}45}46
47// Get the multiplier48self.multiplier = function(){49return self._multiplier;50}51
52// Get the score53self.score = function(){54return self._points;55}56self.highScore = function(){57return self._previousHighScore;58}59self.longestKillChain = function(){60return self._longestKillChain;61}62
63// Reset the miltiplier64self.resetMultiplier = function(){65self._multiplier = 1;66self._enemyChain = self._threshold;67self._powerupChain = 0;68self._killChain = 0;69}70
71// Add points to the model72// Increase the multiplier when 100 points have been added73self.add = function(points){74
75self._points += (points * self._multiplier);76
77// Maintain the killchain values78self._longestKillChain = Math.max(self._longestKillChain, ++self._killChain);79
80if(self._multiplier < self._maxMultiplier && --self._enemyChain == 0){81++self._multiplier;82self._enemyChain = self._threshold;83}84
85// Increment the total enemies86++self._totalEnemies;87
88// When the total enemies killed gets past a89// threshold then callback to make enemies more90// difficult91if(++self._difficultyChain == 10){92self._difficultyChain = 0;93++self._difficultyLevel;94if(self.increaseDifficulty)95self.increaseDifficulty(self._difficultyLevel);96}97
98// If the user gets a kill streak of 20 then99// callback to award an upgrade. Powerup chain is100// reset each time an enemy escapes but already awarded101// powerups stay.102if(++self._powerupChain == 20){103self._powerupChain = 0;104++self._powerupLevel;105if(self.increasePowerup)106self.increasePowerup(self._powerupLevel);107}108}109}110})();