LuaTest - mover.h

// Project      LuaTest
// Source       mover.h
// Date         08/03/18
// Author       yamahara
// Environment  Bcc++ & DXライブラリ & Lua
// Etc          Copyright(C) 2008 sansuido. All rights reserved.

#ifndef         MOVER_DEF
#define         MOVER_DEF

#include        "data.h"

class Mover {

private:
        float   x_, y_;
        float   exp_;
        float   angle_;
        int     timer_;
        
public:
        string  key;    // グラフィック取得用ネーミング
        
                        // コンストラクタ・デストラクタ
        Mover() : key(), x_(0), y_(0), exp_(1.0f), angle_(0), timer_(0) {}
        virtual ~Mover() {}
        
                        // ゲッター・セッター
        virtual float   getX() { return x_; }
        virtual void    setX(float x) { x_ = x; }
        virtual float   calcX(float x) { x_ += x; return x_; }
        virtual float   getY() { return y_; }
        virtual void    setY(float y) { y_ = y; }
        virtual float   calcY(float y) { y_ += y; return y_; }
        
        virtual float   getExp() { return exp_; }
        virtual void    setExp(float exp) { exp_ = exp; }
        virtual float   getAngle() { return angle_; }
        virtual void    setAngle(float angle) { angle_ = angle; }
        
        virtual int     getTimer() { return timer_; }
        
        virtual int     callLua(
                        // Luaに問い合わせる
        Lua             *lua,
        const char      *command
        )
        
        {
                string  call;
                try {
                        call = key + "_" + command;
                        
                        lua_getglobal(lua->getL(), call.c_str());
                        lua_pushlightuserdata(lua->getL(), this);
                        if (lua_pcall(lua->getL(), 1, 0, 0)) {
                                throw lua_tostring(lua->getL(), -1);
                        }
                        
                } catch (string str) {
                        MessageBox(
                                GetMainWindowHandle(),
                                str.c_str(),
                                "lua script error",
                                MB_OK);
                        lua_settop(lua->getL(), 0);
                        
                        return -1;
                }
                
                return 0;
        }
        
        virtual int     init(
        Lua             *lua
        )
        
        {
                
                return callLua(lua, "init");
        }
        
        virtual int     draw(
        Lua             *lua
        )
        
        {
                
                return callLua(lua, "draw");
        }
        
        virtual int     exec(
        Lua             *lua
        )
        {
                int     stat;
                
                stat = callLua(lua, "exec");
                if (stat) return stat;
                
                timer_++;
                return 0;

        }
        
};

#endif