Skip to content

Commit

Permalink
Merge branch 'main' into bugfix-2633
Browse files Browse the repository at this point in the history
  • Loading branch information
spydon authored Aug 7, 2023
2 parents b34d077 + f3de665 commit 61ed09c
Show file tree
Hide file tree
Showing 16 changed files with 112 additions and 24 deletions.
16 changes: 8 additions & 8 deletions examples/lib/stories/input/joystick_advanced_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class JoystickAdvancedExample extends FlameGame with HasCollisionDetection {
columns: 6,
rows: 1,
);
add(ScreenHitbox());
world.add(ScreenHitbox()..anchor = cameraComponent.viewfinder.anchor);
joystick = JoystickComponent(
knob: SpriteComponent(
sprite: sheet.getSpriteById(1),
Expand Down Expand Up @@ -184,13 +184,13 @@ class JoystickAdvancedExample extends FlameGame with HasCollisionDetection {
),
)..add(directionText);

add(player);
add(joystick);
add(flipButton);
add(flopButton);
add(buttonComponent);
add(spriteButtonComponent);
add(shapeButton);
world.add(player);
cameraComponent.viewport.add(joystick);
cameraComponent.viewport.add(flipButton);
cameraComponent.viewport.add(flopButton);
cameraComponent.viewport.add(buttonComponent);
cameraComponent.viewport.add(spriteButtonComponent);
cameraComponent.viewport.add(shapeButton);
cameraComponent.viewport.add(speedWithMargin);
cameraComponent.viewport.add(directionWithMargin);
}
Expand Down
1 change: 0 additions & 1 deletion examples/lib/stories/input/joystick_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class JoystickPlayer extends SpriteComponent
@override
Future<void> onLoad() async {
sprite = await gameRef.loadSprite('layers/player.png');
position = gameRef.size / 2;
add(RectangleHitbox());
}

Expand Down
4 changes: 2 additions & 2 deletions packages/flame/lib/src/camera/camera_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,17 +168,17 @@ class CameraComponent extends Component {
point.x - viewport.position.x + viewport.anchor.x * viewport.size.x,
point.y - viewport.position.y + viewport.anchor.y * viewport.size.y,
);
yield* viewport.componentsAtPoint(viewportPoint, nestedPoints);
if ((world?.isMounted ?? false) &&
currentCameras.length < maxCamerasDepth) {
if (viewport.containsLocalPoint(viewportPoint)) {
currentCameras.add(this);
final worldPoint = viewfinder.transform.globalToLocal(viewportPoint);
yield* world!.componentsAtPoint(worldPoint, nestedPoints);
yield* viewfinder.componentsAtPoint(worldPoint, nestedPoints);
yield* world!.componentsAtPoint(worldPoint, nestedPoints);
currentCameras.removeLast();
}
}
yield* viewport.componentsAtPoint(viewportPoint, nestedPoints);
}

/// A camera that currently performs rendering.
Expand Down
6 changes: 3 additions & 3 deletions packages/flame/test/camera/camera_component_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,15 @@ void main() {
final nested = <Vector2>[];
final it = game.componentsAtPoint(Vector2(400, 300), nested).iterator;
expect(it.moveNext(), true);
expect(it.current, camera.viewport);
expect(nested, [Vector2(400, 300), Vector2(300, 200)]);
expect(it.moveNext(), true);
expect(it.current, component);
expect(nested, [Vector2(400, 300), Vector2(100, 50), Vector2(50, 20)]);
expect(it.moveNext(), true);
expect(it.current, world);
expect(nested, [Vector2(400, 300), Vector2(100, 50)]);
expect(it.moveNext(), true);
expect(it.current, camera.viewport);
expect(nested, [Vector2(400, 300), Vector2(300, 200)]);
expect(it.moveNext(), true);
expect(it.current, game);
expect(nested, [Vector2(400, 300)]);
expect(it.moveNext(), false);
Expand Down
10 changes: 8 additions & 2 deletions packages/flame/test/camera/viewports/circular_viewport_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,18 @@ void main() {

testWithFlameGame('hit testing', (game) async {
final world = _MyWorld();
final viewport = CircularViewport.ellipse(80, 20)
..position = Vector2(20, 30);
final camera = CameraComponent(
world: world,
viewport: CircularViewport.ellipse(80, 20)..position = Vector2(20, 30),
viewport: viewport,
);
game.addAll([world, camera]);
await game.ready();

bool hit(double x, double y) {
return game.componentsAtPoint(Vector2(x, y)).first == world;
final components = game.componentsAtPoint(Vector2(x, y)).toList();
return components.first == viewport && components[1] == world;
}

expect(hit(10, 20), false);
Expand All @@ -118,6 +121,9 @@ void main() {
final nestedPoints = <Vector2>[];
final center = Vector2(100, 50);
for (final component in game.componentsAtPoint(center, nestedPoints)) {
if (component == viewport) {
continue;
}
expect(component, world);
expect(nestedPoints.last, Vector2.zero());
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,20 @@ void main() {

testWithFlameGame('hit testing', (game) async {
final world = World();
final viewport = FixedAspectRatioViewport(aspectRatio: 1);
final camera = CameraComponent(
world: world,
viewport: FixedAspectRatioViewport(aspectRatio: 1),
viewport: viewport,
);
game.addAll([world, camera]);
game.onGameResize(Vector2(100, 200));
await game.ready();

bool hit(double x, double y) {
final components = game.componentsAtPoint(Vector2(x, y)).toList();
return components.isNotEmpty && components.first == world;
return components.isNotEmpty &&
components.first == viewport &&
components[1] == world;
}

for (final x in [0.0, 5.0, 50.0, 100.0]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,57 @@ void main() {
expect(game.tapCancelEvent, equals(0));
},
);

testWithFlameGame(
'viewport components should get events before world',
(game) async {
final component = _TapCallbacksComponent()
..x = 10
..y = 10
..width = 10
..height = 10;
final hudComponent = _TapCallbacksComponent()
..x = 10
..y = 10
..width = 10
..height = 10;
final world = World();
final cameraComponent = CameraComponent(world: world)
..viewfinder.anchor = Anchor.topLeft;

await game.ensureAddAll([world, cameraComponent]);
await world.ensureAdd(component);
await cameraComponent.viewport.ensureAdd(hudComponent);
final dispatcher = game.firstChild<MultiTapDispatcher>()!;

dispatcher.onTapDown(
createTapDownEvents(
game: game,
localPosition: const Offset(12, 12),
globalPosition: const Offset(12, 12),
),
);

expect(hudComponent.tapDownEvent, equals(1));
expect(hudComponent.tapUpEvent, equals(0));
expect(hudComponent.tapCancelEvent, equals(0));

expect(component.tapDownEvent, equals(0));
expect(component.tapUpEvent, equals(0));
expect(component.tapCancelEvent, equals(0));

dispatcher.onTapUp(
createTapUpEvents(
game: game,
localPosition: const Offset(12, 12),
globalPosition: const Offset(12, 12),
),
);

expect(hudComponent.tapUpEvent, equals(1));
expect(component.tapUpEvent, equals(0));
},
);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class GroupLayer extends RenderableLayer<Group> {
required super.parent,
required super.map,
required super.destTileSize,
super.filterQuality,
});

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class FlameImageLayer extends RenderableLayer<ImageLayer> {
required super.map,
required super.destTileSize,
required Image image,
super.filterQuality,
}) : _image = image {
_initImageRepeat();
}
Expand All @@ -46,6 +47,7 @@ class FlameImageLayer extends RenderableLayer<ImageLayer> {
opacity: opacity,
alignment: Alignment.topLeft,
repeat: _repeat,
filterQuality: filterQuality,
);

canvas.restore();
Expand All @@ -69,13 +71,15 @@ class FlameImageLayer extends RenderableLayer<ImageLayer> {
required CameraComponent? camera,
required TiledMap map,
required Vector2 destTileSize,
FilterQuality? filterQuality,
Images? images,
}) async {
return FlameImageLayer(
layer: layer,
parent: parent,
map: map,
destTileSize: destTileSize,
filterQuality: filterQuality,
image: await (images ?? Flame.images).load(layer.image.source!),
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:ui';

import 'package:flame/components.dart';
import 'package:flame/extensions.dart';
import 'package:flame_tiled/src/renderable_layers/renderable_layer.dart';
Expand All @@ -11,6 +13,7 @@ class ObjectLayer extends RenderableLayer<ObjectGroup> {
required super.parent,
required super.map,
required super.destTileSize,
super.filterQuality,
});

@override
Expand All @@ -26,12 +29,14 @@ class ObjectLayer extends RenderableLayer<ObjectGroup> {
ObjectGroup layer,
TiledMap map,
Vector2 destTileSize,
FilterQuality? filterQuality,
) async {
return ObjectLayer(
layer: layer,
parent: null,
map: map,
destTileSize: destTileSize,
filterQuality: filterQuality,
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:ui';

import 'package:flame/cache.dart';
import 'package:flame/components.dart';
import 'package:flame/extensions.dart';
Expand All @@ -19,12 +21,16 @@ abstract class RenderableLayer<T extends Layer> {
/// The parent [Group] layer (if it exists)
final GroupLayer? parent;

/// The [FilterQuality] that should be used by all the layers.
final FilterQuality filterQuality;

RenderableLayer({
required this.layer,
required this.parent,
required this.map,
required this.destTileSize,
});
FilterQuality? filterQuality,
}) : filterQuality = filterQuality ?? FilterQuality.none;

/// [load] is a factory method to create [RenderableLayer] by type of [layer].
static Future<RenderableLayer> load({
Expand All @@ -35,6 +41,7 @@ abstract class RenderableLayer<T extends Layer> {
required CameraComponent? camera,
required Map<Tile, TileFrames> animationFrames,
required TiledAtlas atlas,
FilterQuality? filterQuality,
bool? ignoreFlip,
Images? images,
}) async {
Expand All @@ -47,6 +54,7 @@ abstract class RenderableLayer<T extends Layer> {
animationFrames: animationFrames,
atlas: atlas.clone(),
ignoreFlip: ignoreFlip,
filterQuality: filterQuality,
);
} else if (layer is ImageLayer) {
return FlameImageLayer.load(
Expand All @@ -55,13 +63,15 @@ abstract class RenderableLayer<T extends Layer> {
camera: camera,
map: map,
destTileSize: destTileSize,
filterQuality: filterQuality,
images: images,
);
} else if (layer is ObjectGroup) {
return ObjectLayer.load(
layer,
map,
destTileSize,
filterQuality,
);
} else if (layer is Group) {
final groupLayer = layer;
Expand All @@ -70,6 +80,7 @@ abstract class RenderableLayer<T extends Layer> {
parent: parent,
map: map,
destTileSize: destTileSize,
filterQuality: filterQuality,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class HexagonalTileLayer extends FlameTileLayer {
required super.tiledAtlas,
required super.animationFrames,
required super.ignoreFlip,
super.filterQuality,
});

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class IsometricTileLayer extends FlameTileLayer {
required super.tiledAtlas,
required super.animationFrames,
required super.ignoreFlip,
super.filterQuality,
});

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class OrthogonalTileLayer extends FlameTileLayer {
required super.tiledAtlas,
required super.animationFrames,
required super.ignoreFlip,
super.filterQuality,
});

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class StaggeredTileLayer extends FlameTileLayer {
required super.tiledAtlas,
required super.animationFrames,
required super.ignoreFlip,
super.filterQuality,
});

@override
Expand Down
Loading

0 comments on commit 61ed09c

Please sign in to comment.