summaryrefslogtreecommitdiff
path: root/lib/serializer.dart
blob: d9d5dbe808b23501690f50459e8cbfa3373fe7fe (plain)
import 'dart:convert';

import 'package:alligator_editor/project.dart';

const currentAlligatorVersion = "0.1";
const currentRuntimeVersion = "0.1";

class AlligatorProject {
  String version;
  ProjectConfig projectConfig;

  AlligatorProject({
    this.version = currentAlligatorVersion,
    required this.projectConfig,
  });

  factory AlligatorProject.fromJson(String json) {
    Map<String, dynamic> jsonMap = jsonDecode(json);
    String version = jsonMap['version'];
    ProjectConfig projectConfig = ProjectConfig(
      gameName: jsonMap['project']['name'],
      defaultWindowWidth: jsonMap['project']['defaultWidth'],
      defaultWindowHeight: jsonMap['project']['defaultHeight'],
      defaultWindowMode: WindowMode.fromPascalCase(jsonMap['project']['defaultWindowMode']),
      defaultVsync: jsonMap['project']['vsync'],
    );

    return AlligatorProject(version: version, projectConfig: projectConfig);
  }

  String toJson() {
    Map<String, dynamic> json = {
      'version': this.version,
      'project': {
        'name': this.projectConfig.gameName,
        'defaultWidth': this.projectConfig.defaultWindowWidth,
        'defaultHeight': this.projectConfig.defaultWindowHeight,
        'defaultWindowMode': this.projectConfig.defaultWindowMode,
        'vsync': this.projectConfig.defaultVsync,
      }
    };

    return jsonEncode(json);
  }
}

class AlligatorGame {
  final String alligatorVersion;
  final int? defaultWindowWidth;
  final int? defaultWindowHeight;
  final WindowMode defaultWindowMode;
  final String windowTitle;
  final bool vsync;

  const AlligatorGame({
    this.alligatorVersion = currentRuntimeVersion,
    this.defaultWindowWidth = 1280,
    this.defaultWindowHeight = 720,
    this.defaultWindowMode = WindowMode.windowed,
    this.windowTitle = "Alligator Game",
    this.vsync = true,
  });

  AlligatorGame.fromConfig({
    String alligatorVersion = currentRuntimeVersion,
    required ProjectConfig projectConfig,
  }) : this(
          alligatorVersion: alligatorVersion,
          defaultWindowWidth: projectConfig.defaultWindowWidth,
          defaultWindowHeight: projectConfig.defaultWindowHeight,
          defaultWindowMode: projectConfig.defaultWindowMode,
          windowTitle: projectConfig.gameName,
          vsync: projectConfig.defaultVsync,
        );

  String toJson() {
    Map<String, dynamic> json = {
      'alligator_version': this.alligatorVersion,
      'scenes': {},
      'textures': {},
      'scripts': {},
      'default_scene': "",
      'sprite_manager_capacity': 0,
      'default_window_width': this.defaultWindowWidth,
      'default_window_height': this.defaultWindowHeight,
      'default_window_mode': this.defaultWindowMode.pascalCase,
      'window_title': this.windowTitle,
      'vsync': this.vsync
    };

    return jsonEncode(json);
  }
}