Files
logic-puzzle/lib/domain/book.dart
T

127 lines
3.5 KiB
Dart

import 'dart:convert';
import 'package:flutter/services.dart';
import 'puzzle.dart';
class CampaignLevel {
CampaignLevel(Map<String, dynamic> data)
: chapter = data['chapter'],
title = data['title'],
object = data['object'],
before = data['before'],
after = data['after'],
illustrationAsset = data['illustrationAsset'] as String?,
picture = (data['picture'] as String).split('').map(int.parse).toList(),
clues = data['clues'],
areas = data['areas'];
final String chapter, title, object, before, after, clues, areas;
final String? illustrationAsset;
final List<int> picture;
Puzzle puzzle() {
final p = Puzzle(7, picture);
if (clues.isNotEmpty) {
for (final c in clues.split(',')) {
p.given[int.parse(c)] = true;
}
}
if (areas.isNotEmpty) {
var i = 0;
for (final a in areas.split(';')) {
final v = a.split(',').map(int.parse).toList();
p.regions.add(
Region(String.fromCharCode(65 + i++), v[0], v[1], v[2], v[3]),
);
}
}
return p;
}
}
enum StoryPageType {
dedication,
entry,
puzzle,
memory,
note,
inspection,
closing,
}
class StoryPage {
const StoryPage(
this.id,
this.title,
this.text, {
this.type = StoryPageType.entry,
this.campaignIndex,
this.speaker = '',
this.illustrationAsset,
});
final String id, title, text;
final StoryPageType type;
final int? campaignIndex;
final String speaker;
final String? illustrationAsset;
}
class Book {
Book(this.premise, this.levels, this.pages, this.tracks);
final String premise;
final List<CampaignLevel> levels;
final List<StoryPage> pages;
final Map<String, dynamic> tracks;
int pageFor(int campaignIndex, StoryPageType type) => pages.indexWhere(
(p) => p.campaignIndex == campaignIndex && p.type == type,
);
static Future<Book> load() async {
final campaign = jsonDecode(
await rootBundle.loadString('assets/campaign.json'),
) as Map<String, dynamic>;
final scripts = jsonDecode(
await rootBundle.loadString('assets/narration/scripts.json'),
) as List;
final tracks =
jsonDecode(
await rootBundle.loadString(
'assets/audio/narration_manifest.json',
),
)['tracks']
as Map<String, dynamic>;
final levels = (campaign['levels'] as List)
.map((e) => CampaignLevel(e))
.toList();
final notebook = jsonDecode(
await rootBundle.loadString('assets/notebook.json'),
) as Map<String, dynamic>;
return Book(
campaign['premise'],
levels,
(notebook['pages'] as List).map((record) {
final index = record['campaignIndex'] as int?;
final text = switch (record['textSource']) {
'before' => levels[index!].before,
'after' => levels[index!].after,
'recording' =>
scripts
.where((s) => s['pageId'] == record['id'])
.map((s) => s['text'] as String)
.firstOrNull ??
'',
_ => record['text'] as String? ?? '',
};
return StoryPage(
record['id'],
record['title'],
text,
type: StoryPageType.values.byName(record['type']),
campaignIndex: index,
speaker: record['speaker'] as String? ?? '',
illustrationAsset: record['illustrationAsset'] as String?,
);
}).toList(),
tracks,
);
}
}