-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathachievements.ts
More file actions
82 lines (82 loc) · 3.44 KB
/
achievements.ts
File metadata and controls
82 lines (82 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* A namespace that handles achievement getting, setting, remembering, and displaying!
*/
//% color="#009900"
namespace Achievements {
/**
* Show an achievement.
* @param displayName: The name of the achievement that is shown to the player.
* @param displayDescription: The discription of the achievement that is shown to the player - optional.
* @param icon: The icon of the achievement when shown to the player, must be 8x8 otherwise ignored.
*/
//% block="show achievement with title as $displayName||and description as $displayDescription|at speed $speed|and icon as $icon"
//% icon.shadow=screen_image_picker
//% speed.defl=1
//% expandableArgumentMode="enabled"
//% weight=90
export function showAchievement(displayName: string, displayDescription?: string, speed?: number, icon?: Image) {
if (!(displayDescription)) {
displayDescription = ""
}
Notification.waitForNotificationFinish()
if (displayDescription == "") {
Notification.notify("Achievement get: " + displayName + "!", speed, icon)
} else {
Notification.notify("Achievement get! " + displayName + ": " + displayDescription, speed, icon)
}
}
/**
* Check for an achievement.
* @param achievementName: The name of the achievement that is used internally.
* @param condition: Whether the achievement conditions are true, could be the result of a
* function or whether a number is greater than some other number.
* @return: Returns a boolean depending on whether this is the first time the condition is true.
*/
//% block="check for achievement named $achievementName in condition $condition"
//% icon.shadow=screen_image_picker
//% expandableArgumentMode="enabled"
//% weight=100
export function checkForAchievement(achievementName: string, condition: boolean = false) {
achievementName = "achievement_" + achievementName
let value = 1
if (!(condition)) {
value = 0
}
if (!(blockSettings.exists(achievementName))) {
blockSettings.writeNumber(achievementName, 0)
}
if (value == 1 && blockSettings.readNumber(achievementName) == 0) {
blockSettings.writeNumber(achievementName, 1)
return true
}
return false
}
/**
* Reset an achievement.
* @param achievementName: The name of the achievement that is used internally, does nothing if not found.
*/
//% block="reset achievement named $achievementName"
//% weight=80
export function resetAchievement(achievementName: string) {
achievementName = "achievement_" + achievementName
if (blockSettings.exists(achievementName) && blockSettings.readNumber(achievementName) == 1) {
blockSettings.writeNumber(achievementName, 0)
}
}
/**
* Reset all achievements.
*/
//% block="reset all achievements"
//% weight=70
export function resetAllAchievements() {
function startsWith(startString: string, search: string, rawPos?: number) {
let pos = rawPos > 0 ? rawPos|0 : 0;
return startString.substr(pos, pos + search.length) === search;
}
for (let name of blockSettings.list()) {
if (startsWith(name, "achievement_") && blockSettings.readNumber(name) == 1) {
blockSettings.writeNumber(name, 0)
}
}
}
}