This commit is contained in:
Regalis
2015-07-31 21:05:55 +03:00
parent 23d847a4ac
commit 85b0cda4ca
181 changed files with 4455 additions and 4073 deletions
+129
View File
@@ -0,0 +1,129 @@
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace Subsurface
{
class Skill
{
string name;
int level;
public string Name
{
get { return name; }
}
public int Level
{
get { return level; }
}
public Skill(string name, int level)
{
this.name = name;
this.level = level;
}
}
class Job
{
private JobPrefab prefab;
private Dictionary<string, Skill> skills;
public string Name
{
get { return prefab.Name; }
}
public string Description
{
get { return prefab.Description; }
}
public JobPrefab Prefab
{
get { return prefab; }
}
public List<string> SpawnItemNames
{
get { return prefab.ItemNames; }
}
public List<Skill> Skills
{
get { return skills.Values.ToList(); }
}
//public List<float> SkillLevels
//{
// get { return skills.Values.ToList(); }
//}
public Job(JobPrefab jobPrefab)
{
prefab = jobPrefab;
skills = new Dictionary<string, Skill>();
foreach (KeyValuePair<string, Vector2> skill in prefab.Skills)
{
skills.Add(
skill.Key,
new Skill( skill.Key, (int)Rand.Range(skill.Value.X, skill.Value.Y, false)));
}
}
public Job(XElement element)
{
string name = ToolBox.GetAttributeString(element, "name", "").ToLower();
prefab = JobPrefab.List.Find(jp => jp.Name.ToLower() == name);
skills = new Dictionary<string, Skill>();
foreach (XElement subElement in element.Elements())
{
if (subElement.Name.ToString().ToLower() != "skill") continue;
string skillName = ToolBox.GetAttributeString(subElement, "name", "");
if (string.IsNullOrEmpty(name)) continue;
skills.Add(
skillName,
new Skill(skillName, ToolBox.GetAttributeInt(subElement, "level", 0)));
}
}
public static Job Random()
{
JobPrefab prefab = JobPrefab.List[Rand.Int(JobPrefab.List.Count-1, false)];
return new Job(prefab);
}
public int GetSkillLevel(string skillName)
{
Skill skill = null;
skills.TryGetValue(skillName, out skill);
return (skill==null) ? 0 : skill.Level;
}
public virtual XElement Save(XElement parentElement)
{
XElement jobElement = new XElement("job");
jobElement.Add(new XAttribute("name", Name));
foreach (KeyValuePair<string, Skill> skill in skills)
{
jobElement.Add(new XElement("skill", new XAttribute("name", skill.Value.Name), new XAttribute("level", skill.Value.Level)));
}
parentElement.Add(jobElement);
return jobElement;
}
}
}
@@ -0,0 +1,129 @@
using Microsoft.Xna.Framework;
using System.Collections.Generic;
using System.Globalization;
using System.Xml.Linq;
namespace Subsurface
{
class JobPrefab
{
public static List<JobPrefab> List;
string name;
string description;
//how many crew members can have the job (only one captain etc)
private int maxNumber;
//how many crew members are REQUIRED to have a job
//(i.e. if one captain is required, one captain is chosen even if all the players have set captain to lowest preference)
private int minNumber;
//if set to true, a client that has chosen this as their preferred job will get it no matter what
public bool AllowAlways
{
get;
private set;
}
//names of the items the character spawns with
public List<string> ItemNames;
public Dictionary<string, Vector2> Skills;
public string Name
{
get { return name; }
}
public string Description
{
get { return description; }
}
public int MaxNumber
{
get { return maxNumber; }
}
public int MinNumber
{
get { return minNumber; }
}
public JobPrefab(XElement element)
{
name = element.Name.ToString();
description = ToolBox.GetAttributeString(element, "description", "");
minNumber = ToolBox.GetAttributeInt(element, "minnumber", 0);
maxNumber = ToolBox.GetAttributeInt(element, "maxnumber", 10);
AllowAlways = ToolBox.GetAttributeBool(element, "allowalways", false);
ItemNames = new List<string>();
Skills = new Dictionary<string, Vector2>();
foreach (XElement subElement in element.Elements())
{
switch (subElement.Name.ToString().ToLower())
{
case "item":
string itemName = ToolBox.GetAttributeString(subElement, "name", "");
if (!string.IsNullOrEmpty(itemName)) ItemNames.Add(itemName);
break;
case "skills":
LoadSkills(subElement);
break;
}
}
}
public static JobPrefab Random()
{
return List[Rand.Int(List.Count)];
}
private void LoadSkills(XElement element)
{
foreach (XElement subElement in element.Elements())
{
string skillName = ToolBox.GetAttributeString(subElement, "name", "");
if (string.IsNullOrEmpty(skillName) || Skills.ContainsKey(skillName)) continue;
var levelString = ToolBox.GetAttributeString(subElement, "level", "");
if (levelString.Contains(","))
{
Skills.Add(skillName, ToolBox.ParseToVector2(levelString, false));
}
else
{
float skillLevel = float.Parse(levelString, CultureInfo.InvariantCulture);
Skills.Add(skillName, new Vector2(skillLevel, skillLevel));
}
}
}
public static void LoadAll(List<string> filePaths)
{
List = new List<JobPrefab>();
foreach (string filePath in filePaths)
{
XDocument doc = ToolBox.TryLoadXml(filePath);
if (doc == null) return;
foreach (XElement element in doc.Root.Elements())
{
JobPrefab job = new JobPrefab(element);
List.Add(job);
}
}
}
}
}