-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.dart
115 lines (100 loc) · 3.44 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
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_3d_raycast_engine/asset.dart';
import 'package:flutter_3d_raycast_engine/configurations.dart';
import 'package:flutter_3d_raycast_engine/controller.dart';
import 'package:flutter_3d_raycast_engine/helpers.dart';
import 'package:flutter_3d_raycast_engine/map_editor.dart';
import 'package:flutter_3d_raycast_engine/mini_map_renderer.dart';
import 'package:flutter_3d_raycast_engine/player.dart';
import 'package:flutter_3d_raycast_engine/renderer.dart';
import 'package:flutter_3d_raycast_engine/sprite_manager.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
sprites.addAll(await Asset.loadSprites());
materials.addAll(await Asset.loadMaterials());
map.addAll(await loadMap());
runApp(const MainApp());
}
class MainApp extends StatefulWidget {
const MainApp({super.key});
@override
State<MainApp> createState() => _MainAppState();
}
class _MainAppState extends State<MainApp> {
final Player player = Player(Controller());
final SpriteManager spriteManager = SpriteManager();
bool showMiniMap = true;
bool enableTexture = true;
@override
void initState() {
super.initState();
Timer.periodic(
const Duration(milliseconds: cycleDelay),
(_) => _gameLoop(),
);
}
@override
Widget build(BuildContext context) => KeyboardListener(
focusNode: FocusNode(),
onKeyEvent: (event) {
player.handleKeyEvent(event);
if (event.logicalKey == LogicalKeyboardKey.digit1 &&
event is KeyDownEvent) {
showMiniMap = !showMiniMap;
}
if (event.logicalKey == LogicalKeyboardKey.digit2 &&
event is KeyDownEvent) {
enableTexture = !enableTexture;
}
},
child: MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: SingleChildScrollView(
child: Stack(
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.all(margin),
child: ClipRect(
child: CustomPaint(
painter: Renderer(
player: player,
spriteManager: spriteManager,
enableTexture: enableTexture,
),
size: screenSize,
),
),
),
const Padding(
padding: EdgeInsets.all(margin),
child: MapEditor(),
),
],
),
if (showMiniMap)
Align(
alignment: Alignment.topLeft,
child: Padding(
padding: const EdgeInsets.all(margin * 4),
child: CustomPaint(
painter: MiniMapRenderer(player: player, map: map),
),
),
),
],
),
),
),
),
);
void _gameLoop() {
player.update();
setState(() {});
}
}