From fb99693360b63e673530d950e2ff1e0fe0cf314b Mon Sep 17 00:00:00 2001 From: Brian Caine Date: Tue, 2 Sep 2008 23:50:16 -0400 Subject: [PATCH] Uhh, added some script bindings. Yeah, good stuff. --- TODO | 3 +- include/core/Actor.h | 3 + src/core/Actor.cpp | 307 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/core/Game.cpp | 64 +++++++---- src/core/Level.cpp | 214 +++++++++++++++++++++++++++++++++++ 5 files changed, 570 insertions(+), 21 deletions(-) diff --git a/TODO b/TODO index 1dffc8d..d312b08 100644 --- a/TODO +++ b/TODO @@ -5,10 +5,9 @@ Ok, so this is a list of stuff to do. {in no particular order} * Provide the Level(Level& old_level) function * Provide a way for collision events to be passed * Add mouse click events -* Write an animation plugin * Fix running potpourri in the game directory * Find why stuff in chipmunk randomly refuses to rotate -* Add a rotational momentum (sp?) xml parameter +* Add a rotational inertia xml parameter * Provide a more flexible mechanism for locating plugins * Networking * Fix the engine parameter passing thing diff --git a/include/core/Actor.h b/include/core/Actor.h index d3236ae..e170dc1 100644 --- a/include/core/Actor.h +++ b/include/core/Actor.h @@ -101,6 +101,7 @@ namespace fragrant LevelPair position; float angle; Drawable* graphic; + std::string name; }; class Game; @@ -134,6 +135,8 @@ namespace fragrant void setAngle(float); float getAngle(); + std::string getName(); + // event stuff void raiseEvent(Event event); diff --git a/src/core/Actor.cpp b/src/core/Actor.cpp index 4f339cf..5043642 100644 --- a/src/core/Actor.cpp +++ b/src/core/Actor.cpp @@ -56,6 +56,7 @@ Actor::Actor(ActorData data) ngraphic.position.x = static_cast(c_graphics.pos.x); ngraphic.position.y = static_cast(c_graphics.pos.y); ngraphic.angle = c_graphics.angle; + ngraphic.name = c_graphics.sauce; ngraphic.graphic = data.game->getGraphics(c_graphics.sauce); @@ -109,6 +110,11 @@ float Actor::getAngle() return physics_object->getAngle(); } +std::string Actor::getName() +{ + return initial_params.name; +} + void Actor::raiseEvent(Event event) { std::map::iterator iter; @@ -175,6 +181,18 @@ std::vector Actor::getClassFunctions() results.push_back("getAngularVelocity"); results.push_back("setAngularVelocity"); + results.push_back("removeGraphic"); // actor.removeGraphic(idx) + results.push_back("addGraphic"); // actor.addGraphic(name, pos, angle) + + results.push_back("getGraphicIndex"); + results.push_back("getGraphicListSize"); + + results.push_back("setGraphicPosition"); + results.push_back("getGraphicPosition"); + + results.push_back("setGraphicAngle"); + results.push_back("getGraphicAngle"); + return results; } @@ -220,6 +238,7 @@ Variant* Actor::callFunction(std::string funcname, params.at(1)->get() : params.at(1)->get()); } + catch (...) { std::cout @@ -494,6 +513,294 @@ Variant* Actor::callFunction(std::string funcname, physics_object->setAngularVelocity(nvelocity); } + if (funcname == "removeGraphic") + { + if (params.size() < 1 || !params.at(0)->verifyType()) + { + std::cout + << "Actor::callFunction(funcname=\"removeGraphic\")" + ": wrong params" + << std::endl; + return results; + } + + int idx = params.at(0)->get(); + + if (idx >= sprites.size()) + { + std::cout + << "Actor::callFunction(funcname=\"removeGraphic\")" + ": wrong params" + << std::endl; + return results; + } + + sprites.erase(sprites.begin() + idx); + } + + if (funcname == "addGraphic") + { + if (params.size() < 1 || + !params.at(0)->verifyType()) + { + std::cout + << "Actor::callFunction(funcname=\"addGraphic\")" + ": wrong params" + << std::endl; + return results; + } + + std::string name = params.at(0)->get(); + ActorPair pos = makePair(0,0); + float angle = 0; + + try + { + if (params.size() > 2) + { + pos.x = (params.at(1)->verifyType()) ? + (params.at(1)->get()) : + (params.at(1)->get()); + pos.y = (params.at(2)->verifyType()) ? + (params.at(2)->get()) : + (params.at(2)->get()); + } + } + + catch (...) + { + std::cout + << "Actor::callFunction(funcname=\"addGraphic\"): wrong params" + << std::endl; + return results; + } + + if (params.size() > 3) + { + try + { + angle = (params.at(3)->verifyType()) ? + (params.at(3)->get()) : + (params.at(3)->get()); + } + + catch (...) + { + std::cout + << "Actor::callFunction(funcname=\"addGraphic\")" + ": wrong params" + << std::endl; + return results; + } + } + + ActorGraphic ngraphic; + ngraphic.name = name; + ngraphic.position.x = pos.x; + ngraphic.position.y = pos.y; + ngraphic.angle = angle; + + ngraphic.graphic = initial_params.game->getGraphics(ngraphic.name); + + if (ngraphic.graphic) + { + sprites.push_back(ngraphic); + *results = sprites.size() - 1; + } + + else + { + std::cout + << "Actor::callFunction(funcname=\"addGraphic\")" + ": failed to make graphic" + << std::endl; + return results; + } + } + + if (funcname == "getGraphicIndex") + { + if (params.size() < 1 || !params.at(0)->verifyType()) + { + std::cout + << "Actor::callFunction(funcname=\"getGraphicIndex\")" + ": wrong params" + << std::endl; + return results; + } + + std::string name = params.at(0)->get(); + int cur; + + for (cur = 0; cur < sprites.size(); cur++) + if (sprites.at(cur).name == name) + break; + + if (cur >= sprites.size()) + { + std::cout + << "Actor::callFunction(funcname=\"getGraphicIndex\")" + ": no such sprite" + << std::endl; + cur = -1; + } + + *results = cur; + } + + if (funcname == "getGraphicListSize") + *results = sprites.size(); + + if (funcname == "setGraphicPosition") + { + if (params.size() < 3 || + !params.at(0)->verifyType()) + { + std::cout + << "Actor::callFunction(funcname=\"setGraphicPosition\")" + ": wrong params" + << std::endl; + return results; + } + + int idx = params.at(0)->get(); + ActorPair pos; + + if (idx >= sprites.size() || idx < 0) + { + std::cout + << "Actor::callFunction(funcname=\"setGraphicPosition\")" + ": bad index" + << std::endl; + return results; + } + + try + { + pos.x = (params.at(1)->verifyType()) ? + (params.at(1)->get()) : + (params.at(1)->get()); + pos.y = (params.at(2)->verifyType()) ? + (params.at(2)->get()) : + (params.at(2)->get()); + } + + catch (...) + { + std::cout + << "Actor::callFunction(funcname=\"setGraphicPosition\")" + ": wrong params" + << std::endl; + return results; + } + + sprites.at(idx).position.x = pos.x; + sprites.at(idx).position.y = pos.y; + } + + if (funcname == "getGraphicPosition") + { + if (params.size() < 1 || + !params.at(0)->verifyType()) + { + std::cout + << "Actor::callFunction(funcname=\"getGraphicPosition\")" + ": wrong params" + << std::endl; + return results; + } + + int idx = params.at(0)->get(); + + if (idx >= sprites.size() || idx < 0) + { + std::cout + << "Actor::callFunction(funcname=\"getGraphicPosition\")" + ": bad index" + << std::endl; + return results; + } + + std::vector dresults; + + dresults.push_back(new Variant); + dresults.push_back(new Variant); + + *(dresults.at(0)) = sprites.at(idx).position.x; + *(dresults.at(1)) = sprites.at(idx).position.y; + + *results = dresults; + } + + if (funcname == "setGraphicAngle") + { + if (params.size() < 2 || + !params.at(0)->verifyType()) + { + std::cout + << "Actor::callFunction(funcname=\"setGraphicAngle\")" + ": wrong params" + << std::endl; + return results; + } + + int idx = params.at(0)->get(); + float angle; + + if (idx >= sprites.size() || idx < 0) + { + std::cout + << "Actor::callFunction(funcname=\"setGraphicAngle\")" + ": bad index" + << std::endl; + return results; + } + + try + { + angle = (params.at(1)->verifyType()) ? + (params.at(1)->get()) : + (params.at(1)->get()); + } + + catch (...) + { + std::cout + << "Actor::callFunction(funcname=\"setGraphicAngle\")" + ": wrong params" + << std::endl; + return results; + } + + sprites.at(idx).angle = angle; + } + + if (funcname == "getGraphicAngle") + { + if (params.size() < 1 || + !params.at(0)->verifyType()) + { + std::cout + << "Actor::callFunction(funcname=\"getGraphicAngle\")" + ": wrong params" + << std::endl; + return results; + } + + int idx = params.at(0)->get(); + + if (idx >= sprites.size() || idx < 0) + { + std::cout + << "Actor::callFunction(funcname=\"getGraphicAngle\")" + ": bad index" + << std::endl; + return results; + } + + *results = sprites.at(idx).angle; + } + return results; } diff --git a/src/core/Game.cpp b/src/core/Game.cpp index e22533d..2bd1fa5 100644 --- a/src/core/Game.cpp +++ b/src/core/Game.cpp @@ -265,23 +265,10 @@ Game::Game(GameData gdata, std::string dpresentation, } if (the_plugin->getData().type == PT_Graphics) - { - Graphics ngraphics = the_plugin->getPayload()->get(); - - Drawable* result = ngraphics.makeImage(media_loader, - cur_media.contents); - - if (result) - graphic_samples[cur_media.name] = result; - else - std::cerr << "Game::Game(): Error loading graphic" - << std::endl; - - continue; - } + continue; std::cerr << "Game::Game(): requested plugin is not audio nor " - << "graphical, \"" << cur_media.plugin_name << "\"" + << "graphics, \"" << cur_media.plugin_name << "\"" << std::endl; } } @@ -420,11 +407,50 @@ void Game::raiseEvent(Event event) Drawable* Game::getGraphics(std::string name) { - if (graphic_samples.find(name) == graphic_samples.end()) - throw std::runtime_error( - ("Game::getGraphics(): no such graphic, \"" + name + "\"")); + int cur; + for (cur = 0; cur < data.media.size(); cur++) + if (data.media.at(cur).name == name) + { + GameMedia cur_media = data.media.at(cur); + + Plugin* the_plugin = plugin_loader.getPlugin(cur_media.plugin_name); + + if (!the_plugin) + throw std::runtime_error( + ("Game::getGraphics(): error loading graphic, \"" + + name + "\"")); + + if (!isURL(cur_media.contents) && isRelative(cur_media.contents)) + cur_media.contents = simplifyPath(data.base + path_separator + + cur_media.contents); - return graphic_samples[name]; + if (the_plugin->getData().type == PT_Graphics) + { + Graphics ngraphics = the_plugin->getPayload()->get(); + + Drawable* result = ngraphics.makeImage(media_loader, + cur_media.contents); + + if (result) + { + while (graphic_samples.find(cur_media.name) != + graphic_samples.end()) + cur_media.name += "_"; + + graphic_samples[cur_media.name] = result; + return result; + } + + else + throw std::runtime_error( + ("Game::getGraphics(): error loading graphic, \"" + + name + "\"")); + } + + throw std::runtime_error( + ("Game::getGraphics(): error loading graphic, \"" + + name + "\"")); + } } AudioSample* Game::getAudio(std::string name) diff --git a/src/core/Level.cpp b/src/core/Level.cpp index 126e41c..186967c 100644 --- a/src/core/Level.cpp +++ b/src/core/Level.cpp @@ -243,9 +243,32 @@ std::vector Level::getClassFunctions() results.push_back("getViewport"); results.push_back("setViewport"); + results.push_back("getActorIndicesByName"); + results.push_back("getActor"); + + results.push_back("getActorZIndex"); + results.push_back("setActorZIndex"); + + results.push_back("deleteActor"); + results.push_back("makeActor"); + return results; } +template +struct find_actordata : public std::unary_function +{ + void operator() (TYPE& x) + { + if (x.name == to_find) + *data = x; + } + + std::string to_find; + ActorData* data; + bool found; +}; + Variant* Level::callFunction(std::string funcname, std::vector params, ScriptVM* script_vm) { @@ -291,6 +314,197 @@ Variant* Level::callFunction(std::string funcname, viewport = nviewport; } + if (funcname == "getActorIndicesByName") + { + if (params.size() < 1 || !params.at(0)->verifyType()) + { + std::cout + << "Level::callFunction(funcname=\"getActorIndicesByName\"):" + " wrong params" + << std::endl; + return results; + } + + std::vector nresults; + std::string name = params.at(0)->get(); + + int cur; + for (cur = 0; cur < actors.size(); cur++) + if (actors.at(cur).actor->getName() == name) + { + nresults.push_back(new Variant); + *(nresults.at(nresults.size() - 1)) = cur; + } + + *results = nresults; + } + + if (funcname == "getActor") + { + if (params.size() < 1 || !params.at(0)->verifyType()) + { + std::cout + << "Level::callFunction(funcname=\"getActor\"):" + " wrong params" + << std::endl; + return results; + } + + int idx = params.at(0)->get(); + + if (idx < 0 || idx >= actors.size()) + { + std::cout + << "Level::callFunction(funcname=\"getActor\"):" + " invalid index" + << std::endl; + return results; + } + + *results = dynamic_cast(actors.at(idx).actor); + } + + if (funcname == "getActorZIndex") + { + if (params.size() < 1 || !params.at(0)->verifyType()) + { + std::cout + << "Level::callFunction(funcname=\"getActorZIndex\"):" + " wrong params" + << std::endl; + return results; + } + + int idx = params.at(0)->get(); + + if (idx < 0 || idx >= actors.size()) + { + std::cout + << "Level::callFunction(funcname=\"getActorZIndex\"):" + " invalid index" + << std::endl; + return results; + } + + *results = actors.at(idx).zindex; + } + + if (funcname == "setActorZIndex") + { + if (params.size() < 2 || !params.at(0)->verifyType()) + { + std::cout + << "Level::callFunction(funcname=\"setActorZIndex\"):" + " wrong params" + << std::endl; + return results; + } + + int idx = params.at(0)->get(); + float nzindex; + + try + { + nzindex = params.at(1)->verifyType() ? + params.at(1)->get(): + params.at(1)->get(); + } + + catch(...) + { + std::cout + << "Level::callFunction(funcname=\"setActorZIndex\"):" + " wrong params" + << std::endl; + return results; + } + + if (idx < 0 || idx >= actors.size()) + { + std::cout + << "Level::callFunction(funcname=\"setActorZIndex\"):" + " invalid index" + << std::endl; + return results; + } + + actors.at(idx).zindex = nzindex; + } + + if (funcname == "deleteActor") + { + if (params.size() < 1 || !params.at(0)->verifyType()) + { + std::cout + << "Level::callFunction(funcname=\"deleteActor\"):" + " wrong params" + << std::endl; + return results; + } + + int idx = params.at(0)->get(); + + if (idx < 0 || idx >= actors.size()) + { + std::cout + << "Level::callFunction(funcname=\"deleteActor\"):" + " invalid index" + << std::endl; + return results; + } + + delete actors.at(idx).actor; + actors.erase(actors.begin() + idx); + } + + if (funcname == "makeActor") + { + if (params.size() < 2 || + !params.at(0)->verifyType() || + !params.at(1)->verifyType()) + { + std::cout + << "Level::callFunction(funcname=\"makeActor\"):" + " wrong params" + << std::endl; + return results; + } + + std::string name = params.at(0)->get(); + std::string source = params.at(0)->get(); + + ActorData data; + + find_actordata search; + search.data = &data; + search.to_find = source; + search.found = false; + + std::for_each(actor_defs.begin(), actor_defs.end(), search); + + if (!search.found) + { + std::cout + << "Level::callFunction(funcname=\"makeActor\"):" + " failed to find source" + << std::endl; + *results = -1; + return results; + } + + Actor* nactor = new Actor(data); + + LActor ngraphic; + ngraphic.actor = nactor; + ngraphic.zindex = 0.0; + + pipeline->getTargetVector().push_back( + dynamic_cast(ngraphic.actor)); + actors.push_back(ngraphic); + + *results = actors.size() - 1; + } + return results; } -- 2.11.4.GIT