1 /**
2  * Retrograde Engine
3  *
4  * Authors:
5  *  Mike Bierlee, m.bierlee@lostmoment.com
6  * Copyright: 2014-2021 Mike Bierlee
7  * License:
8  *  This software is licensed under the terms of the MIT license.
9  *  The full terms of the license can be found in the LICENSE.txt file.
10  */
11 
12 module retrograde.pipeline.rsa;
13 
14 import retrograde.graphics.core;
15 import retrograde.file;
16 
17 import std.json;
18 
19 class RetrogradeSpritesheetAnimationReader {
20     private static const initialAnimationKey = "initialAnimation";
21 
22     public SpritesheetAnimation readSpritesheetAnimation(File file) {
23         auto jsonText = file.readAsText();
24         auto spritesheetAnimationJson = parseJSON(jsonText);
25         return composeSpritesheetAnimation(spritesheetAnimationJson);
26     }
27 
28     private SpritesheetAnimation composeSpritesheetAnimation(JSONValue spritesheetAnimationJson) {
29         auto spritesheetAnimation = new SpritesheetAnimation();
30         spritesheetAnimation.framesPerSecond = spritesheetAnimationJson["framesPerSecond"].integer;
31         spritesheetAnimation.spritesheets = composeSpritesheets(spritesheetAnimationJson["spritesheets"].array);
32         spritesheetAnimation.animations = composeAnimations(spritesheetAnimationJson["animations"].array, spritesheetAnimation.spritesheets);
33         spritesheetAnimation.initialAnimation = spritesheetAnimation.animations.values[0];
34         if (initialAnimationKey in spritesheetAnimationJson) {
35             auto initialAnimationName = spritesheetAnimationJson[initialAnimationKey].str;
36             spritesheetAnimation.initialAnimation = spritesheetAnimation.animations[initialAnimationName];
37         }
38 
39         return spritesheetAnimation;
40     }
41 
42     private Spritesheet[ulong] composeSpritesheets(JSONValue[] spritesheetJsons) {
43         Spritesheet[ulong] spriteSheets;
44         foreach (spritesheetJson; spritesheetJsons) {
45             auto spritesheet = new Spritesheet();
46             spritesheet.id = spritesheetJson["id"].integer;
47             spritesheet.rows = spritesheetJson["rows"].integer;
48             spritesheet.columns = spritesheetJson["columns"].integer;
49             spritesheet.fileName = spritesheetJson["fileName"].str;
50             spriteSheets[spritesheet.id] = spritesheet;
51         }
52         return spriteSheets;
53     }
54 
55     public Animation[string] composeAnimations(JSONValue[] animationJsons, ref const Spritesheet[ulong] spritesheets) {
56         Animation[string] animations;
57         foreach (animationJson; animationJsons) {
58             auto animation = new Animation();
59             animation.name = animationJson["name"].str;
60             animation.beginFrame = animationJson["begin"].integer;
61             animation.endFrame = animationJson["end"].integer;
62             animation.spritesheet = cast(Spritesheet) spritesheets[animationJson["spritesheet"].integer];
63             animations[animation.name] = animation;
64         }
65         return animations;
66     }
67 }