Amazing-Python-Scripts

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

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.