-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathskills.js
134 lines (121 loc) · 4.39 KB
/
skills.js
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
function Skills() {
this.current = null;
this.skills = document.createElement("div");
this.description = document.createElement("fieldset");
this.description.classList.add("description");
this.description.textContent = T("Select skill to see it's description")
this.learnButton = document.createElement("button");
this.learnButton.textContent = T("Learn");
this.learnButton.onclick = this.learn.bind(this);
this.learnButton.disabled = true;
this.panel = new Panel(
"skills",
"Skills",
[
this.skills,
util.hr(),
this.description,
util.hr(),
this.learnButton,
]
);
this.update();
this.panel.hooks.show = this.update.bind(this);
this.hash = "";
}
Skills.prototype = {
update: function() {
if (this.panel && !this.panel.visible)
return;
var hash = JSON.stringify(game.player.Skills) + game.player.LP;
if (hash == this.hash)
return;
this.hash = hash;
this.skills.innerHTML = "";
Object.keys(game.player.Skills).forEach(function(name) {
var skill = game.player.Skills[name];
var item = Stats.prototype.createParam(name, skill.Value);
item.name = name;
item.skill = skill;
item.classList.add("skill");
item.onclick = this.select.bind(this, item);
this.skills.appendChild(item);
if (this.current && this.current.name == name)
this.select(item);
}.bind(this));
},
select: function(item) {
if (this.current) {
this.current.classList.remove("selected");
}
item.classList.add("selected");
this.current = item;
this.showDescription(item);
},
nextLvlOf: function(skill) {
for (var i in Character.skillLvls) {
var next = Character.skillLvls[i];
if (next.Value > skill.Value.Max)
return next;
}
return null;
},
showDescription: function(item) {
var skill = item.skill;
var name = item.name;
var text = "";
if (this.descriptions[name])
text = this.descriptions[name] + "\n\n";
var next = this.nextLvlOf(skill);
if (!next) {
text += T("Skill has it's maximum level");
} else {
text += TT("Unlocks {lvl} lvl of the {skill} skill", {lvl: next.Name, skill: name});
text += "\n\n";
text += T("New maximum value") + ": " + next.Value + "\n";
text += T("Cost") + ": " + next.Cost + "\n";
text += TT("You have {amount} LP", {amount: game.player.LP});
text += "\n";
var diff = next.Cost - game.player.LP;
if(diff > 0) {
text += TT("You need {diff} additional LP to learn this skill", {diff: diff});
this.learnButton.disabled = true;
} else {
this.learnButton.disabled = false;
}
}
this.description.textContent = text;
},
descriptions: {
"Survival": "Survival gives you basic recipes like bonfire",
"Stoneworking": "Stoneworking gives you recipes like sharp stone, stone axe, stone hammer, etc.",
"Lumberjacking": "Lumberjacking gives you an ability to chop trees.",
},
learn: function(e) {
if (!this.current)
game.error("No selected skill");
var name = this.current.name;
var oldRecipes = Object.keys(Entity.Recipes)
game.network.send("learn-skill", {name: name }, function(data) {
//TODO: checkme
if (data.Warning)
return;
var newRecipes = Object.keys(Entity.Recipes).filter(function(i) {
return (oldRecipes.indexOf(i) == -1);
});
var newBuildRecipes = false;
var newCraftRecipes = false;
newRecipes.map(function(recipe) {
if (Entity.Recipes[recipe].IsBuilding)
newBuildRecipes = true;
else
newCraftRecipes = true;
Entity.Recipes[recipe].isNew = true;
});
if (newBuildRecipes)
game.controller.highlight("build");
if (newCraftRecipes)
game.controller.highlight("craft");
}.bind(this));
},
}