diff --git a/cmi2/Scene/card.cpp b/cmi2/Scene/card.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4ad8dea7bdc25af20bf22352e31e8f88279fb5d0 --- /dev/null +++ b/cmi2/Scene/card.cpp @@ -0,0 +1,152 @@ +#include "card.h" +#include "cardholder.h" + +Card::Card(const std::string & scriptFilename) +{ + this->LoadScript(scriptFilename); +} + +Card::~Card() +{ + if (this->timer != nullptr) { + this->timer->stop(); + delete this->timer; + } +} + +void Card::LoadScript(const std::string & scriptFilename) +{ + + using namespace luabridge; + + /* Déclaraction du Lua State */ + lua_State *L = luaL_newstate(); + /* Chargement des libraries Lua */ + luaL_openlibs(L); + /* On récupère le namespace Lua (les symboles quoi) */ + getGlobalNamespace(L) + /* C'est ici qu'on "donne accès" au Lua à certaines fonctions et attributs */ + /*Début de la déclaration de la classe en Lua */ + .beginClass < Card > ("Card") + /*Ajout de l'attribut "name", via les méthodes get et set de cet attribut */ + .addProperty("name", &Card::getName, &Card::setName) + /*Ajout de l'attribut "image", via les méthodes get et set de cet + * attribut*/ + .addProperty("image", &Card::getImage, &Card::setImage) + /* Ajout de l'attribut "nameColor", via les méthodes get et set de cet attribut */ + .addProperty("cardBack", &Card::getCardBack, &Card::setCardBack) + /* etc... */ + .addProperty("cardHolder", &Card::getCardHolder) + .addFunction("getFullPath", &Card::getFullPath) + .addFunction("destroy", &Card::destroy) + .addFunction("destroyIn", &Card::destroyIn) + /*Fin de la déclaraction de la classe en Lua */ + .endClass() + .beginClass < GameWindow > ("GameWindow") + .endClass() + .beginClass < Holder > ("Holder") + .addFunction("displayAnimation", &Holder::displayAnimation) + .endClass() + .deriveClass < CardHolder, Holder > ("CardHolder") + .addFunction("displayFaceUp", &CardHolder::displayFaceUp) + .addFunction("displayFaceDown", &CardHolder::displayFaceDown) + .addFunction("displayEmpty", &CardHolder::displayEmpty) + .endClass(); + + /* Cas où le script n'existe pas ou n'a pas pu être ouvert */ + if (luaL_dofile(L, scriptFilename.c_str()) != 0) { + std:: + cout << "Erreur, impossible d'ouvrir le script " << + scriptFilename << std::endl; + } else { + /* Le nom de la table est le même que celui du fichier sans l'extension + * (.lua) donc on enlève 4 caractères.*/ + std::string tableName = + scriptFilename.substr(scriptFilename.find_last_of("/") + 1, + scriptFilename.length()); + tableName = tableName.substr(0, tableName.length() - 4); + + LuaRef table = getGlobal(L, tableName.c_str()); + if (table.isTable()) { /*On ne charge rien si le nom de la table n'est pas + correcte */ + if (table["onCut"].isFunction()) + this->onCutFunc = + std::make_unique < LuaRef > + (table["onCut"]); + if (table["init"].isFunction()) + this->initFunc = + std::make_unique < LuaRef > (table["init"]); + } + init(); + } +} + +std::string Card::getCardBackPath()const +{ + return getFullPath(getCardBack()); +} + +std::string Card::getImagePath()const +{ + return getFullPath(getImage()); +} + +std::string Card::getFullPath(std::string fileName) const +{ + std::string path = CARD_MODS_PATH; + path.append(this->getName().append("/").append(fileName)); + return path; +} + +void Card::onCut(GameWindow * gw, int myPos, int cutPos) +{ + if (onCutFunc) { + try { + (*onCutFunc) (this, gw, myPos, cutPos); + } + catch(luabridge::LuaException const &e) { + std::cerr << "Exception Lua : " << e. + what() << std::endl; + } + } else { + qDebug() << "No onCut function in Lua"; + } + emit finished(); +} + +void Card::init() +{ + if (initFunc) { + try { + (*initFunc) (this); + } + catch(luabridge::LuaException const &e) { + std::cerr << "Exception Lua : " << e. + what() << std::endl; + } + } else { + qDebug() << "No init function in Lua"; + } +} + +void Card::destroy() +{ + if (timer != nullptr) { + delete timer; + timer = nullptr; + } + if (getCardHolder() != nullptr) + getCardHolder()->destroy(); +} + +void Card::destroyIn(unsigned int msecs) +{ + timer = new QTimer(this); + connect(timer, SIGNAL(timeout()), this, SLOT(destroy())); + timer->start(msecs); +} + +void Card::sendDestroySignal() +{ + emit delCardSignal(getCardId()); +}