/
multiple_shapes_example.dart
292 lines (266 loc) · 7.7 KB
/
multiple_shapes_example.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import 'dart:math';
import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
import 'package:flame/experimental.dart';
import 'package:flame/extensions.dart';
import 'package:flame/game.dart';
import 'package:flame/palette.dart';
import 'package:flutter/material.dart' hide Image, Draggable;
enum Shapes { circle, rectangle, polygon }
class MultipleShapesExample extends FlameGame with HasCollisionDetection {
static const description = '''
An example with many hitboxes that move around on the screen and during
collisions they change color depending on what it is that they have collided
with.
The snowman, the component built with three circles on top of each other,
works a little bit differently than the other components to show that you
can have multiple hitboxes within one component.
On this example, you can "throw" the components by dragging them quickly in
any direction.
''';
@override
Future<void> onLoad() async {
add(FpsTextComponent(position: Vector2(0, size.y - 24)));
final screenHitbox = ScreenHitbox();
final snowman = CollidableSnowman(
Vector2.all(150),
Vector2(120, 250),
Vector2(-100, 100),
screenHitbox,
);
MyCollidable lastToAdd = snowman;
add(screenHitbox);
add(snowman);
var totalAdded = 1;
while (totalAdded < 1000) {
lastToAdd = nextRandomCollidable(lastToAdd, screenHitbox);
final lastBottomRight = lastToAdd.toAbsoluteRect().bottomRight;
if (lastBottomRight.dx < size.x && lastBottomRight.dy < size.y) {
add(lastToAdd);
totalAdded++;
} else {
break;
}
}
}
final _rng = Random();
final _distance = Vector2(100, 0);
MyCollidable nextRandomCollidable(
MyCollidable lastCollidable,
ScreenHitbox screenHitbox,
) {
final collidableSize = Vector2.all(50) + Vector2.random(_rng) * 100;
final isXOverflow = lastCollidable.position.x +
lastCollidable.size.x / 2 +
_distance.x +
collidableSize.x >
size.x;
var position = _distance + Vector2(0, lastCollidable.position.y + 200);
if (!isXOverflow) {
position = (lastCollidable.position + _distance)
..x += collidableSize.x / 2;
}
final velocity = (Vector2.random(_rng) - Vector2.random(_rng)) * 400;
return randomCollidable(
position,
collidableSize,
velocity,
screenHitbox,
rng: _rng,
);
}
}
abstract class MyCollidable extends PositionComponent
with DragCallbacks, CollisionCallbacks, GestureHitboxes {
double rotationSpeed = 0.0;
final Vector2 velocity;
final delta = Vector2.zero();
double angleDelta = 0;
final Color _defaultColor = Colors.blue.withOpacity(0.8);
final Color _collisionColor = Colors.green.withOpacity(0.8);
late final Paint _dragIndicatorPaint;
final ScreenHitbox screenHitbox;
ShapeHitbox? hitbox;
MyCollidable(
Vector2 position,
Vector2 size,
this.velocity,
this.screenHitbox,
) : super(position: position, size: size, anchor: Anchor.center) {
_dragIndicatorPaint = BasicPalette.white.paint();
}
@override
void onMount() {
hitbox?.paint.color = _defaultColor;
super.onMount();
}
@override
void update(double dt) {
super.update(dt);
if (isDragged) {
return;
}
delta.setFrom(velocity * dt);
position.add(delta);
angleDelta = dt * rotationSpeed;
angle = (angle + angleDelta) % (2 * pi);
// Takes rotation into consideration (which topLeftPosition doesn't)
final topLeft = absoluteCenter - (scaledSize / 2);
if (topLeft.x + scaledSize.x < 0 ||
topLeft.y + scaledSize.y < 0 ||
topLeft.x > screenHitbox.scaledSize.x ||
topLeft.y > screenHitbox.scaledSize.y) {
final moduloSize = screenHitbox.scaledSize + scaledSize;
topLeftPosition = topLeftPosition % moduloSize;
}
}
@override
void render(Canvas canvas) {
if (isDragged) {
final localCenter = scaledSize.toOffset() / 2;
canvas.drawCircle(localCenter, 5, _dragIndicatorPaint);
}
}
@override
void onCollisionStart(
Set<Vector2> intersectionPoints,
PositionComponent other,
) {
super.onCollisionStart(intersectionPoints, other);
hitbox?.paint.color = _collisionColor;
}
@override
void onCollisionEnd(PositionComponent other) {
super.onCollisionEnd(other);
if (!isColliding) {
hitbox?.paint.color = _defaultColor;
}
}
@override
void onDragEnd(DragEndEvent event) {
super.onDragEnd(event);
velocity.setFrom(event.velocity / 10);
}
}
class CollidablePolygon extends MyCollidable {
CollidablePolygon(
Vector2 position,
Vector2 size,
Vector2 velocity,
ScreenHitbox screenHitbox,
) : super(position, size, velocity, screenHitbox) {
hitbox = PolygonHitbox.relative(
[
Vector2(-1.0, 0.0),
Vector2(-0.8, 0.6),
Vector2(0.0, 1.0),
Vector2(0.6, 0.9),
Vector2(1.0, 0.0),
Vector2(0.6, -0.8),
Vector2(0, -1.0),
Vector2(-0.8, -0.8),
],
parentSize: size,
)..renderShape = true;
add(hitbox!);
}
}
class CollidableRectangle extends MyCollidable {
CollidableRectangle(
super.position,
super.size,
super.velocity,
super.screenHitbox,
) {
hitbox = RectangleHitbox()..renderShape = true;
add(hitbox!);
}
}
class CollidableCircle extends MyCollidable {
CollidableCircle(
super.position,
super.size,
super.velocity,
super.screenHitbox,
) {
hitbox = CircleHitbox()..renderShape = true;
add(hitbox!);
}
}
class SnowmanPart extends CircleHitbox {
@override
final renderShape = true;
final startColor = Colors.white.withOpacity(0.8);
final Color hitColor;
SnowmanPart(double radius, Vector2 position, this.hitColor)
: super(radius: radius, position: position, anchor: Anchor.center) {
paint.color = startColor;
}
@override
void onCollisionStart(Set<Vector2> intersectionPoints, ShapeHitbox other) {
super.onCollisionStart(intersectionPoints, other);
if (other.hitboxParent is ScreenHitbox) {
paint.color = startColor;
} else {
paint.color = hitColor.withOpacity(0.8);
}
}
@override
void onCollisionEnd(ShapeHitbox other) {
super.onCollisionEnd(other);
if (!isColliding) {
paint.color = startColor;
}
}
}
class CollidableSnowman extends MyCollidable {
CollidableSnowman(
Vector2 position,
Vector2 size,
Vector2 velocity,
ScreenHitbox screenHitbox,
) : super(position, size, velocity, screenHitbox) {
rotationSpeed = 0.3;
anchor = Anchor.topLeft;
final top = SnowmanPart(
size.x * 0.3,
Vector2(size.x / 2, size.y * 0.15),
Colors.red,
);
final middle = SnowmanPart(
size.x * 0.4,
Vector2(size.x / 2, size.y * 0.40),
Colors.yellow,
);
final bottom = SnowmanPart(
size.x / 2,
Vector2(size.x / 2, size.y - size.y / 4),
Colors.green,
);
add(bottom);
add(middle);
add(top);
}
}
MyCollidable randomCollidable(
Vector2 position,
Vector2 size,
Vector2 velocity,
ScreenHitbox screenHitbox, {
Random? rng,
}) {
final _rng = rng ?? Random();
final rotationSpeed = 0.5 - _rng.nextDouble();
final shapeType = Shapes.values[_rng.nextInt(Shapes.values.length)];
switch (shapeType) {
case Shapes.circle:
return CollidableCircle(position, size, velocity, screenHitbox)
..rotationSpeed = rotationSpeed;
case Shapes.rectangle:
return CollidableRectangle(position, size, velocity, screenHitbox)
..rotationSpeed = rotationSpeed;
case Shapes.polygon:
return CollidablePolygon(position, size, velocity, screenHitbox)
..rotationSpeed = rotationSpeed;
}
}