]> git.localhorst.tv Git - l2e.git/commitdiff
use scripts for map triggers
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sat, 13 Oct 2012 18:40:37 +0000 (20:40 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sat, 13 Oct 2012 18:40:37 +0000 (20:40 +0200)
src/main.cpp
src/map/MapState.cpp
src/map/MapState.h
src/map/Trigger.cpp
src/map/Trigger.h

index beef2feba24a0a74497c282b4dcf112a1d832350..a8a722552e2d8126cd6c22e40f88a92720dbcc9f 100644 (file)
@@ -19,6 +19,7 @@
 #include "common/Ikari.h"
 #include "common/Inventory.h"
 #include "common/Item.h"
+#include "common/Script.h"
 #include "common/Spell.h"
 #include "common/Stats.h"
 #include "geometry/Vector.h"
@@ -65,6 +66,7 @@ using common::Hero;
 using common::Ikari;
 using common::Inventory;
 using common::Item;
+using common::Script;
 using common::Spell;
 using common::Stats;
 using geometry::Vector;
@@ -518,11 +520,43 @@ int main(int argc, char **argv) {
                map2.SetWidth(1);
                map2.SetBattleBackground(bg);
 
-               triggers1[0].map = &map2;
-               triggers1[0].target = Vector<int>(6, 2);
-
-               triggers2[0].map = &map1;
-               triggers2[0].target = Vector<int>(8, 3);
+               unsigned char transition1text[4 + sizeof(int) + sizeof(Map *) + sizeof(Vector<int>)];
+               int i(0);
+               transition1text[i++] = Script::CODE_MOVE_I0;
+               *reinterpret_cast<int *>(transition1text + i) = 1;
+               i += sizeof(int);
+               transition1text[i++] = Script::CODE_MOVE_A0;
+               *reinterpret_cast<Map **>(transition1text + i) = &map2;
+               i += sizeof(Map *);
+               transition1text[i++] = Script::CODE_MOVE_V0;
+               *reinterpret_cast<Vector<int> *>(transition1text + i) = Vector<int>(6, 2);
+               i += sizeof(Vector<int>);
+               transition1text[i++] = Script::CODE_SYSCALL;
+
+               Script transition1;
+               transition1.text = transition1text;
+               transition1.textlen = sizeof(transition1text);
+
+               triggers1[0].SetScript(&transition1);
+
+               unsigned char transition2text[4 + sizeof(int) + sizeof(Map *) + sizeof(Vector<int>)];
+               i = 0;
+               transition2text[i++] = Script::CODE_MOVE_I0;
+               *reinterpret_cast<int *>(transition2text + i) = 1;
+               i += sizeof(int);
+               transition2text[i++] = Script::CODE_MOVE_A0;
+               *reinterpret_cast<Map **>(transition2text + i) = &map1;
+               i += sizeof(Map *);
+               transition2text[i++] = Script::CODE_MOVE_V0;
+               *reinterpret_cast<Vector<int> *>(transition2text + i) = Vector<int>(8, 3);
+               i += sizeof(Vector<int>);
+               transition2text[i++] = Script::CODE_SYSCALL;
+
+               Script transition2;
+               transition2.text = transition2text;
+               transition2.textlen = sizeof(transition2text);
+
+               triggers2[0].SetScript(&transition2);
 
                gameState.heroes[0].MapEntity().Position() = Vector<float>(64, 128);
 
index a716f950e8f0977ebbf14cf456bf64bbf7997f1d..6b025319b42747e00accb8ef41badb87018e0df3 100644 (file)
@@ -349,14 +349,8 @@ bool MapState::CheckMoveTrigger() {
 }
 
 void MapState::RunTrigger(Trigger &trigger) {
-       // TODO: run trigger script
-       if (trigger.map) {
-               ctrl->PushState(new ColorFade(this, 0, 500, true));
-               ctrl->PushState(new TransitionState(this, trigger.map, trigger.target));
-               ColorFade *fadeOut(new ColorFade(this, 0, 500, false));
-               fadeOut->SetLeadOutTime(500);
-               ctrl->PushState(fadeOut);
-       }
+       if (!trigger.HasScript()) return;
+       runner.Run(*this, trigger.GetScript());
 }
 
 void MapState::UpdateFollower(Entity &e) {
@@ -446,4 +440,18 @@ bool MapState::ZCompare(const Entity *lhs, const Entity *rhs) {
        return lhs->Position().Y() < rhs->Position().Y();
 }
 
+
+void MapState::HandleSyscall(common::ScriptRunner &r) {
+       switch (r.Integer0()) {
+               case TRANSITION: {
+                       ctrl->PushState(new ColorFade(this, 0, 500, true));
+                       ctrl->PushState(new TransitionState(this, reinterpret_cast<Map *>(r.Address0()), r.Vector0()));
+                       ColorFade *fadeOut(new ColorFade(this, 0, 500, false));
+                       fadeOut->SetLeadOutTime(500);
+                       ctrl->PushState(fadeOut);
+                       break;
+               }
+       }
+}
+
 }
index bf19f4d5d315b0e8750d0e544588a336083f59cd..952bcee393d242cca8d73cdf793c23f5afba062e 100644 (file)
@@ -12,6 +12,8 @@
 #include "fwd.h"
 #include "../app/State.h"
 #include "../common/fwd.h"
+#include "../common/ScriptHost.h"
+#include "../common/ScriptRunner.h"
 #include "../geometry/Vector.h"
 #include "../graphics/Camera.h"
 
@@ -20,7 +22,8 @@
 namespace map {
 
 class MapState
-: public app::State {
+: public app::State
+, public common::ScriptHost {
 
 public:
        explicit MapState(common::GameConfig *, Map *);
@@ -45,6 +48,8 @@ public:
 
        void Transition(Map *, const geometry::Vector<int> &coordinates);
 
+       virtual void HandleSyscall(common::ScriptRunner &);
+
 private:
        static bool ZCompare(const Entity *lhs, const Entity *rhs);
 
@@ -68,12 +73,18 @@ private:
        bool CheckMoveTrigger();
        void RunTrigger(Trigger &);
 
+       enum Syscalls {
+               TRANSITION = 1,
+               WARP = 2,
+       };
+
 private:
        common::GameConfig *game;
        app::Application *ctrl;
        Map *map;
        Entity *controlled;
        Entity *pushed;
+       common::ScriptRunner runner;
        app::Timer<float> moveTimer;
        geometry::Vector<int> lastLock;
        graphics::Camera camera;
index 3b915119b988e2ff9870d48f96ba657ac5392336..f454613579cd890134f1ec53110c505351be791e 100644 (file)
@@ -10,8 +10,8 @@
 namespace map {
 
 Trigger::Trigger()
-: type(TYPE_CONTACT)
-, map(0) {
+: script(0)
+, type(TYPE_CONTACT) {
 
 }
 
index 222f8138f98803cc9afaa4eb75f947684b144d98..13b85ac491a08a65b5264991bc6918fa2325f385 100644 (file)
@@ -10,6 +10,7 @@
 
 #include "Entity.h"
 #include "fwd.h"
+#include "../common/Script.h"
 #include "../geometry/Vector.h"
 
 namespace map {
@@ -31,21 +32,21 @@ public:
 public:
        const geometry::Vector<int> &TilePosition() const { return tilePosition; }
        Type GetType() const { return type; }
+       bool HasScript() const { return script; }
+       common::Script &GetScript() { return *script; }
+       const common::Script &GetScript() const { return *script; }
 
 // temporary setters
 public:
        void SetTilePosition(const geometry::Vector<int> &p) { tilePosition = p; }
        void SetType(Type t) { type = t; }
+       void SetScript(common::Script *s) { script = s; }
 
 private:
+       common::Script *script;
        geometry::Vector<int> tilePosition;
        Type type;
 
-// temporary members until scripting is implemented
-public:
-       Map *map;
-       geometry::Vector<int> target;
-
 };
 
 }