• Advertisement
  • entries
  • comments
  • views

About this blog

Random words and other things

Entries in this blog

Old Code

Last week I rediscovered a 12 year old game project I was working on. I decided to look at it and write a blog about it.

Here's a summary:

Part 1: Archaeology - The rediscovery and an overview of the repository structure
Part 2: First look at the code - I open the door and peek inside, outlining the high level structure or concepts - it's a look back in time to GameDev.net in 2004 (Enginuity era)
Part 3: PhysFS & Entities - I remove the dependency on PhysicsFS to get the game starting. I explore the Entity structure and find dragons.
Part 4: Compile Times - I start cleaning up the code and apply some header discipline to drastically improve compilation times
Part 5 - It lives! - I get the game running and stare into face of a resurrected monster. I find that a lot of the systems and mechanics are no longer present or functional - as a result, the 'game' isn't a game at all anymore and won't be without significant work.
Part 6 - Cleanup - I start the cleanup of dead systems, removing a bunch of obviously unused junk. I extricate the smart pointer system in favour of a cleaner more deterministic ownership model.
Part 7 - You didn't need it - I start to remove everything else that has no material benefit to the current feature set in the game. The goal is to keep only the code that is useful. A *lot* of code is removed.
Part 8 - Journey's End - I remove the final thorn in the code, the entity system, keeping only what is needed. I realise that there's no more to remove.

It's been a fun journey and the current result is arguably the code that should have been written 12 years ago. A great lesson in not over-engineering things for the sake of it and evolving as you go. It's also a view into the past and a realisation that you can learn and change a lot in 12 years.

I'm pondering what to do next. The codebase is still "old", but improving it will result in writing a lot of new code. I may modernise it a bit and then keep going. Not decided yet. I hope you enjoy reading.

Day 2

I started day 2 pleased with that I had a playable game at the end of the first day, but it wasn't finished yet. Cracking on, I set myself some goals for the day. The first three were continuations from the first day.

[indent=1]? Add "game over" detection (no more moves) & message

[indent=1]? Add game reset and "new game" logic

[indent=1]? Add scoring + display of scoring on screen

[indent=1]? Add high score and score load/save

[indent=1]? Add win condition (2048 tile) & message display

[indent=1]? Clean up graphics
[indent=1]? Improve user interface
[indent=1]? Add sounds
[indent=1]? Create Android build (stretch)

Game Over

Implementing this was pretty simple. For the game to be lost, you have to satisfy two conditions.

  1. There are no empty tiles on the board
  2. You cannot squash any tiles in any direction

I added this by keeping track of the number of occupied tiles on the board. Whenever one was squashed, it went down and one spawned, it went up. Simple. To check for "no tiles empty", it was a case of providing a property which took the total tile count and subtracted the occupied count. I added some unit tests around this to make sure the counts were ok. I'm glad I did, as I refactored something later and this broke in a subtle way..

The second check for a game over condition is the CanISquash? condition. I implemented this by duplicating the squash routines and made them return true if they would squash. This was a bit of a hack, as I should really have refactored the squashing to take a flag so that I didn't have the copy paste everywhere. I don't like code duplication, and having my unit tests for squashing should let me refactor this pretty quickly.

With the game over recognised, I needed to add the concept of "game state". I added this to my player component, but I could easily have added it to a new component. I had two states at this point.public enum TileGameState{ Playing, GameOver,}
Adding the game over message was also simple to begin with. I created a GuiTextLabel class and a GuiRenderingService which took this label and printed the contents to the screen. I implemented a GuiService (the name sucks) which kept track of the game state and used it to modify the visibility of the text label. _gameOverLabel.Visible = (_player.State == TileGameState.GameOver);
This would be the start of my "battle" with the UI that took up a bunch of unexpected time.

New Game

I added a new state to my game state machine, "NewGame" which leaves us with:public enum TileGameState{ NewGame, Playing, GameOver,}
I hooked up the Spacebar key to be recognised during the game state and reset us to the new game state.

So the state transition of my game looks like.New Game --> Playing --> Game Over --> New Game
Starting a new game currently involves several things: