forked from github/game-off-2013
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinventory.js
110 lines (95 loc) · 2.8 KB
/
inventory.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
"use strict";
define([], function() {
function Inventory() {
var items = [];
var currentItemIndex = 0;
function add(name) {
var item = {};
item.name = name;
item.animator = new Animator(item);
item.animator.deactivate();
items.push(item);
}
function select( name ) {
items.forEach( function(item, index) {
if( item.name === name ) {
item.animator.activate();
currentItemIndex = index;
}
else {
item.animator.deactivate();
}
});
}
function clear() {
items.length = [];
}
function getCurrentItem() {
return items[currentItemIndex];
}
function nextItem() {
if( ++currentItemIndex >= items.length ) {
currentItemIndex = 0;
}
notifyOnItemChanged();
}
function previousItem() {
if( --currentItemIndex < 0 ) {
currentItemIndex = items.length-1;
}
notifyOnItemChanged();
}
function notifyOnItemChanged() {
onItemChangedListeners.forEach( function(callback) {
callback(items[currentItemIndex]);
});
}
var onItemChangedListeners = [];
function addItemChangedListener( callback ) {
onItemChangedListeners.push(callback);
}
return {
add: add,
items:items,
clear:clear,
nextItem: nextItem,
select: select,
previousItem: previousItem,
getCurrentItem: getCurrentItem,
addItemChangedListener: addItemChangedListener,
};
}
function Animator( item ) {
var state = {scale:0.25};
function activate( rate ) {
new TWEEN.Tween( state )
.to( {scale:5.0}, rate )
.easing( TWEEN.Easing.Circular.Out )
.onStart( function() {
})
.onUpdate( function() {
item.scale = this.scale;
})
.start();
}
function deactivate( rate ) {
new TWEEN.Tween( state )
.to( {scale:1.0}, rate )
.easing( TWEEN.Easing.Circular.In )
.onStart( function() {
})
.onUpdate( function() {
item.scale = this.scale;
})
.start();
}
var result = {
activate: activate,
deactivate: deactivate,
};
return result;
}
return {
Inventory:Inventory
};
});