Implementing the piece spawning cycle
Following the previous architecture design session, I started implementing the basic structure in the game.
There were some changes needed, but overall the structure seems to work fairly well, and I could easily set up a 2-player scene, where the correct input events are routed correctly to the right PlayerController
and new pieces are spawned in the corresponding MatchSliceController
:
Note: I renamed the Game...
classes to Match...
classes to avoid potential confusion with gamepad controlling code.
The problem
If you look closely, you might see an issue with the pile of pieces at the bottom:
I haven't seen this explicitly mentioned in the Apple docs, but it sure looks as if the physics engine is approximating our shape as a convex polygon, instead of a concave polygon.
This means that I'll need to create these concave nodes as multiple-joined-squares. I actually started going down this route previously, but backtracked, because it seemed to me that it might be too much hassle than it's worth.
People who know more about how game physics engines work probably would've groaned hard when I decided to abandon that path and create the collision shape as a concave CGPath.
Implementing the 2-player layout
The structure I ended up with was:
- SKScene propogates input event and input source to the network layer
- the network layer looks to see if there are any local players mapped to the given input source, and if so - the input event is sent to the corresponding
PlayerController
. If not, it is discarded - in a networked game (not implemented yet), this is the point where the input would also be broadcast to remote players
- the
PlayerController
checks if there is an active (i.e. falling) node in itsMatchSliceController
. If so, it applies movement
And for the collision:
- the
MatchController
is the physics contact delegate for the entire scene - if any node collides, we check if any of the
MatchSliceController
instances have it as an active node - if so - the
MatchSliceController
removes the piece as an active piece, allows it to be rotated, and spawns a new active piece
Screenshots
Here's the new 2 player layout in action:
And here's what happens if you let the pieces touch the spawn point: