Repository navigation
Expand file tree
/
Copy pathscripting-integration-example.js
More file actions
230 lines (188 loc) · 5.98 KB
/
Copy pathscripting-integration-example.js
File metadata and controls
230 lines (188 loc) · 5.98 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/**
* Scripting System Integration Example
* Shows how to integrate the ScriptManager into your game
*/
import { scriptManager } from '../src/scripting/ScriptManager.js';
/**
* Example 1: Initialize the scripting system
*/
async function initializeScriptingSystem() {
console.log('🎮 Initializing scripting system...');
// Initialize with hot-reload enabled for development
await scriptManager.init({
hotReload: true // Set to false in production
});
console.log('✅ Scripting system ready!');
}
/**
* Example 2: Load and use utility functions
*/
async function useUtilityFunctions() {
// Load core utils
const utilsScript = await scriptManager.loadScript('core', 'utils.js');
const utils = utilsScript.data;
// Use utility functions
const distance = utils.distance3D(
{ x: 0, y: 0, z: 0 },
{ x: 10, y: 0, z: 10 }
);
console.log('Distance:', distance);
const randomNum = utils.randomInt(1, 100);
console.log('Random number:', randomNum);
const interpolated = utils.lerp(0, 100, 0.5);
console.log('Lerp result:', interpolated);
}
/**
* Example 3: Load and use game constants
*/
async function useGameConstants() {
const constantsScript = await scriptManager.loadScript('core', 'constants.js');
const constants = constantsScript.data;
console.log('Base health:', constants.STATS.BASE_HEALTH);
console.log('Attack range:', constants.COMBAT.ATTACK_RANGE);
console.log('Animations:', constants.ANIMATIONS);
}
/**
* Example 4: Create a character with abilities
*/
async function createCharacterWithAbilities(character) {
// Load the slash ability
const abilityScript = await scriptManager.loadAbility('warrior/slash.js');
const { SlashAbility } = abilityScript.data;
// Create ability instance
const slashAbility = new SlashAbility(character);
// Add to character's abilities
if (!character.abilities) {
character.abilities = [];
}
character.abilities.push(slashAbility);
console.log(`✅ Added ${slashAbility.name} ability to ${character.name}`);
return slashAbility;
}
/**
* Example 5: Load AI behavior for enemy
*/
async function setupEnemyAI(enemy) {
// Load goblin behavior
const behaviorScript = await scriptManager.loadBehavior('enemy/goblin.json');
const behavior = behaviorScript.data;
// Apply behavior to enemy
enemy.behavior = behavior.behaviorTree;
enemy.stats = behavior.stats;
enemy.loot = behavior.loot;
console.log(`✅ Applied ${behavior.name} to enemy`);
console.log('Enemy stats:', enemy.stats);
return behavior;
}
/**
* Example 6: Use ability in combat
*/
async function performAbilityAttack(character, target) {
// Get character's first ability
const ability = character.abilities[0];
if (!ability) {
console.log('❌ No abilities available');
return;
}
// Check if can use
const canUse = ability.canUse();
if (!canUse.canUse) {
console.log(`❌ Cannot use ability: ${canUse.reason}`);
return;
}
// Execute ability
console.log(`⚔️ ${character.name} uses ${ability.name}!`);
const success = await ability.execute(target);
if (success) {
console.log('✅ Ability executed successfully!');
} else {
console.log('❌ Ability failed');
}
}
/**
* Example 7: Hot-reload a script during development
*/
async function hotReloadScript() {
console.log('🔄 Hot-reloading slash ability...');
// Reload the script
await scriptManager.reloadScript('abilities/warrior/slash.js');
console.log('✅ Script reloaded!');
}
/**
* Example 8: Complete game initialization with scripting
*/
async function initializeGame() {
try {
// 1. Initialize scripting system
await initializeScriptingSystem();
// 2. Load core scripts
await useUtilityFunctions();
await useGameConstants();
// 3. Create player character
const player = {
name: 'Hero',
position: { x: 0, y: 0, z: 0 },
health: 100,
mana: 50,
stamina: 100,
stats: { strength: 10 }
};
// 4. Add abilities to player
await createCharacterWithAbilities(player);
// 5. Create enemy
const enemy = {
name: 'Goblin',
position: { x: 5, y: 0, z: 5 },
health: 80,
takeDamage: function(damage) {
this.health -= damage;
console.log(`${this.name} took ${damage} damage! HP: ${this.health}`);
}
};
// 6. Setup enemy AI
await setupEnemyAI(enemy);
// 7. Simulate combat
console.log('\n⚔️ Starting combat simulation...\n');
await performAbilityAttack(player, enemy);
console.log('\n✅ Game initialized successfully!');
} catch (error) {
console.error('❌ Error initializing game:', error);
}
}
/**
* Example 9: Get all loaded scripts
*/
function listLoadedScripts() {
const scripts = scriptManager.getAllScripts();
console.log('\n📜 Loaded Scripts:');
scripts.forEach(script => {
console.log(` - ${script.key} (${script.category})`);
});
}
/**
* Example 10: Cleanup on game exit
*/
function cleanupScripting() {
console.log('🧹 Cleaning up scripting system...');
scriptManager.dispose();
console.log('✅ Cleanup complete');
}
// Export examples
export {
initializeScriptingSystem,
useUtilityFunctions,
useGameConstants,
createCharacterWithAbilities,
setupEnemyAI,
performAbilityAttack,
hotReloadScript,
initializeGame,
listLoadedScripts,
cleanupScripting
};
// Run example if this file is executed directly
if (import.meta.url === `file://${process.argv[1]}`) {
initializeGame().then(() => {
listLoadedScripts();
});
}