Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- test.lua
- player = Character.new("Hero", 100)
- player:setHealth(80)
- healer = Character.new("Healer", 100)
- merchant = Character.new("Merchant", 100)
- hp = player:getHealth()
- name = player:getName()
- print("Character name: "..name..". HP = "..hp)
- function Healer_interact(first, second)
- if(player:getHealth() < 50) then
- first:heal(second)
- else
- first:say("You're gonna be allright, "..second:getName())
- end
- first:say("Hello, let me heal your wounds, "..second:getName())
- end
- function Merchant_interact(first, second)
- first:say("Do you have money? No? Then get out!")
- end
- // main.cpp
- extern "C" {
- # include "lua.h"
- # include "lauxlib.h"
- # include "lualib.h"
- }
- #include <iostream>
- #include <string>
- #include "luawrapper.hpp"
- class Character {
- public:
- Character(const char* name, int hp);
- void say(const char* text);
- void heal(Character* character);
- const char* getName() { return name;}
- int getHealth() { return health; }
- void setHealth(int hp) { health = hp; }
- void interact(lua_State* L, Character* second);
- private:
- const char* name;
- int health;
- };
- void Character::interact(lua_State* L, Character* second) {
- std::string functionName = name + std::string("_interact");
- lua_getglobal(L, functionName.c_str());
- luaW_push(L, this);
- luaW_push(L, second);
- lua_pcall(L, 2, 0, 0);
- }
- Character::Character(const char* name, int hp) {
- this->name = name;
- health = hp;
- }
- void Character::say(const char* text) {
- std::cout<<name<<":"<<text<<std::endl;
- }
- void Character::heal(Character* character) {
- character->setHealth(100);
- }
- Character* Character_new(lua_State* L) {
- const char* name = luaL_checkstring(L, 1);
- int hp = luaL_checknumber(L, 2);
- return new Character(name, hp);
- }
- int Character_getName(lua_State* L) {
- Character* character = luaW_check<Character>(L, 1);
- lua_pushstring(L, character->getName());
- return 1;
- }
- int Character_getHealth(lua_State* L) {
- Character* character = luaW_check<Character>(L, 1);
- lua_pushnumber(L, character->getHealth());
- return 1;
- }
- int Character_setHealth(lua_State* L) {
- Character* character = luaW_check<Character>(L, 1);
- int hp = luaL_checknumber(L, 2);
- character->setHealth(hp);
- return 0;
- }
- int Character_say(lua_State* L) {
- Character* character = luaW_check<Character>(L, 1);
- const char* text = luaL_checkstring(L, 2);
- character->say(text);
- return 0;
- }
- int Character_heal(lua_State* L) {
- Character* character = luaW_check<Character>(L, 1);
- Character* character2 = luaW_check<Character>(L, 2);
- character->heal(character2);
- return 0;
- }
- static luaL_Reg Character_table[] = {
- { NULL, NULL }
- };
- static luaL_Reg Character_metatable[] = {
- { "getName", Character_getName },
- { "getHealth", Character_getHealth },
- { "setHealth", Character_setHealth },
- { "say", Character_say },
- { "heal", Character_heal},
- { NULL, NULL }
- };
- static int luaopen_Character(lua_State* L) {
- luaW_register<Character>(L, "Character", Character_table, Character_metatable, Character_new);
- return 1;
- }
- int main() {
- lua_State* L = luaL_newstate();
- luaL_openlibs(L);
- luaopen_Character(L);
- if (luaL_loadfile(L, "test.lua")) {
- std::cout<<"Error, can't open script"<<std::endl;
- }
- lua_pcall(L, 0, 0, 0);
- lua_getglobal(L, "player");
- Character* player = luaW_to<Character>(L, -1);
- lua_getglobal(L, "healer");
- Character* healer = luaW_to<Character>(L, -1);
- lua_getglobal(L, "merchant");
- Character* merchant = luaW_to<Character>(L, -1);
- healer->interact(L, player);
- merchant->interact(L, player);
- std::cout<<"Now player's HP is "<<player->getHealth()<<std::endl;
- int x;
- std::cin>>x;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement