3

私は会社のためにゲームを開発しています。私はこのゲームを 2 か月間だけ開発します。会社から、機能を追加する必要があるときに別のプログラマーを雇えるように、コードをクリーンで拡張可能にするように依頼されました。Uncle Bob の Clean Code を読みました。いくつかのコンセプトを実践するのは本当に難しいと思いました。これは私のクラスの 1 つです。

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

メニュー画面に天と地があると仮定すると(つまり、そのようなものはゲームの一部ではありません)、これまでのところとても良い. アクターが別々のクラスにいるように見えますが、これは良いことです。「MenuScreen」は、単一の責任として理にかなっているようなものです。

于 2014-07-03T07:07:18.123 に答える
1

私は libgdx を使って作業しており、かなり大きなゲームをグループで取り組んでいます。私が特に便利だと思ったのは、レンダリングされたコンポーネントを分割することです。たとえば、静的要素用に別の UI クラスがあります。次に、すべての動的コンポーネントを保持する「レンダリング シーン」クラスを作成します。

これは、メニューではあまり一般的ではありません。ここではやり過ぎなので具体的な例を挙げるのは難しいですが、ゲームのコア機能を実際に作り始めるときの良い習慣です。

(入力マルチプレクサは調べるのに良いものです)。

ただし、今すぐコードを調整するだけなので、メニュー用に別の「レンダリング」クラスを作成してみませんか? ステージで初期化し、俳優を追加するだけです。これにより、クラスを肥大化させることなくアニメーション シーケンスを追加できますMenuScreen。あなたがしなければならないことは、それがうまくいくようにすることMenuUI.render();です。UIScreenAdapterをハードコーディングするのではなく、ゲーム画面のトランジションに使用する必要があります。次に、メニュー画面の UI を変更したい場合は、新しい UI モジュールをプラグインするだけです。

これが役立つことを願っています:d

于 2014-07-03T07:44:36.130 に答える