Files
flame/test/components/composed_component_test.dart
Lukas Klingsbo bde4585fa0 Collision detection (#633)
* 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

* Use percentage of size instead of absolute size

* Separate hull from PositionComponent

* Clarify hull example

* Fix formatting

* Change to relative import

* Use mixin for hitbox

* Update changelog

* Rename HasHitbox to Hitbox

* Clarified names

* Add spaces within braces

* Removed extra spaces in the braces

* Moved point rotation to Vector2 extension

* Render hitbox within extension

* Added collision detection

* Add tests

* Separate classes into files

* Fix formatting

* Move geometry files into geometry directory

* Use relative import for mixin

* Begin intersections between different shapes

* Add shape class

* Align with rebase

* Fix CHANGELOG

* Fix children positioning

* New polygon intersection algorithm

* No anchor for shape in PoC

* Remove unused imports

* Smarter bounding rectangle comparisons

* Formatting

* Add Circle to Circle collision

* Circle-polygon intersections

* Explanation of circle-circle intersections

* Properly render circle circle collisions

* Fix formatting

* Better example

* Update docs for collision detection

* Fix formatting

* Add polygon definition example

* Update documentation about the shapes

* Moved premature rc6 changelog line

* Added a cache system for shape calculations

* Fix formatting

* Fix formatting

* Fix imports

* Add collidable polygon to example

* Use anchorPosition for PositionComponent containsPoint

* Fix angle problem for Rectangle

* collisionCallback -> onCollision

* Fixed Erick's comments

* Improve collision detection example

* Fix #662, zero size doesn't contain any points

* Fix formatting

* Can't contain point if x or y is 0

* Fix formatting

* Fix test

* Remove unnecessary collidable example part

* Align with Draggable overhaul

* Updated collision detection docs

* Fix PR comments

* Have more sensible Circle constructor

* Clarify shape fields

* Need ensureInitialized

* Update docs to conform with switched constructors

* Fix new definitions

* Fix formatting

* Update documentation

* Fix formatting

* Fix formatting

* Exclude metrics check for test files

* Add another simpler example of collision detection

* Updated according to comments

* Fix comments

* Fix more comments

* Fix more comments

* Fix relative import

* Fix comments

* Moved export of geometry

* Fix comments

* Remove unused import

* Fix assert for shape.component

* Fix comments

* Expect instead of assert in test
2021-02-22 00:44:11 +01:00

136 lines
3.3 KiB
Dart

import 'dart:ui';
import 'package:flame/components.dart';
import 'package:flame/game.dart';
import 'package:flutter/gestures.dart';
import 'package:test/test.dart';
import '../util/mock_canvas.dart';
class MyGame extends BaseGame with HasTapableComponents {}
class MyTap extends PositionComponent with Tapable {
Vector2 gameSize;
int tapTimes = 0;
bool get tapped => tapTimes > 0;
bool updated = false;
bool rendered = false;
@override
void update(double dt) {
super.update(dt);
updated = true;
}
@override
void render(Canvas canvas) {
super.render(canvas);
rendered = true;
}
@override
void onGameResize(Vector2 gameSize) {
super.onGameResize(gameSize);
this.gameSize = gameSize;
}
@override
bool onTapDown(TapDownDetails details) {
++tapTimes;
return true;
}
}
class MyAsyncChild extends MyTap {
@override
Future<void> onLoad() => Future.value();
}
class MyComposed extends PositionComponent with HasGameRef, Tapable {}
Vector2 size = Vector2.all(300);
void main() {
group('composable component test', () {
test('adds the child to the component', () {
final MyTap child = MyTap();
final MyComposed wrapper = MyComposed();
wrapper.addChild(child);
expect(true, wrapper.containsChild(child));
});
test('removes the child from the component', () {
final MyTap child = MyTap();
final MyComposed wrapper = MyComposed();
wrapper.addChild(child);
expect(true, wrapper.containsChild(child));
wrapper.removeChild(child);
expect(false, wrapper.containsChild(child));
});
test(
'when child is async loading, adds the child to the component after loading',
() async {
final MyAsyncChild child = MyAsyncChild();
final MyComposed wrapper = MyComposed();
await wrapper.addChild(child);
expect(true, wrapper.containsChild(child));
},
);
test('taps and resizes children', () {
final MyGame game = MyGame();
final MyTap child = MyTap();
final MyComposed wrapper = MyComposed();
game.onResize(size);
child.size = Vector2.all(1);
game.add(wrapper);
wrapper.addChild(child);
game.update(0.0);
game.onTapDown(1, TapDownDetails(globalPosition: const Offset(0.0, 0.0)));
expect(child.gameSize, size);
expect(child.tapped, true);
});
test('tap on offset children', () {
final MyGame game = MyGame();
final MyTap child = MyTap()
..position = Vector2.all(100)
..size = Vector2.all(100);
final MyComposed wrapper = MyComposed()
..position = Vector2.all(100)
..size = Vector2.all(300);
game.onResize(size);
game.add(wrapper);
wrapper.addChild(child);
game.update(0.0);
game.onTapDown(1, TapDownDetails(globalPosition: const Offset(250, 250)));
expect(child.gameSize, size);
expect(child.tapped, true);
expect(child.tapTimes, 1);
});
test('updates and renders children', () {
final MyGame game = MyGame();
game.onResize(Vector2.all(100));
final MyTap child = MyTap();
final MyComposed wrapper = MyComposed();
wrapper.addChild(child);
game.add(wrapper);
game.update(0.0);
game.render(MockCanvas());
expect(child.rendered, true);
expect(child.updated, true);
});
});
}