-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcyclobots-pixi-launcher.js
149 lines (131 loc) · 3.88 KB
/
cyclobots-pixi-launcher.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
/*
* dam-plus-widgets-web.js, pixi-min.js, pixi-viewport.js, and cyclobots.js must be loaded before this source.
* After loading this source, call launch() to create and start the gewgaw.
*/
function removeVisuals(cyclobots) {
cyclobots.forEachBot(function(bot) {
if (bot.graphics) {
bot.graphics.destroy();
}
bot.graphics = undefined;
});
}
function setClassicVisuals(cyclobots, viewport) {
cyclobots.forEachBot(function(bot) {
if (bot.graphics) return;
var graphics = new PIXI.Graphics();
graphics.lineStyle(0);
graphics.beginFill(0xff0000);
graphics.drawCircle(0, 0, 10);
graphics.endFill();
viewport.addChild(graphics);
bot.graphics = graphics;
});
}
function setBlurredVisuals(cyclobots, viewport) {
cyclobots.forEachBot(function(bot) {
if (bot.graphics) return;
var graphics = new PIXI.Graphics();
graphics.lineStyle(0);
graphics.beginFill(0xff0000);
graphics.drawCircle(0, 0, 10);
graphics.endFill();
graphics.filters = [new PIXI.filters.BlurFilter()];
viewport.addChild(graphics);
bot.graphics = graphics;
});
}
function setUpPixiApp(config, cyclobots) {
var app = new PIXI.Application({
width: config.width,
height: config.height,
forceCanvas: config.forceCanvas,
antialias: true,
backgroundColor : 0xffffff
});
var viewport = new PIXI.extras.Viewport({
screenWidth: config.width,
screenHeight: config.height,
worldWidth: 1000,
worldHeight: 1000,
interaction: app.renderer.plugins.interaction
});
app.stage.addChild(viewport);
viewport.drag();
config.container.insertBefore(app.view, config.container.firstChild);
app.ticker.add(function(delta) {
cyclobots.update();
});
return { app: app, viewport: viewport };
}
function getRenderer(app) {
if (app.renderer instanceof PIXI.WebGLRenderer) {
return "WebGL";
} else if (app.renderer instanceof PIXI.CanvasRenderer) {
return "Canvas";
}
return "unknown";
}
function launch(config) {
var cyclobots = new Cyclobots().init({
width: config.width,
height: config.height,
onUpdateBot: function(bot) {
if (!bot.graphics) return;
bot.graphics.x = bot.x;
bot.graphics.y = bot.y;
}
});
var r = setUpPixiApp(config, cyclobots);
var app = r.app;
var viewport = r.viewport;
setClassicVisuals(cyclobots, viewport);
var div=DAM.maker('div'), button=DAM.maker('button'), span=DAM.maker('span');
/*----- renderer panel -----*/
var renderer = getRenderer(app);
var rendererSpan = span("Renderer: " + renderer + ".");
var forceRendererButton = null;
if (renderer !== "Canvas") {
forceRendererButton = button(
"Force Canvas renderer",
{
onclick: function(e) {
removeVisuals(cyclobots);
app.destroy(true, true);
config.forceCanvas = true;
r = setUpPixiApp(config, cyclobots);
app = r.app;
viewport = r.viewport;
setClassicVisuals(cyclobots, app.stage);
forceRendererButton.remove();
rendererSpan.innerHTML = "Renderer: Canvas.";
}
}
);
}
var rendererPanel = div(
rendererSpan,
forceRendererButton
);
/*----- visuals panel -----*/
var visualsPanel = div(
DAM.makeSelect(
{
title: "Visuals:",
options: [
{ text: "Classic", value: "1", setVisuals: setClassicVisuals },
{ text: "Blurred", value: "2", setVisuals: setBlurredVisuals }
],
onchange: function(option) {
removeVisuals(cyclobots);
option.setVisuals(cyclobots, viewport);
}
}
)
);
var actionsPanel = div(
button("Mass confusion!", { onclick: function(e) { cyclobots.massConfusion(); } }),
button("Revolution!", { onclick: function(e) { cyclobots.shuffle(); } })
);
config.container.appendChild(div(rendererPanel, visualsPanel, actionsPanel));
}