mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-12 19:01:09 +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 * 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
86 lines
1.9 KiB
Dart
86 lines
1.9 KiB
Dart
import 'package:flame/components.dart';
|
|
import 'package:flame/flame.dart';
|
|
import 'package:flame/game.dart';
|
|
import 'package:flame/extensions.dart';
|
|
import 'package:flame/geometry.dart';
|
|
import 'package:flame/gestures.dart';
|
|
|
|
import 'package:flutter/material.dart' hide Image, Draggable;
|
|
|
|
import 'dart:ui';
|
|
|
|
void main() async {
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
await Flame.device.fullScreen();
|
|
final game = MyGame();
|
|
runApp(
|
|
GameWidget(
|
|
game: game,
|
|
),
|
|
);
|
|
}
|
|
|
|
class MyCollidable extends PositionComponent
|
|
with HasGameRef<MyGame>, Hitbox, Collidable {
|
|
Vector2 velocity;
|
|
final _collisionColor = Colors.amber;
|
|
bool _isWallHit = false;
|
|
bool _isCollision = false;
|
|
|
|
MyCollidable(Vector2 position) {
|
|
this.position = position;
|
|
size = Vector2.all(100);
|
|
anchor = Anchor.center;
|
|
addShape(HitboxCircle());
|
|
}
|
|
|
|
@override
|
|
Future<void> onLoad() async {
|
|
final center = gameRef.size / 2;
|
|
velocity = (center - position).normalized() * 150;
|
|
}
|
|
|
|
@override
|
|
void update(double dt) {
|
|
super.update(dt);
|
|
if (_isWallHit) {
|
|
remove();
|
|
return;
|
|
}
|
|
debugColor = _isCollision ? _collisionColor : const Color(0xFFFF00FF);
|
|
position.add(velocity * dt);
|
|
_isCollision = false;
|
|
}
|
|
|
|
@override
|
|
void render(Canvas canvas) {
|
|
super.render(canvas);
|
|
renderShapes(canvas);
|
|
}
|
|
|
|
@override
|
|
void onCollision(Set<Vector2> points, Collidable other) {
|
|
if (other is ScreenCollidable) {
|
|
_isWallHit = true;
|
|
return;
|
|
}
|
|
_isCollision = true;
|
|
}
|
|
}
|
|
|
|
class MyGame extends BaseGame with HasCollidables, TapDetector {
|
|
final TextConfig fpsTextConfig = TextConfig(
|
|
color: const Color(0xFFFFFFFF),
|
|
);
|
|
|
|
@override
|
|
Future<void> onLoad() async {
|
|
add(ScreenCollidable());
|
|
}
|
|
|
|
@override
|
|
void onTapDown(TapDownDetails details) {
|
|
add(MyCollidable(details.localPosition.toVector2()));
|
|
}
|
|
}
|