Files
flame/doc/examples/gestures/lib/main_tapables_hitbox.dart
Lukas Klingsbo 0593e35766 Add hitbox to PositionComponent (#618)
* 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
2021-01-20 23:39:01 +01:00

59 lines
1.2 KiB
Dart

import 'package:flame/components.dart';
import 'package:flutter/material.dart';
import 'package:flame/game.dart';
void main() {
runApp(
Container(
padding: const EdgeInsets.all(50),
color: const Color(0xFFA9A9A9),
child: GameWidget(
game: MyGame(),
),
),
);
}
class TapablePolygon extends PositionComponent with Tapable, Hitbox {
TapablePolygon({Vector2 position}) {
size = Vector2.all(100);
// The hitbox is defined as percentages of the full size of the component
shape = [
Vector2(-1.0, 0.0),
Vector2(-0.8, 0.6),
Vector2(0.0, 1.0),
Vector2(0.6, 0.9),
Vector2(1.0, 0.0),
Vector2(0.6, -0.8),
Vector2(0, -1.0),
Vector2(-0.8, -0.8),
];
this.position = position ?? Vector2.all(150);
}
@override
bool onTapUp(TapUpDetails details) {
return true;
}
@override
bool onTapDown(TapDownDetails details) {
angle += 1.0;
size.add(Vector2.all(10));
return true;
}
@override
bool onTapCancel() {
return true;
}
}
class MyGame extends BaseGame with HasTapableComponents {
MyGame() {
debugMode = true;
add(TapablePolygon()..anchor = Anchor.center);
add(TapablePolygon()..y = 350);
}
}