Two layouts, and whoever presses first is player one
Open the live demo · Read the source · View on GitHub
PlayerSeats keeps every way of holding a game at one machine, each a
FlameInputBridge with its own bindings and state, and lets the game claim
one as the next player when it sees it pressed. Here the two ways are two
halves of one keyboard. Nobody plays until they press their join key, and
seats go in the order people join, whichever layout is listed first.
Step 1: A table and a state for each layout #
Each layout is a bridge of its own: the same ActionMap and InputState
pair a native flutter3d_game reads, one per player, so a key reaches the
player it is bound for and no other.
/// What a layout presses to join, and to do nothing else yet.
const GameAction join = GameAction('join');
/// One way of holding the game: four keys to walk and one to join, in a
/// table and a state of its own.
FlameInputBridge _layout(
LogicalKeyboardKey up,
LogicalKeyboardKey left,
LogicalKeyboardKey down,
LogicalKeyboardKey right,
LogicalKeyboardKey joins,
) => FlameInputBridge(
actions: ActionMap(
actions: ActionSet.common,
buttons: Bindings()
..bind(InputSource.key(up.keyId), GameAction.moveForward)
..bind(InputSource.key(left.keyId), GameAction.moveLeft)
..bind(InputSource.key(down.keyId), GameAction.moveBack)
..bind(InputSource.key(right.keyId), GameAction.moveRight)
..bind(InputSource.key(joins.keyId), join),
),
inputState: InputState(),
);
/// WASD and Space on the left of the keyboard, the arrows and Slash on the
/// right: two players at one keyboard, neither of them a player yet.
PlayerSeats _twoLayouts() => PlayerSeats(<FlameInputBridge>[
_layout(
LogicalKeyboardKey.keyW,
LogicalKeyboardKey.keyA,
LogicalKeyboardKey.keyS,
LogicalKeyboardKey.keyD,
LogicalKeyboardKey.space,
),
_layout(
LogicalKeyboardKey.arrowUp,
LogicalKeyboardKey.arrowLeft,
LogicalKeyboardKey.arrowDown,
LogicalKeyboardKey.arrowRight,
LogicalKeyboardKey.slash,
),
]);
Step 2: Claim a seat by pressing #
What counts as asking to join is the game's to say. Here it is the join key
going down this step on a layout nobody has claimed. claim makes it the
next player, and seated keeps them in the order they came.
/// Seats every free layout whose join key went down this step, in the order
/// the seats have them, and answers the ones it seated.
List<FlameInputBridge> seatNewcomers(PlayerSeats seats) => <FlameInputBridge>[
for (final FlameInputBridge layout in seats.free.toList())
if (layout.inputState.pressed(join) && seats.claim(layout)) layout,
];
Step 3: Keys from the keyboard, not the focus #
listenToKeyboard feeds every layout from HardwareKeyboard, so a key
reaches the game wherever the focus is, and a key let go while a button or a
menu had the focus is still let go. stepEnds closes every layout's step
after the frame, the free ones as well, so a press made to join is seen once
and not on every frame after.
/// The game: every layout fed from the keyboard itself and closed after each
/// frame, a marker for each, hidden until its layout joins.
final class _Lobby extends FlameGame with HasFlutter3d {
_Lobby(this._eye, this.seats);
final CameraNode _eye;
final PlayerSeats seats;
final List<MeshNode> markers = <MeshNode>[];
late final TextComponent _caption = flameCaption(
'Space joins WASD, / joins the arrows',
);
@override
CameraNode createCamera3d() => _eye;
@override
void onOpen3d() {
final DeviceMesh cube = DeviceMesh.upload(
device,
CuboidShape(size: Vector3.all(0.8)).build(),
);
for (var i = 0; i < seats.candidates.length; i++) {
final MeshNode marker =
MeshNode(
cube,
RenderMaterial(name: 'player', baseColor: LinearColor.white),
name: 'layout $i',
)
..setPosition(-1.5 + 3.0 * i, 0.4, 0.0)
..isVisible = false;
markers.add(marker);
scene.add(marker);
}
addAll(<Component>[
// From `HardwareKeyboard`, wherever the focus is.
seats.listenToKeyboard(),
_Seating(this),
// Every layout's step closed, the free ones too, so a press made to
// join is seen once.
...seats.stepEnds(),
_caption,
]);
}
Step 4: A marker for each player #
A layout that has just joined shows its cube in its seat's colour: amber for player one, blue for player two. From then on it walks with its own four keys. Press Slash before Space and the arrows are player one.
/// A layout that has just joined shows its marker in its seat's colour;
/// every seated layout walks its own.
void seatAndWalk(double dt) {
for (final FlameInputBridge joined in seatNewcomers(seats)) {
final int seat = seats.seated.indexOf(joined);
final MeshNode marker = markers[seats.candidates.indexOf(joined)];
marker
..material.baseColor = _fromSrgb(_seatColours[seat])
..isVisible = true;
}
for (final FlameInputBridge player in seats.seated) {
final Vector2 axis = player.inputState.moveAxis;
final MeshNode marker = markers[seats.candidates.indexOf(player)];
final Vector3 at = marker.readPosition();
marker.setPosition(
(at.x + axis.x * 3.0 * dt).clamp(-4.0, 4.0),
at.y,
(at.z - axis.y * 3.0 * dt).clamp(-4.0, 4.0),
);
}
_caption.text = switch (seats.seated.length) {
0 => 'Space joins WASD, / joins the arrows',
1 => 'player one is in; the other layout can still join',
_ => 'two players, in the order they pressed',
};
}