Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Fix examples for v1.9.0 #2757

Merged
merged 3 commits into from Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/lib/stories/animations/benchmark_example.dart
Expand Up @@ -21,16 +21,16 @@ starts to drop in FPS, this is without any sprite batching and such.
Future<void> onLoad() async {
await camera.viewport.addAll([
FpsTextComponent(
position: size - Vector2(0, 50),
position: size - Vector2(10, 50),
anchor: Anchor.bottomRight,
),
emberCounter = TextComponent(
position: size - Vector2(0, 25),
position: size - Vector2(10, 25),
anchor: Anchor.bottomRight,
priority: 1,
),
]);
world.add(Ember(size: emberSize, position: size / 2));
world.add(Ember(size: emberSize));
children.register<Ember>();
}

Expand Down
Expand Up @@ -8,6 +8,8 @@
import 'package:examples/stories/camera_and_viewport/zoom_example.dart';
import 'package:flame/game.dart';

import 'package:examples/stories/camera_and_viewport/camera_follow_and_world_bounds.dart';

Check notice on line 11 in examples/lib/stories/camera_and_viewport/camera_and_viewport.dart

View workflow job for this annotation

GitHub Actions / analyze

Sort directive sections alphabetically.

Try sorting the directives. See https://dart.dev/lints/directives_ordering to learn more about this problem.

void addCameraAndViewportStories(Dashbook dashbook) {
dashbook.storiesOf('Camera & Viewport')
..add(
Expand Down Expand Up @@ -74,5 +76,12 @@
'camera_and_viewport/camera_component_properties_example.dart',
),
info: CameraComponentPropertiesExample.description,
)
..add(
'Follow and World bounds',
(_) => GameWidget(game: CameraFollowAndWorldBoundsExample()),
codeLink:
baseLink('camera_and_viewport/camera_follow_and_world_bounds.dart'),
info: CameraFollowAndWorldBoundsExample.description,
);
}
Expand Up @@ -14,22 +14,18 @@ class FixedResolutionExample extends FlameGame
Resize the window or change device orientation to see the difference.
''';

final Vector2 viewportResolution;

FixedResolutionExample({
required this.viewportResolution,
});
required Vector2 viewportResolution,
}) : super(
camera: CameraComponent.withFixedResolution(
width: viewportResolution.x,
height: viewportResolution.y,
),
);

@override
Future<void> onLoad() async {
final flameSprite = await loadSprite('layers/player.png');
final world = World();
final cameraComponent = CameraComponent.withFixedResolution(
width: viewportResolution.x,
height: viewportResolution.y,
world: world,
);
addAll([world, cameraComponent]);

world.add(Background());
world.add(
Expand Down
Expand Up @@ -23,20 +23,19 @@ class FollowComponentExample extends FlameGame
respects the camera transformation.
''';

FollowComponentExample({required this.viewportResolution});
FollowComponentExample({required this.viewportResolution})
: super(
camera: CameraComponent.withFixedResolution(
width: viewportResolution.x,
height: viewportResolution.y,
),
);

late MovableEmber ember;
final Vector2 viewportResolution;

@override
Future<void> onLoad() async {
final world = World();
camera = CameraComponent.withFixedResolution(
width: viewportResolution.x,
height: viewportResolution.y,
world: world,
);

world.add(Map());
world.add(ember = MovableEmber());
camera.setBounds(Map.bounds);
Expand All @@ -58,6 +57,8 @@ class MovableEmber extends Ember<FollowComponentExample>
final Vector2 velocity = Vector2.zero();
late final TextComponent positionText;
late final Vector2 textPosition;
late final maxPosition = Vector2.all(Map.size - size.x / 2);
late final minPosition = -maxPosition;

MovableEmber() : super(priority: 2);

Expand All @@ -78,6 +79,7 @@ class MovableEmber extends Ember<FollowComponentExample>
super.update(dt);
final deltaPosition = velocity * (speed * dt);
position.add(deltaPosition);
position.clamp(minPosition, maxPosition);
positionText.text = '(${x.toInt()}, ${y.toInt()})';
}

Expand Down
20 changes: 8 additions & 12 deletions examples/lib/stories/camera_and_viewport/zoom_example.dart
Expand Up @@ -9,24 +9,20 @@ class ZoomExample extends FlameGame with ScrollDetector, ScaleDetector {
''';

ZoomExample({
required this.viewportResolution,
});

final Vector2 viewportResolution;
late SpriteComponent flame;
required Vector2 viewportResolution,
}) : super(
camera: CameraComponent.withFixedResolution(
width: viewportResolution.x,
height: viewportResolution.y,
),
);

@override
Future<void> onLoad() async {
final flameSprite = await loadSprite('flame.png');

camera = CameraComponent.withFixedResolution(
world: world,
width: viewportResolution.x,
height: viewportResolution.y,
);

world.add(
flame = SpriteComponent(
SpriteComponent(
sprite: flameSprite,
size: Vector2(149, 211),
)..anchor = Anchor.center,
Expand Down
23 changes: 12 additions & 11 deletions examples/lib/stories/collision_detection/quadtree_example.dart
Expand Up @@ -204,24 +204,25 @@ class Player extends SpriteComponent
required super.position,
required super.size,
required super.priority,
}) {
Sprite.load(
});

bool canMoveLeft = true;
bool canMoveRight = true;
bool canMoveTop = true;
bool canMoveBottom = true;
final hitbox = RectangleHitbox();

@override
Future<void> onLoad() async {
sprite = await Sprite.load(
'retro_tiles.png',
srcSize: Vector2.all(tileSize),
srcPosition: Vector2(tileSize * 3, tileSize),
).then((value) {
sprite = value;
});
);

add(hitbox);
}

final hitbox = RectangleHitbox();
bool canMoveLeft = true;
bool canMoveRight = true;
bool canMoveTop = true;
bool canMoveBottom = true;

@override
void onCollisionStart(
Set<Vector2> intersectionPoints,
Expand Down
4 changes: 2 additions & 2 deletions examples/lib/stories/components/keys_example.dart
Expand Up @@ -36,9 +36,9 @@ class _KeysExampleWidgetState extends State<KeysExampleWidget> {
child: GameWidget(game: game),
),
Positioned(
left: 0,
left: 20,
top: 222,
width: 340,
width: 300,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expand Down
20 changes: 6 additions & 14 deletions examples/lib/stories/experimental/experimental.dart
@@ -1,21 +1,13 @@
import 'package:dashbook/dashbook.dart';
import 'package:examples/commons/commons.dart';
import 'package:examples/stories/experimental/camera_follow_and_world_bounds.dart';
import 'package:examples/stories/experimental/shapes.dart';
import 'package:flame/game.dart';

void addExperimentalStories(Dashbook dashbook) {
dashbook.storiesOf('Experimental')
..add(
'Shapes',
(_) => GameWidget(game: ShapesExample()),
codeLink: baseLink('experimental/shapes.dart'),
info: ShapesExample.description,
)
..add(
'Follow and World bounds',
(_) => GameWidget(game: CameraFollowAndWorldBoundsExample()),
codeLink: baseLink('experimental/camera_follow_and_world_bounds.dart'),
info: CameraFollowAndWorldBoundsExample.description,
);
dashbook.storiesOf('Experimental').add(
'Shapes',
(_) => GameWidget(game: ShapesExample()),
codeLink: baseLink('experimental/shapes.dart'),
info: ShapesExample.description,
);
}
40 changes: 24 additions & 16 deletions examples/lib/stories/input/gesture_hitboxes_example.dart
Expand Up @@ -27,22 +27,21 @@
final shapeSize =
Vector2.all(100) + Vector2.all(50.0).scaled(_rng.nextDouble());
final shapeAngle = _rng.nextDouble() * 6;
final hitbox = () {
switch (shapeType) {
case Shapes.circle:
return CircleHitbox();
case Shapes.rectangle:
return RectangleHitbox();
case Shapes.polygon:
final points = [
-Vector2.random(_rng),
Vector2.random(_rng)..x *= -1,
Vector2.random(_rng),
Vector2.random(_rng)..y *= -1,
];
return PolygonHitbox.relative(points, parentSize: shapeSize);
}
}();
ShapeHitbox hitbox;
switch (shapeType) {
case Shapes.circle:
hitbox = CircleHitbox();
case Shapes.rectangle:
hitbox = RectangleHitbox();
case Shapes.polygon:
final points = [
-Vector2.random(_rng),
Vector2.random(_rng)..x *= -1,
Vector2.random(_rng),
Vector2.random(_rng)..y *= -1,
];
hitbox = PolygonHitbox.relative(points, parentSize: shapeSize);
}
return MyShapeComponent(
hitbox: hitbox,
position: position,
Expand Down Expand Up @@ -84,13 +83,22 @@
removeFromParent();
}

@override
void update(double dt) {
if (isHovered) {
print('hovered');

Check notice on line 89 in examples/lib/stories/input/gesture_hitboxes_example.dart

View workflow job for this annotation

GitHub Actions / analyze

Don't invoke 'print' in production code.

Try using a logging framework. See https://dart.dev/lints/avoid_print to learn more about this problem.
}
}

@override
void onHoverEnter() {
print('dooo something');

Check notice on line 95 in examples/lib/stories/input/gesture_hitboxes_example.dart

View workflow job for this annotation

GitHub Actions / analyze

Don't invoke 'print' in production code.

Try using a logging framework. See https://dart.dev/lints/avoid_print to learn more about this problem.

Check warning on line 95 in examples/lib/stories/input/gesture_hitboxes_example.dart

View workflow job for this annotation

GitHub Actions / test

Unknown word (dooo)
hitbox.paint.color = hitbox.paint.color.darken(0.5);
}

@override
void onHoverExit() {
print('naaaoww');

Check notice on line 101 in examples/lib/stories/input/gesture_hitboxes_example.dart

View workflow job for this annotation

GitHub Actions / analyze

Don't invoke 'print' in production code.

Try using a logging framework. See https://dart.dev/lints/avoid_print to learn more about this problem.

Check warning on line 101 in examples/lib/stories/input/gesture_hitboxes_example.dart

View workflow job for this annotation

GitHub Actions / test

Unknown word (naaaoww)
hitbox.paint.color = baseColor;
}
}
20 changes: 11 additions & 9 deletions examples/lib/stories/input/joystick_advanced_example.dart
Expand Up @@ -33,7 +33,7 @@ class JoystickAdvancedExample extends FlameGame with HasCollisionDetection {
columns: 6,
rows: 1,
);
world.add(ScreenHitbox()..anchor = camera.viewfinder.anchor);
world.add(ScreenHitbox());
joystick = JoystickComponent(
knob: SpriteComponent(
sprite: sheet.getSpriteById(1),
Expand Down Expand Up @@ -179,14 +179,16 @@ class JoystickAdvancedExample extends FlameGame with HasCollisionDetection {
)..add(directionText);

world.add(player);
camera.viewport.add(joystick);
camera.viewport.add(flipButton);
camera.viewport.add(flopButton);
camera.viewport.add(buttonComponent);
camera.viewport.add(spriteButtonComponent);
camera.viewport.add(shapeButton);
camera.viewport.add(speedWithMargin);
camera.viewport.add(directionWithMargin);
camera.viewport.addAll([
joystick,
flipButton,
flopButton,
buttonComponent,
spriteButtonComponent,
shapeButton,
speedWithMargin,
directionWithMargin,
]);
}

@override
Expand Down
4 changes: 2 additions & 2 deletions examples/lib/stories/input/joystick_example.dart
Expand Up @@ -25,7 +25,7 @@ class JoystickExample extends FlameGame {
);
player = JoystickPlayer(joystick);

add(player);
add(joystick);
world.add(player);
camera.viewport.add(joystick);
}
}
12 changes: 9 additions & 3 deletions packages/flame/lib/src/collisions/hitboxes/screen_hitbox.dart
Expand Up @@ -7,12 +7,16 @@ import 'package:flame/src/collisions/hitboxes/rectangle_hitbox.dart';
/// viewport of the game.
class ScreenHitbox<T extends FlameGame> extends PositionComponent
with CollisionCallbacks, HasGameReference<T> {
bool _hasWorldAncestor = false;
@override
Future<void> onLoad() async {
await super.onLoad();
add(RectangleHitbox());
game.camera.viewfinder.transform.addListener(_updatePosition);
_updatePosition();
_hasWorldAncestor = findParent<World>() != null;
if (_hasWorldAncestor) {
game.camera.viewfinder.transform.addListener(_updatePosition);
_updatePosition();
}
}

void _updatePosition() {
Expand All @@ -26,6 +30,8 @@ class ScreenHitbox<T extends FlameGame> extends PositionComponent
void onGameResize(Vector2 size) {
super.onGameResize(size);
this.size = size;
_updatePosition();
if (_hasWorldAncestor) {
_updatePosition();
}
}
}
7 changes: 7 additions & 0 deletions packages/flame/lib/src/extensions/vector2.dart
Expand Up @@ -108,6 +108,13 @@ extension Vector2Extension on Vector2 {
}
}

/// Clamps this vector so that it is within or equals to the bounds defined by
/// [min] and [max].
void clamp(Vector2 min, Vector2 max) {
x = x.clamp(min.x, max.x);
y = y.clamp(min.y, max.y);
}

/// Project this onto [other].
///
/// [other] needs to have a length > 0;
Expand Down