@yantra-core/sutra
Version:
A JavaScript behavior tree library for easily creating and managing complex behavior patterns in game development.
62 lines (51 loc) • 1.37 kB
JavaScript
import Sutra from '../lib/sutra.js';
const bossFightBehavior = new Sutra();
// Adding conditions equivalent to xstate guards
bossFightBehavior.addCondition('isBossDamaged', () => {
// Logic to check if the boss is damaged
});
bossFightBehavior.addCondition('isBossDefeated', () => {
// Logic to check if the boss is defeated
});
// Adding actions equivalent to xstate actions
bossFightBehavior.addAction({
if: 'game::start',
then: [{ action: 'startBossFight' }]
});
bossFightBehavior.addAction({
if: 'entity::damage',
then: [
{
if: 'isBossDamaged',
then: [{ action: 'updateEntity' }],
}
],
else: [
{
if: 'entity::remove',
then: [
{
if: 'isBossDefeated',
then: [{ action: 'endRound' }]
}
]
}
]
});
bossFightBehavior.addAction({
if: 'COMPLETE_UPDATE',
then: [{ action: 'returnToActive' }]
});
bossFightBehavior.addAction({
if: 'START_NEW_ROUND',
then: [{ action: 'restartIdle' }]
});
// Actions could be defined as methods or functions that perform the necessary operations
// In game loop
bossFightBehavior.tick();
// Serialize to JSON
const jsonRepresentation = bossFightBehavior.serializeToJson();
console.log(jsonRepresentation);
// Export to English
const englishRepresentation = bossFightBehavior.exportToEnglish();
console.log(englishRepresentation);