-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflatspace.js
104 lines (86 loc) · 2.33 KB
/
flatspace.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
"use strict";
define(['boxworld', 'boxview', 'boxdebugdraw', 'boxbody'], function( boxworld, boxview, boxdebugdraw, boxbody ) {
var world, view, debugDraw = undefined;
function initialize() {
world = boxworld.create();
view = boxview.create( {
width:640,
height:480,
pixelsPerMeter:13
});
document.getElementById("flatspace").appendChild( view.canvas );
debugDraw = boxdebugdraw.create( world, view );
}
function update() {
world.update();
render();
}
function render() {
view.clear();
debugDraw.render();
}
var warpholes = {};
function populate() {
var ground = world.add(
boxbody.edge,
{
x:0,
y:-15,
width:20,
height:0,
}
);
warpholes[16] = world.add(
boxbody.hole,
{
x:0,
y:-8,
radius:1
}
);
warpholes[16].onContact = function( object ) {
if( object.is( ball ) ) {
var isOpen = warpholes[16].isOpen === true;
if( isOpen ) {
notifyBallTransited( 16 );
ball.isMarkedForDeletion = true;
}
}
}
var ball = world.add(
boxbody.ball,
{
x:0,
y:8,
radius:1
}
);
ball.onContact = function(object) {
if( object.is( ground ) ) {
console.log("The ball has contacted the ground.");
}
};
}
function setWarpholeState( id, isOpen ) {
var warphole = warpholes[id];
if( warphole ) {
warphole.isOpen = isOpen;
}
}
var ballTransitListener = undefined;
function setBallTransitListener( callback ) {
ballTransitListener = callback;
}
function notifyBallTransited( id ) {
if( ballTransitListener ) {
ballTransitListener( id );
}
}
return {
initialize: initialize,
populate: populate,
update: update,
setWarpholeState: setWarpholeState,
setBallTransitListener: setBallTransitListener,
}
});