3

我正在为一家公司开发游戏。我只会开发这个游戏两个月。我的公司要求我使我的代码干净且可扩展,以便他们在需要添加更多功能时可以聘请其他程序员。我读过 Bob 叔叔的 Clean Code。我发现很难将一些概念付诸实践。这是我的一门课

public class MenuScreen extends ScreenAdapter {

private final Game game;

private Stage stage;
private Actor groundActor;
private Actor skyActor;
private Actor titleActor;
private Actor playbtnActor;
private Actor optionbtnActor;

public MenuScreen(Game _game) {
    this.game = _game;
}

@Override
public void show() {

    this.stage = new Stage(new ScreenViewport());

    createGround();
    createSky();
    createTitle();
    createPlayButton();
    createOptionButton();
    setupStage();

    Gdx.input.setInputProcessor(this.stage);

}

public void createGround() {
    TextureRegion groundTextureRegion = Assets.instance
            .getTextureRegionByName("bg-ground");
    this.groundActor = new SRActor(groundTextureRegion);
    this.groundActor.setPosition(0, 0);
}

public void createSky() {
    TextureRegion skyTextureRegion = Assets.instance
            .getTextureRegionByName("bg-sky");
    this.skyActor = new SRActor(skyTextureRegion);
    this.skyActor.setPosition(0, 0);
}

public void createTitle() {
    TextureRegion titleTextureRegion = 
            Assets.instance.getTextureRegionByName("etc-gametitlebanner");
    this.titleActor = new SRActor(titleTextureRegion);

    int bottomLeftX = (int) ((Gdx.graphics.getWidth() - this.titleActor
            .getWidth()) / 2);
    int bottomLeftY = (int) Math.floor(0.6 * this.groundActor.getHeight());
    this.titleActor.setPosition(bottomLeftX, bottomLeftY);
}

public void createPlayButton() {

    TextureRegion playbtnTextureRegion = 
            Assets.instance.getTextureRegionByName("etc-playbtn");
    this.playbtnActor = new SRActor(playbtnTextureRegion);

    int bottomLeftX = (int) (0.01 * this.playbtnActor.getWidth())
            + (Gdx.graphics.getWidth() - (int) this.playbtnActor.getWidth())
            / 2;
    int bottomLeftY = (int) (this.titleActor.getY() + 0.25 * this.titleActor
            .getHeight());
    this.playbtnActor.setPosition(bottomLeftX, bottomLeftY);
}

public void createOptionButton() {
    TextureRegion optionbtnTextureRegion = Assets.instance
            .getTextureRegionByName("etc-optionbtn");
    this.optionbtnActor = new SRActor(optionbtnTextureRegion);

    int bottomLeftX = (int) (0.01 * this.optionbtnActor.getWidth())
            + (Gdx.graphics.getWidth() - (int) this.optionbtnActor
                    .getWidth()) / 2;
    int bottomLeftY = (int) (this.titleActor.getY() + 0.05 * this.titleActor
            .getHeight());
    this.optionbtnActor.setPosition(bottomLeftX, bottomLeftY);

}

public void setupStage() {

    this.stage.addActor(this.skyActor);
    this.stage.addActor(this.groundActor);
    this.stage.addActor(this.titleActor);
    this.stage.addActor(this.playbtnActor);
    this.stage.addActor(this.optionbtnActor);

}

@Override
public void render(float delta) {
    this.stage.act(Gdx.graphics.getDeltaTime());
    this.stage.draw();
}

@Override
public void resize(int width, int height) {
    this.stage.getViewport().update(width, height, true);
}

@Override
public void hide() {
    dispose();
}

@Override
public void dispose() {
    this.stage.dispose();
}

}

我的问题是这个类是否违反了单一责任原则?我需要将每个演员分开到自己的班级吗?如果是这样,我应该如何重构我的代码?

4

3 回答 3

1

这对我来说似乎没问题。如果你所有不同的演员都有相同的行为,只是他们使用的纹理、大小、位置等不同,那么有一个SRActor具有不同属性的演员似乎是合适的。

但是,我建议您使用静态工厂模式并将生成移动到另一个类。所以你只会做stage.addActor(SRActorFactory.createOptionButton())例子。

于 2014-07-03T07:09:32.217 回答
1

假设菜单屏幕有天空和地面(即那些东西不是游戏的一部分),那么到目前为止一切都很好。看起来你的演员在不同的班级,这很好。“MenuScreen”是一种作为单一职责有意义的事情。

于 2014-07-03T07:07:18.123 回答
1

我一直在使用 libgdx,并且一直在与一群人一起开发一款非常大的游戏。我发现特别有用的是拆分渲染的组件。例如,我有一个用于静态元素的单独 UI 类。然后有一个包含所有动态组件的“渲染场景”类。

这在菜单中并不普遍。所以很难给出一个具体的例子,因为这有点矫枉过正,但当你开始在游戏中实际制作核心功能时,这是一个很好的做法。

(输入多路复用器是值得研究的好东西)。

但是,现在只需对您的代码进行一些调整,您为什么不为您的菜单创建一个单独的“渲染”类呢?只需使用舞台初始化它,然后向其中添加演员。这将允许您添加动画序列而不会使您的MenuScreen课程臃肿。做到这一点,您所要做的就是这样做,MenuUI.render();然后它就可以工作了。您ScreenAdapter应该用于游戏屏幕中的过渡,而不是对 UI 进行硬编码。然后,如果您想更改菜单屏幕的 UI,您只需插入一个新的 UI 模块。

希望这会有所帮助:d

于 2014-07-03T07:44:36.130 回答