hytopia 0.1.46 → 0.1.48

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -124,6 +124,27 @@ Readonly&lt;[PlayerOrientationState](./server.playerorientationstate.md)<!-- -->
124
124
  The current [PlayerOrientationState](./server.playerorientationstate.md) of the player.
125
125
 
126
126
 
127
+ </td></tr>
128
+ <tr><td>
129
+
130
+ [ui](./server.player.ui.md)
131
+
132
+
133
+ </td><td>
134
+
135
+ `readonly`
136
+
137
+
138
+ </td><td>
139
+
140
+ PlayerUI
141
+
142
+
143
+ </td><td>
144
+
145
+ The UI for the player.
146
+
147
+
127
148
  </td></tr>
128
149
  <tr><td>
129
150
 
@@ -0,0 +1,13 @@
1
+ <!-- Do not edit this file. It is automatically generated by API Documenter. -->
2
+
3
+ [Home](./index.md) &gt; [server](./server.md) &gt; [Player](./server.player.md) &gt; [ui](./server.player.ui.md)
4
+
5
+ ## Player.ui property
6
+
7
+ The UI for the player.
8
+
9
+ **Signature:**
10
+
11
+ ```typescript
12
+ readonly ui: PlayerUI;
13
+ ```
@@ -0,0 +1,53 @@
1
+ <!-- Do not edit this file. It is automatically generated by API Documenter. -->
2
+
3
+ [Home](./index.md) &gt; [server](./server.md) &gt; [Simulation](./server.simulation.md) &gt; [enableDebugRaycasting](./server.simulation.enabledebugraycasting.md)
4
+
5
+ ## Simulation.enableDebugRaycasting() method
6
+
7
+ Enables or disables debug raycasting for the simulation. This will render lines for the raycast that disappear after a few seconds.
8
+
9
+ **Signature:**
10
+
11
+ ```typescript
12
+ enableDebugRaycasting(enabled: boolean): void;
13
+ ```
14
+
15
+ ## Parameters
16
+
17
+ <table><thead><tr><th>
18
+
19
+ Parameter
20
+
21
+
22
+ </th><th>
23
+
24
+ Type
25
+
26
+
27
+ </th><th>
28
+
29
+ Description
30
+
31
+
32
+ </th></tr></thead>
33
+ <tbody><tr><td>
34
+
35
+ enabled
36
+
37
+
38
+ </td><td>
39
+
40
+ boolean
41
+
42
+
43
+ </td><td>
44
+
45
+ Whether to enable debug raycasting.
46
+
47
+
48
+ </td></tr>
49
+ </tbody></table>
50
+ **Returns:**
51
+
52
+ void
53
+
@@ -4,7 +4,7 @@
4
4
 
5
5
  ## Simulation.enableDebugRendering() method
6
6
 
7
- Enables or disables debug rendering for the simulation. When enabled, all colliders, rigid body and raycast outlines will be rendered in the world. Do not enable this in production. In large worlds enabling this can cause noticable lag and RTT spikes.
7
+ Enables or disables debug rendering for the simulation. When enabled, all colliders and rigid body outlines will be rendered in the world. Do not enable this in production. In large worlds enabling this can cause noticable lag and RTT spikes.
8
8
 
9
9
  **Signature:**
10
10
 
@@ -0,0 +1,13 @@
1
+ <!-- Do not edit this file. It is automatically generated by API Documenter. -->
2
+
3
+ [Home](./index.md) &gt; [server](./server.md) &gt; [Simulation](./server.simulation.md) &gt; [isDebugRaycastingEnabled](./server.simulation.isdebugraycastingenabled.md)
4
+
5
+ ## Simulation.isDebugRaycastingEnabled property
6
+
7
+ Whether the simulation has debug raycasting enabled
8
+
9
+ **Signature:**
10
+
11
+ ```typescript
12
+ get isDebugRaycastingEnabled(): boolean;
13
+ ```
@@ -61,6 +61,27 @@ RAPIER.Vector3
61
61
  The gravity vector for the simulation.
62
62
 
63
63
 
64
+ </td></tr>
65
+ <tr><td>
66
+
67
+ [isDebugRaycastingEnabled](./server.simulation.isdebugraycastingenabled.md)
68
+
69
+
70
+ </td><td>
71
+
72
+ `readonly`
73
+
74
+
75
+ </td><td>
76
+
77
+ boolean
78
+
79
+
80
+ </td><td>
81
+
82
+ Whether the simulation has debug raycasting enabled
83
+
84
+
64
85
  </td></tr>
65
86
  <tr><td>
66
87
 
@@ -158,6 +179,20 @@ Description
158
179
  Casts a ray through the simulation.
159
180
 
160
181
 
182
+ </td></tr>
183
+ <tr><td>
184
+
185
+ [enableDebugRaycasting(enabled)](./server.simulation.enabledebugraycasting.md)
186
+
187
+
188
+ </td><td>
189
+
190
+
191
+ </td><td>
192
+
193
+ Enables or disables debug raycasting for the simulation. This will render lines for the raycast that disappear after a few seconds.
194
+
195
+
161
196
  </td></tr>
162
197
  <tr><td>
163
198
 
@@ -169,7 +204,7 @@ Casts a ray through the simulation.
169
204
 
170
205
  </td><td>
171
206
 
172
- Enables or disables debug rendering for the simulation. When enabled, all colliders, rigid body and raycast outlines will be rendered in the world. Do not enable this in production. In large worlds enabling this can cause noticable lag and RTT spikes.
207
+ Enables or disables debug rendering for the simulation. When enabled, all colliders and rigid body outlines will be rendered in the world. Do not enable this in production. In large worlds enabling this can cause noticable lag and RTT spikes.
173
208
 
174
209
 
175
210
  </td></tr>
@@ -0,0 +1,91 @@
1
+ <html>
2
+ <body>
3
+ <!--
4
+ We MUST always import the Hytopia UI library!
5
+ Importing this library also imports the hytopia global.
6
+
7
+ If we were using React, or Svelte, or another framework, we
8
+ can install the HYTPIA UI library as a package and import it
9
+ with `npm install hytopia-ui` and then import it as
10
+ `import hytopia from 'hytopia-ui';`
11
+ -->
12
+ <script src="https://unpkg.com/hytopia-ui@latest/dist/index.umd.js"></script>
13
+
14
+ <script>
15
+ document.addEventListener('DOMContentLoaded', () => {
16
+ // Handle received data from server
17
+ hytopia.ui.onData = data => {
18
+ if (data.type === 'playerList') {
19
+ const { list } = data;
20
+
21
+ // Update player count
22
+ document.getElementById('player-count').textContent = `Players Connected: ${list.length}`;
23
+
24
+ // Clear and rebuild player list
25
+ const playerListElement = document.getElementById('player-list');
26
+ playerListElement.innerHTML = '';
27
+
28
+ // Add each player to the list
29
+ list.forEach(player => {
30
+ const pos = player.position;
31
+ const playerRow = document.createElement('div');
32
+ playerRow.className = 'player-row';
33
+ playerRow.textContent = `${player.username}: x:${pos.x.toFixed(1)}, y:${pos.y.toFixed(1)}, z:${pos.z.toFixed(1)}`;
34
+ playerListElement.appendChild(playerRow);
35
+ });
36
+ }
37
+ }
38
+
39
+ // Send data to server on teleport btn press
40
+ document.getElementById('teleport-btn').addEventListener('click', () => {
41
+ // We can send any arbitrary object to the server. We as the developer define
42
+ // whatever schema we want to use for data between client and server.
43
+ // For now we'll just use a "type" property we made up so the server knows
44
+ // what to do with the data.
45
+ hytopia.ui.sendData({ type: 'teleport' });
46
+ });
47
+ });
48
+ </script>
49
+
50
+ <div id="player-panel">
51
+ <div id="player-count">Players Connected: 0</div>
52
+ <div id="player-list"></div>
53
+ <button id="teleport-btn">Click to Teleport!</button>
54
+ </div>
55
+
56
+ <style>
57
+ #player-panel {
58
+ position: absolute;
59
+ right: 20px;
60
+ top: 20px;
61
+ background-color: rgba(0, 0, 0, 0.7);
62
+ color: white;
63
+ padding: 15px;
64
+ border-radius: 8px;
65
+ min-width: 200px;
66
+ font-family: Arial, sans-serif;
67
+ letter-spacing: 0.5px;
68
+ }
69
+ .player-row {
70
+ margin: 8px 0;
71
+ }
72
+ #teleport-btn {
73
+ width: 100%;
74
+ padding: 8px;
75
+ margin-top: 15px;
76
+ background-color: #4CAF50;
77
+ color: white;
78
+ border: none;
79
+ border-radius: 4px;
80
+ cursor: pointer;
81
+ font-family: Arial, sans-serif;
82
+ font-weight: bold;
83
+ text-transform: uppercase;
84
+ letter-spacing: 1px;
85
+ }
86
+ #teleport-btn:hover {
87
+ background-color: #45a049;
88
+ }
89
+ </style>
90
+ </body>
91
+ </html>
@@ -0,0 +1,91 @@
1
+ import {
2
+ startServer,
3
+ Audio,
4
+ Player,
5
+ PlayerEntity,
6
+ PlayerUI,
7
+ } from 'hytopia';
8
+
9
+ import worldMap from './assets/map.json';
10
+
11
+ // Simple map for player -> singular controlled entity
12
+ const playerEntityMap = new Map<Player, PlayerEntity>();
13
+
14
+ startServer(world => {
15
+ world.loadMap(worldMap);
16
+
17
+ world.onPlayerJoin = player => {
18
+ // Load the UI for the player.
19
+ // This is loaded directly into a sandboxed iframe
20
+ // overlaying the game. As long as it's an HTML file,
21
+ // it can be loaded. You can bundle React, Svelte, or
22
+ // any other framework into the UI you want to serve,
23
+ // or just use plain HTML like in this example.
24
+ player.ui.load('ui/index.html');
25
+
26
+ const playerEntity = new PlayerEntity({
27
+ player,
28
+ name: 'Player',
29
+ modelUri: 'models/player.gltf',
30
+ modelLoopedAnimations: [ 'idle' ],
31
+ modelScale: 0.5,
32
+ });
33
+
34
+ playerEntity.spawn(world, { x: 0, y: 10, z: 0 });
35
+
36
+ // Set the player entity on our map for when
37
+ // we do player list updates.
38
+ playerEntityMap.set(player, playerEntity);
39
+
40
+ // Handle data sent from player to server by client UI interactions we define.
41
+ // See the ui/index.html file for the UI code that sends the data.
42
+ player.ui.onData = (playerUI: PlayerUI, data: Record<string, any>) => {
43
+ if (data.type === 'teleport') {
44
+ const randomX = Math.random() * 40 - 20; // Random between -20 and 20
45
+ const randomY = Math.random() * 13 + 2; // Random between 2 and 15
46
+ const randomZ = Math.random() * 40 - 20; // Random between -20 and 20
47
+
48
+ playerEntity.setTranslation({ x: randomX, y: randomY, z: randomZ });
49
+ }
50
+ };
51
+ };
52
+
53
+ world.onPlayerLeave = player => {
54
+ world.entityManager.getAllPlayerEntities(player).forEach(entity => entity.despawn());
55
+ // Remove the player entity from our map for our list.
56
+ playerEntityMap.delete(player);
57
+ };
58
+
59
+ // Update the player list every 1 second, no need to send too frequently.
60
+ // We want to balance not sending too much UI data too frequently, because
61
+ // it is currently sent over the same connection as game packets and will
62
+ // compete for critical game packet bandwidth on slower player connections.
63
+ setInterval(updatePlayerList, 1000);
64
+
65
+ new Audio({
66
+ uri: 'audio/music/overworld.mp3',
67
+ loop: true,
68
+ volume: 0.2,
69
+ }).play(world);
70
+ });
71
+
72
+ function updatePlayerList() {
73
+ // Create a list of all connected players and their positions
74
+ const playerListData = Array.from(playerEntityMap).map(([ player, entity ]) => {
75
+ // For each player, return their username and current position
76
+ return {
77
+ username: player.username,
78
+ position: entity.getTranslation(), // Gets x,y,z coordinate
79
+ };
80
+ });
81
+
82
+ // Send the updated player list to every connected player's UI
83
+ for (const [ player ] of playerEntityMap) {
84
+ player.ui.sendData({
85
+ type: 'playerList', // Message type for UI to handle
86
+ list: playerListData, // List of all players and positions
87
+ });
88
+ }
89
+ }
90
+
91
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hytopia",
3
- "version": "0.1.46",
3
+ "version": "0.1.48",
4
4
  "description": "The HYTOPIA SDK makes it easy for developers to create massively multiplayer games using JavaScript or TypeScript.",
5
5
  "main": "server.js",
6
6
  "bin": {
package/server.api.json CHANGED
@@ -13461,6 +13461,37 @@
13461
13461
  "isProtected": false,
13462
13462
  "isAbstract": false
13463
13463
  },
13464
+ {
13465
+ "kind": "Property",
13466
+ "canonicalReference": "server!Player#ui:member",
13467
+ "docComment": "/**\n * The UI for the player.\n */\n",
13468
+ "excerptTokens": [
13469
+ {
13470
+ "kind": "Content",
13471
+ "text": "readonly ui: "
13472
+ },
13473
+ {
13474
+ "kind": "Reference",
13475
+ "text": "PlayerUI",
13476
+ "canonicalReference": "server!~PlayerUI:class"
13477
+ },
13478
+ {
13479
+ "kind": "Content",
13480
+ "text": ";"
13481
+ }
13482
+ ],
13483
+ "isReadonly": true,
13484
+ "isOptional": false,
13485
+ "releaseTag": "Public",
13486
+ "name": "ui",
13487
+ "propertyTypeTokenRange": {
13488
+ "startIndex": 1,
13489
+ "endIndex": 2
13490
+ },
13491
+ "isStatic": false,
13492
+ "isProtected": false,
13493
+ "isAbstract": false
13494
+ },
13464
13495
  {
13465
13496
  "kind": "Property",
13466
13497
  "canonicalReference": "server!Player#username:member",
@@ -22362,10 +22393,58 @@
22362
22393
  "isAbstract": false,
22363
22394
  "name": "castRay"
22364
22395
  },
22396
+ {
22397
+ "kind": "Method",
22398
+ "canonicalReference": "server!Simulation#enableDebugRaycasting:member(1)",
22399
+ "docComment": "/**\n * Enables or disables debug raycasting for the simulation. This will render lines for the raycast that disappear after a few seconds.\n *\n * @param enabled - Whether to enable debug raycasting.\n */\n",
22400
+ "excerptTokens": [
22401
+ {
22402
+ "kind": "Content",
22403
+ "text": "enableDebugRaycasting(enabled: "
22404
+ },
22405
+ {
22406
+ "kind": "Content",
22407
+ "text": "boolean"
22408
+ },
22409
+ {
22410
+ "kind": "Content",
22411
+ "text": "): "
22412
+ },
22413
+ {
22414
+ "kind": "Content",
22415
+ "text": "void"
22416
+ },
22417
+ {
22418
+ "kind": "Content",
22419
+ "text": ";"
22420
+ }
22421
+ ],
22422
+ "isStatic": false,
22423
+ "returnTypeTokenRange": {
22424
+ "startIndex": 3,
22425
+ "endIndex": 4
22426
+ },
22427
+ "releaseTag": "Public",
22428
+ "isProtected": false,
22429
+ "overloadIndex": 1,
22430
+ "parameters": [
22431
+ {
22432
+ "parameterName": "enabled",
22433
+ "parameterTypeTokenRange": {
22434
+ "startIndex": 1,
22435
+ "endIndex": 2
22436
+ },
22437
+ "isOptional": false
22438
+ }
22439
+ ],
22440
+ "isOptional": false,
22441
+ "isAbstract": false,
22442
+ "name": "enableDebugRaycasting"
22443
+ },
22365
22444
  {
22366
22445
  "kind": "Method",
22367
22446
  "canonicalReference": "server!Simulation#enableDebugRendering:member(1)",
22368
- "docComment": "/**\n * Enables or disables debug rendering for the simulation. When enabled, all colliders, rigid body and raycast outlines will be rendered in the world. Do not enable this in production. In large worlds enabling this can cause noticable lag and RTT spikes.\n *\n * @param enabled - Whether to enable debug rendering.\n */\n",
22447
+ "docComment": "/**\n * Enables or disables debug rendering for the simulation. When enabled, all colliders and rigid body outlines will be rendered in the world. Do not enable this in production. In large worlds enabling this can cause noticable lag and RTT spikes.\n *\n * @param enabled - Whether to enable debug rendering.\n */\n",
22369
22448
  "excerptTokens": [
22370
22449
  {
22371
22450
  "kind": "Content",
@@ -22512,6 +22591,36 @@
22512
22591
  "isProtected": false,
22513
22592
  "isAbstract": false
22514
22593
  },
22594
+ {
22595
+ "kind": "Property",
22596
+ "canonicalReference": "server!Simulation#isDebugRaycastingEnabled:member",
22597
+ "docComment": "/**\n * Whether the simulation has debug raycasting enabled\n */\n",
22598
+ "excerptTokens": [
22599
+ {
22600
+ "kind": "Content",
22601
+ "text": "get isDebugRaycastingEnabled(): "
22602
+ },
22603
+ {
22604
+ "kind": "Content",
22605
+ "text": "boolean"
22606
+ },
22607
+ {
22608
+ "kind": "Content",
22609
+ "text": ";"
22610
+ }
22611
+ ],
22612
+ "isReadonly": true,
22613
+ "isOptional": false,
22614
+ "releaseTag": "Public",
22615
+ "name": "isDebugRaycastingEnabled",
22616
+ "propertyTypeTokenRange": {
22617
+ "startIndex": 1,
22618
+ "endIndex": 2
22619
+ },
22620
+ "isStatic": false,
22621
+ "isProtected": false,
22622
+ "isAbstract": false
22623
+ },
22515
22624
  {
22516
22625
  "kind": "Property",
22517
22626
  "canonicalReference": "server!Simulation#isDebugRenderingEnabled:member",
package/server.d.ts CHANGED
@@ -1813,6 +1813,8 @@ export declare class Player {
1813
1813
  /** The camera for the player. */
1814
1814
  readonly camera: PlayerCamera;
1815
1815
 
1816
+ /** The UI for the player. */
1817
+ readonly ui: PlayerUI;
1816
1818
 
1817
1819
 
1818
1820
 
@@ -1846,6 +1848,7 @@ export declare class Player {
1846
1848
 
1847
1849
 
1848
1850
 
1851
+
1849
1852
  }
1850
1853
 
1851
1854
  /**
@@ -2183,6 +2186,45 @@ export declare type PlayerOrientationState = {
2183
2186
  yaw: number;
2184
2187
  };
2185
2188
 
2189
+ /**
2190
+ * The UI for a player.
2191
+ *
2192
+ * @remarks
2193
+ * UI allows control of all in-game overlays a player
2194
+ * sees. UI is controlled by HTML, CSS and JavaScript
2195
+ * files you provide in your `assets` folder.
2196
+ *
2197
+ * @public
2198
+ */
2199
+ declare class PlayerUI {
2200
+ /** The player that the UI belongs to. @readonly */
2201
+ readonly player: Player;
2202
+ /**
2203
+ * A function that is called when the player's client UI
2204
+ * sends data to the server.
2205
+ *
2206
+ * @remarks
2207
+ * Data sent is an object of any shape defined by you
2208
+ * and controlled with invocations of `hytopia.ui.sendData()`
2209
+ * from your loaded client UI files.
2210
+ *
2211
+ * @param playerUI - The PlayerUI instance that the data is from.
2212
+ * @param data - The data sent from the client UI.
2213
+ */
2214
+ onData?: (playerUI: PlayerUI, data: object) => void;
2215
+
2216
+ /**
2217
+ * Loads client UI for the player.
2218
+ * @param htmlUri - The ui html uri to load.
2219
+ */
2220
+ load(htmlUri: string): void;
2221
+ /**
2222
+ * Sends data to the player's client UI.
2223
+ * @param data - The data to send to the client UI.
2224
+ */
2225
+ sendData(data: object): void;
2226
+ }
2227
+
2186
2228
  /**
2187
2229
  * The port the server will run on. You can override
2188
2230
  * this in your .env by setting PORT. When deployed in
@@ -2929,6 +2971,9 @@ export declare class Simulation {
2929
2971
 
2930
2972
 
2931
2973
 
2974
+
2975
+ /** Whether the simulation has debug raycasting enabled */
2976
+ get isDebugRaycastingEnabled(): boolean;
2932
2977
  /** Whether the simulation has debug rendering enabled. */
2933
2978
  get isDebugRenderingEnabled(): boolean;
2934
2979
  /** The gravity vector for the simulation. */
@@ -2948,9 +2993,16 @@ export declare class Simulation {
2948
2993
  castRay(origin: RAPIER.Vector3, direction: RAPIER.Vector3, length: number, options?: RayCastOptions): Block | Entity | null;
2949
2994
 
2950
2995
 
2996
+ /**
2997
+ * Enables or disables debug raycasting for the simulation.
2998
+ * This will render lines for the raycast that disappear
2999
+ * after a few seconds.
3000
+ * @param enabled - Whether to enable debug raycasting.
3001
+ */
3002
+ enableDebugRaycasting(enabled: boolean): void;
2951
3003
  /**
2952
3004
  * Enables or disables debug rendering for the simulation.
2953
- * When enabled, all colliders, rigid body and raycast outlines
3005
+ * When enabled, all colliders and rigid body outlines
2954
3006
  * will be rendered in the world. Do not enable this in production.
2955
3007
  * In large worlds enabling this can cause noticable lag and RTT spikes.
2956
3008
  * @param enabled - Whether to enable debug rendering.