Skip to content

Commit

Permalink
fix: Add missing hitbox parameters (#2070)
Browse files Browse the repository at this point in the history
This PR adds the position parameter to PolygonHitbox and isSolid parameters to all hitbox classes' .relative named constructors.

Also returns PolygonComponent's internal topLeft parameter to private _topLeft as no longer required outside of the class file.
  • Loading branch information
mattmyne authored Oct 16, 2022
1 parent f5970f3 commit 8aacb55
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ class CircleHitbox extends CircleComponent with ShapeHitbox {
required super.parentSize,
super.angle,
super.anchor,
bool isSolid = false,
}) : shouldFillParent = false,
super.relative();
super.relative() {
this.isSolid = isSolid;
}

@override
void fillParent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class PolygonHitbox extends PolygonComponent
with ShapeHitbox, PolygonRayIntersection {
PolygonHitbox(
super.vertices, {
super.position,
super.angle,
super.anchor,
bool isSolid = false,
Expand All @@ -28,9 +29,12 @@ class PolygonHitbox extends PolygonComponent
required super.parentSize,
double super.angle = 0,
super.anchor,
bool isSolid = false,
}) : super.relative(
shrinkToBounds: true,
);
) {
this.isSolid = isSolid;
}

@override
void fillParent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ class RectangleHitbox extends RectangleComponent
required super.parentSize,
super.angle,
super.anchor,
bool isSolid = false,
}) : shouldFillParent = false,
super.relative(
shrinkToBounds: true,
);
) {
this.isSolid = isSolid;
}

@override
void fillParent() {
Expand Down
19 changes: 9 additions & 10 deletions packages/flame/lib/src/geometry/polygon_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,33 +106,32 @@ class PolygonComponent extends ShapeComponent {

// Used to not create new Vector2 objects when calculating the top left of the
// bounds of the polygon.
@internal
final topLeft = Vector2.zero();
final _topLeft = Vector2.zero();

@protected
void refreshVertices({required List<Vector2> newVertices}) {
assert(
newVertices.length == _vertices.length,
'A polygon can not change their number of vertices',
);
topLeft.setFrom(newVertices[0]);
_topLeft.setFrom(newVertices[0]);
newVertices.forEachIndexed((i, _) {
final newVertex = newVertices[i];
_vertices[i].setFrom(newVertex);
topLeft.x = min(topLeft.x, newVertex.x);
topLeft.y = min(topLeft.y, newVertex.y);
_topLeft.x = min(_topLeft.x, newVertex.x);
_topLeft.y = min(_topLeft.y, newVertex.y);
});
_path
..reset()
..addPolygon(
vertices.map((p) => (p - topLeft).toOffset()).toList(growable: false),
vertices.map((p) => (p - _topLeft).toOffset()).toList(growable: false),
true,
);
if (shrinkToBounds) {
final bounds = _path.getBounds();
size.setValues(bounds.width, bounds.height);
if (!manuallyPositioned) {
position = Anchor.topLeft.toOtherAnchorPosition(topLeft, anchor, size);
position = Anchor.topLeft.toOtherAnchorPosition(_topLeft, anchor, size);
}
}
}
Expand All @@ -151,7 +150,7 @@ class PolygonComponent extends ShapeComponent {
vertices.forEachIndexed((i, vertex) {
_globalVertices[i]
..setFrom(vertex)
..sub(topLeft)
..sub(_topLeft)
..multiply(scale)
..add(position)
..rotate(angle, center: position);
Expand Down Expand Up @@ -213,8 +212,8 @@ class PolygonComponent extends ShapeComponent {
for (var i = 0; i < _vertices.length; i++) {
final edge = getEdge(i, vertices: vertices);
final isOutside = (edge.to.x - edge.from.x) *
(point.y - edge.from.y + topLeft.y) -
(point.x - edge.from.x + topLeft.x) * (edge.to.y - edge.from.y) >
(point.y - edge.from.y + _topLeft.y) -
(point.x - edge.from.x + _topLeft.x) * (edge.to.y - edge.from.y) >
0;
if (isOutside) {
return false;
Expand Down
48 changes: 48 additions & 0 deletions packages/flame/test/components/position_component_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,54 @@ void main() {
expect(component.containsPoint(point), true);
});

testWithFlameGame('component with hitbox with position contains point',
(game) async {
final component = _MyHitboxComponent();
component.position.setValues(1.0, 1.0);
component.anchor = Anchor.topLeft;
component.size.setValues(2.0, 2.0);
final hitbox = PolygonHitbox(
[
Vector2(1, 0),
Vector2(0, -1),
Vector2(-1, 0),
Vector2(0, 1),
],
position: Vector2(5, 6),
);
component.add(hitbox);
await game.ensureAdd(component);

final point =
component.position + (component.size / 4) + hitbox.position;
expect(component.containsPoint(point), true);
});

testWithFlameGame('component with hitbox with position just misses point',
(game) async {
final component = _MyHitboxComponent();
component.position.setValues(1.0, 1.0);
component.anchor = Anchor.topLeft;
component.size.setValues(2.0, 2.0);
final hitbox = PolygonHitbox(
[
Vector2(1, 0),
Vector2(0, -1),
Vector2(-1, 0),
Vector2(0, 1),
],
position: Vector2(5, 6),
);
component.add(hitbox);
await game.ensureAdd(component);

final point = component.position +
(component.size / 4) -
Vector2(0.01, 0) +
hitbox.position;
expect(component.containsPoint(point), false);
});

testWithFlameGame(
'component with anchor topLeft contains point on edge',
(game) async {
Expand Down

0 comments on commit 8aacb55

Please sign in to comment.