-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
293 lines (242 loc) · 8.1 KB
/
app.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
(function() {
'use strict';
var globals = typeof window === 'undefined' ? global : window;
if (typeof globals.require === 'function') return;
var modules = {};
var cache = {};
var aliases = {};
var has = ({}).hasOwnProperty;
var expRe = /^\.\.?(\/|$)/;
var expand = function(root, name) {
var results = [], part;
var parts = (expRe.test(name) ? root + '/' + name : name).split('/');
for (var i = 0, length = parts.length; i < length; i++) {
part = parts[i];
if (part === '..') {
results.pop();
} else if (part !== '.' && part !== '') {
results.push(part);
}
}
return results.join('/');
};
var dirname = function(path) {
return path.split('/').slice(0, -1).join('/');
};
var localRequire = function(path) {
return function expanded(name) {
var absolute = expand(dirname(path), name);
return globals.require(absolute, path);
};
};
var initModule = function(name, definition) {
var hot = null;
hot = hmr && hmr.createHot(name);
var module = {id: name, exports: {}, hot: hot};
cache[name] = module;
definition(module.exports, localRequire(name), module);
return module.exports;
};
var expandAlias = function(name) {
return aliases[name] ? expandAlias(aliases[name]) : name;
};
var _resolve = function(name, dep) {
return expandAlias(expand(dirname(name), dep));
};
var require = function(name, loaderPath) {
if (loaderPath == null) loaderPath = '/';
var path = expandAlias(name);
if (has.call(cache, path)) return cache[path].exports;
if (has.call(modules, path)) return initModule(path, modules[path]);
throw new Error("Cannot find module '" + name + "' from '" + loaderPath + "'");
};
require.alias = function(from, to) {
aliases[to] = from;
};
var extRe = /\.[^.\/]+$/;
var indexRe = /\/index(\.[^\/]+)?$/;
var addExtensions = function(bundle) {
if (extRe.test(bundle)) {
var alias = bundle.replace(extRe, '');
if (!has.call(aliases, alias) || aliases[alias].replace(extRe, '') === alias + '/index') {
aliases[alias] = bundle;
}
}
if (indexRe.test(bundle)) {
var iAlias = bundle.replace(indexRe, '');
if (!has.call(aliases, iAlias)) {
aliases[iAlias] = bundle;
}
}
};
require.register = require.define = function(bundle, fn) {
if (typeof bundle === 'object') {
for (var key in bundle) {
if (has.call(bundle, key)) {
require.register(key, bundle[key]);
}
}
} else {
modules[bundle] = fn;
delete cache[bundle];
addExtensions(bundle);
}
};
require.list = function() {
var list = [];
for (var item in modules) {
if (has.call(modules, item)) {
list.push(item);
}
}
return list;
};
var hmr = globals._hmr && new globals._hmr(_resolve, require, modules, cache);
require._cache = cache;
require.hmr = hmr && hmr.wrap;
require.brunch = true;
globals.require = require;
})();
(function() {
var global = window;
var __makeRelativeRequire = function(require, mappings, pref) {
var none = {};
var tryReq = function(name, pref) {
var val;
try {
val = require(pref + '/node_modules/' + name);
return val;
} catch (e) {
if (e.toString().indexOf('Cannot find module') === -1) {
throw e;
}
if (pref.indexOf('node_modules') !== -1) {
var s = pref.split('/');
var i = s.lastIndexOf('node_modules');
var newPref = s.slice(0, i).join('/');
return tryReq(name, newPref);
}
}
return none;
};
return function(name) {
if (name in mappings) name = mappings[name];
if (!name) return;
if (name[0] !== '.' && pref) {
var val = tryReq(name, pref);
if (val !== none) return val;
}
return require(name);
}
};
document.addEventListener('DOMContentLoaded', function() {
// do your setup here
});
/* TODO: find a better way to refer to Con mod than "monster.abilities[2].mod"
* Ideally, should be able to refer to it by name
*
* I think we might actually want to go back to storing a lot of these as
* objects instead of arrays, actually. The orders matter only in one place,
* and the elements of the collection don't actually change.
*
* The thing to do would be to have the objects, and then have an object
* somewhere called Orderings, which contains array orderings for all the
* various objects out there.
* Or possibly: actually implement an ordered-dict sort of object.
* Or maybe: write a function that turns an object into an array sorted
* according to some property of each of the object's values.
*
* Probably the first of these. It's the least prone to screwing up.
*/
var app = angular.module("monsterBuilder", []);
app.controller("monsterController", function($scope, $http, $filter) {
/* load variables from JSON */
$http.get("js/lists.json").then(function(lists){
for (list in lists.data){
$scope[list] = lists.data[list];
}
/* initialize dependent variables */
$scope.monster.size = getFromArray($scope.SIZES, 'Medium');
//$filter('filter')($scope.SIZES, {name:'Medium'})[0];
//$scope.monster.speed.ground = $scope.monster.size.speed;
// bind ability scores to mods (and save bonuses?)
for (var ability in $scope.monster.abilities) {
bindModToAbility($scope.monster.abilities[ability]);
}
// calculate monster hp
$scope.$watchGroup(["monster.size.hd", "monster.hd", "monster.abilities[2].mod"],
function() {
$scope.monster.hp = diceAverage($scope.monster.hd,
$scope.monster.size.hd, getAbility('Con').mod);
if ($scope.builder.hdMode == "fixed") {
//set target hp to current hp
$scope.builder.hpTarget = $scope.monster.hp;
}
});
// bind target hp to HD
$scope.$watch("builder.hpTarget", function() {
if ($scope.builder.hdMode == "dynamic") {
$scope.monster.hd = getNumDiceFromAverage($scope.builder.hpTarget,
$scope.monster.size.hd, getAbility('Con').mod);
}
});
// functions dependent on loaded variables
$scope.newAttack = function() {
var attack = $scope.builder.defaultAttack;
$scope.monster.attacks.push(attack);
bindTotalDamageToAttack(attack);
}
$scope.goToTab = function(tab) {
$scope.builder.tab = tab;
}
});
/* PRIVATE FUNCTIONS */
function expressionFromObject(objectName) { //TODO: change name
return $scope[objectName]; //no, gotta split this on dots
}
/* binds an ability's mod to its base score */
function bindModToAbility(ability) {
$scope.$watch(function(){
return ability.score;
}, function(){
ability.mod = Math.floor((ability.score - 10) / 2);
});
}
//old, messy version of ability binding
/*function bindModToAbility(ability) {
$scope.$watch("abilities['" + ability + "'].score", function(score) {
$scope.abilities[ability].mod = Math.floor((score - 10) / 2);
});
}*/
function bindTotalDamageToAttack(attack) {
$scope.$watchGroup(function(){
console.log([attack.damage.dice.num, attack.damage.dice.size, "monster.abilities[0].mod"]);
return [attack.damage.dice.num, attack.damage.dice.size, "monster.abilities[0].mod"];
}, function(){
attack.damage.total = diceAverage(attack.damage.dice.num, attack.damage.dice.size);
});
}
function diceAverage(num, size, bonus) {
bonus = bonus || 0;
return Math.floor(num * ((size + 1) / 2 + bonus));
}
function getNumDiceFromAverage(average, size, bonus) {
bonus = bonus || 0;
return Math.round(average / ((size + 1) / 2 + bonus));
}
function getFromArray(array, value) {
try {
return $filter('filter')(array, {id: value})[0];
} catch (err) {
return null;
}
}
function getAbility(ability) {
//console.log(getFromArray($scope.monster.abilities, ability));
console.log($scope.monster.abilities);
return getFromArray($scope.monster.abilities, ability);
}
});
require.register("___globals___", function(exports, require, module) {
});})();require('___globals___');
//# sourceMappingURL=app.js.map