Skip to content

Commit

Permalink
Merge pull request #650 from code-dot-org/hoc2023-initial-ai
Browse files Browse the repository at this point in the history
hoc2023: initial AI
  • Loading branch information
breville authored Sep 19, 2023
2 parents 31a89e6 + 15e6e8c commit eed1c1a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ module.exports = class DanceAPI {
changePropEachBy: (group, property, val) => {
return nativeAPI.changePropEachBy(group, property, val);
},
ai: value => {
nativeAPI.ai(value);
},
};
}
};
27 changes: 27 additions & 0 deletions src/p5.dance.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ module.exports = class DanceParty {
i18n = {
measure: () => 'Measure:',
},
doAi,
// For testing: Can provide a custom resource loader class
// to load fixtures and/or isolate us entirely from network activity
resourceLoader = new ResourceLoader(),
Expand Down Expand Up @@ -108,6 +109,9 @@ module.exports = class DanceParty {
this.world.SPRITE_NAMES = constants.SPRITE_NAMES;
this.world.MOVE_NAMES = constants.MOVE_NAMES;

// Store a reference to the main repo's AI function.
this.doAi = doAi;

if (spriteConfig) {
spriteConfig(this.world);
}
Expand Down Expand Up @@ -1116,6 +1120,29 @@ module.exports = class DanceParty {
}, this);
}

// Called when executing the AI block.
async ai(value) {
console.log('AI:', value);

// Call the main repo's doAI function which will transform this
// block's value into a useful response.
const response = await this.doAi(value);

const params = JSON.parse(response);
console.log('handle AI:', params);

this.setBackgroundEffect(
params.backgroundEffect,
params.backgroundColor
);

this.setForegroundEffect(params.foregroundEffect);

if (params.setDancer) {
this.makeNewDanceSprite('MOOSE', 'harold', null);
}
}

// Music Helpers

getEnergy(range) {
Expand Down

0 comments on commit eed1c1a

Please sign in to comment.