-
Notifications
You must be signed in to change notification settings - Fork 9
/
mini_map_renderer.dart
88 lines (74 loc) · 2.22 KB
/
mini_map_renderer.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
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter_3d_raycast_engine/configurations.dart';
import 'package:flutter_3d_raycast_engine/map_information.dart';
import 'package:flutter_3d_raycast_engine/player.dart';
import 'package:flutter_3d_raycast_engine/vector.dart';
class MiniMapRenderer extends CustomPainter {
const MiniMapRenderer({
required this.player,
required this.map,
});
final Player player;
final List<MapInformation> map;
@override
void paint(Canvas canvas, Size size) {
player.castRayWall().rays.forEach((ray) => ray.draw(canvas));
_drawPlayer(canvas);
_drawMap(canvas);
}
void _drawMap(Canvas canvas) {
canvas.drawRRect(
RRect.fromLTRBR(
-margin,
-margin,
mapRange + margin,
mapRange + margin,
const Radius.circular(4),
),
Paint()..color = Colors.black.withOpacity(0.5),
);
final size = map.length ~/ mapSize;
for (var row = 0; row < size; row++) {
for (var column = 0; column < size; column++) {
canvas
..drawRect(
Rect.fromLTWH(
column * mapScale,
row * mapScale,
mapScale,
mapScale,
),
Paint()
..color =
materials[map[row * mapSize + column].materialIndex].color,
)
..drawCircle(
Offset(
column * mapScale + mapScale / 2,
row * mapScale + mapScale / 2,
),
mapScale / 3,
Paint()
..color =
materials[map[row * mapSize + column].spriteIndex].color,
);
}
}
}
void _drawPlayer(Canvas canvas) {
final position = player.position.toOffset;
final angle = player.angle;
final paint = Paint()
..color = Colors.red
..strokeWidth = playerRadius;
canvas.drawCircle(position, mapScale / 2, paint);
final playerDirection = Vector(
x: position.dx + sin(angle) * mapScale,
y: position.dy + cos(angle) * mapScale,
);
canvas.drawLine(position, playerDirection.toOffset, paint);
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) => true;
}