@series-inc/rundot-3d-engine 0.3.0 → 0.4.0

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.
@@ -1,204 +1,204 @@
1
- # GameObject
2
-
3
- GameObject is the base entity class in the Rundot 3D Engine, extending THREE.Object3D to provide entity-component architecture with automatic lifecycle management.
4
-
5
- ## Quick Start
6
-
7
- ```typescript
8
- import { GameObject } from "@series-ai/rundot-3d-engine"
9
-
10
- // Create a GameObject (automatically added to scene)
11
- const player = new GameObject("Player")
12
- player.position.set(0, 1, 0)
13
-
14
- // Add components for behavior
15
- const renderer = new MeshRenderer("character_mesh")
16
- player.addComponent(renderer)
17
-
18
- const controller = new PlayerController()
19
- player.addComponent(controller)
20
- ```
21
-
22
- ## Common Use Cases
23
-
24
- ### Creating Child Objects
25
-
26
- ```typescript
27
- // Create parent-child hierarchy
28
- const parent = new GameObject("Parent")
29
- const child = new GameObject("Child")
30
-
31
- // Attach child to parent
32
- parent.add(child)
33
- child.position.set(2, 0, 0) // Local position relative to parent
34
- ```
35
-
36
- ### Getting Components
37
-
38
- ```typescript
39
- // Retrieve a component from the same GameObject
40
- const meshRenderer = player.getComponent(MeshRenderer)
41
- if (meshRenderer) {
42
- meshRenderer.setVisible(false)
43
- }
44
-
45
- // Check if a component exists
46
- if (player.hasComponent(RigidBodyComponent)) {
47
- console.log("Player has physics enabled")
48
- }
49
- ```
50
-
51
- ### Enable/Disable Objects
52
-
53
- ```typescript
54
- // Disable GameObject and all its components
55
- player.setEnabled(false) // Triggers onDisabled() on all components
56
-
57
- // Enable GameObject
58
- player.setEnabled(true) // Triggers onEnabled() on all components
59
-
60
- // Check enabled state (includes parent hierarchy)
61
- if (player.isEnabled()) {
62
- console.log("Player is active")
63
- }
64
- ```
65
-
66
- ### Cleanup
67
-
68
- ```typescript
69
- // Properly dispose GameObject and all components
70
- player.dispose() // Cleans up components, removes from scene, disposes meshes
71
-
72
- // Remove a single component
73
- player.removeComponent(RigidBodyComponent) // Triggers onCleanup() on component
74
- ```
75
-
76
- ## API Overview
77
-
78
- ### Constructor
79
-
80
- - `new GameObject(name?: string)` - Creates GameObject and adds to scene. Auto-generates name if not provided.
81
-
82
- ### Component Management
83
-
84
- - `addComponent<T>(component: T): T` - Adds a component instance. Throws if duplicate type exists.
85
- - `getComponent<T>(type): T | undefined` - Gets component of specified type.
86
- - `hasComponent<T>(type): boolean` - Checks if component exists.
87
- - `removeComponent<T>(type): boolean` - Removes and cleans up component.
88
-
89
- ### Hierarchy
90
-
91
- - `add(...objects)` - Add child objects (THREE.Object3D or GameObject).
92
- - `parent` - Reference to parent object.
93
- - `children` - Array of child objects.
94
-
95
- ### Lifecycle
96
-
97
- - `dispose()` - Cleanup GameObject, all components, and children recursively.
98
- - `setEnabled(enabled: boolean)` - Set enabled state, triggering component callbacks.
99
- - `isEnabled(): boolean` - Check if enabled (includes parent hierarchy).
100
-
101
- ### Three.js Properties
102
-
103
- GameObjects inherit all THREE.Object3D properties:
104
- - `position: THREE.Vector3` - World or local position
105
- - `rotation: THREE.Euler` - Rotation in radians
106
- - `scale: THREE.Vector3` - Scale factors
107
- - `quaternion: THREE.Quaternion` - Alternative rotation representation
108
- - `visible: boolean` - Visibility (use setEnabled() for components)
109
-
110
- ## Patterns & Best Practices
111
-
112
- ### Always Name Your GameObjects
113
-
114
- ```typescript
115
- // Good - Easy to debug
116
- const pickup = new GameObject("MoneyPickup")
117
-
118
- // Avoid - Auto-generated name is harder to track
119
- const pickup = new GameObject()
120
- ```
121
-
122
- ### Use Child GameObjects for Hierarchy
123
-
124
- ```typescript
125
- // Good - Organized hierarchy
126
- const character = new GameObject("Character")
127
- const visualsObject = new GameObject("Visuals")
128
- const meshRenderer = new MeshRenderer("character_model")
129
- visualsObject.addComponent(meshRenderer)
130
- character.add(visualsObject)
131
-
132
- // Now you can manipulate visuals separately
133
- visualsObject.position.set(0, -0.5, 0) // Offset visual from collision center
134
- ```
135
-
136
- ### Proper Cleanup
137
-
138
- ```typescript
139
- // Always dispose GameObjects when done
140
- onGameEnd() {
141
- this.player.dispose() // Cleans up ALL components and children
142
- }
143
-
144
- // Don't just remove from scene
145
- // Bad: this.player.removeFromParent() // Components still running!
146
- ```
147
-
148
- ### Component Communication
149
-
150
- ```typescript
151
- // Good - Get component reference in onCreate
152
- class MyComponent extends Component {
153
- private meshRenderer?: MeshRenderer
154
-
155
- protected onCreate(): void {
156
- this.meshRenderer = this.getComponent(MeshRenderer)
157
- }
158
-
159
- public update(deltaTime: number): void {
160
- if (this.meshRenderer) {
161
- // Use cached reference
162
- }
163
- }
164
- }
165
- ```
166
-
167
- ## Anti-Patterns
168
-
169
- ### Don't Create GameObjects in Update
170
-
171
- ```typescript
172
- // Bad - Creates new GameObject every frame
173
- public update(deltaTime: number): void {
174
- const temp = new GameObject("Temp") // Memory leak!
175
- }
176
-
177
- // Good - Create once, reuse or pool
178
- ```
179
-
180
- ### Don't Forget to Dispose
181
-
182
- ```typescript
183
- // Bad - Memory leak
184
- spawnPickup() {
185
- const pickup = new GameObject("Pickup")
186
- // ... use it briefly ...
187
- // Never disposed!
188
- }
189
-
190
- // Good - Always clean up
191
- spawnPickup() {
192
- const pickup = new GameObject("Pickup")
193
- // ... use it ...
194
- pickup.dispose() // Clean up when done
195
- }
196
- ```
197
-
198
- ## Related Systems
199
-
200
- - [Component](Component.md) - Base class for GameObject behaviors
201
- - [VenusGame](VenusGame.md) - Main game class that manages the scene
202
- - [MeshRenderer](../rendering/MeshRenderer.md) - Render 3D meshes on GameObjects
203
- - [RigidBodyComponent](../physics/RigidBodyComponent.md) - Add physics to GameObjects
204
-
1
+ # GameObject
2
+
3
+ GameObject is the base entity class in the Rundot 3D Engine, extending THREE.Object3D to provide entity-component architecture with automatic lifecycle management.
4
+
5
+ ## Quick Start
6
+
7
+ ```typescript
8
+ import { GameObject } from "@series-ai/rundot-3d-engine"
9
+
10
+ // Create a GameObject (automatically added to scene)
11
+ const player = new GameObject("Player")
12
+ player.position.set(0, 1, 0)
13
+
14
+ // Add components for behavior
15
+ const renderer = new MeshRenderer("character_mesh")
16
+ player.addComponent(renderer)
17
+
18
+ const controller = new PlayerController()
19
+ player.addComponent(controller)
20
+ ```
21
+
22
+ ## Common Use Cases
23
+
24
+ ### Creating Child Objects
25
+
26
+ ```typescript
27
+ // Create parent-child hierarchy
28
+ const parent = new GameObject("Parent")
29
+ const child = new GameObject("Child")
30
+
31
+ // Attach child to parent
32
+ parent.add(child)
33
+ child.position.set(2, 0, 0) // Local position relative to parent
34
+ ```
35
+
36
+ ### Getting Components
37
+
38
+ ```typescript
39
+ // Retrieve a component from the same GameObject
40
+ const meshRenderer = player.getComponent(MeshRenderer)
41
+ if (meshRenderer) {
42
+ meshRenderer.setVisible(false)
43
+ }
44
+
45
+ // Check if a component exists
46
+ if (player.hasComponent(RigidBodyComponent)) {
47
+ console.log("Player has physics enabled")
48
+ }
49
+ ```
50
+
51
+ ### Enable/Disable Objects
52
+
53
+ ```typescript
54
+ // Disable GameObject and all its components
55
+ player.setEnabled(false) // Triggers onDisabled() on all components
56
+
57
+ // Enable GameObject
58
+ player.setEnabled(true) // Triggers onEnabled() on all components
59
+
60
+ // Check enabled state (includes parent hierarchy)
61
+ if (player.isEnabled()) {
62
+ console.log("Player is active")
63
+ }
64
+ ```
65
+
66
+ ### Cleanup
67
+
68
+ ```typescript
69
+ // Properly dispose GameObject and all components
70
+ player.dispose() // Cleans up components, removes from scene, disposes meshes
71
+
72
+ // Remove a single component
73
+ player.removeComponent(RigidBodyComponent) // Triggers onCleanup() on component
74
+ ```
75
+
76
+ ## API Overview
77
+
78
+ ### Constructor
79
+
80
+ - `new GameObject(name?: string)` - Creates GameObject and adds to scene. Auto-generates name if not provided.
81
+
82
+ ### Component Management
83
+
84
+ - `addComponent<T>(component: T): T` - Adds a component instance. Throws if duplicate type exists.
85
+ - `getComponent<T>(type): T | undefined` - Gets component of specified type.
86
+ - `hasComponent<T>(type): boolean` - Checks if component exists.
87
+ - `removeComponent<T>(type): boolean` - Removes and cleans up component.
88
+
89
+ ### Hierarchy
90
+
91
+ - `add(...objects)` - Add child objects (THREE.Object3D or GameObject).
92
+ - `parent` - Reference to parent object.
93
+ - `children` - Array of child objects.
94
+
95
+ ### Lifecycle
96
+
97
+ - `dispose()` - Cleanup GameObject, all components, and children recursively.
98
+ - `setEnabled(enabled: boolean)` - Set enabled state, triggering component callbacks.
99
+ - `isEnabled(): boolean` - Check if enabled (includes parent hierarchy).
100
+
101
+ ### Three.js Properties
102
+
103
+ GameObjects inherit all THREE.Object3D properties:
104
+ - `position: THREE.Vector3` - World or local position
105
+ - `rotation: THREE.Euler` - Rotation in radians
106
+ - `scale: THREE.Vector3` - Scale factors
107
+ - `quaternion: THREE.Quaternion` - Alternative rotation representation
108
+ - `visible: boolean` - Visibility (use setEnabled() for components)
109
+
110
+ ## Patterns & Best Practices
111
+
112
+ ### Always Name Your GameObjects
113
+
114
+ ```typescript
115
+ // Good - Easy to debug
116
+ const pickup = new GameObject("MoneyPickup")
117
+
118
+ // Avoid - Auto-generated name is harder to track
119
+ const pickup = new GameObject()
120
+ ```
121
+
122
+ ### Use Child GameObjects for Hierarchy
123
+
124
+ ```typescript
125
+ // Good - Organized hierarchy
126
+ const character = new GameObject("Character")
127
+ const visualsObject = new GameObject("Visuals")
128
+ const meshRenderer = new MeshRenderer("character_model")
129
+ visualsObject.addComponent(meshRenderer)
130
+ character.add(visualsObject)
131
+
132
+ // Now you can manipulate visuals separately
133
+ visualsObject.position.set(0, -0.5, 0) // Offset visual from collision center
134
+ ```
135
+
136
+ ### Proper Cleanup
137
+
138
+ ```typescript
139
+ // Always dispose GameObjects when done
140
+ onGameEnd() {
141
+ this.player.dispose() // Cleans up ALL components and children
142
+ }
143
+
144
+ // Don't just remove from scene
145
+ // Bad: this.player.removeFromParent() // Components still running!
146
+ ```
147
+
148
+ ### Component Communication
149
+
150
+ ```typescript
151
+ // Good - Get component reference in onCreate
152
+ class MyComponent extends Component {
153
+ private meshRenderer?: MeshRenderer
154
+
155
+ protected onCreate(): void {
156
+ this.meshRenderer = this.getComponent(MeshRenderer)
157
+ }
158
+
159
+ public update(deltaTime: number): void {
160
+ if (this.meshRenderer) {
161
+ // Use cached reference
162
+ }
163
+ }
164
+ }
165
+ ```
166
+
167
+ ## Anti-Patterns
168
+
169
+ ### Don't Create GameObjects in Update
170
+
171
+ ```typescript
172
+ // Bad - Creates new GameObject every frame
173
+ public update(deltaTime: number): void {
174
+ const temp = new GameObject("Temp") // Memory leak!
175
+ }
176
+
177
+ // Good - Create once, reuse or pool
178
+ ```
179
+
180
+ ### Don't Forget to Dispose
181
+
182
+ ```typescript
183
+ // Bad - Memory leak
184
+ spawnPickup() {
185
+ const pickup = new GameObject("Pickup")
186
+ // ... use it briefly ...
187
+ // Never disposed!
188
+ }
189
+
190
+ // Good - Always clean up
191
+ spawnPickup() {
192
+ const pickup = new GameObject("Pickup")
193
+ // ... use it ...
194
+ pickup.dispose() // Clean up when done
195
+ }
196
+ ```
197
+
198
+ ## Related Systems
199
+
200
+ - [Component](Component.md) - Base class for GameObject behaviors
201
+ - [VenusGame](VenusGame.md) - Main game class that manages the scene
202
+ - [MeshRenderer](../rendering/MeshRenderer.md) - Render 3D meshes on GameObjects
203
+ - [RigidBodyComponent](../physics/RigidBodyComponent.md) - Add physics to GameObjects
204
+