mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-13 03:10:42 +08:00
* Move out collision detection methods * Add possibility to define a hull for PositionComponents * Add example of how to use hull with tapable * Update contains point comment * Fix contains point * Hull should be based on center position * Remove collision detection parts * Added tests * Use percentage of size instead of absolute size * Separate hull from PositionComponent * Clarify hull example * Fix formatting * Override correct method * Use mixin for hitbox * Update changelog * Rename HasHitbox to Hitbox * Clarified names * Center to edge is considered as 1.0 * Fix test * Add spaces within braces * Removed extra spaces in the braces * Add hitbox docs * Fix link * Moved point rotation to Vector2 extension * Render hitbox within extension * Fix rebase * Fix rebase * Fix formatting
124 lines
3.7 KiB
Dart
124 lines
3.7 KiB
Dart
import 'dart:math' as math;
|
|
|
|
import 'package:flame/components.dart';
|
|
import 'package:test/test.dart';
|
|
|
|
class MyComponent extends PositionComponent {}
|
|
|
|
class MyHitboxComponent extends PositionComponent with Hitbox {}
|
|
|
|
void main() {
|
|
group('PositionComponent overlap test', () {
|
|
test('overlap', () {
|
|
final PositionComponent component = MyComponent();
|
|
component.position = Vector2(2.0, 2.0);
|
|
component.size = Vector2(4.0, 4.0);
|
|
component.angle = 0.0;
|
|
component.anchor = Anchor.center;
|
|
|
|
final point = Vector2(2.0, 2.0);
|
|
expect(component.containsPoint(point), true);
|
|
});
|
|
|
|
test('overlap on edge', () {
|
|
final PositionComponent component = MyComponent();
|
|
component.position = Vector2(2.0, 2.0);
|
|
component.size = Vector2(2.0, 2.0);
|
|
component.angle = 0.0;
|
|
component.anchor = Anchor.center;
|
|
|
|
final point = Vector2(1.0, 1.0);
|
|
expect(component.containsPoint(point), true);
|
|
});
|
|
|
|
test('not overlapping with x', () {
|
|
final PositionComponent component = MyComponent();
|
|
component.position = Vector2(2.0, 2.0);
|
|
component.size = Vector2(2.0, 2.0);
|
|
component.angle = 0.0;
|
|
component.anchor = Anchor.center;
|
|
|
|
final point = Vector2(4.0, 1.0);
|
|
expect(component.containsPoint(point), false);
|
|
});
|
|
|
|
test('not overlapping with y', () {
|
|
final PositionComponent component = MyComponent();
|
|
component.position = Vector2(2.0, 2.0);
|
|
component.size = Vector2(2.0, 2.0);
|
|
component.angle = 0.0;
|
|
component.anchor = Anchor.center;
|
|
|
|
final point = Vector2(1.0, 4.0);
|
|
expect(component.containsPoint(point), false);
|
|
});
|
|
|
|
test('overlapping with angle', () {
|
|
final PositionComponent component = MyComponent();
|
|
component.position = Vector2(2.0, 2.0);
|
|
component.size = Vector2(2.0, 2.0);
|
|
component.angle = math.pi / 4;
|
|
component.anchor = Anchor.center;
|
|
|
|
final point = Vector2(3.1, 2.0);
|
|
expect(component.containsPoint(point), true);
|
|
});
|
|
|
|
test('not overlapping with angle', () {
|
|
final PositionComponent component = MyComponent();
|
|
component.position = Vector2(2.0, 2.0);
|
|
component.size = Vector2(2.0, 2.0);
|
|
component.angle = math.pi / 4;
|
|
component.anchor = Anchor.center;
|
|
|
|
final point = Vector2(1.0, 0.1);
|
|
expect(component.containsPoint(point), false);
|
|
});
|
|
|
|
test('overlapping with angle and topLeft anchor', () {
|
|
final PositionComponent component = MyComponent();
|
|
component.position = Vector2(1.0, 1.0);
|
|
component.size = Vector2(2.0, 2.0);
|
|
component.angle = math.pi / 4;
|
|
component.anchor = Anchor.topLeft;
|
|
|
|
final point = Vector2(1.0, 3.1);
|
|
expect(component.containsPoint(point), true);
|
|
});
|
|
|
|
test('component with hitbox contains point', () {
|
|
final size = Vector2(2.0, 2.0);
|
|
final Hitbox component = MyHitboxComponent();
|
|
component.position = Vector2(1.0, 1.0);
|
|
component.anchor = Anchor.topLeft;
|
|
component.size = size;
|
|
component.shape = [
|
|
Vector2(1, 0),
|
|
Vector2(0, -1),
|
|
Vector2(-1, 0),
|
|
Vector2(0, 1),
|
|
];
|
|
|
|
final point = component.position + component.size / 4;
|
|
expect(component.containsPoint(point), true);
|
|
});
|
|
|
|
test('component with hitbox does not contains point', () {
|
|
final size = Vector2(2.0, 2.0);
|
|
final Hitbox component = MyHitboxComponent();
|
|
component.position = Vector2(1.0, 1.0);
|
|
component.anchor = Anchor.topLeft;
|
|
component.size = size;
|
|
component.shape = [
|
|
Vector2(1, 0),
|
|
Vector2(0, -1),
|
|
Vector2(-1, 0),
|
|
Vector2(0, 1),
|
|
];
|
|
|
|
final point = Vector2(1.1, 1.1);
|
|
expect(component.containsPoint(point), false);
|
|
});
|
|
});
|
|
}
|