I'm working on a simple block-based puzzle game.
The game play consists pretty much of moving blocks around in the game area, so it's a trivial physics simulation. My implementation, however, is in my opinion far from ideal and I'm wondering if you can give me any pointers on how to do it better.
I've split the code up into two areas: Game logic and UI, as I did with a lot of puzzle games:
- The game logic is responsible for the general rules of the game (e.g. the formal rule system in chess)
- The UI displays the game area and pieces (e.g. chess board and pieces) and is responsible for animations (e.g. animated movement of chess pieces)
The game logic represents the game state as a logical grid, where each unit is one cell's width/height on the grid. So for a grid of width 6, you can move a block of width 2 four times until it collides with the boundary.
The UI takes this grid, and draws it by converting logical sizes into pixel sizes (that is, multiplies it by a constant). However, since the game has hardly any game logic, my game logic layer  doesn't have much to do except collision detection. Here's how it works:
- Player starts to drag a piece
- UI asks game logic for the legal movement area of that piece and lets the player drag it within that area
- Player lets go of a piece
- UI snaps the piece to the grid (so that it is at a valid logical position)
- UI tells game logic the new logical position (via mutator methods, which I'd rather avoid)
I'm not quite happy with that:
- I'm writing unit tests for my game logic layer, but not the UI, and it turned out all the tricky code is in the UI: Stopping the piece from colliding with others or the boundary and snapping it to the grid.
- I don't like the fact that the UI tells the game logic about the new state, I would rather have it call a
movePieceLeft()method or something like that, as in my other games, but I didn't get far with that approach, because the game logic knows nothing about the dragging and snapping that's possible in the UI.
I think the best thing to do would be to get rid of my game logic layer and implement a physics layer instead. I've got a few questions regarding that:
- Is such a physics layer common, or is it more typical to have the game logic layer do this?
- Would the snapping to grid and piece dragging code belong to the UI or the physics layer?
- Would such a physics layer typically work with pixel sizes or with some kind of logical unit, like my game logic layer?
- I've seen event-based collision detection in a game's code base once, that is, the player would just drag the piece, the UI would render that obediently and notify the physics system, and the physics system would call a onCollision() method on the piece once a collision is detected. What is more common? This approach or asking for the legal movement area first?
 layer is probably not the right word for what I mean, but subsystem sounds overblown and class is misguiding, because each layer can consist of several classes.