Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
refactor: made MapBounds immutable (#144)
Browse files Browse the repository at this point in the history
* refactor: made MapBounds immutable

* refactor: remove redundant import
  • Loading branch information
alestiago authored Feb 17, 2024
1 parent fc14e1c commit a417e99
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 30 deletions.
12 changes: 2 additions & 10 deletions packages/trashy_road/lib/game_settings.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import 'dart:typed_data';

import 'package:flame/components.dart';
import 'package:trashy_road/src/game/game.dart';

abstract class GameSettings {
/// The dimensions of the grid.
static final Vector2 gridDimensions = _ImmutableVector2(128, 64);
static final Vector2 gridDimensions = UnmodifiableVector2View(128, 64);
}

extension TrashyRoadVector on Vector2 {
Expand All @@ -25,10 +24,3 @@ extension TrashyRoadVector on Vector2 {
/// Modifications are made to the object.
void toGameSize() => multiply(GameSettings.gridDimensions);
}

class _ImmutableVector2 extends Vector2 {
_ImmutableVector2(double x, double y)
: super.fromFloat64List(
UnmodifiableFloat64ListView(Float64List.fromList([x, y])),
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:flame/components.dart';
import 'package:flame_tiled/flame_tiled.dart';
import 'package:trashy_road/src/game/entities/barrel/barrel.dart';
import 'package:trashy_road/src/game/game.dart';
import 'package:trashy_road/src/game/model/map_bounds.dart';

/// The different layers in the Tiled map.
enum _TiledLayer {
Expand Down Expand Up @@ -47,7 +46,12 @@ class TrashyRoadWorld extends Component {
tiled.tileMap.getObjectGroup(_TiledLayer.borderLayer.name);
tiled.addAll(borderLayer.objects.map(MapEdge.fromTiledObject));

bounds = MapBounds(tiled.topLeftPosition, tiled.bottomLeftPosition);
bounds = MapBounds.fromLTWH(
tiled.topLeftPosition.x,
tiled.topLeftPosition.y,
tiled.width,
tiled.height,
);
}

final TiledComponent tiled;
Expand All @@ -74,7 +78,3 @@ extension on RenderableTiledMap {
return objectGroup;
}
}

extension on TiledComponent {
Vector2 get bottomLeftPosition => topLeftPosition + Vector2(width, height);
}
1 change: 1 addition & 0 deletions packages/trashy_road/lib/src/game/game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export 'bloc/game_bloc.dart';
export 'components/components.dart';
export 'debug_game.dart';
export 'entities/entities.dart';
export 'models/models.dart';
export 'view/view.dart';
export 'widgets/widgets.dart';

Expand Down
14 changes: 0 additions & 14 deletions packages/trashy_road/lib/src/game/model/map_bounds.dart

This file was deleted.

26 changes: 26 additions & 0 deletions packages/trashy_road/lib/src/game/models/map_bounds.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:flame/components.dart';
import 'package:meta/meta.dart';
import 'package:trashy_road/src/game/models/models.dart';

@immutable
class MapBounds {
const MapBounds._(this.topLeft, this.bottomRight);

/// Creates a [MapBounds] from the left, top, width and height.
///
/// Similar to `Rect.fromLTWH` from `dart:ui`.
factory MapBounds.fromLTWH(double x, double y, double width, double height) {
final topLeft = UnmodifiableVector2View(x, y);
final bottomRight = UnmodifiableVector2View(x + width, y + height);
return MapBounds._(topLeft, bottomRight);
}

final UnmodifiableVector2View topLeft;
final UnmodifiableVector2View bottomRight;

bool isPointInside(Vector2 position) =>
position.x >= topLeft.x &&
position.x <= bottomRight.x &&
position.y <= bottomRight.y &&
position.y >= topLeft.y;
}
2 changes: 2 additions & 0 deletions packages/trashy_road/lib/src/game/models/models.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export 'map_bounds.dart';
export 'unmodifiable_vector2.dart';
10 changes: 10 additions & 0 deletions packages/trashy_road/lib/src/game/models/unmodifiable_vector2.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'dart:typed_data';

import 'package:flame/components.dart' show Vector2;

class UnmodifiableVector2View extends Vector2 {
UnmodifiableVector2View(double x, double y)
: super.fromFloat64List(
UnmodifiableFloat64ListView(Float64List.fromList([x, y])),
);
}

0 comments on commit a417e99

Please sign in to comment.