-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamicglyph.js
More file actions
81 lines (75 loc) · 3 KB
/
dynamicglyph.js
File metadata and controls
81 lines (75 loc) · 3 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
Game.DynamicGlyph = function(properties) {
properties = properties || {};
// Call the glyph's construtor with our set of properties
Game.Glyph.call(this, properties);
// Instantiate any properties from the passed object
this._name = properties['name'] || '';
// Create an object which will keep track what mixins we have
// attached to this entity based on the name property
this._attachedMixins = {};
// Create a similar object for groups
this._attachedMixinGroups = {};
this._description = properties['description'] || '';
// Setup the object's mixins
let mixins = properties['mixins'] || [];
for (let i = 0; i < mixins.length; i++) {
// Copy over all properties from each mixin as long as it's not the name or the init property.
// We also make sure not to override a property that already exists on the entity.
for (let key in mixins[i]) {
if (key != 'init' && key != 'name' && !this.hasOwnProperty(key)) {
this[key] = mixins[i][key];
}
}
// Add the name of this mixin to our attached mixins
this._attachedMixins[mixins[i].name] = true;
// If a group name is present, add it
if (mixins[i].groupName) {
this._attachedMixinGroups[mixins[i].groupName] = true;
}
// Finally call the init function if there is one
if (mixins[i].init) {
mixins[i].init.call(this, properties);
}
}
};
// Make dynamic glyphs inherit all the functionality from glyphs
Game.DynamicGlyph.extend(Game.Glyph);
Game.DynamicGlyph.prototype.hasMixin = function(obj) {
// Allow passing the mixin itself or the name / group name as a string
if (typeof obj === 'object') {
return this._attachedMixins[obj.name];
} else {
return this._attachedMixins[obj] || this._attachedMixinGroups[obj];
}
};
Game.DynamicGlyph.prototype.setName = function(name) {
this._name = name;
};
Game.DynamicGlyph.prototype.getName = function() {
return this._name;
};
Game.DynamicGlyph.prototype.describe = function() {
return this._name;
};
Game.DynamicGlyph.prototype.describeA = function(capitalize) {
// Optional parameter to capitalize the a/an.
let prefixes = capitalize ? ['A', 'An'] : ['a', 'an'];
let string = this.describe();
if (string === 'cerberus' || string === 'death' || string === 'gold'){
return string;
}
let firstLetter = string.charAt(0).toLowerCase();
// If word starts by a vowel, use an, else use a. Note that this is not perfect.
let prefix = 'aeiou'.indexOf(firstLetter) >= 0 ? 1 : 0;
return prefixes[prefix] + ' ' + string;
};
Game.DynamicGlyph.prototype.describeThe = function(capitalize) {
const prefix = capitalize ? 'The' : 'the';
if (this.describe() === 'cerberus' || this.describe() === 'death'){
return this.describe();
}
return prefix + ' ' + this.describe();
};
Game.DynamicGlyph.prototype.getDescription = function() {
return this._description;
};