From d47f76d4a6aabc303286104faacaea63466f118c Mon Sep 17 00:00:00 2001 From: Devin Jean Date: Wed, 26 Jun 2024 11:44:16 -0500 Subject: [PATCH] basic state machines (#72) --- extensions/StateMachine/extension.json | 4 +++ extensions/StateMachine/index.js | 47 ++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 extensions/StateMachine/extension.json create mode 100644 extensions/StateMachine/index.js diff --git a/extensions/StateMachine/extension.json b/extensions/StateMachine/extension.json new file mode 100644 index 0000000..94246f2 --- /dev/null +++ b/extensions/StateMachine/extension.json @@ -0,0 +1,4 @@ +{ + "description": "create and visualize state machines", + "useDev": false +} diff --git a/extensions/StateMachine/index.js b/extensions/StateMachine/index.js new file mode 100644 index 0000000..df8c85c --- /dev/null +++ b/extensions/StateMachine/index.js @@ -0,0 +1,47 @@ +(function () { + class StateMachine extends Extension { + constructor(ide) { + super('StateMachine'); + } + + getMenu() { + return { + 'visualize': function () { + alert('Coming Soon!'); + }, + }; + } + + getCategories() { + return [ + new Extension.Category('StateMachine', new Color(150, 150, 150)), + ]; + } + + getPalette() { + const blocks = [ + new Extension.Palette.Block('smTransition'), + new Extension.Palette.Block('smInState'), + ]; + return [ + new Extension.PaletteCategory('StateMachine', blocks, SpriteMorph), + new Extension.PaletteCategory('StateMachine', blocks, StageMorph), + ]; + } + + getBlocks() { + return [ + new Extension.Block('smTransition', 'command', 'StateMachine', 'transition %var to %s', [], function (machine, state) { + this.doSetVar(machine, state); + this.doStop(); + }).terminal().for(SpriteMorph, StageMorph), + new Extension.Block('smInState', 'predicate', 'StateMachine', '%var in state %s ?', [], function (machine, state) { + const variables = this.context.variables; + return variables.getVar(machine) == state; + }).for(SpriteMorph, StageMorph), + ]; + } + } + + NetsBloxExtensions.register(StateMachine); +})();