[8250] | 1 | |
---|
| 2 | |
---|
| 3 | |
---|
[8248] | 4 | #include <iostream> |
---|
| 5 | |
---|
| 6 | #include "luaincl.h" |
---|
[8250] | 7 | #include "base_object.h" |
---|
| 8 | #include "lunar.h" |
---|
[8248] | 9 | |
---|
[8250] | 10 | #include "script_class.h" |
---|
[8248] | 11 | |
---|
[8250] | 12 | |
---|
[8248] | 13 | class Account : public BaseObject { |
---|
| 14 | lua_Number m_balance; |
---|
| 15 | public: |
---|
| 16 | static const char className[]; |
---|
| 17 | static Lunar<Account>::RegType methods[]; |
---|
| 18 | |
---|
| 19 | Account(lua_State *L) { m_balance = luaL_checknumber(L, 1); } |
---|
[8250] | 20 | Account(double balance=0) : m_balance(balance) { this->setClassID(CL_ACCOUNT, "Account"); } |
---|
[8248] | 21 | |
---|
| 22 | void deposit (float value) { m_balance += value; }; |
---|
| 23 | void withdraw(float value) { m_balance -= value; }; |
---|
| 24 | float balance() { return m_balance; }; |
---|
| 25 | |
---|
| 26 | int deposit (lua_State *L) { m_balance += luaL_checknumber(L, 1); return 0; } |
---|
| 27 | int withdraw(lua_State *L) { m_balance -= luaL_checknumber(L, 1); return 0; } |
---|
| 28 | int balance (lua_State *L) { lua_pushnumber(L, m_balance); return 1; } |
---|
| 29 | ~Account() { printf("deleted Account (%p)\n", this); } |
---|
| 30 | }; |
---|
| 31 | |
---|
| 32 | const char Account::className[] = "Account"; |
---|
| 33 | |
---|
| 34 | Lunar<Account>::RegType Account::methods[] = { |
---|
| 35 | {"deposit", new ExecutorLua1<Account, float>(&Account::deposit)}, |
---|
| 36 | {"withdraw", new ExecutorLua1<Account, float>(&Account::withdraw)}, |
---|
| 37 | {"balance", new ExecutorLua0ret<Account, float>(&Account::balance)}, |
---|
| 38 | {0,NULL} |
---|
| 39 | }; |
---|
[8250] | 40 | |
---|
| 41 | CREATE_SCRIPTABLE_CLASS(Account, CL_ACCOUNT); |
---|
| 42 | |
---|