Files
raycaster/lib/main.dart
T
hans d1b99c2af2 Paint the map
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
2026-08-30 17:21:48 +02:00

197 lines
4.3 KiB
Dart

import "package:flutter/material.dart";
// dart format off
const List<int> map = [
1,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,1,
1,0,1,0,0,0,0,1,
1,0,1,0,0,0,0,1,
1,0,0,0,0,0,0,1,
1,0,0,0,0,2,0,1,
1,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,
];
// dart format on
const int mapX = 8;
const int mapY = 8;
const int mapSize = 32;
void main() {
runApp(const MainApp());
}
class MainApp extends StatelessWidget {
const MainApp({super.key});
@override
Widget build(BuildContext context) {
return Directionality(
textDirection: TextDirection.ltr,
child: LayoutBuilder(
builder: (context, constraints) => const Row(
spacing: mapSize / 1,
textDirection: TextDirection.ltr,
children: [
Flexible(child: Map()),
Flexible(child: Background()),
],
),
),
);
}
}
// * Map
class Map extends StatelessWidget {
const new({super.key});
@override
Widget build(BuildContext context) {
const double width = mapX * mapSize / 1;
const double height = mapY * mapSize / 1;
return const Center(
child: FittedBox(
fit: BoxFit.contain,
child: CustomPaint(
size: Size(width, height),
painter: MapPainter(
mapData: map,
mapX: mapX,
mapY: mapY,
tileSize: mapSize / 1,
),
),
),
);
}
}
class MapPainter extends CustomPainter {
final List<int> mapData;
final int mapX;
final int mapY;
final double tileSize;
const new({
required this.mapData,
required this.mapX,
required this.mapY,
required this.tileSize,
});
@override
void paint(Canvas canvas, Size size) {
final paintWhite = Paint()..color = Colors.white;
final paintBlack = Paint()..color = Colors.black;
for (int i = 0; i < mapData.length; i++) {
final double x = (i % mapX) * tileSize;
final double y = (i ~/ mapX) * tileSize;
canvas.drawRect(
Rect.fromLTWH(x, y, tileSize - 1, tileSize - 1),
mapData[i] == 0 ? paintBlack : paintWhite,
);
}
}
@override
bool shouldRepaint(covariant oldDelegate) => false;
@override
bool shouldRebuildSemantics(covariant oldDelegate) => false;
}
// * Background
class Background extends StatelessWidget {
const new({
super.key,
});
@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (context, constraints) => Stack(
textDirection: TextDirection.ltr,
children: [
CustomPaint(
size: constraints.biggest,
painter: Ceiling(),
),
CustomPaint(
size: constraints.biggest,
painter: Floor(),
),
],
),
);
}
}
class Ceiling extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final Rect rect = Offset.zero & size;
final double halfHeight = rect.height / 2;
final Rect topHalf = Rect.fromLTWH(
rect.left,
rect.top,
rect.width,
halfHeight,
);
const LinearGradient gradient = LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Color.fromRGBO(32, 32, 32, 1.0),
Color.fromRGBO(96, 96, 96, 1.0),
],
stops: [0.0, 1.0],
);
canvas.drawRect(
topHalf,
Paint()..shader = gradient.createShader(topHalf),
);
}
@override
bool shouldRepaint(covariant oldDelegate) => false;
@override
bool shouldRebuildSemantics(covariant oldDelegate) => false;
}
class Floor extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final Rect rect = Offset.zero & size;
final double halfHeight = rect.height / 2;
final Rect bottomHalf = Rect.fromLTWH(
rect.left,
halfHeight,
rect.width,
rect.bottom,
);
const LinearGradient gradient = LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Color.fromRGBO(32, 32, 0, 1.0),
Color.fromRGBO(96, 96, 0, 1.0),
],
stops: [0.0, 1.0],
);
canvas.drawRect(
bottomHalf,
Paint()..shader = gradient.createShader(bottomHalf),
);
}
@override
bool shouldRepaint(covariant oldDelegate) => false;
@override
bool shouldRebuildSemantics(covariant oldDelegate) => false;
}