-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSprite.js
156 lines (128 loc) · 4.13 KB
/
Sprite.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
(function(root) {
root.Sprite = Sprite;
function Sprite() {
this.shapes = [];
this.ellipses = [];
this.sprites = [];
this.createNewShape();
this.current_color;
this.setOffset(new Point(0, 0));
}
_.extend(Sprite.prototype, Backbone.Events, {
'setOffset': function setOffset(offset) {
this.offset = offset;
this.offset.on('change', function() {
this.trigger('change');
}.bind(this));
},
'setCurrentColor': function setCurrentColor(color) {
this.getLastShape().applyColor(color);
this.current_color = color;
},
'addEllipse': function addEllipse(ellipse) {
this.ellipses.push(ellipse);
this.trigger('ellipse:add', ellipse);
},
'addShape': function addShape(shp) {
this.shapes.push(shp);
},
'getShapes': function getShapes() {
return _(this.shapes).clone();
},
'getLastShape': function getLastShape() {
return _(this.shapes).last();
},
'createNewShape': function createNewShape() {
var shape = new Shape().applyColor(this.current_color);
this.shapes.push(shape);
this.trigger('shape:add', shape);
},
'addPoint': function addPoint(point) {
point = point.diff(this.offset);
var last_shape = this.getLastShape();
last_shape.addPoint(point);
if (last_shape.is_closed)
this.createNewShape();
},
'undoLastPoint': function undoLastPoint() {
var current_shape = this.getLastShape();
if (!current_shape.hasPoints()) {
this.shapes.pop();
current_shape = this.getLastShape();
}
current_shape.removeLastPoint();
},
'scale': function scale(scale) {
this.shapes.forEach(function(shape) {
shape.scale(scale);
});
},
'addSprite': function addSprite(sprite) {
this.sprites.push(sprite);
this.trigger('sprite:add', sprite);
},
'goRight': function goRight() {
this.offset.x += 5;
},
'goLeft': function goLeft() {
this.offset.x -= 5;
},
'jump': function jump() {
var start_time = new Date().getTime();
var start_value = this.offset.y;
delta_value = -60;
duration = 500;
var easing = Easing.easeOutQuad.bind(null, start_value, delta_value, duration);
var interval_id = setInterval(function() {
var delta_time = new Date().getTime() - start_time;
var val = easing(delta_time);
this.offset.y = val;
if (delta_time >= 500)
clearInterval(interval_id);
}.bind(this), 10);
setTimeout(function() {
var start_time = new Date().getTime();
var start_value = this.offset.y;
delta_value = 60;
duration = 500;
var easing = Easing.easeInQuad.bind(null, start_value, delta_value, duration);
var interval_id = setInterval(function() {
var delta_time = new Date().getTime() - start_time;
var val = easing(delta_time);
this.offset.y = val;
if (delta_time >= 500)
clearInterval(interval_id);
}.bind(this), 10);
}.bind(this), 500)
},
'save': function save(sprite_name) {
localStorage.setItem(sprite_name, JSON.stringify(this));
},
'toJSON': function toJSON() {
return {
'shapes': this.shapes,
'sprites': this.sprites,
'offset': this.offset
};
}
});
Sprite.deserialize = function deserialize(raw_obj) {
// support old versions
if (_(raw_obj).isArray())
raw_obj = {'shapes': raw_obj};
var sprite = new Sprite();
raw_obj.shapes.forEach(function(raw_shape) {
var shape = Shape.deserialize(raw_shape);
sprite.addShape(shape);
});
if (raw_obj.sprites) {
raw_obj.sprites.forEach(function(raw_sprite) {
var child_sprite = Sprite.deserialize(raw_sprite);
sprite.addSprite(child_sprite);
});
}
if (raw_obj.offset)
sprite.setOffset(Point.deserialize(raw_obj.offset));
return sprite;
}
})(this);