mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-02 03:15:43 +08:00
feat: Accept CollisionType in hitbox constructor (#2509)
With this PR we accept the `CollisionType` directly in the constructor.
This commit is contained in:
@ -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
|
||||
|
||||
@ -25,7 +25,7 @@ class EmberPlayer extends SpriteAnimationComponent with TapCallbacks {
|
||||
),
|
||||
);
|
||||
|
||||
add(CircleHitbox()..collisionType = CollisionType.active);
|
||||
add(CircleHitbox());
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@ -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),
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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),
|
||||
|
||||
@ -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));
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -21,7 +21,7 @@ class EnemyComponent extends SpriteAnimationComponent
|
||||
textureSize: Vector2.all(16),
|
||||
),
|
||||
);
|
||||
add(CircleHitbox()..collisionType = CollisionType.passive);
|
||||
add(CircleHitbox(collisionType: CollisionType.passive));
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@ -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() {
|
||||
|
||||
@ -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);
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
Reference in New Issue
Block a user