Escape a GameLogic god class.

When programming a simple game as an example. I always tend to get left with a large GameLogic class somewhere which is reponsible for gluing together all of the other portions of the software.

A Common example of the GameLogic class I talk about contains functions such as these.

startNewGame();
showAd();
prepareLevel();
startLevel();
gameComplete();
restartLevel()
gameOver();

This class can become very large.

Is there any advice for how better to structure my future games? Should I be splitting these functions out into dedicated classes somehow as to reduce the complexity of the god class?

1

Personally, I like the idea of stages, scenes and actors a lot, even for small projects. That way, you can always add new classes and just plop them in, and they act as individual objects that can be thought of as such.

By creating classes for all of your rendered items like so:

  • Stage (MainMenu)
    • Scene (MenuNavigator)
      • Actor (MenuItem)
      • Actor (MenuItem)
      • Actor (MenuItem)
      • Actor (MenuItem)
  • Stage (InGame)
    • Scene (Gameplay)
      • Actor (StarUnderlay)
      • Actor (Ship)
      • Actor (Enemy)
    • Scene (ScoreOverlay)
      • Actor (Text)
      • Actor (Text)
      • Actor (Text)
    • Scene (GameOver)
      • Actor (Text)
      • Actor (MenuItem)

You get a world of benefits.

By classing out your entities, you’ll have reusability (Such as MenuItem, Text, and creating enemies that use a common class or anonymous classes). This means that you’ll be rewriting less code and sharing logic across all objects.

By classing out scenes, you can also reuse this code, such as having an InGame Stage that uses a ScoreOverlay, as well as an InReplay Stage that also uses that score display, where the data is filled in by the stage.

By classing out your stages, you can put your core logic there as it is where your user is currently at in the game. A MainMenu stage can swap out for an InGame stage, and just by swapping out the object being rendered, you won’t have to worry about deallocating the right objects as you’ll either have garbage collection or the ability to request child objects be deallocated.

The idea is to reduce the responsibility both now and in the future of all of your classes, and thus you end up with individual units working together to produce a final product. If one thing breaks, you’re more likely to know where and why, and you won’t be digging through a 800 line file, but maybe a 100 line file.

When you go to pull it together, you can think of it as if you’re putting on a play (hence why stages, scenes and actors are common used wordings). You need a monster to appear? Then you add a new actor to portray that monster. When that monster is no longer needed, it’s pulled out of the scene.

Yes, you will likely end up with a good amount of classes, but a lot of people will argue that 100 classes is better than 1,000 lines, because again, you’ll always know what’s what when concepts are separated.

0

Should I be splitting these functions out into dedicated classes somehow as to reduce the complexity of the god class?

Yes, that’s a good idea. A good place to start might be to break up your single monolithic class into smaller classes with related functionality. Managing levels seems to be a common theme, so maybe creating a LevelManager class that contains prepareLevel(), startLevel(), and restartLevel(). Code related to ads could be moved to an AdManager class.

The exact details of how you refactor this will greatly depend on how your game is currently implemented, but I hope this gives you a good idea of how to start.

When the game is simple, such a class should not be that large.

When the game progresses to large, you should split it up in the way that feels the most natural to you. I tend to end up with a simple finite state machine for the application state, then run a kernel (i.e updating prioritized tasks) in each state.

These tasks often live in a ‘scene’ so I can load the MainMenuScene, and then it can load LevelOne. Each scene in turn has the ability to run ‘tasks’. A task is simply something that can be updated, and can send and receive messages to other tasks.

Lastly, game objects that live in the ‘scene’ are both a task and it follows the decorator pattern so that I can add aspects of behavior to one or many of the objects in the scene.

As an example:

FSM
   State (start)
      Task (load assets and saves)
      ...
   State (main menu)
      Task (listen for menu selection)
   State (level1)
      Task & Kernel level1 main 
         GameObject (player)
            Component (camera)
            Component (rigidbody)
            Task (physics)

Maybe this makes sense, maybe not. Tell me in a comment and I can try to make it easier to understand.

Also, this is just one way to go about it – not sure The One True Way exists.

You should try drawing it out on a sheet of paper or in some kind of UML mocking program. It will take a few iterations to sort out what seems to model most accurately which roles should be assigned to which classes.

Given your example though, you have a few things that are completely unrelated in the same class:

startNewGame();
showAd();
prepareLevel();
startLevel();
gameComplete();
restartLevel()
gameOver();

Your main method should really only initialize and call start on a game object.

I think that your prepareLevel method should be in another class(the level class)

I am thinking something like:

Main
--------

public static void main (String[] args){

  new Game().start

}

Game
-------

// Should be the main driving class for menus and such, but shouldn't do any real work
// as that should be in renderers, input processors.Should do all the calls such as:
//   -- Show menu
//   -- Start()

public start(){
  Campaign c = new Campaign();
  gameCore.loadCampaign(c)
}


GameCore
---------
// Game logic not related to rendering.


loadCampaign(Campaign c){
   LevelProcessor.startCampaign(c); // -- maybe this shows your screen and such
}


Campaign
---------
// List of levels, in order


LevelProcessor
-----------

// Makes some calls to load all the assets in your level, shoves it to your game container.

Container
-----------
// Handles the calls and stuff to a renderer which draws your view, handles calls to 
// Input handlers and such.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật