Control Sources


This is part of a series on how this game was implemented technically, as part of the Game Jam it was developed for. It is recommended to read them roughly in-order as later blog entries may refer to earlier ones.

Technical:

  1. State Machine
  2. Pathfinding/Movement
  3. Isometric Rendering
  4. API
  5. Control Sources
  6. AI
  7. Networking
  8. Fog of War
  9. Bit Packing
  10. Palette Shifting

Other:

----

Control Sources

In the previous blog post, we specified how the user can interact with the game through the UI layer. But that raises a question, How do we  implement AI or networking?

Control Sources!

The control source is just a name I invented for putting a switch at the top-most layer and checking who is actually in control of the game.

Each player in a game is given one of 3 possible control sources:

  • AI
  • Network
  • Local

If you click a button, nothing happens unless the control source = local. 

For menus, the control source is always "local", but when you're actually playing the game, the control source can be set to be different for each player. (The player, and their opponent)

If you want local co-op or couch pvp, you can set both players to control source = local. This was used for testing before implementing other game modes.

For single-player, we want AI,  Setting the enemy to control source = AI, the update() loop will call an AI() code and it'll work on whatever the current player is. The AI logic isn't specifically tied to a particular player. It just works on the current player. You can in theory have AI vs AI auto-battles.

Finally "Network". If a  network packet arrives, it's stored, then in the update() loop, it's read back. This gives PvP over the network with just a handful of functions. After the initial sync, it can be done in just by decoding the packet and calling the relevant API layer

Here is the complete network code for handling a packet once the game is running. It's 4 lines of decoding the packet, and 3 lines calling the relevant API. The API layer doesn't care if the player is a person with a mouse, or an AI or a network packet, so the game "just works"

    const [idlex,idley] = Bit.GET_XY(data.idle_select);
    const [movx,movy] = Bit.GET_XY(data.move_destination);
    const [tgtx,tgty] = Bit.GET_XY(data.target_select);
    const preferredPath = data.preferred_path;
    await Sy_api.api_idle_selectCharacter(dataidlex,idley);
    await Sy_api.api_mov_selectDestination(movx,movy,preferredPath);
    await Sy_api.api_tgt_selectTarget(tgtx,tgty,preferredPath);

Leave a comment

Log in with itch.io to leave a comment.