mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-02 20:13:50 +08:00
Inheritance example for contact callbacks
This commit is contained in:
@ -17,6 +17,7 @@ void main() async {
|
|||||||
|
|
||||||
class Ball extends BodyComponent {
|
class Ball extends BodyComponent {
|
||||||
Paint originalPaint, currentPaint;
|
Paint originalPaint, currentPaint;
|
||||||
|
bool giveNudge = false;
|
||||||
|
|
||||||
Ball(Vector2 position, Box2DComponent box) : super(box) {
|
Ball(Vector2 position, Box2DComponent box) : super(box) {
|
||||||
originalPaint = _randomPaint();
|
originalPaint = _randomPaint();
|
||||||
@ -66,16 +67,30 @@ class Ball extends BodyComponent {
|
|||||||
Offset(math.sin(angle) * radius, math.cos(angle) * radius);
|
Offset(math.sin(angle) * radius, math.cos(angle) * radius);
|
||||||
c.drawLine(p, p + lineRotation, blue);
|
c.drawLine(p, p + lineRotation, blue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void update(double t) {
|
||||||
|
super.update(t);
|
||||||
|
if (giveNudge) {
|
||||||
|
body.applyLinearImpulse(Vector2(0, 10000), body.getLocalCenter(), true);
|
||||||
|
giveNudge = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class BallContactCallback implements ContactCallback<Ball, Ball> {
|
class WhiteBall extends Ball {
|
||||||
@override
|
WhiteBall(Vector2 position, Box2DComponent box) : super(position, box) {
|
||||||
ContactTypes types = ContactTypes(Ball, Ball);
|
originalPaint = BasicPalette.white.paint;
|
||||||
|
currentPaint = originalPaint;
|
||||||
BallContactCallback();
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class BallContactCallback extends ContactCallback<Ball, Ball> {
|
||||||
@override
|
@override
|
||||||
void begin(Ball ball1, Ball ball2, Contact contact) {
|
void begin(Ball ball1, Ball ball2, Contact contact) {
|
||||||
|
if (ball1 is WhiteBall || ball2 is WhiteBall) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (ball1.currentPaint != ball1.originalPaint) {
|
if (ball1.currentPaint != ball1.originalPaint) {
|
||||||
ball1.currentPaint = ball2.currentPaint;
|
ball1.currentPaint = ball2.currentPaint;
|
||||||
} else {
|
} else {
|
||||||
@ -87,11 +102,33 @@ class BallContactCallback implements ContactCallback<Ball, Ball> {
|
|||||||
void end(Ball ball1, Ball ball2, Contact contact) {}
|
void end(Ball ball1, Ball ball2, Contact contact) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class WhiteBallContactCallback extends ContactCallback<Ball, WhiteBall> {
|
||||||
|
@override
|
||||||
|
void begin(Ball ball, WhiteBall whiteBall, Contact contact) {
|
||||||
|
ball.giveNudge = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void end(Ball ball, WhiteBall whiteBall, Contact contact) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
class BallWallContactCallback extends ContactCallback<Ball, Wall> {
|
||||||
|
@override
|
||||||
|
void begin(Ball ball, Wall wall, Contact contact) {
|
||||||
|
wall.paint = ball.currentPaint;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void end(Ball ball1, Wall wall, Contact contact) {}
|
||||||
|
}
|
||||||
|
|
||||||
class MyGame extends Box2DGame {
|
class MyGame extends Box2DGame {
|
||||||
MyGame(Box2DComponent box) : super(box) {
|
MyGame(Box2DComponent box) : super(box) {
|
||||||
final List<BodyComponent> boundaries = createBoundaries(box);
|
final List<BodyComponent> boundaries = createBoundaries(box);
|
||||||
boundaries.forEach(add);
|
boundaries.forEach(add);
|
||||||
addContactCallback(BallContactCallback());
|
addContactCallback(BallContactCallback());
|
||||||
|
addContactCallback(BallWallContactCallback());
|
||||||
|
addContactCallback(WhiteBallContactCallback());
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@ -99,7 +136,11 @@ class MyGame extends Box2DGame {
|
|||||||
super.onTapDown(details);
|
super.onTapDown(details);
|
||||||
final Vector2 position =
|
final Vector2 position =
|
||||||
Vector2(details.globalPosition.dx, details.globalPosition.dy);
|
Vector2(details.globalPosition.dx, details.globalPosition.dy);
|
||||||
add(Ball(position, box));
|
if (math.Random().nextInt(10) < 2) {
|
||||||
|
add(WhiteBall(position, box));
|
||||||
|
} else {
|
||||||
|
add(Ball(position, box));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user