Skip to content

Commit

Permalink
refactor: Player tuple to object and enlargen lobby rectangle (#88)
Browse files Browse the repository at this point in the history
* feat: Player id

* Refactor player tuple to object and enlargen lobby rectangle

* Fix missing buttons

* dispose last

* super.dispose last

---------

Co-authored-by: Erick Zanardo <[email protected]>
  • Loading branch information
spydon and erickzanardo authored Jul 3, 2023
1 parent dbc0d67 commit 83c0e2a
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 72 deletions.
2 changes: 1 addition & 1 deletion lib/game/components/score_panel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ScorePanel extends RectangleComponent
game.ships.values.map(
(ship) => ScoreBox(
shipRef: ship,
position: _computeTarget(ship.playerNumber),
position: _computeTarget(ship.player.slotNumber),
),
),
);
Expand Down
25 changes: 8 additions & 17 deletions lib/game/components/ship.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import 'package:flutter/services.dart';
import 'package:gamepads/gamepads.dart';
import 'package:lightrunners/game/game.dart';
import 'package:lightrunners/game/player.dart';
import 'package:lightrunners/ui/ui.dart';
import 'package:lightrunners/utils/gamepad_map.dart';
import 'package:lightrunners/utils/input_handler_utils.dart';

Expand Down Expand Up @@ -47,9 +46,6 @@ const playerKeyboardControlsMapping = [
),
];

final _shipColors =
GamePalette.shipValues.map((color) => Paint()..color = color).toList();

final shipSprites = [
'netunos_wrath.png',
'purple_haze.png',
Expand Down Expand Up @@ -83,19 +79,14 @@ class Ship extends SpriteComponent
CollisionCallbacks {
static final _random = Random();

Ship(this.playerNumber, this.gamepadId)
: player = Player(
color: _shipColors[playerNumber].color,
),
moveJoystick = _makeJoystick(gamepadId, leftXAxis, leftYAxis),
Ship(this.player) :
moveJoystick = _makeJoystick(player.gamepadId, leftXAxis, leftYAxis),
super(size: Vector2(40, 80), anchor: Anchor.center) {
paint = _shipColors[playerNumber];
spritePath = shipSprites[playerNumber];
paint = Paint()..color = player.color;
spritePath = shipSprites[player.slotNumber];
}

final Player player;
final int playerNumber;
final String? gamepadId; // null means keyboard
int score = 0;
late final String spritePath;

Expand Down Expand Up @@ -140,7 +131,7 @@ class Ship extends SpriteComponent

game.world.add(
Bullet(
ownerPlayerNumber: playerNumber,
ownerPlayerNumber: player.slotNumber,
position: positionOfAnchor(Anchor.topCenter),
velocity: bulletVector * bulletSpeed,
color: paint.color,
Expand All @@ -153,7 +144,7 @@ class Ship extends SpriteComponent
RawKeyEvent event,
Set<LogicalKeyboardKey> keysPressed,
) {
final keyboardControl = playerKeyboardControlsMapping[playerNumber];
final keyboardControl = playerKeyboardControlsMapping[player.slotNumber];

if (event.isKeyPressed(keyboardControl.shoot)) {
fire();
Expand Down Expand Up @@ -259,12 +250,12 @@ class Ship extends SpriteComponent
) {
super.onCollisionStart(intersectionPoints, other);
if (other is Bullet) {
if (other.ownerPlayerNumber == playerNumber) {
if (other.ownerPlayerNumber == player.slotNumber) {
return;
}
velocity.add(other.velocity..scale(0.2));
other.removeFromParent();
} else if (other is Ship && playerNumber < other.playerNumber) {
} else if (other is Ship && player.slotNumber < other.player.slotNumber) {
_nextVelocity.setFrom(other.velocity.scaled(other.weightFactor));
other.velocity.setFrom(velocity.scaled(weightFactor));
velocity.setFrom(_nextVelocity);
Expand Down
15 changes: 2 additions & 13 deletions lib/game/lightrunners_game.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:async';
import 'dart:math';
import 'dart:ui';

import 'package:flame/components.dart';
Expand All @@ -18,7 +17,7 @@ import 'package:lightrunners/utils/utils.dart';

class LightRunnersGame extends FlameGame
with HasKeyboardHandlerComponents, HasCollisionDetection {
final List<String?> players;
final List<Player> players;
late final Rect playArea;
late final CameraComponent cameraComponent;
late final World world;
Expand Down Expand Up @@ -78,19 +77,9 @@ class LightRunnersGame extends FlameGame
void _createShips() {
ships = {
for (var i = 0; i < players.length; i++)
players[i] ?? 'keyboard_$i': Ship(i, players[i]),
players[i].gamepadId ?? 'keyboard_$i': Ship(players[i]),
};

if (ships.isEmpty) {
print('No controllers found, using keyboard only');
ships[''] = Ship(0, null);
}

// TODO(any): correctly configure player numbers from the app
for (final ship in ships.values) {
ship.player.playerId = Random().nextInt(999);
}

_subscription = Gamepads.events.listen((event) {
ships[event.gamepadId]?.onGamepadEvent(event);
});
Expand Down
31 changes: 27 additions & 4 deletions lib/game/player.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
import 'package:flame/extensions.dart';
import 'dart:ui';

import 'package:lightrunners/ui/palette.dart';

class Player {
int? playerId;
final Color color;
final int slotNumber;
final int? playerId;
final String? gamepadId;
final String? username;

Player({
required this.color,
required this.slotNumber,
this.playerId,
this.gamepadId,
this.username,
});

Color get color => GamePalette.shipValues[slotNumber];

Player copyWith({
int? slotNumber,
int? playerId,
String? gamepadId,
String? username,
}) {
return Player(
slotNumber: slotNumber ?? this.slotNumber,
playerId: playerId ?? this.playerId,
gamepadId: gamepadId ?? this.gamepadId,
username: username ?? this.username,
);
}
}
7 changes: 5 additions & 2 deletions lib/game/view/game_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import 'package:flutter/material.dart';
import 'package:lightrunners/audio/audio.dart';
import 'package:lightrunners/end_game/end_game.dart';
import 'package:lightrunners/game/game.dart';
import 'package:lightrunners/game/player.dart';

class GamePage extends StatefulWidget {
const GamePage({required this.players, super.key});

final List<String?> players;
final List<Player> players;

static Route<void> route({required List<String?> players}) {
static Route<void> route({
required List<Player> players,
}) {
return MaterialPageRoute<void>(
maintainState: false,
builder: (_) => GamePage(players: players),
Expand Down
Loading

0 comments on commit 83c0e2a

Please sign in to comment.