From b47b815b538d69141238d06c2529b89b70b7d22d Mon Sep 17 00:00:00 2001 From: RuiAlonso Date: Wed, 23 Mar 2022 17:58:22 +0100 Subject: [PATCH] feat: added new Elevation for manage priority of BodyComponents --- lib/game/components/components.dart | 1 + lib/game/components/elevation.dart | 76 +++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 lib/game/components/elevation.dart diff --git a/lib/game/components/components.dart b/lib/game/components/components.dart index a255e652..037ee8a8 100644 --- a/lib/game/components/components.dart +++ b/lib/game/components/components.dart @@ -3,6 +3,7 @@ export 'baseboard.dart'; export 'board.dart'; export 'board_side.dart'; export 'bonus_word.dart'; +export 'elevation.dart'; export 'flipper.dart'; export 'initial_position.dart'; export 'jetpack_ramp.dart'; diff --git a/lib/game/components/elevation.dart b/lib/game/components/elevation.dart new file mode 100644 index 00000000..c38217af --- /dev/null +++ b/lib/game/components/elevation.dart @@ -0,0 +1,76 @@ +import 'package:flame_forge2d/flame_forge2d.dart'; +import 'package:flutter/material.dart'; + +/// {@template elevated} +/// Modifies priority of the [BodyComponent] to specify in which z-index level +/// [BodyComponent] is. +/// {@endtemplate} +mixin Elevated on BodyComponent { + int _elevation = Elevation.board.order; + + /// {@macro elevated} + int get elevation => _elevation; + + set elevation(int value) { + _elevation = value; + if (!isLoaded) { + // TODO(ruimiguel): Use loaded.whenComplete once provided. + mounted.whenComplete(_applyElevation); + } else { + _applyElevation(); + } + } + + void _applyElevation() { + priority = elevation; + reorderChildren(); + } + + void _applyCustomElevation(int customElevation) { + priority = customElevation; + reorderChildren(); + } +} + +/// The [Elevation]s a [BodyComponent] can be in. +/// +/// Each [Elevation] is associated with a different board level from ground, to +/// define several z-index heights. +/// +/// Usually used with [Elevated]. +enum Elevation { + /// The ground level. + board, + + /// Level for Jetpack group elements. + jetpack, + + /// Level for Spaceship group elements. + spaceship, + + /// Level for SpaceshipExitRail. + spaceshipExitRail, +} + +/// {@template elevation_order} +/// Specifies the order of each [Elevation]. +/// +/// Used by [Elevated] to specify what is the priority of [BodyComponent]. +/// {@endtemplate} +@visibleForTesting +extension ElevationOrder on Elevation { + /// {@macro elevation_order} + + int get order { + switch (this) { + case Elevation.board: + return 1; + case Elevation.jetpack: + return 2; + case Elevation.spaceship: + return 3; + case Elevation.spaceshipExitRail: + return 2; + } + } +}