feat: imported input-box pets, with dsh-Go's sprite packs
Nothing is built in: Settings → 宠物 shows "尚未导入宠物" until a zip is picked. The zip holds a dsh-Go pack.json and PNG sheets; it is validated (pack.json, every named sheet) and unpacked under the app's private files directory, with enable, size (40/52/64) and remove kept on the device. A pack's canvas, anchor, per-animation box and frame list drive rendering: PetSlot reserves the area above the baseline and stands the sprite on the composer's top edge (or on the activity cap while a turn runs). Idle, thinking, working and error follow the chat's state, and a tap plays the pack's react-left/react-right. Only one sheet is decoded at a time. path_provider's JNI binding does not load on this engine, so MainActivity exposes the files directory over a tiny method channel instead. dist/deepy-whale-pet.zip is the Deepy whale for testing. Tests cover the real pack manifest, frame/state mapping and zip import. 0.4.0 (400).
This commit is contained in:
1 parent
ff0dbf1b37
commit
d72af036a1
16 files changed
+1410
-12
No files matched your search
@@ -0,0 +1,213 @@
|
||||
// A pet package, in the shape dsh-Go's packs already use: `pack.json` points
|
||||
// at PNG sprite sheets, each frame cropped from a canvas of a few logical
|
||||
// pixels, and names the animations for each mood.
|
||||
import 'dart:ui';
|
||||
|
||||
/// One sheet frame in the play order, with how long it stays up.
|
||||
class PetFrame {
|
||||
final int index;
|
||||
final int ms;
|
||||
const PetFrame(this.index, this.ms);
|
||||
}
|
||||
|
||||
class PetAnimation {
|
||||
final String name;
|
||||
final String sheet;
|
||||
final int frameW;
|
||||
final int frameH;
|
||||
final int cols;
|
||||
final bool loop;
|
||||
|
||||
/// Where this animation sits on the pack's logical canvas.
|
||||
final Rect box;
|
||||
final List<PetFrame> frames;
|
||||
|
||||
const PetAnimation({
|
||||
required this.name,
|
||||
required this.sheet,
|
||||
required this.frameW,
|
||||
required this.frameH,
|
||||
required this.cols,
|
||||
required this.loop,
|
||||
required this.box,
|
||||
required this.frames,
|
||||
});
|
||||
|
||||
int get totalMs => frames.fold(0, (sum, f) => sum + f.ms);
|
||||
|
||||
factory PetAnimation.fromJson(String name, Map<String, dynamic> j) {
|
||||
final box = _rect(j['box'], const Rect.fromLTWH(0, 0, 1, 1));
|
||||
final cols = _int(j['cols'], 1).clamp(1, 4096);
|
||||
final total = _int(j['ms'], 50 * cols);
|
||||
final frames = <PetFrame>[];
|
||||
final raw = j['frames'];
|
||||
if (raw is List) {
|
||||
for (final e in raw) {
|
||||
if (e is! Map) continue;
|
||||
final ms = _int(e['ms'], 50);
|
||||
frames.add(PetFrame(_int(e['i'], 0), ms <= 0 ? 50 : ms));
|
||||
}
|
||||
}
|
||||
if (frames.isEmpty) {
|
||||
final each = (total / cols).round().clamp(16, 2000);
|
||||
for (var i = 0; i < cols; i++) {
|
||||
frames.add(PetFrame(i, each));
|
||||
}
|
||||
}
|
||||
return PetAnimation(
|
||||
name: name,
|
||||
sheet: j['sheet'] as String? ?? '',
|
||||
frameW: _int(j['frameW'], (box.width * 5).round()),
|
||||
frameH: _int(j['frameH'], (box.height * 5).round()),
|
||||
cols: cols,
|
||||
loop: j['loop'] != false,
|
||||
box: box,
|
||||
frames: frames,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// What a pack says. Only the parts this client draws are kept; reactions
|
||||
/// and idle pools are read as far as they can be played.
|
||||
class PetPack {
|
||||
final String id;
|
||||
final String name;
|
||||
final String author;
|
||||
final String license;
|
||||
final String source;
|
||||
final bool pixelArt;
|
||||
|
||||
/// The logical canvas the frames are placed on.
|
||||
final Size canvas;
|
||||
|
||||
/// Where the pet's feet rest on that canvas.
|
||||
final Offset anchor;
|
||||
final Rect contentBox;
|
||||
|
||||
final Map<String, PetAnimation> animations;
|
||||
final Map<String, List<String>> states;
|
||||
|
||||
/// tapLeft / tapRight / tickle / drag -> animation name.
|
||||
final Map<String, String> reactions;
|
||||
|
||||
const PetPack({
|
||||
required this.id,
|
||||
required this.name,
|
||||
required this.author,
|
||||
required this.license,
|
||||
required this.source,
|
||||
required this.pixelArt,
|
||||
required this.canvas,
|
||||
required this.anchor,
|
||||
required this.contentBox,
|
||||
required this.animations,
|
||||
required this.states,
|
||||
required this.reactions,
|
||||
});
|
||||
|
||||
bool get isValid =>
|
||||
animations.isNotEmpty && states.values.any((list) => list.any(animations.containsKey));
|
||||
|
||||
String? get idleName {
|
||||
for (final name in states['idle'] ?? const <String>[]) {
|
||||
if (animations.containsKey(name)) return name;
|
||||
}
|
||||
return animations.keys.isEmpty ? null : animations.keys.first;
|
||||
}
|
||||
|
||||
/// The first animation of [state] the pack actually has, then idle.
|
||||
String? animationForState(String state) {
|
||||
for (final name in states[state] ?? const <String>[]) {
|
||||
if (animations.containsKey(name)) return name;
|
||||
}
|
||||
return idleName;
|
||||
}
|
||||
|
||||
PetAnimation? animation(String? name) =>
|
||||
name == null ? null : animations[name];
|
||||
|
||||
factory PetPack.fromJson(Map<String, dynamic> j) {
|
||||
final canvasJson = j['canvas'];
|
||||
final cw = _double(canvasJson is Map ? canvasJson['w'] : null, 52);
|
||||
final ch = _double(canvasJson is Map ? canvasJson['h'] : null, cw);
|
||||
final anchorJson = j['anchor'];
|
||||
final ax = _double(anchorJson is Map ? anchorJson['x'] : null, cw / 2);
|
||||
final ay = _double(anchorJson is Map ? anchorJson['baseline'] : null, ch);
|
||||
|
||||
final animations = <String, PetAnimation>{};
|
||||
final rawAnims = j['animations'];
|
||||
if (rawAnims is Map) {
|
||||
for (final e in rawAnims.entries) {
|
||||
final value = e.value;
|
||||
if (e.key is String && value is Map) {
|
||||
animations[e.key as String] =
|
||||
PetAnimation.fromJson(e.key as String, Map<String, dynamic>.from(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final states = <String, List<String>>{};
|
||||
final rawStates = j['states'];
|
||||
if (rawStates is Map) {
|
||||
for (final e in rawStates.entries) {
|
||||
final list = e.value;
|
||||
if (e.key is String && list is List) {
|
||||
states[e.key as String] = [
|
||||
for (final v in list)
|
||||
if (v is String) v,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final reactions = <String, String>{};
|
||||
final rawReactions = j['reactions'];
|
||||
if (rawReactions is Map) {
|
||||
for (final key in const ['tapLeft', 'tapRight', 'tickle', 'drag']) {
|
||||
final v = rawReactions[key];
|
||||
if (v is String) {
|
||||
reactions[key] = v;
|
||||
} else if (v is Map) {
|
||||
final file = v['file'];
|
||||
final files = v['files'];
|
||||
if (file is String) {
|
||||
reactions[key] = file;
|
||||
} else if (files is List && files.isNotEmpty && files.first is String) {
|
||||
reactions[key] = files.first as String;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return PetPack(
|
||||
id: j['id'] as String? ?? '',
|
||||
name: j['name'] as String? ?? '',
|
||||
author: j['author'] as String? ?? '',
|
||||
license: j['license'] as String? ?? '',
|
||||
source: j['source'] as String? ?? '',
|
||||
pixelArt: j['pixelArt'] != false,
|
||||
canvas: Size(cw, ch),
|
||||
anchor: Offset(ax, ay),
|
||||
contentBox: _rect(j['contentBox'], Rect.fromLTWH(0, 0, cw, ch)),
|
||||
animations: animations,
|
||||
states: states,
|
||||
reactions: reactions,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
int _int(Object? v, int fallback) =>
|
||||
v is num ? v.toInt() : (v is String ? int.tryParse(v) ?? fallback : fallback);
|
||||
|
||||
double _double(Object? v, double fallback) =>
|
||||
v is num ? v.toDouble() : (v is String ? double.tryParse(v) ?? fallback : fallback);
|
||||
|
||||
Rect _rect(Object? v, Rect fallback) {
|
||||
if (v is! Map) return fallback;
|
||||
return Rect.fromLTWH(
|
||||
_double(v['x'], fallback.left),
|
||||
_double(v['y'], fallback.top),
|
||||
_double(v['w'], fallback.width),
|
||||
_double(v['h'], fallback.height),
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user