-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spellbook.xaml.cs
200 lines (184 loc) · 6.07 KB
/
Spellbook.xaml.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
namespace WpfApplication1
{
public partial class Spellbook : Window
{
SpellbookLevel[] spellbookLevels;
TextBlock[] lvlLabels;
SpellbookLevel selectedLevel;
string spellcaster;
public Spellbook(string directory, int firstLevel, int lastLevel, string spellcaster)
{
this.spellcaster = spellcaster;
InitializeComponent();
lvlLabels = new TextBlock[10];
lvlLabels[0] = textBlock1;
lvlLabels[1] = textBlock2;
lvlLabels[2] = textBlock3;
lvlLabels[3] = textBlock4;
lvlLabels[4] = textBlock5;
lvlLabels[5] = textBlock6;
lvlLabels[6] = textBlock7;
lvlLabels[7] = textBlock8;
lvlLabels[8] = textBlock9;
lvlLabels[9] = textBlock10;
spellbookLevels = new SpellbookLevel[10];
for (int i = firstLevel; i <= lastLevel; i++)
{
System.IO.StreamReader SR;
try
{
SR = System.IO.File.OpenText(directory + "\\spells\\" + spellcaster + "\\" + i.ToString() + ".txt");
spellbookLevels[i] = new SpellbookLevel(SR.ReadToEnd());
}
catch
{
spellbookLevels[i] = new SpellbookLevel("");
}
lvlLabels[i].DataContext = spellbookLevels[i];
}
spellbookLevels[firstLevel].Selected = true;
selectedLevel = spellbookLevels[firstLevel];
allSpells.DataContext = selectedLevel;
preparedSpells.DataContext = selectedLevel;
}
private void textBlock10_GotFocus(object sender, MouseButtonEventArgs e)
{
TextBlock senderBlock = (TextBlock)sender;
if (selectedLevel != null)
selectedLevel.Selected = false;
selectedLevel = spellbookLevels[(int)App.getFloatFromString(senderBlock.Text)];
selectedLevel.Selected = true;
allSpells.DataContext = selectedLevel;
preparedSpells.DataContext = selectedLevel;
}
private void RandomizeBook_Click(object sender, RoutedEventArgs e)
{
int casterLevel = (int)App.getFloatFromString(casterLevelBox.Text);
for (int i = 0; i < spellbookLevels.Count(); i++)
{
int maxLevel = ((casterLevel+1)/2);
if (i <= maxLevel)
spellbookLevels[i].RandomizeSpellBook(Math.Min(2 + maxLevel - i + ((casterLevel + 1) % 2), 5));
}
}
}
public class SpellbookLevel
{
bool selected = false;
ObservableCollection<Spell> allSpells;
ObservableCollection<Spell> casterSpells;
public SpellbookLevel(string levelText)
{
allSpells = new ObservableCollection<Spell>();
casterSpells = new ObservableCollection<Spell>();
string[] schools = levelText.Split('#');
foreach (string school in schools)
{
string[] spellsFromSchool = school.Split('!');
for (int i = 1; i < spellsFromSchool.Count(); i++)
{
string[] spellSegments = spellsFromSchool[i].Split(':');
Spell spell = new Spell();
spell.spellName = spellSegments[0];
spell.spellSchool = spellsFromSchool[0];
allSpells.Add(spell);
}
}
}
public void RandomizeSpellBook(int numSpells)
{
casterSpells.Clear();
if (allSpells.Count > 0)
{
Random random = new Random();
for (int i = 0; i < numSpells; i++)
{
int randomSpellIndex = random.Next(0, allSpells.Count);
casterSpells.Add(allSpells[randomSpellIndex]);
}
}
}
public event EventHandler BackgroundColorChanged;
private void OnBackgroundColorChanged()
{
if (BackgroundColorChanged != null)
BackgroundColorChanged(this, EventArgs.Empty);
}
public event EventHandler SelectedChanged;
private void OnSelectedChanged()
{
if (SelectedChanged != null)
SelectedChanged(this, EventArgs.Empty);
}
public bool Selected
{
get { return selected; }
set
{
selected = value;
OnSelectedChanged();
OnBackgroundColorChanged();
}
}
public string BackgroundColor
{
get
{
if (Selected)
return "AntiqueWhite";
else
return "White";
}
}
public ObservableCollection<Spell> AllSpells
{
get
{
return allSpells;
}
}
public ObservableCollection<Spell> CasterSpells
{
get
{
return casterSpells;
}
}
}
public struct Spell
{
public string spellName;
public string spellLevel;
public string spellDescription;
public string spellSchool;
public override string ToString()
{
return spellName;
}
}
public enum Schools
{
Abjuration = 0,
Conjuration = 1,
Divination = 2,
Enchantment = 3,
Evocation = 4,
Illusion = 5,
Necromancy = 6,
Transmutation = 7,
Universal = 8
}
}