|
35 | 35 | using namespace libscratchcpp; |
36 | 36 |
|
37 | 37 | const std::unordered_map<Engine::HatType, bool> Engine::m_hatRestartExistingThreads = { |
38 | | - { HatType::GreenFlag, true }, { HatType::BroadcastReceived, true }, { HatType::BackdropChanged, true }, { HatType::CloneInit, false }, |
39 | | - { HatType::KeyPressed, false }, { HatType::TargetClicked, true }, { HatType::WhenGreaterThan, false } |
| 38 | + { HatType::WhenTouchingObject, false }, { HatType::GreenFlag, true }, { HatType::BroadcastReceived, true }, { HatType::BackdropChanged, true }, |
| 39 | + { HatType::CloneInit, false }, { HatType::KeyPressed, false }, { HatType::TargetClicked, true }, { HatType::WhenGreaterThan, false } |
40 | 40 | }; |
41 | 41 |
|
42 | 42 | const std::unordered_map<Engine::HatType, bool> Engine::m_hatEdgeActivated = { |
43 | | - { HatType::GreenFlag, false }, { HatType::BroadcastReceived, false }, { HatType::BackdropChanged, false }, { HatType::CloneInit, false }, |
44 | | - { HatType::KeyPressed, false }, { HatType::TargetClicked, false }, { HatType::WhenGreaterThan, true } |
| 43 | + { HatType::WhenTouchingObject, true }, { HatType::GreenFlag, false }, { HatType::BroadcastReceived, false }, { HatType::BackdropChanged, false }, |
| 44 | + { HatType::CloneInit, false }, { HatType::KeyPressed, false }, { HatType::TargetClicked, false }, { HatType::WhenGreaterThan, true } |
45 | 45 | }; |
46 | 46 |
|
47 | 47 | Engine::Engine() : |
@@ -82,6 +82,7 @@ void Engine::clear() |
82 | 82 | m_scripts.clear(); |
83 | 83 | m_functions.clear(); |
84 | 84 |
|
| 85 | + m_whenTouchingObjectHats.clear(); |
85 | 86 | m_greenFlagHats.clear(); |
86 | 87 | m_backdropChangeHats.clear(); |
87 | 88 | m_broadcastHats.clear(); |
@@ -516,24 +517,24 @@ void Engine::step() |
516 | 517 | bool oldValue = false; |
517 | 518 | auto hatBlock = thread->script()->topBlock(); |
518 | 519 | assert(hatBlock); |
519 | | - assert(hatBlock->fieldAt(0)); // TODO: Edge-activated hats currently support only one field |
520 | | - int fieldValueId = hatBlock->fieldAt(0)->specialValueId(); |
521 | | - assert(fieldValueId != -1); |
522 | | - auto it = m_edgeActivatedHatValues.find(hatType); |
| 520 | + |
| 521 | + Target *target = hatBlock->target(); |
| 522 | + assert(target); |
| 523 | + auto it = m_edgeActivatedHatValues.find(hatBlock.get()); |
523 | 524 |
|
524 | 525 | if (it == m_edgeActivatedHatValues.cend()) { |
525 | | - m_edgeActivatedHatValues[hatType] = {}; |
| 526 | + m_edgeActivatedHatValues[hatBlock.get()] = {}; |
526 | 527 | } else { |
527 | | - const std::unordered_map<int, bool> &values = it->second; |
528 | | - auto fieldIt = values.find(fieldValueId); |
| 528 | + auto &map = it->second; |
| 529 | + auto it = map.find(target); |
529 | 530 |
|
530 | | - if (fieldIt != values.cend()) |
531 | | - oldValue = fieldIt->second; |
| 531 | + if (it != map.cend()) |
| 532 | + oldValue = it->second; |
532 | 533 | } |
533 | 534 |
|
534 | | - bool newValue = thread->script()->runHatPredicate(); |
| 535 | + bool newValue = thread->script()->runHatPredicate(hatBlock->target()); |
535 | 536 | bool edgeWasActivated = !oldValue && newValue; // changed from false true |
536 | | - m_edgeActivatedHatValues[hatType][fieldValueId] = newValue; |
| 537 | + m_edgeActivatedHatValues[hatBlock.get()][target] = newValue; |
537 | 538 |
|
538 | 539 | if (!edgeWasActivated) |
539 | 540 | stopThread(thread.get()); |
@@ -1080,6 +1081,11 @@ int Engine::findBroadcastById(const std::string &broadcastId) const |
1080 | 1081 | return it - m_broadcasts.begin(); |
1081 | 1082 | } |
1082 | 1083 |
|
| 1084 | +void Engine::addWhenTouchingObjectScript(std::shared_ptr<Block> hatBlock) |
| 1085 | +{ |
| 1086 | + addHatToMap(m_whenTouchingObjectHats, m_scripts[hatBlock].get()); |
| 1087 | +} |
| 1088 | + |
1083 | 1089 | void Engine::addGreenFlagScript(std::shared_ptr<Block> hatBlock) |
1084 | 1090 | { |
1085 | 1091 | addHatToMap(m_greenFlagHats, m_scripts[hatBlock].get()); |
@@ -1511,6 +1517,9 @@ const std::vector<Script *> &Engine::getHats(Target *target, HatType type) |
1511 | 1517 | } |
1512 | 1518 |
|
1513 | 1519 | switch (type) { |
| 1520 | + case HatType::WhenTouchingObject: |
| 1521 | + return m_whenTouchingObjectHats[target]; |
| 1522 | + |
1514 | 1523 | case HatType::GreenFlag: |
1515 | 1524 | return m_greenFlagHats[target]; |
1516 | 1525 |
|
|
0 commit comments