Skip to content
New issue

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

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

Already on GitHub? # to your account

refactor: Use new "super"-constructors in ShapeComponents #1752

Merged
merged 6 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class RectangleHitbox extends RectangleComponent with ShapeHitbox {
super.relation, {
super.position,
required super.parentSize,
double super.angle,
super.angle,
super.anchor,
}) : shouldFillParent = false,
super.relative(
Expand Down
21 changes: 6 additions & 15 deletions packages/flame/lib/src/geometry/circle_component.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:math';
import 'dart:ui';

import 'package:flame/components.dart';
import 'package:flame/extensions.dart';
Expand All @@ -19,27 +18,19 @@ class CircleComponent extends ShapeComponent implements SizeProvider {
super.children,
super.priority,
super.paint,
}) : super(
size: Vector2.all((radius ?? 0) * 2),
);
}) : super(size: Vector2.all((radius ?? 0) * 2));

/// With this constructor you define the [CircleComponent] in relation to the
/// [parentSize]. For example having a [relation] of 0.5 would create a circle
/// that fills half of the [parentSize].
CircleComponent.relative(
double relation, {
Vector2? position,
required Vector2 parentSize,
double angle = 0,
Anchor? anchor,
Paint? paint,
}) : this(
radius: relation * (min(parentSize.x, parentSize.y) / 2),
position: position,
angle: angle,
anchor: anchor,
paint: paint,
);
super.position,
super.angle,
super.anchor,
super.paint,
}) : super(size: Vector2.all(relation * min(parentSize.x, parentSize.y)));

/// Get the radius of the circle before scaling.
double get radius {
Expand Down
89 changes: 32 additions & 57 deletions packages/flame/lib/src/geometry/rectangle_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,70 +6,45 @@ import 'package:meta/meta.dart';

class RectangleComponent extends PolygonComponent {
RectangleComponent({
Vector2? position,
Vector2? size,
double? angle,
Anchor? anchor,
Iterable<Component>? children,
int? priority,
Paint? paint,
}) : super(
sizeToVertices(size ?? Vector2.zero(), anchor),
position: position,
size: size,
angle: angle,
anchor: anchor,
children: children,
priority: priority,
paint: paint,
);
super.position,
super.size,
super.angle,
super.anchor,
super.children,
super.priority,
super.paint,
}) : super(sizeToVertices(size ?? Vector2.zero(), anchor));

RectangleComponent.square({
Vector2? position,
double? size,
double? angle,
Anchor? anchor,
int? priority,
Paint? paint,
}) : this(
position: position,
size: size != null ? Vector2.all(size) : null,
angle: angle,
anchor: anchor,
priority: priority,
paint: paint,
);
double size = 0,
super.position,
super.angle,
super.anchor,
super.priority,
super.paint,
super.children,
}) : super(sizeToVertices(Vector2.all(size), anchor));

/// With this constructor you define the [RectangleComponent] in relation to
/// the [parentSize]. For example having [relation] as of (0.8, 0.5) would
/// the `parentSize`. For example having [relation] as of (0.8, 0.5) would
/// create a rectangle that fills 80% of the width and 50% of the height of
/// [parentSize].
/// `parentSize`.
RectangleComponent.relative(
Vector2 relation, {
required Vector2 parentSize,
Vector2? position,
Vector2? scale,
double? angle = 0,
Anchor? anchor,
int? priority,
Paint? paint,
bool? shrinkToBounds,
}) : super.relative(
[
relation.clone(),
Vector2(relation.x, -relation.y),
-relation,
Vector2(-relation.x, relation.y),
],
position: position,
scale: scale,
parentSize: parentSize,
angle: angle,
anchor: anchor,
priority: priority,
paint: paint,
shrinkToBounds: shrinkToBounds,
);
required super.parentSize,
super.position,
super.scale,
super.angle,
super.anchor,
super.priority,
super.paint,
super.shrinkToBounds,
}) : super.relative([
relation.clone(),
Vector2(relation.x, -relation.y),
-relation,
Vector2(-relation.x, relation.y),
]);

/// This factory will create a [RectangleComponent] from a positioned [Rect].
factory RectangleComponent.fromRect(
Expand Down