diff options
author | Samuel Rødal <srodal@gmail.com> | 2008-11-28 02:49:23 +0100 |
---|---|---|
committer | Samuel Rødal <srodal@gmail.com> | 2008-11-28 02:49:23 +0100 |
commit | 632841ac82c84e9c57e39936eb20383d7584c33d (patch) | |
tree | 818d09c74e7e3608b25e48fe62d80fc81429d650 /mazescene.cpp | |
parent | 9ca87699b5a358cfa7304b6a76d02d03d48865ab (diff) |
Add some fun presets.
Diffstat (limited to 'mazescene.cpp')
-rw-r--r-- | mazescene.cpp | 74 |
1 files changed, 54 insertions, 20 deletions
diff --git a/mazescene.cpp b/mazescene.cpp index 47dee11..bab951e 100644 --- a/mazescene.cpp +++ b/mazescene.cpp @@ -1,6 +1,7 @@ #include "mazescene.h" #include <QCheckBox> +#include <QComboBox> #include <QGLWidget> #include <QGraphicsProxyWidget> #include <QPainter> @@ -723,31 +724,56 @@ void MazeScene::addEntity(Entity *entity) m_entities << entity; } -const char *defaultSource = - "// available functions:\n" - "// entity.turnLeft()\n" - "// entity.turnRight()\n" - "// entity.turnTowards(x, y)\n" - "// entity.walk()\n" - "// entity.stop()\n" - "// rand()\n" - "// script.display()\n" - "\n" - "// available variables:\n" - "// my_x\n" - "// my_y\n" - "// player_x\n" - "// player_y\n" - "// time\n" - "\n" - "entity.stop();\n"; - static QScriptValue qsRand(QScriptContext *, QScriptEngine *engine) { QScriptValue value(engine, qrand() / (RAND_MAX + 1.0)); return value; } +void ScriptWidget::setPreset(int preset) +{ + const char *presets[] = + { + "// available functions:\n" + "// entity.turnLeft()\n" + "// entity.turnRight()\n" + "// entity.turnTowards(x, y)\n" + "// entity.walk()\n" + "// entity.stop()\n" + "// rand()\n" + "// script.display()\n" + "\n" + "// available variables:\n" + "// my_x\n" + "// my_y\n" + "// player_x\n" + "// player_y\n" + "// time\n" + "\n" + "entity.stop();\n", + "entity.walk();\n" + "if ((time % 20000) < 10000) {\n" + " entity.turnTowards(10, 2.5);\n" + " if (my_x >= 5.5)\n" + " entity.stop();\n" + "} else {\n" + " entity.turnTowards(-10, 2.5);\n" + " if (my_x <= 2.5)\n" + " entity.stop();\n" + "}\n", + "dx = player_x - my_x;\n" + "dy = player_y - my_y;\n" + "if (dx * dx + dy * dy < 5) {\n" + " entity.stop();\n" + "} else {\n" + " entity.walk();\n" + " entity.turnTowards(player_x, player_y);\n" + "}\n" + }; + + m_sourceEdit->setPlainText(QLatin1String(presets[preset])); +} + ScriptWidget::ScriptWidget(MazeScene *scene, Entity *entity) : m_scene(scene) , m_entity(entity) @@ -759,12 +785,20 @@ ScriptWidget::ScriptWidget(MazeScene *scene, Entity *entity) layout()->addWidget(m_statusView); m_sourceEdit = new QPlainTextEdit; - m_sourceEdit->setPlainText(QLatin1String(defaultSource)); layout()->addWidget(m_sourceEdit); QPushButton *compileButton = new QPushButton(QLatin1String("Compile")); layout()->addWidget(compileButton); + QComboBox *combo = new QComboBox; + layout()->addWidget(combo); + + combo->addItem(QLatin1String("Default")); + combo->addItem(QLatin1String("Patrol")); + combo->addItem(QLatin1String("Follow")); + + setPreset(0); + connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(setPreset(int))); connect(compileButton, SIGNAL(clicked()), this, SLOT(updateSource())); m_engine = new QScriptEngine(this); |