-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.dart
120 lines (101 loc) · 2.57 KB
/
main.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
import 'dart:async';
import 'dart:math';
import 'package:flame/components.dart';
import 'package:flame/effects.dart';
import 'package:flame/game.dart';
import 'package:flame/input.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
final _random = Random();
class BuzzComponent extends RectangleComponent with HasGameRef<MyGame> {
BuzzComponent({
required this.target,
}) : super(
priority: 200,
size: Vector2.all(10),
anchor: Anchor.center,
paint: Paint()..color = Colors.red,
);
final PositionComponent target;
bool _beingRemoved = false;
@override
void update(double dt) {
super.update(dt);
if (!gameRef.buzzActive || target.isRemoved) {
removeFromParent();
_beingRemoved = true;
}
}
@override
FutureOr<void> onLoad() async {
await super.onLoad();
position = target.position.clone() +
Vector2(
_random.nextDouble() * target.size.x,
_random.nextDouble() * target.size.y,
);
_animate();
}
void _animate() {
//if (_beingRemoved) {
// return;
//}
final animateTo = target.position.clone() +
Vector2(
_random.nextDouble() * target.size.x,
_random.nextDouble() * target.size.y,
);
add(
MoveEffect.to(
animateTo,
LinearEffectController(.08),
onComplete: () => _animate(),
),
);
}
}
class MyGame extends FlameGame with HasKeyboardHandlerComponents {
late final RectangleComponent target;
bool buzzActive = false;
@override
FutureOr<void> onLoad() async {
await super.onLoad();
camera = CameraComponent.withFixedResolution(width: 256, height: 240);
world.add(
target = RectangleComponent(
anchor: Anchor.center,
size: Vector2.all(50),
paint: Paint()..color = Colors.white,
),
);
add(
KeyboardListenerComponent(
keyDown: {
LogicalKeyboardKey.space: (_) {
return false;
},
},
keyUp: {
LogicalKeyboardKey.space: (_) {
buzzActive = true;
for (var i = 0; i < 200; i++) {
world.add(BuzzComponent(target: target));
}
add(
TimerComponent(
period: 1,
onTick: () {
buzzActive = false;
},
),
);
return false;
},
},
),
);
}
}
void main() {
runApp(GameWidget.controlled(gameFactory: MyGame.new));
}