1 /*
2     Copyright © 2020, Luna Nielsen
3     Distributed under the 2-Clause BSD License, see LICENSE file.
4     
5     Authors: Luna Nielsen
6 */
7 module engine.vn.dialg;
8 import engine.vn.render.dialg;
9 import engine.vn.log;
10 import engine.vn;
11 import engine;
12 
13 /**
14     Manager for dialogue
15 */
16 class DialogueManager {
17 private:
18     DialogueRenderer renderer;
19     string speaker;
20     dstring dspeaker;
21     VNLog log;
22     VNState state;
23 
24 public:
25     this(VNState state) {
26         this.state = state;
27 
28         log = new VNLog();
29         renderer = new DialogueRenderer();
30         if (!GameAtlas.has("ui_vnbar")) {
31             GameAtlas.add("ui_vnbar", "assets/textures/ui/vn-bar.png");
32         }
33     }
34 
35     /**
36         Whether the hide the dialogue box
37     */
38     bool hide = false;
39 
40     /**
41         Whether the dialogue requested to automatically advance
42     */
43     bool autoNext = false;
44 
45     /**
46         Whether the text is done scrolling
47     */
48     bool done() {
49         return renderer.done;
50     }
51 
52     /**
53         Skip to end of text
54     */
55     void skip() {
56         renderer.skip();
57     }
58 
59     /**
60         Update the dialogue manager
61     */
62     void update() {
63         renderer.update();
64     }
65 
66     /**
67         Push dialogue/action to the renderer
68         Automatically pu
69     */
70     void push(dstring dialogue, dstring origin = null) {
71         speaker = origin.toDString;
72         dspeaker = origin;
73         renderer.pushText(dialogue);
74         log.add(origin, dialogue);
75     }
76 
77     /**
78         Draw dialogue box
79     */
80     void draw() {
81         if (hide) return;
82 
83         // Set font size
84         GameFont.changeSize(48);
85 
86         // Draw the bar
87         GameBatch.draw("ui_vnbar", vec4(0, 1080-292, 1920, 292));
88         GameBatch.flush();
89 
90         // Draw name tag
91         if (speaker.length > 0) {
92             GameFont.draw(
93                 speaker in state.characters ? 
94                 state.characters[speaker].displayName : 
95                 dspeaker, 
96                 vec2(32, 804)
97             );
98         }
99         GameFont.flush();
100 
101         // Slow type the dialogue
102         renderer.draw(vec2(48, 900));
103     }
104 }