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.script.instr;
8 import engine.vn.script;
9 import engine;
10 
11 /**
12     Instruction that shows a character
13 */
14 class CGInstr : IScriptInstr {
15 public:
16     string cgfile;
17     
18     this(string cgfile = null) {
19         this.cgfile = cgfile;
20     }
21 
22     bool execute(Script script) {
23         script.state.cg = cgfile is null ? null : new Texture("assets/textures/backgrounds/"~cgfile);
24         return false;
25     }
26 
27     bool isBlocking() {
28         return false;
29     }
30 }
31 
32 /**
33     Instruction that plays a sound effect
34 */
35 class SFXInstr : IScriptInstr {
36 public:
37     string sfxfile;
38     
39     this(string sfxfile) {
40         this.sfxfile = sfxfile;
41     }
42 
43     bool execute(Script script) {
44         (new Sound(sfxfile)).play();
45         return false;
46     }
47 
48     bool isBlocking() {
49         return false;
50     }
51 }
52 
53 /**
54     Instruction that sets the scene's music
55 */
56 class MusicInstr : IScriptInstr {
57 public:
58     string musicFile;
59     
60     this(string musicFile) {
61         this.musicFile = musicFile;
62     }
63 
64     bool execute(Script script) {
65         if (script.state.music !is null) {
66             script.state.music.stop();
67         }
68         script.state.music = new Music(musicFile);
69         script.state.music.setLooping(true);
70         script.state.music.play();
71         return false;
72     }
73 
74     bool isBlocking() {
75         return false;
76     }
77 }
78 
79 /**
80     Instruction that shows a character
81 */
82 class ShowInstr : IScriptInstr {
83 public:
84     bool side;
85     string texture;
86     string character;
87     
88     this(string character, string texture=null, bool side=false) {
89         this.character = character;
90         this.texture = texture;
91         this.side = side;
92     }
93 
94     bool execute(Script script) {
95         if (character in script.state.characters) {
96             script.state.characters[character].shown = true;
97         
98             if (texture !is null) {
99                 script.state.characters[character].setTexture(texture);
100             }
101 
102             script.state.characters[character].position = !side ?
103                 vec2(256, 1080) : vec2(1920-256, 1080);
104             script.state.characters[character].isFlipped = side;
105         }
106         return false;
107     }
108 
109     bool isBlocking() {
110         return false;
111     }
112 }
113 
114 /**
115     Instruction that hides a character
116 */
117 class HideInstr : IScriptInstr {
118 public:
119     string character;
120     
121     this(string character) {
122         this.character = character;
123     }
124 
125     bool execute(Script script) {
126         if (character in script.state.characters) {
127             script.state.characters[character].shown = false;
128         }
129         return false;
130     }
131 
132     bool isBlocking() {
133         return false;
134     }
135 }
136 
137 /**
138     Instruction that causes a character to say something
139 */
140 class SayInstr : IScriptInstr {
141 public:
142     bool show;
143     string origin;
144     string text;
145 
146     this(string text, string origin=null, bool show=false) {
147         this.origin = origin;
148         this.text = text;
149         this.show = show;
150     }
151 
152     /**
153         Executes the instruction
154     */
155     bool execute(Script script) {
156         script.state.dialogue.push(text.toEngineString(), origin.toEngineString());
157 
158         // If show is enabled show the character
159         if (origin in script.state.characters) {
160 
161             // Activates the character
162             script.state.characters[origin].activate();
163 
164             if (show) {
165                 script.state.characters[origin].shown = true;
166             }
167         }
168         return true;
169     }
170 
171     bool isBlocking() {
172         return true;
173     }
174 }
175 /**
176     Instruction that causes a character to say something, this is nonblocking
177 */
178 class NSayInstr : SayInstr {
179 public:
180     bool show;
181     string origin;
182     string text;
183 
184     this(string text, string origin=null, bool show=false) {
185         super(text, origin, show);
186     }
187 
188     /**
189         Executes the instruction
190     */
191     override bool execute(Script script) {
192         script.state.markAutoNext();
193         return super.execute(script);
194     }
195 
196     override bool isBlocking() {
197         return super.isBlocking();
198     }
199 }
200 
201 /**
202     Instruction that executes D code
203 */
204 class CodeInstr : IScriptInstr {
205 public:
206     void delegate(Script script) instr;
207 
208     this(void delegate(Script script) instr) {
209         this.instr = instr;
210     }
211 
212     /**
213         Executes the instruction
214     */
215     bool execute(Script script) {
216         instr(script);
217         return false;
218     }
219 
220     bool isBlocking() {
221         return false;
222     }
223 }