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

Commit

Permalink
chore: remove some jank
Browse files Browse the repository at this point in the history
  • Loading branch information
OlliePugh committed Feb 17, 2024
1 parent 7e5bbc7 commit 52f1a6e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
import 'dart:ui';

import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
import 'package:flame_behaviors/flame_behaviors.dart';
import 'package:tiled/tiled.dart';
import 'package:trashy_road/game_settings.dart';

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

class MapBorder extends Obstacle {
class MapBorder extends PositionedEntity {
MapBorder._({required super.position, required super.size})
: super(
blockOnlyBottomTile: false,
anchor: Anchor.topLeft,
children: [
RectangleComponent(size: Vector2.copy(size)..toGameSize())
..setColor(
const Color.fromARGB(64, 0, 0, 0),
),
],
);
) {
final gameSize = Vector2.copy(size)..toGameSize();
addAll([
PropagatingCollisionBehavior(
RectangleHitbox(
size: gameSize,
),
),
RectangleComponent(size: gameSize)
..setColor(
const Color.fromARGB(64, 0, 0, 0),
),
]);
}

/// Derives a [MapBorder] from a [TiledObject].
factory MapBorder.fromTiledObject(TiledObject tiledObject) {
Expand Down
15 changes: 4 additions & 11 deletions packages/trashy_road/lib/src/game/entities/obstacle/obstacle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,19 @@ class Obstacle extends PositionedEntity {
Obstacle({
required Vector2 super.size,
required Vector2 super.position,
Anchor anchor = Anchor.bottomLeft,
super.children,
Iterable<Behavior>? behaviors,
bool blockOnlyBottomTile = true,
}) : super(
anchor: anchor,
anchor: Anchor.bottomLeft,
priority: position.y.floor(),
behaviors: [
if (behaviors != null) ...behaviors,
PropagatingCollisionBehavior(
RectangleHitbox(
anchor: Anchor.topCenter,
size: (blockOnlyBottomTile ? Vector2.all(0.8) : size)
..toGameSize(),
position: Vector2(
size.x / 2,
blockOnlyBottomTile
? size.y - GameSettings.gridDimensions.y
: 0,
),
size: Vector2.all(0.8)..toGameSize(),
position:
Vector2(size.x / 2, size.y - GameSettings.gridDimensions.y),
),
),
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// FIXME(alestiago): Avoid ignoring deprecated_member_use as soon as:
// https://github.com/flame-engine/flame/pull/3002 is released.
// ignore_for_file: deprecated_member_use

import 'dart:async';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import 'package:trashy_road/src/game/game.dart';

/// Communicates to the [PlayerKeyboardMovingBehavior] that the player
/// has collided with an [Obstacle].
class PlayerObstacleBehavior extends CollisionBehavior<Obstacle, Player> {
class PlayerObstacleBehavior<T extends PositionedEntity>
extends CollisionBehavior<T, Player> {
@override
void onCollisionStart(Set<Vector2> intersectionPoints, Obstacle other) {
void onCollisionStart(Set<Vector2> intersectionPoints, T other) {
super.onCollisionStart(intersectionPoints, other);
parent.findBehavior<PlayerMovingBehavior>().bounceBack();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class Player extends PositionedEntity with ZIndex {
),
PlayerCollectingTrashBehavior(),
PlayerDepositingTrashBehavior(),
PlayerObstacleBehavior(),
PlayerObstacleBehavior<Obstacle>(),
PlayerObstacleBehavior<MapBorder>(),
PausingBehavior<Player>(
selector: (player) =>
player.findBehaviors<PlayerKeyboardMovingBehavior>(),
Expand Down

0 comments on commit 52f1a6e

Please sign in to comment.