Skip to content

Commit

Permalink
Updated wizard world
Browse files Browse the repository at this point in the history
  • Loading branch information
MadsAndreasen committed Mar 31, 2024
1 parent 5744996 commit 33af3de
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 36 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
{
"type": "lldb",
"request": "launch",
"name": "lldb mdoor",
"program": "${workspaceFolder}/builddir/example/mdoor/mdoor",
"name": "Wizard world",
"program": "${workspaceFolder}/builddir/example/wizard/wizard",
"args": [],
"cwd": "${workspaceFolder}"
},
Expand Down
53 changes: 20 additions & 33 deletions example/wizard/wizard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@
#include <iostream>


WizardsWorld::WizardsWorld() :
Hsm("WizardsWorld", &world),
world("World", nullptr, [&] (auto event){startStd(event);}),
house("house", &world, [&] (auto event){houseAction(event);}),
hall("hall", &house, [&] (auto event){hallAction(event);}),
kitchen("kitchen", &house, [&] (auto event){kitchenAction(event);}),
garden("garden", &world, [&] (auto event){gardenAction(event);}),
frontGarden("frontGarden", &garden, [&] (auto event){frontGardenAction(event);}),
well("well", &garden, [&] (auto event){wellAction(event);})
{
transitions = std::vector<Transition>{
Transition{&frontGarden, &NORTH, &hall},
Transition{&frontGarden, &WEST, &well},
Transition{&hall, &NORTH, &kitchen},
Transition{&hall, &SOUTH, &frontGarden},
Transition{&kitchen, &SOUTH, &hall},
Transition{&kitchen, &SMELL, &kitchen, [&]() { getHungry(); }},
Transition{&kitchen, &EAT, &kitchen, [&]() { eat(); }},
Transition{&well, &EAST, &frontGarden}
WizardsWorld::WizardsWorld() : Hsm("WizardsWorld")
{
auto house = std::make_shared<State>("house", nullptr, [&] (auto event){houseAction(event);});
auto hall = std::make_shared<State>("hall", house, [&] (auto event){hallAction(event);});
auto kitchen = std::make_shared<State>("kitchen", house, [&] (auto event){kitchenAction(event);});
auto garden = std::make_shared<State>("garden", nullptr, [&] (auto event){gardenAction(event);});
auto frontGarden = std::make_shared<State>("frontGarden", garden, [&] (auto event){frontGardenAction(event);});
auto well = std::make_shared<State>("well", garden, [&] (auto event){wellAction(event);});

auto states = {frontGarden, house, hall, kitchen, garden,well};
auto transitions = {
Transition{frontGarden, &NORTH, hall},
Transition{frontGarden, &WEST, well},
Transition{hall, &NORTH, kitchen},
Transition{hall, &SOUTH, frontGarden},
Transition{kitchen, &SOUTH, hall},
Transition{kitchen, &SMELL, kitchen, [&]() { getHungry(); }},
Transition{kitchen, &EAT, kitchen, [&]() { eat(); }},
Transition{well, &EAST, frontGarden}
};
enable();
activate(states, transitions);
}

void WizardsWorld::north()
Expand Down Expand Up @@ -61,19 +61,6 @@ void WizardsWorld::noAction(StdEvents event)

}

void WizardsWorld::startStd(StdEvents event)
{
switch (event)
{
case StdEvents::START:
transitionTo(&frontGarden);
break;
case StdEvents::ENTRY:
case StdEvents::EXIT:
break;
}
}

void WizardsWorld::houseAction(StdEvents event)
{
if(StdEvents::ENTRY == event) {
Expand Down
1 change: 0 additions & 1 deletion example/wizard/wizard.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class WizardsWorld : public Hsm {
void take();

private:
State world, house, hall, kitchen, garden, frontGarden, well;
Event NORTH, EAST, SOUTH, WEST;
Event SMELL, EAT;

Expand Down

0 comments on commit 33af3de

Please sign in to comment.