From 2ad47aa3758fe5d04ba1764f275a0fe30706d200 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Sat, 13 Oct 2012 20:40:37 +0200 Subject: [PATCH] use scripts for map triggers --- src/main.cpp | 44 +++++++++++++++++++++++++++++++++++++++----- src/map/MapState.cpp | 24 ++++++++++++++++-------- src/map/MapState.h | 13 ++++++++++++- src/map/Trigger.cpp | 4 ++-- src/map/Trigger.h | 11 ++++++----- 5 files changed, 75 insertions(+), 21 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index beef2fe..a8a7225 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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(6, 2); - - triggers2[0].map = &map1; - triggers2[0].target = Vector(8, 3); + unsigned char transition1text[4 + sizeof(int) + sizeof(Map *) + sizeof(Vector)]; + int i(0); + transition1text[i++] = Script::CODE_MOVE_I0; + *reinterpret_cast(transition1text + i) = 1; + i += sizeof(int); + transition1text[i++] = Script::CODE_MOVE_A0; + *reinterpret_cast(transition1text + i) = &map2; + i += sizeof(Map *); + transition1text[i++] = Script::CODE_MOVE_V0; + *reinterpret_cast *>(transition1text + i) = Vector(6, 2); + i += sizeof(Vector); + 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)]; + i = 0; + transition2text[i++] = Script::CODE_MOVE_I0; + *reinterpret_cast(transition2text + i) = 1; + i += sizeof(int); + transition2text[i++] = Script::CODE_MOVE_A0; + *reinterpret_cast(transition2text + i) = &map1; + i += sizeof(Map *); + transition2text[i++] = Script::CODE_MOVE_V0; + *reinterpret_cast *>(transition2text + i) = Vector(8, 3); + i += sizeof(Vector); + transition2text[i++] = Script::CODE_SYSCALL; + + Script transition2; + transition2.text = transition2text; + transition2.textlen = sizeof(transition2text); + + triggers2[0].SetScript(&transition2); gameState.heroes[0].MapEntity().Position() = Vector(64, 128); diff --git a/src/map/MapState.cpp b/src/map/MapState.cpp index a716f95..6b02531 100644 --- a/src/map/MapState.cpp +++ b/src/map/MapState.cpp @@ -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(r.Address0()), r.Vector0())); + ColorFade *fadeOut(new ColorFade(this, 0, 500, false)); + fadeOut->SetLeadOutTime(500); + ctrl->PushState(fadeOut); + break; + } + } +} + } diff --git a/src/map/MapState.h b/src/map/MapState.h index bf19f4d..952bcee 100644 --- a/src/map/MapState.h +++ b/src/map/MapState.h @@ -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 &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 moveTimer; geometry::Vector lastLock; graphics::Camera camera; diff --git a/src/map/Trigger.cpp b/src/map/Trigger.cpp index 3b91511..f454613 100644 --- a/src/map/Trigger.cpp +++ b/src/map/Trigger.cpp @@ -10,8 +10,8 @@ namespace map { Trigger::Trigger() -: type(TYPE_CONTACT) -, map(0) { +: script(0) +, type(TYPE_CONTACT) { } diff --git a/src/map/Trigger.h b/src/map/Trigger.h index 222f813..13b85ac 100644 --- a/src/map/Trigger.h +++ b/src/map/Trigger.h @@ -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 &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 &p) { tilePosition = p; } void SetType(Type t) { type = t; } + void SetScript(common::Script *s) { script = s; } private: + common::Script *script; geometry::Vector tilePosition; Type type; -// temporary members until scripting is implemented -public: - Map *map; - geometry::Vector target; - }; } -- 2.39.2