Skip to content

Commit

Permalink
fix: Update to latest Flutter and Flame versions
Browse files Browse the repository at this point in the history
  • Loading branch information
luanpotter committed Jul 15, 2024
1 parent 356138d commit 24f51eb
Show file tree
Hide file tree
Showing 17 changed files with 146 additions and 135 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ migrate_working_dir/
*.ipr
*.iws
.idea/
.vscode/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
Expand Down
24 changes: 17 additions & 7 deletions lib/game/components/game_border.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ class GameBorder extends PositionComponent

position = game.playArea.topLeft.toVector2();
size = game.playArea.size.toVector2();
rRect = RRect.fromRectAndRadius(Vector2.zero() & size, _radius);

clipArea = game.playArea.inflate(200.0).toFlameRectangle();
clipAreaCenter = clipArea.center;
final rect = Vector2.zero() & size;
rRect = RRect.fromRectAndRadius(rect, _radius);
clipArea = rect.toFlameRectangle();
}

@override
Expand All @@ -54,9 +54,7 @@ class GameBorder extends PositionComponent

for (final (color, path) in _generateBorderClips(clipArea)) {
canvas.save();
canvas.translateVector(clipAreaCenter);
canvas.clipPath(path);
canvas.translateVector(-clipAreaCenter);
canvas.drawRRect(rRect, paint..color = color);
canvas.restore();
}
Expand Down Expand Up @@ -91,7 +89,11 @@ class GameBorder extends PositionComponent
.toList();
}

Path _generateClipPath(double alpha1, double alpha2, Rectangle clipArea) {
static Path _generateClipPath(
double alpha1,
double alpha2,
Rectangle clipArea,
) {
final cornerAngle = atan(clipArea.height / clipArea.width);
final cornerAngles = [
cornerAngle,
Expand All @@ -116,10 +118,18 @@ class GameBorder extends PositionComponent
final points = angles
.map((e) => Vector2(cos(e), sin(e))..scale(clipLength))
.map((e) => LineSegment(Vector2.zero(), e))
.map((e) => e.translate(clipArea.center))
.map((e) => clipArea.intersections(e).firstOrNull)
.nonNulls
.map((e) => e.toOffset())
.toList();
return Path()..addPolygon([Offset.zero, ...points], true);
final center = clipArea.center.toOffset();
return Path()..addPolygon([center, ...points], true);
}
}

extension on LineSegment {
LineSegment translate(Vector2 delta) {
return LineSegment(from + delta, to + delta);
}
}
5 changes: 3 additions & 2 deletions lib/game/components/score_panel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import 'package:lightrunners/game/components/score_box.dart';
import 'package:lightrunners/game/lightrunners_game.dart';
import 'package:lightrunners/ui/palette.dart';
import 'package:lightrunners/utils/constants.dart';
import 'package:lightrunners/utils/utils.dart';

class ScorePanel extends RectangleComponent
with HasGameReference<LightRunnersGame> {
@override
void onLoad() {
position = Vector2(game.camera.viewport.size.x - scoreBoxWidth, 0);
size = Vector2(scoreBoxWidth, game.camera.viewport.size.y);
position = Vector2(fixedSize.x - scoreBoxWidth, 0) - fixedSize / 2;
size = Vector2(scoreBoxWidth, fixedSize.y);
paint = Paint()..color = GamePalette.black;
addAll(
game.ships.values.map(
Expand Down
2 changes: 1 addition & 1 deletion lib/game/components/ship.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class Ship extends SpriteComponent

@override
bool onKeyEvent(
RawKeyEvent event,
KeyEvent event,
Set<LogicalKeyboardKey> keysPressed,
) {
final keyboardControl = playerKeyboardControlsMapping[player.slotNumber];
Expand Down
19 changes: 14 additions & 5 deletions lib/game/lightrunners_game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:ui';

import 'package:flame/components.dart';
import 'package:flame/extensions.dart';
import 'package:flame/game.dart';
import 'package:flame/input.dart';
import 'package:gamepads/gamepads.dart';
Expand All @@ -24,9 +25,12 @@ class LightRunnersGame extends FlameGame
StreamSubscription<GamepadEvent>? _subscription;
final void Function(Map<Player, int>) onEndGame;

LightRunnersGame({required this.players, required this.onEndGame})
: super(
LightRunnersGame({
required this.players,
required this.onEndGame,
}) : super(
camera: CameraComponent.withFixedResolution(
world: World(),
width: fixedSize.x,
height: fixedSize.y,
),
Expand All @@ -42,9 +46,14 @@ class LightRunnersGame extends FlameGame
fixedSize.x - 2 * screenMargin - scoreBoxWidth - 2 * scoreBoxMargin,
fixedSize.y - 2 * screenMargin,
);
camera.viewport.add(ScorePanel());
world.addAll([Background(), GameBorder()]);
world.addAll([Spotlight(), ...ships.values]);

world.addAll([
Background(),
GameBorder(),
Spotlight(),
...ships.values,
ScorePanel(),
]);

late final CountDown countDown;
add(
Expand Down
5 changes: 3 additions & 2 deletions lib/lobby/view/lobby_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:lightrunners/game/view/game_page.dart';
import 'package:lightrunners/title/view/title_page.dart';
import 'package:lightrunners/ui/palette.dart';
import 'package:lightrunners/utils/gamepad_map.dart';
import 'package:lightrunners/utils/input_handler_utils.dart';
import 'package:lightrunners/widgets/screen_scaffold.dart';

class LobbyPage extends StatefulWidget {
Expand Down Expand Up @@ -73,9 +74,9 @@ class _LobbyPageState extends State<LobbyPage> {
fontWeight: FontWeight.bold,
);

return RawKeyboardListener(
return KeyboardListener(
focusNode: _focusNode,
onKey: (RawKeyEvent event) {
onKeyEvent: (KeyEvent event) {
if (event.isKeyPressed(LogicalKeyboardKey.space)) {
setState(() {
_players.add(Player(slotNumber: _players.length));
Expand Down
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ void main() async {
Scores.init();
runApp(
CRTShader(
enabled: true,
enabled: false,
child: MaterialApp(
debugShowCheckedModeBanner: false,
home: const TitlePage(),
Expand Down
25 changes: 1 addition & 24 deletions lib/utils/flame_utils.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import 'package:flame/experimental.dart';
import 'package:flame/extensions.dart';
import 'package:flame/geometry.dart';

// TODO(luan): optmize, test, and move this to Flame
// TODO(luan): optimize, test, and move this to Flame

const _epsilon = 10e-9;

Expand All @@ -20,25 +19,3 @@ bool isSameVector(Vector2 v1, Vector2 v2, {double epsilon = _epsilon}) {
Vector2 lineMidPoint(LineSegment line) {
return line.from + (line.to - line.from) / 2;
}

extension RectangleExtension on Rectangle {
Rect toRect() => Rect.fromLTWH(left, top, width, height);

List<Vector2> intersections(LineSegment line) {
return edges.expand((e) => e.intersections(line)).toList();
}

List<LineSegment> get edges => [topEdge, rightEdge, bottomEdge, leftEdge];

LineSegment get topEdge => LineSegment(topLeft, topRight);
LineSegment get rightEdge => LineSegment(topRight, bottomRight);
LineSegment get bottomEdge => LineSegment(bottomRight, bottomLeft);
LineSegment get leftEdge => LineSegment(bottomLeft, topLeft);

List<Vector2> get vertices => [topLeft, topRight, bottomRight, bottomLeft];

Vector2 get topLeft => Vector2(left, top);
Vector2 get topRight => Vector2(right, top);
Vector2 get bottomRight => Vector2(right, bottom);
Vector2 get bottomLeft => Vector2(left, bottom);
}
10 changes: 8 additions & 2 deletions lib/utils/input_handler_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ class GamepadJoystick {
}

bool readArrowLikeKeysIntoVector2(
RawKeyEvent event,
KeyEvent event,
Set<LogicalKeyboardKey> keysPressed,
Vector2 vector, {
required LogicalKeyboardKey up,
required LogicalKeyboardKey down,
required LogicalKeyboardKey left,
required LogicalKeyboardKey right,
}) {
final isDown = event is RawKeyDownEvent;
final isDown = event is KeyDownEvent;
if (event.logicalKey == up) {
if (isDown) {
vector.y = -1;
Expand Down Expand Up @@ -79,3 +79,9 @@ bool readArrowLikeKeysIntoVector2(
}
return true;
}

extension IsKeyPressed on KeyEvent {
bool isKeyPressed(LogicalKeyboardKey logicalKey) {
return this is KeyDownEvent && this.logicalKey == logicalKey;
}
}
24 changes: 1 addition & 23 deletions lib/utils/triangle2.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'dart:math';
import 'dart:ui';

import 'package:collection/collection.dart';
Expand Down Expand Up @@ -89,7 +88,7 @@ class Triangle2 {
}

Circle circumcircle() {
return _circleFromPoints(vertices)!;
return Circle.fromPoints(v1, v2, v3)!;
}

Triangle2 translateBy(Vector2 offset) {
Expand All @@ -103,24 +102,3 @@ class Triangle2 {
return 'Triangle2(vertices: $vertices)';
}
}

// TODO(luan): add to Flame: Circle.fromPoints
Circle? _circleFromPoints(List<Vector2> points) {
final p1 = points[0];
final p2 = points[1];
final p3 = points[2];

final offset = pow(p2.x, 2) + pow(p2.y, 2);
final bc = (pow(p1.x, 2) + pow(p1.y, 2) - offset) / 2.0;
final cd = (offset - pow(p3.x, 2) - pow(p3.y, 2)) / 2.0;
final det = (p1.x - p2.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p2.y);
if (det == 0) {
return null;
}

final centerX = (bc * (p2.y - p3.y) - cd * (p1.y - p2.y)) / det;
final centerY = (cd * (p1.x - p2.x) - bc * (p2.x - p3.x)) / det;
final radius = sqrt(pow(p2.x - centerX, 2) + pow(p2.y - centerY, 2));

return Circle(Vector2(centerX, centerY), radius);
}
4 changes: 0 additions & 4 deletions lib/utils/utils.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import 'dart:math';

import 'package:flame/extensions.dart';

const tau = 2 * pi;

void moveTowards(Vector2 pos, Vector2 target, double ds) {
final diff = target - pos;
if (diff.length < ds) {
Expand Down
8 changes: 4 additions & 4 deletions macos/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ PODS:
- audioplayers_darwin (0.0.1):
- FlutterMacOS
- FlutterMacOS (1.0.0)
- gamepads_darwin (0.0.1):
- gamepads_darwin (0.1.1):
- FlutterMacOS
- path_provider_foundation (0.0.1):
- Flutter
Expand All @@ -27,9 +27,9 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS:
audioplayers_darwin: dcad41de4fbd0099cb3749f7ab3b0cb8f70b810c
FlutterMacOS: 8f6f14fa908a6fb3fba0cd85dbd81ec4b251fb24
gamepads_darwin: a41ed976610d94faace1821c306a173e3ac55c3a
path_provider_foundation: c68054786f1b4f3343858c1e1d0caaded73f0be9
gamepads_darwin: 07af6c60c282902b66574c800e20b2b26e68fda8
path_provider_foundation: 29f094ae23ebbca9d3d0cec13889cd9060c0e943

PODFILE CHECKSUM: 236401fc2c932af29a9fcf0e97baeeb2d750d367

COCOAPODS: 1.12.1
COCOAPODS: 1.15.0
2 changes: 1 addition & 1 deletion macos/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0920;
LastUpgradeCheck = 1300;
LastUpgradeCheck = 1510;
ORGANIZATIONNAME = "";
TargetAttributes = {
331C80D4294CF70F00263BE5 = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1300"
LastUpgradeVersion = "1510"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 1 addition & 1 deletion macos/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Cocoa
import FlutterMacOS

@NSApplicationMain
@main
class AppDelegate: FlutterAppDelegate {
override func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return true
Expand Down
Loading

0 comments on commit 24f51eb

Please sign in to comment.