Advertisement
eliasdaler

Lua tutorial part 3. paste 2.

Dec 22nd, 2013
801
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.83 KB | None | 0 0
  1. -- test.lua
  2. player = Character.new("Hero", 100)
  3. player:setHealth(80)
  4.  
  5. healer = Character.new("Healer", 100)
  6. merchant = Character.new("Merchant", 100)
  7.  
  8. hp = player:getHealth()
  9. name = player:getName()
  10. print("Character name: "..name..". HP = "..hp)
  11.  
  12. function Healer_interact(first, second)
  13.     if(player:getHealth() < 50) then
  14.         first:heal(second)
  15.     else
  16.         first:say("You're gonna be allright, "..second:getName())
  17.     end
  18.     first:say("Hello, let me heal your wounds, "..second:getName())
  19. end
  20.  
  21. function Merchant_interact(first, second)
  22.     first:say("Do you have money? No? Then get out!")
  23. end
  24.  
  25. // main.cpp
  26. extern "C" {
  27. # include "lua.h"
  28. # include "lauxlib.h"
  29. # include "lualib.h"
  30. }
  31.  
  32. #include <iostream>
  33. #include <string>
  34. #include "luawrapper.hpp"
  35.  
  36. class Character {
  37. public:
  38.     Character(const char* name, int hp);
  39.     void say(const char* text);
  40.     void heal(Character* character);
  41.     const char* getName() { return name;}
  42.     int getHealth() { return health; }
  43.     void setHealth(int hp) { health = hp; }
  44.  
  45.     void interact(lua_State* L, Character* second);
  46. private:
  47.     const char* name;
  48.     int health;
  49. };
  50.  
  51. void Character::interact(lua_State* L, Character* second) {
  52.     std::string functionName = name + std::string("_interact");
  53.     lua_getglobal(L, functionName.c_str());
  54.     luaW_push(L, this);
  55.     luaW_push(L, second);
  56.     lua_pcall(L, 2, 0, 0);
  57. }
  58.  
  59. Character::Character(const char* name, int hp) {
  60.     this->name = name;
  61.     health = hp;
  62. }
  63.  
  64. void Character::say(const char* text) {
  65.     std::cout<<name<<":"<<text<<std::endl;
  66. }
  67.  
  68. void Character::heal(Character* character) {
  69.     character->setHealth(100);
  70. }
  71.  
  72. Character* Character_new(lua_State* L) {
  73.     const char* name = luaL_checkstring(L, 1);
  74.     int hp = luaL_checknumber(L, 2);
  75.     return new Character(name, hp);
  76. }
  77.  
  78. int Character_getName(lua_State* L) {
  79.     Character* character = luaW_check<Character>(L, 1);
  80.     lua_pushstring(L, character->getName());
  81.     return 1;
  82. }
  83.  
  84.  
  85. int Character_getHealth(lua_State* L) {
  86.     Character* character = luaW_check<Character>(L, 1);
  87.     lua_pushnumber(L, character->getHealth());
  88.     return 1;
  89. }
  90.  
  91. int Character_setHealth(lua_State* L) {
  92.     Character* character = luaW_check<Character>(L, 1);
  93.     int hp = luaL_checknumber(L, 2);
  94.     character->setHealth(hp);
  95.     return 0;
  96. }
  97.  
  98. int Character_say(lua_State* L) {
  99.     Character* character = luaW_check<Character>(L, 1);
  100.     const char* text = luaL_checkstring(L, 2);
  101.     character->say(text);
  102.     return 0;
  103. }
  104.  
  105. int Character_heal(lua_State* L) {
  106.     Character* character = luaW_check<Character>(L, 1);
  107.     Character* character2 = luaW_check<Character>(L, 2);
  108.     character->heal(character2);
  109.     return 0;
  110. }
  111.  
  112. static luaL_Reg Character_table[] = {
  113.     { NULL, NULL }
  114. };
  115.  
  116. static luaL_Reg Character_metatable[] = {
  117.     { "getName", Character_getName },
  118.     { "getHealth", Character_getHealth },
  119.     { "setHealth", Character_setHealth },
  120.     { "say", Character_say },
  121.     { "heal", Character_heal},
  122.     { NULL, NULL }
  123. };
  124.  
  125. static int luaopen_Character(lua_State* L) {
  126.     luaW_register<Character>(L, "Character", Character_table, Character_metatable, Character_new);
  127.     return 1;
  128. }
  129.  
  130. int main() {
  131.     lua_State* L = luaL_newstate();  
  132.     luaL_openlibs(L);
  133.     luaopen_Character(L);
  134.     if (luaL_loadfile(L, "test.lua")) {
  135.         std::cout<<"Error, can't open script"<<std::endl;
  136.     }
  137.    
  138.     lua_pcall(L, 0, 0, 0);
  139.     lua_getglobal(L, "player");
  140.     Character* player = luaW_to<Character>(L, -1);
  141.     lua_getglobal(L, "healer");
  142.     Character* healer = luaW_to<Character>(L, -1);
  143.     lua_getglobal(L, "merchant");
  144.     Character* merchant = luaW_to<Character>(L, -1);
  145.  
  146.     healer->interact(L, player);
  147.     merchant->interact(L, player);
  148.     std::cout<<"Now player's HP is "<<player->getHealth()<<std::endl;
  149.     int x;
  150.     std::cin>>x;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement