Skip to content

Commit

Permalink
wip: delivery quest
Browse files Browse the repository at this point in the history
  • Loading branch information
hythl0day committed Jul 29, 2022
1 parent 62d0135 commit 5b7d62e
Show file tree
Hide file tree
Showing 46 changed files with 379 additions and 289 deletions.
24 changes: 15 additions & 9 deletions bin/build_game.dart → bin/debug_build_game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import 'dart:io';

import 'package:hetu_script/hetu_script.dart';
import 'package:hetu_script_dev_tools/hetu_script_dev_tools.dart';
import 'package:pub_semver/pub_semver.dart';

final version = '0.1.0-pre29';

const source1Path = 'game/main.ht';
const source2Path = 'story/main.ht';
const out1Path = 'packages/game/assets/game.mod';
const out2Path = 'packages/game/assets/story.mod';
const out1Path = 'packages/game/assets/mods/game.mod';
const out2Path = 'packages/game/assets/mods/story.mod';

final sourceContext =
HTFileSystemResourceContext(root: 'packages/game/scripts/');
Expand All @@ -15,12 +18,15 @@ final hetu = Hetu(
sourceContext: sourceContext,
);

void compileGameMod(String sourcePath, String outPath) {
void compileGameMod(String sourcePath, String outPath,
[String? versionString]) {
Version? modVersion;
if (versionString != null) {
modVersion = Version.parse(versionString);
}
final source = sourceContext.getResource(sourcePath);
print('started parsing ${source.fullName}');
final module = hetu.bundle(source);
print(
'${module.errors.length} syntactic error(s) occurred while parsing [game/main.ht].');
final module = hetu.bundle(source, version: modVersion);
if (module.errors.isNotEmpty) {
for (final err in module.errors) {
print(err);
Expand All @@ -32,11 +38,11 @@ void compileGameMod(String sourcePath, String outPath) {
outFile.createSync(recursive: true);
}
outFile.writeAsBytesSync(bytes);
print('successfully written to [$outPath].');
print('successfully compiled [$sourcePath] to [$outPath].');
}
}

void main() {
compileGameMod(source1Path, out1Path);
compileGameMod(source2Path, out2Path);
compileGameMod(source1Path, out1Path, version);
compileGameMod(source2Path, out2Path, version);
}
9 changes: 7 additions & 2 deletions build.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import os

os.system(
'dart run test/cli_test.dart')
version = '0.1.0-pre29'

dir_list = os.listdir('packages/game/scripts')

for dir_name in dir_list:
os.system(
f'hetu compile packages/game/scripts/{dir_name}/main.ht packages/game/assets/mods/{dir_name}.mod -v "{version}"')
Binary file removed packages/game/assets/game.mod
Binary file not shown.
Binary file added packages/game/assets/mods/game.mod
Binary file not shown.
Binary file added packages/game/assets/mods/story.mod
Binary file not shown.
Binary file removed packages/game/assets/story.mod
Binary file not shown.
4 changes: 4 additions & 0 deletions packages/game/lib/ui/dialog/game_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ class _GameDialogState extends State<GameDialog> {

_currentAvatar = _currentContent!['icon'];
_currentSay = _currentContent!['lines'][_currentSayIndex];
final displayName = _currentContent!['displayName'];
if (displayName != null) {
_currentSay = '$displayName: $_currentSay';
}
_timer = Timer.periodic(const Duration(milliseconds: 80), (timer) {
_letterCount++;
if (_letterCount > _currentSay.length) {
Expand Down
86 changes: 54 additions & 32 deletions packages/game/lib/ui/main_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,25 @@ class MainMenu extends StatefulWidget {
class _MainMenuState extends State<MainMenu> {
GameLocalization get locale => engine.locale;

final savedFiles = <SaveInfo>[];
// 模组信息,key是模组名字,value代表是否启用
final _modsInfo = <String, bool>{
'story': true,
};

final _savedFiles = <SaveInfo>[];

@override
void initState() {
super.initState();

engine.registerSceneConstructor('worldmap', ([dynamic args]) async {
// 因为生成世界时会触发一些mod的回调函数,因此需要先载入 mod 数据
for (final key in _modsInfo.keys) {
if (_modsInfo[key] == true) {
engine.invoke('load', moduleName: key);
}
}

HTStruct worldData;
final path = args!['path'];
if (path != null) {
Expand All @@ -58,6 +70,7 @@ class _MainMenuState extends State<MainMenu> {
} else {
worldData = engine.invoke('createWorldMap', namedArgs: args);
}

return WorldMapScene(worldData: worldData, controller: engine);
});

Expand All @@ -67,41 +80,50 @@ class _MainMenuState extends State<MainMenu> {
}

Future<void> refreshSaves() async {
savedFiles.clear();
savedFiles.addAll(await _getSavedFiles());
_savedFiles.clear();
_savedFiles.addAll(await _getSavedFiles());
}

Future<bool> _prepareData() async {
await refreshSaves();
if (engine.isLoaded) return false;
await engine.init(externalFunctions: externalGameFunctions);
// if (kDebugMode) {
// engine.loadModFromAssets(
// 'game/main.ht',
// moduleName: 'game',
// namedArgs: {'lang': 'zh', 'gameEngine': engine},
// isMainMod: true,
// );
// engine.loadModFromAssets(
// 'story/main.ht',
// moduleName: 'story',
// );
// } else {
final game = await rootBundle.load('assets/game.mod');
final gameBytes = game.buffer.asUint8List();
engine.loadModFromBytes(
gameBytes,
moduleName: 'game',
namedArgs: {'lang': 'zh', 'gameEngine': engine},
isMainMod: true,
);
final mod = await rootBundle.load('assets/story.mod');
final modBytes = mod.buffer.asUint8List();
engine.loadModFromBytes(
modBytes,
moduleName: 'story',
);
// }
if (kDebugMode) {
engine.loadModFromAssets(
'game/main.ht',
moduleName: 'game',
namedArgs: {'lang': 'zh', 'gameEngine': engine},
isMainMod: true,
);
for (final key in _modsInfo.keys) {
if (_modsInfo[key] == true) {
engine.loadModFromAssets(
'$key/main.ht',
moduleName: key,
);
}
}
} else {
final game = await rootBundle.load('assets/mods/game.mod');
final gameBytes = game.buffer.asUint8List();
engine.loadModFromBytes(
gameBytes,
moduleName: 'game',
namedArgs: {'lang': 'zh', 'gameEngine': engine},
isMainMod: true,
);
for (final key in _modsInfo.keys) {
if (_modsInfo[key] == true) {
final mod = await rootBundle.load('assets/mods/$key.mod');
final modBytes = mod.buffer.asUint8List();
engine.loadModFromBytes(
modBytes,
moduleName: key,
);
}
}
}

engine.invoke('build', positionalArgs: [context]);
engine.isLoaded = true;
return true;
Expand Down Expand Up @@ -156,7 +178,7 @@ class _MainMenuState extends State<MainMenu> {
padding: const EdgeInsets.only(top: 20.0),
child: ElevatedButton(
onPressed: () {
LoadGameDialog.show(context, list: savedFiles)
LoadGameDialog.show(context, list: _savedFiles)
.then((SaveInfo? info) {
if (info != null) {
showDialog(
Expand All @@ -173,7 +195,7 @@ class _MainMenuState extends State<MainMenu> {
setState(() {});
});
} else {
if (savedFiles.isEmpty) {
if (_savedFiles.isEmpty) {
setState(() {});
}
}
Expand Down
82 changes: 54 additions & 28 deletions packages/game/lib/ui/overlay/main_game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,36 +114,39 @@ class _MainGameOverlayState extends State<MainGameOverlay>
final e = event as MapInteractionEvent;
if (engine.isOnDesktop) {
if (e.buttons & kPrimaryButton == kPrimaryButton) {
_menuPosition = null;
final hero = _scene.map.hero;
if (hero == null) return;
if (hero.isMoving) return;
final terrain = _scene.map.selectedTerrain;
if (terrain == null) return;
List<int>? route;
if (terrain.tilePosition != hero.tilePosition) {
final start = engine.invoke('getTerrain',
positionalArgs: [hero.left, hero.top, _scene.worldData]);
final end = engine.invoke('getTerrain',
positionalArgs: [terrain.left, terrain.top, _scene.worldData]);
List? calculatedRoute = engine.invoke('calculateRoute',
positionalArgs: [start, end, _scene.worldData]);
if (calculatedRoute != null) {
route = List<int>.from(calculatedRoute);
if (_menuPosition != null) {
_menuPosition = null;
} else {
final hero = _scene.map.hero;
if (hero == null) return;
if (hero.isMoving) return;
final terrain = _scene.map.selectedTerrain;
if (terrain == null) return;
List<int>? route;
if (terrain.tilePosition != hero.tilePosition) {
final start = engine.invoke('getTerrain',
positionalArgs: [hero.left, hero.top, _scene.worldData]);
final end = engine.invoke('getTerrain',
positionalArgs: [terrain.left, terrain.top, _scene.worldData]);
List? calculatedRoute = engine.invoke('calculateRoute',
positionalArgs: [start, end, _scene.worldData]);
if (calculatedRoute != null) {
route = List<int>.from(calculatedRoute);
if (terrain.locationId != null) {
_scene.map.moveHeroToTilePositionByRoute(
route,
onDestinationCallback: () =>
_enterLocation(terrain.locationId!),
);
} else {
_scene.map.moveHeroToTilePositionByRoute(route);
}
}
} else {
if (terrain.locationId != null) {
_scene.map.moveHeroToTilePositionByRoute(
route,
onDestinationCallback: () =>
_enterLocation(terrain.locationId!),
);
} else {
_scene.map.moveHeroToTilePositionByRoute(route);
_enterLocation(terrain.locationId!);
}
}
} else {
if (terrain.locationId != null) {
_enterLocation(terrain.locationId!);
}
}
} else if (e.buttons & kSecondaryButton == kSecondaryButton) {
if (_scene.map.hero?.isMoving ?? false) return;
Expand Down Expand Up @@ -463,6 +466,29 @@ class _MainGameOverlayState extends State<MainGameOverlay>
});
}

final stringBuffer = StringBuffer();
stringBuffer.writeln(
'坐标: ${selectedTerrain.left}, ${selectedTerrain.top}');

final zoneData = engine.invoke('getZoneByIndex',
positionalArgs: [selectedTerrain.zoneIndex]);
final zoneName = zoneData['name'];
if (zoneName != null) {
stringBuffer.writeln(zoneName);
}

if (selectedTerrain.nationId != null) {
final nationData = engine.invoke('getNationById',
positionalArgs: [selectedTerrain.nationId]);
stringBuffer.writeln('${nationData['name']}');
}

if (selectedTerrain.locationId != null) {
final locationData = engine.invoke('getLocationById',
positionalArgs: [selectedTerrain.locationId]);
stringBuffer.writeln('${locationData['name']}');
}

screenWidgets.add(
WorldMapPopup(
left: _menuPosition!.x - WorldMapPopup.defaultSize / 2,
Expand Down Expand Up @@ -507,7 +533,7 @@ class _MainGameOverlayState extends State<MainGameOverlay>
onTalk: closePopup,
restIcon: isTappingHeroPosition,
onRest: closePopup,
// description: stringBuffer.toString(),
description: stringBuffer.toString(),
),
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/game/lib/ui/shared/loading_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ class LoadingScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Material(
color: Colors.black,
child: Container(
padding: const EdgeInsets.all(25.0),
alignment: Alignment.bottomRight,
color: Colors.black,
child: Text(
text,
style: const TextStyle(fontSize: 18.0),
Expand Down
Loading

0 comments on commit 5b7d62e

Please sign in to comment.