/
ItemScroller.js
94 lines (76 loc) · 3.13 KB
/
ItemScroller.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
(function(window) {
var ItemScroller = function(props) {this.initialize(props);};
var p = ItemScroller.prototype = new Container();
p.Container_initialize = p.initialize;
p.initialize = function(props) {
this.Container_initialize();
var _p = this.props = props || {};
var that = this,
trackingStage = props.trackingStage,
scaleStage = (props && props.scaleStage) ? props.scaleStage : this,
wrapRadius = (props && props.wrapRadius) ? props.wrapRadius : 300,
numItems = (props && props.numItems) ? props.numItems : 60,
globalScaleStageCenter = new Point(),
direction = 0,
items = [];
console.log("_p.app: ", _p.app);
this.hitTarget = undefined;
var populate = function(spread) {
for (var i = 0; i < numItems; i++) {
makeItem(i);
}
function makeItem(i) {
var color = (i === 0) ? '#0F0' : '#0FF';
var item;
var rand = Math.random();
item = new GravityWellRepelBubble(_p.app);
// item = new GravityWellBubble(_p.ship);
// if (rand < 0.3) {
// item = new GravityWellRepelBubble(_p.app);
// } else if (rand < 0.6) {
// item = new GravityWellRepel(_p.app);
// } else {
// item = new GravityWellRepel(_p.app);
// }
// var item = PTUtils.makeCircle(color, 2);
var spawnPoint = PTUtils.polarDegrees(((Math.random()*(wrapRadius-500)))+500, Math.random() * 360);
item.x = spawnPoint.x;
item.y = spawnPoint.y;
item.i = i;
items.push(item);
that.addChild(item);
}
};
populate(10);
this.tick = function() {
globalScaleStageCenter = scaleStage.localToGlobal(0, 0);
direction = PTUtils.angleRadians(trackingStage.getAmountToMove(), new Point() );
for (var i = 0; i < items.length; i++) {
var item = items[i];
var globalItemCenter = item.localToGlobal(0, 0);
var localItemPos = scaleStage.globalToLocal(globalItemCenter.x, globalItemCenter.y);
// if (i === 0) { console.log("localItemPos: " + localItemPos); }
var distanceFromCenter = PTUtils.distance(localItemPos, new Point());
if ( distanceFromCenter > wrapRadius ) {
var localScaleStageCenter = that.globalToLocal(globalScaleStageCenter.x, globalScaleStageCenter.y);
var respawnAngleRadians = PTUtils.getOppositeAngleRadians(direction);
var respawnPoint = PTUtils.polarRadians(wrapRadius, respawnAngleRadians);
item.x = localScaleStageCenter.x + respawnPoint.x;
item.y = localScaleStageCenter.y + respawnPoint.y;
}
}
// _.each(items, function(item) {
// var globalItemCenter = item.localToGlobal(0, 0);
// var distanceFromCenter = PTUtils.distance(globalItemCenter, globalScaleStageCenter);
// if ( distanceFromCenter > wrapRadius ) {
// var localScaleStageCenter = that.globalToLocal(globalScaleStageCenter.x, globalScaleStageCenter.y);
// var respawnAngleRadians = PTUtils.getOppositeAngleRadians(direction);
// var respawnPoint = PTUtils.polarRadians(wrapRadius, respawnAngleRadians);
// item.x = localScaleStageCenter.x + respawnPoint.x;
// item.y = localScaleStageCenter.y + respawnPoint.y;
// }
// });
};
};
window.ItemScroller = ItemScroller;
}(window));