-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathbasketball.js
More file actions
124 lines (100 loc) · 3.16 KB
/
Copy pathbasketball.js
File metadata and controls
124 lines (100 loc) · 3.16 KB
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
const Basketball = world.findComponentStoreByName("Basketball");
const HoopSensor = world.findComponentStoreByName("HoopSensor");
world.onload = () => {
const actionBarListener = thirdroom.actionBar.createListener();
const thumbnail = world.findImageByName("Basketball");
thirdroom.actionBar.setItems([
{
id: "basketball",
label: "Basketball",
thumbnail,
},
]);
const basketball = world.findNodeByName("Basketball");
const impulse = new WebSG.Vector3();
const collisionListener = world.createCollisionListener();
const root = world.createUIElement({
backgroundColor: [0, 0, 0, 0.5],
flexDirection: "row",
width: 1024,
height: 512,
});
const uiCanvas = world.createUICanvas({
size: [1, 0.5],
width: 1024,
height: 512,
root,
});
const scoreboard = world.findNodeByName("Scoreboard");
const teamScores = [0, 0];
const teamScoreTextElements = [
createTeamScoreboard(root, "Red", [1, 0, 0, 1]),
createTeamScoreboard(root, "Blue", [0, 0, 1, 1]),
];
scoreboard.uiCanvas = uiCanvas;
function countScore(team) {
const index = team - 1;
teamScoreTextElements[index].value = teamScores[index] += 2;
uiCanvas.redraw();
}
world.onupdate = (dt, time) => {
for (const actionId of actionBarListener.actions()) {
if (actionId !== "basketball") {
continue;
}
const ball = world.createNode({
mesh: basketball.mesh,
});
const origin = world.primaryInputSourceOrigin;
const direction = world.primaryInputSourceDirection;
ball.setForwardDirection(direction);
ball.translation.set(origin).addScaledVector(direction, 0.5);
ball.collider = basketball.collider;
ball.addPhysicsBody({
type: WebSG.PhysicsBodyType.Rigid,
mass: 1,
});
ball.addInteractable({
type: WebSG.InteractableType.Grabbable,
});
ball.addComponent(Basketball);
impulse.set(direction).multiplyScalar(8);
ball.physicsBody.applyImpulse(impulse);
world.environment.addNode(ball);
}
for (const collision of collisionListener.collisions()) {
if (!collision.started) {
continue;
}
if (collision.nodeA.hasComponent(Basketball) && collision.nodeB.hasComponent(HoopSensor)) {
countScore(collision.nodeB.getComponent(HoopSensor).team);
} else if (collision.nodeB.hasComponent(Basketball) && collision.nodeA.hasComponent(HoopSensor)) {
countScore(collision.nodeA.getComponent(HoopSensor).team);
}
}
};
};
function createTeamScoreboard(scoreboard, teamName, teamColor) {
const teamContainer = world.createUIElement({
flexDirection: "column",
flexGrow: 1,
padding: [100, 0, 0, 100],
});
const teamNameText = world.createUIText({
value: teamName,
fontSize: 128,
color: teamColor,
fontWeight: "bold",
});
teamContainer.addChild(teamNameText);
const teamScoreText = world.createUIText({
value: "0",
fontSize: 256,
color: [1, 1, 1, 1],
fontWeight: "bold",
padding: [20, 0, 0, 60],
});
teamContainer.addChild(teamScoreText);
scoreboard.addChild(teamContainer);
return teamScoreText;
}