Я разрабатываю игру для компании. Я буду развивать эту игру только 2 месяца. Моя компания просит меня сделать мой код чистым и расширяемым, чтобы они могли нанять другого программиста, когда им нужно добавить дополнительные функции. Я прочитал «Чистый код» дяди Боба. Мне было очень сложно воплотить концепцию на практике. Это один из моего классаДизайн классов в программировании игр
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();
}
}
Мой вопрос делает этот класс нарушает Single Ответственность Принцип? Нужно ли разделять каждого актера на свой класс? И если да, то как мне реорганизовать мой код?
Есть много рамок игры, которые существуют (JGame хорошо для 2d игр). Попытка дизайна для «расширяемости» в первый раз очень тяжелая - боссы просят об этом, но пока вам действительно не нужно расширяться, и вы знаете, каким образом вы, вероятно, совершаете ошибку и тратите свои усилия. Сначала я предлагаю вам взглянуть на существующие рамки. – Fuhrmanator