-
-
Notifications
You must be signed in to change notification settings - Fork 871
/
raycast_max_distance_example.dart
130 lines (114 loc) · 2.99 KB
/
raycast_max_distance_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
import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
import 'package:flame/effects.dart';
import 'package:flame/extensions.dart';
import 'package:flame/game.dart';
import 'package:flame/geometry.dart';
import 'package:flame/palette.dart';
import 'package:flutter/material.dart';
class RaycastMaxDistanceExample extends FlameGame with HasCollisionDetection {
static const description = '''
This examples showcases how raycast APIs can be used to detect hits within certain range.
''';
static const _maxDistance = 50.0;
late Ray2 _ray;
late _Character _character;
final _result = RaycastResult<ShapeHitbox>();
final _text = TextComponent(
text: "Hey! Who's there?",
anchor: Anchor.center,
textRenderer: TextPaint(
style: const TextStyle(
fontSize: 8,
color: Colors.amber,
),
),
)..positionType = PositionType.viewport;
@override
void onLoad() {
camera.viewport = FixedResolutionViewport(Vector2(320, 180));
_addMovingWall();
add(
_character = _Character(
maxDistance: _maxDistance,
position: size / 2 - Vector2(50, 0),
anchor: Anchor.center,
),
);
_text.position = _character.position - Vector2(0, 50);
_ray = Ray2(
origin: _character.absolutePosition,
direction: Vector2(1, 0),
);
}
void _addMovingWall() {
add(
RectangleComponent(
position: size / 2,
size: Vector2(20, 40),
anchor: Anchor.center,
paint: BasicPalette.red.paint(),
children: [
RectangleHitbox(),
MoveByEffect(
Vector2(50, 0),
EffectController(
duration: 2,
alternate: true,
infinite: true,
),
),
],
),
);
}
@override
void update(double dt) {
collisionDetection.raycast(_ray, maxDistance: _maxDistance, out: _result);
if (_result.isActive) {
if (!camera.shaking) {
camera.shake(duration: 0.2, intensity: 1);
}
if (!_text.isMounted) {
add(_text);
}
} else {
_text.removeFromParent();
}
super.update(dt);
}
}
class _Character extends PositionComponent {
_Character({required this.maxDistance, super.position, super.anchor});
final double maxDistance;
final _rayOriginPoint = Offset.zero;
late final _rayEndPoint = Offset(maxDistance, 0);
final _rayPaint = BasicPalette.gray.paint();
@override
Future<void>? onLoad() async {
add(
CircleComponent(
radius: 20,
anchor: Anchor.center,
paint: BasicPalette.green.paint(),
)..scale = Vector2(0.55, 1),
);
add(
CircleComponent(
radius: 10,
anchor: Anchor.center,
paint: _rayPaint,
),
);
add(
RectangleComponent(
size: Vector2(10, 3),
position: Vector2(12, 5),
),
);
}
@override
void render(Canvas canvas) {
canvas.drawLine(_rayOriginPoint, _rayEndPoint, _rayPaint);
}
}