在服务器和客户端之间使用相同的数据

mar*_*and 2 c++

我正在设计一个仅限网络的游戏.我想在服务器端和客户端之间共享存储在C++类中的游戏数据.

这样做的好处是可以将数据放在同一个地方,从而简化了维护工作.但是,我的课程设计有问题,因为:

  1. 在服务器端,只需要逻辑
  2. 在客户端,您想要绘制,渲染等

换句话说,我不想在服务器上安装图形库,反之亦然.

该课程可能如下所示:

class Fire : public Spell {
private:
    /* some data */

public:
    void execute(Player &p)
    {
        // call server actions
    }

    void draw(Player &p)
    {
        // call rendering and such
    }
};
Run Code Online (Sandbox Code Playgroud)

Execute是仅用于服务器的功能,draw用于客户端.我真的不想使用宏来确定数据的哪一侧,因为它往往是丑陋和不可维护的.

我正在寻找一种精心设计的方法来实现这一目标.

你会做什么/使用什么?

SHR*_*SHR 5

我想你应该得出你需要分裂类并从Spell开始创建两个类的结论:

//put here all common spell things
class Spell{
};

//add to spell the server logic
class SpellLogic: public Spell{
public:
   virtual void execute(Player &p);
};

//add here the client graphic
class SpellGraphic: public Spell{
public:
   virtual void draw(Player &p);
};
Run Code Online (Sandbox Code Playgroud)

每个法术推导将被分成两个.

客户端将使用Graphics类,服务器将使用逻辑.