feat: Accept CollisionType in hitbox constructor (#2509)

With this PR we accept the `CollisionType` directly in the constructor.
This commit is contained in:
Lukas Klingsbo
2023-04-18 15:57:16 +02:00
committed by GitHub
parent 80019a9407
commit 89926227c5
15 changed files with 32 additions and 20 deletions

View File

@ -163,9 +163,9 @@ The `CollisionType` enum contains the following values:
- `inactive` will not collide with any other `Collidable`s
So if you have hitboxes that you don't need to check collisions against each other you can mark
them as passive by setting `collisionType = CollisionType.passive`, this could for example be
ground components or maybe your enemies don't need to check collisions between each other, then they
could be marked as `passive` too.
them as passive by setting `collisionType: CollisionType.passive` in the constructor, this could for
example be ground components or maybe your enemies don't need to check collisions between each
other, then they could be marked as `passive` too.
Imagine a game where there are a lot of bullets, that can't collide with each other, flying towards
the player, then the player would be set to `CollisionType.active` and the bullets would be set to

View File

@ -25,7 +25,7 @@ class EmberPlayer extends SpriteAnimationComponent with TapCallbacks {
),
);
add(CircleHitbox()..collisionType = CollisionType.active);
add(CircleHitbox());
}
@override

View File

@ -30,7 +30,7 @@ class WaterEnemy extends SpriteAnimationComponent
(gridPosition.x * size.x) + xOffset,
game.size.y - (gridPosition.y * size.y),
);
add(RectangleHitbox()..collisionType = CollisionType.passive);
add(RectangleHitbox(collisionType: CollisionType.passive));
add(
MoveEffect.by(
Vector2(-2 * size.x, 0),

View File

@ -27,7 +27,7 @@ class GroundBlock extends SpriteComponent with HasGameRef<EmberQuestGame> {
(gridPosition.x * size.x) + xOffset,
game.size.y - (gridPosition.y * size.y),
);
add(RectangleHitbox()..collisionType = CollisionType.passive);
add(RectangleHitbox(collisionType: CollisionType.passive));
if (gridPosition.x == 9 && position.x > game.lastBlockXPosition) {
game.lastBlockKey = _blockKey;
game.lastBlockXPosition = position.x + size.x;

View File

@ -22,7 +22,7 @@ class PlatformBlock extends SpriteComponent with HasGameRef<EmberQuestGame> {
(gridPosition.x * size.x) + xOffset,
game.size.y - (gridPosition.y * size.y),
);
add(RectangleHitbox()..collisionType = CollisionType.passive);
add(RectangleHitbox(collisionType: CollisionType.passive));
}
@override

View File

@ -24,7 +24,7 @@ class Star extends SpriteComponent with HasGameRef<EmberQuestGame> {
(gridPosition.x * size.x) + xOffset + (size.x / 2),
game.size.y - (gridPosition.y * size.y) - (size.y / 2),
);
add(RectangleHitbox()..collisionType = CollisionType.passive);
add(RectangleHitbox(collisionType: CollisionType.passive));
add(
SizeEffect.by(
Vector2(-24, -24),

View File

@ -401,7 +401,7 @@ Now we just need to finish the `onLoad` method. So make your `onLoad` method lo
position = Vector2((gridPosition.x * size.x) + _xOffset,
game.size.y - (gridPosition.y * size.y),
);
add(RectangleHitbox()..collisionType = CollisionType.passive);
add(RectangleHitbox(collisionType: CollisionType.passive));
}
```

View File

@ -36,7 +36,7 @@ class Star extends SpriteComponent
(gridPosition.x * size.x) + xOffset + (size.x / 2),
game.size.y - (gridPosition.y * size.y) - (size.y / 2),
);
add(RectangleHitbox()..collisionType = CollisionType.passive);
add(RectangleHitbox(collisionType: CollisionType.passive));
add(
SizeEffect.by(
Vector2(-24, -24),
@ -132,7 +132,7 @@ class WaterEnemy extends SpriteAnimationComponent
(gridPosition.x * size.x) + xOffset + (size.x / 2),
game.size.y - (gridPosition.y * size.y) - (size.y / 2),
);
add(RectangleHitbox()..collisionType = CollisionType.passive);
add(RectangleHitbox(collisionType: CollisionType.passive));
add(
MoveEffect.by(
Vector2(-2 * size.x, 0),
@ -220,7 +220,7 @@ class GroundBlock extends SpriteComponent with HasGameRef<EmberQuestGame> {
position = Vector2((gridPosition.x * size.x) + xOffset,
game.size.y - (gridPosition.y * size.y),
);
add(RectangleHitbox()..collisionType = CollisionType.passive);
add(RectangleHitbox(collisionType: CollisionType.passive));
}
@override
@ -348,7 +348,7 @@ class GroundBlock extends SpriteComponent with HasGameRef<EmberQuestGame> {
position = Vector2((gridPosition.x * size.x) + xOffset,
game.size.y - (gridPosition.y * size.y),
);
add(RectangleHitbox()..collisionType = CollisionType.passive);
add(RectangleHitbox(collisionType: CollisionType.passive));
if (gridPosition.x == 9 && position.x > game.lastBlockXPosition) {
game.lastBlockKey = _blockKey;
game.lastBlockXPosition = position.x + size.x;

View File

@ -21,7 +21,7 @@ class EnemyComponent extends SpriteAnimationComponent
textureSize: Vector2.all(16),
),
);
add(CircleHitbox()..collisionType = CollisionType.passive);
add(CircleHitbox(collisionType: CollisionType.passive));
}
@override

View File

@ -337,7 +337,7 @@ class Water extends SpriteComponent
mixin GameCollidable on PositionComponent {
void initCollision() {
add(RectangleHitbox()..collisionType = CollisionType.passive);
add(RectangleHitbox(collisionType: CollisionType.passive));
}
void initCenter() {

View File

@ -5,8 +5,8 @@ import 'package:flame/geometry.dart';
/// [CollisionDetection] is the foundation of the collision detection system in
/// Flame.
///
/// If the [HasCollisionDetection] mixin is added to the game, [run] is
/// called every tick to check for collisions
/// If the [HasCollisionDetection] mixin is added to the game, [run] is called
/// every tick to check for collisions.
abstract class CollisionDetection<T extends Hitbox<T>,
B extends Broadphase<T>> {
final B broadphase;
@ -20,8 +20,8 @@ abstract class CollisionDetection<T extends Hitbox<T>,
void addAll(Iterable<T> items) => items.forEach(add);
/// Removes the [item] from the collision detection, if you just want
/// to temporarily inactivate it you can set
/// Removes the [item] from the collision detection, if you just want to
/// temporarily inactivate it you can set
/// `collisionType = CollisionType.inactive;` instead.
void remove(T item) => broadphase.remove(item);

View File

@ -15,8 +15,10 @@ class CircleHitbox extends CircleComponent with ShapeHitbox {
super.angle,
super.anchor,
bool isSolid = false,
CollisionType collisionType = CollisionType.active,
}) : shouldFillParent = radius == null && position == null {
this.isSolid = isSolid;
this.collisionType = collisionType;
}
/// With this constructor you define the [CircleHitbox] in relation to the
@ -29,9 +31,11 @@ class CircleHitbox extends CircleComponent with ShapeHitbox {
super.angle,
super.anchor,
bool isSolid = false,
CollisionType collisionType = CollisionType.active,
}) : shouldFillParent = false,
super.relative() {
this.isSolid = isSolid;
this.collisionType = collisionType;
}
@override

View File

@ -12,8 +12,10 @@ class PolygonHitbox extends PolygonComponent
super.angle,
super.anchor,
bool isSolid = false,
CollisionType collisionType = CollisionType.active,
}) {
this.isSolid = isSolid;
this.collisionType = collisionType;
}
/// With this constructor you define the [PolygonHitbox] in relation to the
@ -30,8 +32,10 @@ class PolygonHitbox extends PolygonComponent
double super.angle = 0,
super.anchor,
bool isSolid = false,
CollisionType collisionType = CollisionType.active,
}) : super.relative(shrinkToBounds: true) {
this.isSolid = isSolid;
this.collisionType = collisionType;
}
@override

View File

@ -17,8 +17,10 @@ class RectangleHitbox extends RectangleComponent
super.anchor,
super.priority,
bool isSolid = false,
CollisionType collisionType = CollisionType.active,
}) : shouldFillParent = size == null && position == null {
this.isSolid = isSolid;
this.collisionType = collisionType;
}
/// With this constructor you define the [RectangleHitbox] in relation to
@ -32,11 +34,13 @@ class RectangleHitbox extends RectangleComponent
super.angle,
super.anchor,
bool isSolid = false,
CollisionType collisionType = CollisionType.active,
}) : shouldFillParent = false,
super.relative(
shrinkToBounds: true,
) {
this.isSolid = isSolid;
this.collisionType = collisionType;
}
@override

View File

@ -12,7 +12,7 @@ class EnemyComponent extends SpriteAnimationComponent
EnemyComponent(double x, double y)
: super(position: Vector2(x, y), size: Vector2.all(25)) {
add(RectangleHitbox()..collisionType = CollisionType.passive);
add(RectangleHitbox(collisionType: CollisionType.passive));
}
@override