-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScenarioSystem.cs
182 lines (142 loc) · 5.37 KB
/
ScenarioSystem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System;
using System.Collections.Generic;
using HumanRamen.Entities.Components;
using HumanRamen.Essentials;
using HumanRamen.UI;
using HumanRamen.Scene;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended.Entities;
using MonoGame.Extended.Entities.Systems;
using NLua;
using static HumanRamen.UI.Choices;
namespace HumanRamen.Entities.Systems
{
public interface IScenarioContent { }
public class ScenarioSystem : UpdateSystem, ICommandHandler
{
public enum State
{
Wait,
Dialog,
Choice,
Battle,
}
private readonly Logger _l = new Logger("GameStateSystem");
private readonly Lua _lua = new Lua();
private readonly IGameContent _content;
private readonly UISystem _ui;
private readonly Commander _commander;
private readonly string _startScript = "./Lua/start.lua";
private State _state;
private Entity _scenarioEntity;
private Scenario.Node _currentNode;
private bool _currentNodeInited;
private SpriteComponent _backgroundSprite;
// private Characters _characters;
private Dictionary<string, Entity> _characterEntities = new Dictionary<string, Entity>();
private Dictionary<string, SpriteComponent> _characterSprites = new Dictionary<string, SpriteComponent>();
public ScenarioSystem(IGameContent content, UISystem ui, Commander commander)
{
_content = content;
_ui = ui;
_commander = commander;
_commander.RegisterHandler("UI", this);
}
public override void Initialize(World world)
{
_scenarioEntity = world.CreateEntity();
_lua.LoadCLRPackage();
var characters = new Characters();
_lua["Characters"] = characters;
var scenario = new Scenario();
_lua["Scenario"] = scenario;
_lua.DoFile(_startScript);
if (_lua.GetString("Background") != "")
{
_backgroundSprite = new SpriteComponent(_content.GetType().GetProperty(_lua.GetString("Background")).GetValue(_content) as Texture2D);
_backgroundSprite.IsBackground = true;
_scenarioEntity.Attach(_backgroundSprite);
}
// Create character's entity
foreach (var chr in characters.Pool)
{
var name = chr.Value.Name;
var spritePath = chr.Value.SpritePath;
_l.Debug(String.Format("Add chr: name {0}, sprite {1}", name, spritePath));
_characterEntities[name] = world.CreateEntity();
var tex = _content.GetType().GetProperty(spritePath).GetValue(_content) as Texture2D;
var spriteComponent =
new SpriteComponent(tex);
spriteComponent.Position = new Rectangle(150, 30, tex.Width / 2, tex.Height / 2);
spriteComponent.Depth = 0.0f;
_characterSprites.Add(name, spriteComponent);
_characterEntities[name].Attach(spriteComponent);
}
_currentNode = scenario.Start;
// TODO: check node type
_state = State.Dialog;
_ui.IsDialogBackgroundVisible = true;
}
public override void Update(GameTime gameTime)
{
if (!_currentNodeInited && _state == State.Battle) {
_l.Debug("We are in battle stance");
_currentNodeInited = true;
}
if (!_currentNodeInited && _state == State.Dialog)
{
_ui.UpdateDialog(new Dialog(_currentNode.DialogueName, _currentNode.DialogueText));
_currentNodeInited = true;
}
if (!_currentNodeInited && _state == State.Choice)
{
var choicesList = new List<Choice>();
foreach (var choice in _currentNode.Responses)
{
choicesList.Add(new Choice(choice.Key, choice.Key));
}
var choices = new Choices(choicesList);
_ui.UpdateChoices(choices);
_currentNodeInited = true;
}
}
public override void Dispose()
{
_lua.Dispose();
base.Dispose();
}
public void HandleCommand(string topic, string command)
{
if (_state == State.Dialog && topic == "UI" && command == "Continue")
{
_state = State.Choice;
_currentNodeInited = false;
return;
}
if (_state == State.Choice && topic == "UI")
{
if (!_currentNode.Responses.ContainsKey(command))
{
throw new ExceptionNullNode(command);
}
_currentNode = _currentNode.Responses[command];
if (_currentNode.Type == Scenario.Node.NodeType.Dialog) {
_state = State.Dialog;
_currentNodeInited = false;
return;
} else if (_currentNode.Type == Scenario.Node.NodeType.Battle) {
_state = State.Battle;
_currentNodeInited = false;
return;
}
}
}
}
public class ExceptionNullNode : Exception
{
public ExceptionNullNode(string command) : base(String.Format("Node with key {0} is NULL", command))
{
}
}
}