mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-02 11:43:19 +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
|
- `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
|
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
|
them as passive by setting `collisionType: CollisionType.passive` in the constructor, this could for
|
||||||
ground components or maybe your enemies don't need to check collisions between each other, then they
|
example be ground components or maybe your enemies don't need to check collisions between each
|
||||||
could be marked as `passive` too.
|
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
|
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
|
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
|
@override
|
||||||
|
|||||||
@ -30,7 +30,7 @@ class WaterEnemy extends SpriteAnimationComponent
|
|||||||
(gridPosition.x * size.x) + xOffset,
|
(gridPosition.x * size.x) + xOffset,
|
||||||
game.size.y - (gridPosition.y * size.y),
|
game.size.y - (gridPosition.y * size.y),
|
||||||
);
|
);
|
||||||
add(RectangleHitbox()..collisionType = CollisionType.passive);
|
add(RectangleHitbox(collisionType: CollisionType.passive));
|
||||||
add(
|
add(
|
||||||
MoveEffect.by(
|
MoveEffect.by(
|
||||||
Vector2(-2 * size.x, 0),
|
Vector2(-2 * size.x, 0),
|
||||||
|
|||||||
@ -27,7 +27,7 @@ class GroundBlock extends SpriteComponent with HasGameRef<EmberQuestGame> {
|
|||||||
(gridPosition.x * size.x) + xOffset,
|
(gridPosition.x * size.x) + xOffset,
|
||||||
game.size.y - (gridPosition.y * size.y),
|
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) {
|
if (gridPosition.x == 9 && position.x > game.lastBlockXPosition) {
|
||||||
game.lastBlockKey = _blockKey;
|
game.lastBlockKey = _blockKey;
|
||||||
game.lastBlockXPosition = position.x + size.x;
|
game.lastBlockXPosition = position.x + size.x;
|
||||||
|
|||||||
@ -22,7 +22,7 @@ class PlatformBlock extends SpriteComponent with HasGameRef<EmberQuestGame> {
|
|||||||
(gridPosition.x * size.x) + xOffset,
|
(gridPosition.x * size.x) + xOffset,
|
||||||
game.size.y - (gridPosition.y * size.y),
|
game.size.y - (gridPosition.y * size.y),
|
||||||
);
|
);
|
||||||
add(RectangleHitbox()..collisionType = CollisionType.passive);
|
add(RectangleHitbox(collisionType: CollisionType.passive));
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|||||||
@ -24,7 +24,7 @@ class Star extends SpriteComponent with HasGameRef<EmberQuestGame> {
|
|||||||
(gridPosition.x * size.x) + xOffset + (size.x / 2),
|
(gridPosition.x * size.x) + xOffset + (size.x / 2),
|
||||||
game.size.y - (gridPosition.y * size.y) - (size.y / 2),
|
game.size.y - (gridPosition.y * size.y) - (size.y / 2),
|
||||||
);
|
);
|
||||||
add(RectangleHitbox()..collisionType = CollisionType.passive);
|
add(RectangleHitbox(collisionType: CollisionType.passive));
|
||||||
add(
|
add(
|
||||||
SizeEffect.by(
|
SizeEffect.by(
|
||||||
Vector2(-24, -24),
|
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,
|
position = Vector2((gridPosition.x * size.x) + _xOffset,
|
||||||
game.size.y - (gridPosition.y * size.y),
|
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),
|
(gridPosition.x * size.x) + xOffset + (size.x / 2),
|
||||||
game.size.y - (gridPosition.y * size.y) - (size.y / 2),
|
game.size.y - (gridPosition.y * size.y) - (size.y / 2),
|
||||||
);
|
);
|
||||||
add(RectangleHitbox()..collisionType = CollisionType.passive);
|
add(RectangleHitbox(collisionType: CollisionType.passive));
|
||||||
add(
|
add(
|
||||||
SizeEffect.by(
|
SizeEffect.by(
|
||||||
Vector2(-24, -24),
|
Vector2(-24, -24),
|
||||||
@ -132,7 +132,7 @@ class WaterEnemy extends SpriteAnimationComponent
|
|||||||
(gridPosition.x * size.x) + xOffset + (size.x / 2),
|
(gridPosition.x * size.x) + xOffset + (size.x / 2),
|
||||||
game.size.y - (gridPosition.y * size.y) - (size.y / 2),
|
game.size.y - (gridPosition.y * size.y) - (size.y / 2),
|
||||||
);
|
);
|
||||||
add(RectangleHitbox()..collisionType = CollisionType.passive);
|
add(RectangleHitbox(collisionType: CollisionType.passive));
|
||||||
add(
|
add(
|
||||||
MoveEffect.by(
|
MoveEffect.by(
|
||||||
Vector2(-2 * size.x, 0),
|
Vector2(-2 * size.x, 0),
|
||||||
@ -220,7 +220,7 @@ class GroundBlock extends SpriteComponent with HasGameRef<EmberQuestGame> {
|
|||||||
position = Vector2((gridPosition.x * size.x) + xOffset,
|
position = Vector2((gridPosition.x * size.x) + xOffset,
|
||||||
game.size.y - (gridPosition.y * size.y),
|
game.size.y - (gridPosition.y * size.y),
|
||||||
);
|
);
|
||||||
add(RectangleHitbox()..collisionType = CollisionType.passive);
|
add(RectangleHitbox(collisionType: CollisionType.passive));
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@ -348,7 +348,7 @@ class GroundBlock extends SpriteComponent with HasGameRef<EmberQuestGame> {
|
|||||||
position = Vector2((gridPosition.x * size.x) + xOffset,
|
position = Vector2((gridPosition.x * size.x) + xOffset,
|
||||||
game.size.y - (gridPosition.y * size.y),
|
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) {
|
if (gridPosition.x == 9 && position.x > game.lastBlockXPosition) {
|
||||||
game.lastBlockKey = _blockKey;
|
game.lastBlockKey = _blockKey;
|
||||||
game.lastBlockXPosition = position.x + size.x;
|
game.lastBlockXPosition = position.x + size.x;
|
||||||
|
|||||||
@ -21,7 +21,7 @@ class EnemyComponent extends SpriteAnimationComponent
|
|||||||
textureSize: Vector2.all(16),
|
textureSize: Vector2.all(16),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
add(CircleHitbox()..collisionType = CollisionType.passive);
|
add(CircleHitbox(collisionType: CollisionType.passive));
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|||||||
@ -337,7 +337,7 @@ class Water extends SpriteComponent
|
|||||||
|
|
||||||
mixin GameCollidable on PositionComponent {
|
mixin GameCollidable on PositionComponent {
|
||||||
void initCollision() {
|
void initCollision() {
|
||||||
add(RectangleHitbox()..collisionType = CollisionType.passive);
|
add(RectangleHitbox(collisionType: CollisionType.passive));
|
||||||
}
|
}
|
||||||
|
|
||||||
void initCenter() {
|
void initCenter() {
|
||||||
|
|||||||
@ -5,8 +5,8 @@ import 'package:flame/geometry.dart';
|
|||||||
/// [CollisionDetection] is the foundation of the collision detection system in
|
/// [CollisionDetection] is the foundation of the collision detection system in
|
||||||
/// Flame.
|
/// Flame.
|
||||||
///
|
///
|
||||||
/// If the [HasCollisionDetection] mixin is added to the game, [run] is
|
/// If the [HasCollisionDetection] mixin is added to the game, [run] is called
|
||||||
/// called every tick to check for collisions
|
/// every tick to check for collisions.
|
||||||
abstract class CollisionDetection<T extends Hitbox<T>,
|
abstract class CollisionDetection<T extends Hitbox<T>,
|
||||||
B extends Broadphase<T>> {
|
B extends Broadphase<T>> {
|
||||||
final B broadphase;
|
final B broadphase;
|
||||||
@ -20,8 +20,8 @@ abstract class CollisionDetection<T extends Hitbox<T>,
|
|||||||
|
|
||||||
void addAll(Iterable<T> items) => items.forEach(add);
|
void addAll(Iterable<T> items) => items.forEach(add);
|
||||||
|
|
||||||
/// Removes the [item] from the collision detection, if you just want
|
/// Removes the [item] from the collision detection, if you just want to
|
||||||
/// to temporarily inactivate it you can set
|
/// temporarily inactivate it you can set
|
||||||
/// `collisionType = CollisionType.inactive;` instead.
|
/// `collisionType = CollisionType.inactive;` instead.
|
||||||
void remove(T item) => broadphase.remove(item);
|
void remove(T item) => broadphase.remove(item);
|
||||||
|
|
||||||
|
|||||||
@ -15,8 +15,10 @@ class CircleHitbox extends CircleComponent with ShapeHitbox {
|
|||||||
super.angle,
|
super.angle,
|
||||||
super.anchor,
|
super.anchor,
|
||||||
bool isSolid = false,
|
bool isSolid = false,
|
||||||
|
CollisionType collisionType = CollisionType.active,
|
||||||
}) : shouldFillParent = radius == null && position == null {
|
}) : shouldFillParent = radius == null && position == null {
|
||||||
this.isSolid = isSolid;
|
this.isSolid = isSolid;
|
||||||
|
this.collisionType = collisionType;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// With this constructor you define the [CircleHitbox] in relation to the
|
/// With this constructor you define the [CircleHitbox] in relation to the
|
||||||
@ -29,9 +31,11 @@ class CircleHitbox extends CircleComponent with ShapeHitbox {
|
|||||||
super.angle,
|
super.angle,
|
||||||
super.anchor,
|
super.anchor,
|
||||||
bool isSolid = false,
|
bool isSolid = false,
|
||||||
|
CollisionType collisionType = CollisionType.active,
|
||||||
}) : shouldFillParent = false,
|
}) : shouldFillParent = false,
|
||||||
super.relative() {
|
super.relative() {
|
||||||
this.isSolid = isSolid;
|
this.isSolid = isSolid;
|
||||||
|
this.collisionType = collisionType;
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|||||||
@ -12,8 +12,10 @@ class PolygonHitbox extends PolygonComponent
|
|||||||
super.angle,
|
super.angle,
|
||||||
super.anchor,
|
super.anchor,
|
||||||
bool isSolid = false,
|
bool isSolid = false,
|
||||||
|
CollisionType collisionType = CollisionType.active,
|
||||||
}) {
|
}) {
|
||||||
this.isSolid = isSolid;
|
this.isSolid = isSolid;
|
||||||
|
this.collisionType = collisionType;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// With this constructor you define the [PolygonHitbox] in relation to the
|
/// With this constructor you define the [PolygonHitbox] in relation to the
|
||||||
@ -30,8 +32,10 @@ class PolygonHitbox extends PolygonComponent
|
|||||||
double super.angle = 0,
|
double super.angle = 0,
|
||||||
super.anchor,
|
super.anchor,
|
||||||
bool isSolid = false,
|
bool isSolid = false,
|
||||||
|
CollisionType collisionType = CollisionType.active,
|
||||||
}) : super.relative(shrinkToBounds: true) {
|
}) : super.relative(shrinkToBounds: true) {
|
||||||
this.isSolid = isSolid;
|
this.isSolid = isSolid;
|
||||||
|
this.collisionType = collisionType;
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|||||||
@ -17,8 +17,10 @@ class RectangleHitbox extends RectangleComponent
|
|||||||
super.anchor,
|
super.anchor,
|
||||||
super.priority,
|
super.priority,
|
||||||
bool isSolid = false,
|
bool isSolid = false,
|
||||||
|
CollisionType collisionType = CollisionType.active,
|
||||||
}) : shouldFillParent = size == null && position == null {
|
}) : shouldFillParent = size == null && position == null {
|
||||||
this.isSolid = isSolid;
|
this.isSolid = isSolid;
|
||||||
|
this.collisionType = collisionType;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// With this constructor you define the [RectangleHitbox] in relation to
|
/// With this constructor you define the [RectangleHitbox] in relation to
|
||||||
@ -32,11 +34,13 @@ class RectangleHitbox extends RectangleComponent
|
|||||||
super.angle,
|
super.angle,
|
||||||
super.anchor,
|
super.anchor,
|
||||||
bool isSolid = false,
|
bool isSolid = false,
|
||||||
|
CollisionType collisionType = CollisionType.active,
|
||||||
}) : shouldFillParent = false,
|
}) : shouldFillParent = false,
|
||||||
super.relative(
|
super.relative(
|
||||||
shrinkToBounds: true,
|
shrinkToBounds: true,
|
||||||
) {
|
) {
|
||||||
this.isSolid = isSolid;
|
this.isSolid = isSolid;
|
||||||
|
this.collisionType = collisionType;
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|||||||
@ -12,7 +12,7 @@ class EnemyComponent extends SpriteAnimationComponent
|
|||||||
|
|
||||||
EnemyComponent(double x, double y)
|
EnemyComponent(double x, double y)
|
||||||
: super(position: Vector2(x, y), size: Vector2.all(25)) {
|
: super(position: Vector2(x, y), size: Vector2.all(25)) {
|
||||||
add(RectangleHitbox()..collisionType = CollisionType.passive);
|
add(RectangleHitbox(collisionType: CollisionType.passive));
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|||||||
Reference in New Issue
Block a user