First commit
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
//using Microsoft.Xna.Framework;
|
||||
//using Microsoft.Xna.Framework.Graphics;
|
||||
//using System;
|
||||
//using System.Collections.Generic;
|
||||
//using System.Linq;
|
||||
//using System.Text;
|
||||
|
||||
//namespace Subsurface
|
||||
//{
|
||||
// static class EventManager
|
||||
// {
|
||||
// static Event activeEvent;
|
||||
|
||||
// static GUIFrame infoPanel;
|
||||
|
||||
// const int MaxPreviousEvents = 6;
|
||||
// const float PreviouslyUsedWeight = 10.0f;
|
||||
|
||||
// static List<Event> previousEvents = new List<Event>();
|
||||
|
||||
// public static Event ActiveEvent
|
||||
// {
|
||||
// get { return activeEvent; }
|
||||
// }
|
||||
|
||||
// public static void StartShift()
|
||||
// {
|
||||
// int eventCount = Event.list.Count();
|
||||
|
||||
// //create an array where the probability of an event being selected will be calculated
|
||||
// float[] eventProbability = new float[eventCount];
|
||||
|
||||
// float probabilitySum = 0.0f;
|
||||
|
||||
// for (int i = 0; i < eventCount; i++)
|
||||
// {
|
||||
// eventProbability[i] = Event.list[i].Commonness;
|
||||
|
||||
// //if the event has been previously selected, it's less likely to be selected now
|
||||
// int previousEventIndex = previousEvents.FindIndex(x => x == Event.list[i]);
|
||||
// if (previousEventIndex>=0)
|
||||
// {
|
||||
// //how many shifts ago was the event last selected
|
||||
// int eventDist = eventCount - previousEventIndex;
|
||||
|
||||
// float weighting = (1.0f / eventDist) * PreviouslyUsedWeight;
|
||||
|
||||
// eventProbability[i] *= weighting;
|
||||
// }
|
||||
|
||||
// probabilitySum += eventProbability[i];
|
||||
// }
|
||||
|
||||
// float randomNumber = (float)Game1.random.NextDouble()*probabilitySum;
|
||||
|
||||
// for (int i = 0; i < eventCount; i++)
|
||||
// {
|
||||
// if (randomNumber <= eventProbability[i])
|
||||
// {
|
||||
// SelectEvent(Event.list[i]);
|
||||
// return;
|
||||
// }
|
||||
|
||||
// randomNumber -= eventProbability[i];
|
||||
// }
|
||||
// }
|
||||
|
||||
// public static void SelectEvent(Event selectedEvent)
|
||||
// {
|
||||
// if (selectedEvent == null) return;
|
||||
|
||||
// activeEvent = selectedEvent;
|
||||
// previousEvents.Add(activeEvent);
|
||||
|
||||
// activeEvent.Init();
|
||||
// }
|
||||
|
||||
// public static void Update(GameTime gameTime)
|
||||
// {
|
||||
// if (activeEvent==null) return;
|
||||
// activeEvent.Update(gameTime);
|
||||
// }
|
||||
|
||||
// public static void DrawInfo(SpriteBatch spriteBatch)
|
||||
// {
|
||||
// if (activeEvent==null || !activeEvent.IsStarted) return;
|
||||
|
||||
// if (infoPanel == null)
|
||||
// {
|
||||
// infoPanel = new GUIFrame(new Rectangle(Game1.GraphicsWidth - 320, 20, 300, 100), Color.White * 0.8f);
|
||||
// infoPanel.Padding = GUI.style.smallPadding;
|
||||
// infoPanel.AddChild(new GUITextBlock(new Rectangle(0, 0, 200, 20), activeEvent.Name, Color.Transparent, Color.Black));
|
||||
// infoPanel.AddChild(new GUITextBlock(new Rectangle(0, 0, 200, 50), activeEvent.Description, Color.Transparent, Color.Black));
|
||||
// }
|
||||
|
||||
|
||||
// //infoPanel.Draw(spriteBatch);
|
||||
// }
|
||||
|
||||
// public static void EventFinished(Event e)
|
||||
// {
|
||||
// if (e != activeEvent) return;
|
||||
// infoPanel.AddChild(new GUITextBlock(new Rectangle(0, 0, 200, 80), "Finished!", Color.Transparent, Color.Black));
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
@@ -0,0 +1,59 @@
|
||||
using System.Xml.Linq;
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
namespace Subsurface
|
||||
{
|
||||
class MonsterEvent : ScriptedEvent
|
||||
{
|
||||
private string characterFile;
|
||||
|
||||
private int minAmount, maxAmount;
|
||||
|
||||
private Character[] monsters;
|
||||
|
||||
public MonsterEvent(XElement element)
|
||||
: base (element)
|
||||
{
|
||||
characterFile = ToolBox.GetAttributeString(element, "characterfile", "");
|
||||
|
||||
minAmount = ToolBox.GetAttributeInt(element, "minamount", 1);
|
||||
maxAmount = ToolBox.GetAttributeInt(element, "maxamount", 1);
|
||||
}
|
||||
|
||||
protected override void Start()
|
||||
{
|
||||
WayPoint randomWayPoint = WayPoint.GetRandom(WayPoint.SpawnType.Enemy);
|
||||
|
||||
int amount = Game1.random.Next(minAmount, maxAmount);
|
||||
|
||||
monsters = new Character[amount];
|
||||
|
||||
for (int i = 0; i < amount; i++)
|
||||
{
|
||||
monsters[i] = new Character(characterFile,
|
||||
(randomWayPoint == null) ? Vector2.Zero : randomWayPoint.SimPosition);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public override void Update(float deltaTime)
|
||||
{
|
||||
base.Update(deltaTime);
|
||||
|
||||
if (!isStarted) return;
|
||||
|
||||
if (!isFinished)
|
||||
{
|
||||
bool monstersDead = true;
|
||||
for (int i = 0; i < monsters.Length; i++)
|
||||
{
|
||||
if (monsters[i].IsDead) continue;
|
||||
|
||||
monstersDead = false;
|
||||
break;
|
||||
}
|
||||
if (monstersDead) Finished();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
namespace Subsurface
|
||||
{
|
||||
class PropertyTask : Task
|
||||
{
|
||||
Item item;
|
||||
|
||||
|
||||
public delegate bool IsFinishedHandler();
|
||||
private IsFinishedHandler IsFinishedChecker;
|
||||
|
||||
public PropertyTask(TaskManager taskManager, Item item, IsFinishedHandler isFinished, float priority, string name)
|
||||
: base(taskManager, priority, name)
|
||||
{
|
||||
this.item = item;
|
||||
IsFinishedChecker = isFinished;
|
||||
|
||||
taskManager.TaskStarted(this);
|
||||
}
|
||||
|
||||
public override void Update(float deltaTime)
|
||||
{
|
||||
if (IsFinishedChecker())
|
||||
{
|
||||
Finished();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
namespace Subsurface
|
||||
{
|
||||
class RepairTask : Task
|
||||
{
|
||||
Item item;
|
||||
|
||||
public RepairTask(TaskManager taskManager, Item item, float priority, string name)
|
||||
: base(taskManager, priority, name)
|
||||
{
|
||||
this.item = item;
|
||||
|
||||
taskManager.TaskStarted(this);
|
||||
}
|
||||
|
||||
public override void Update(float deltaTime)
|
||||
{
|
||||
if (item.Condition > 50.0f) Finished();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace Subsurface
|
||||
{
|
||||
class ScriptedEvent
|
||||
{
|
||||
private static string configFile = "Content/randomevents.xml";
|
||||
|
||||
const int MaxPreviousEvents = 6;
|
||||
const float PreviouslyUsedWeight = 10.0f;
|
||||
|
||||
static List<int> previousEvents = new List<int>();
|
||||
|
||||
protected string name;
|
||||
protected string description;
|
||||
|
||||
protected int commonness;
|
||||
protected int difficulty;
|
||||
|
||||
//the time after starting a shift after which the event is started
|
||||
//the time is set to a random value between startTimeMin and startTimeMax at the start of the shift
|
||||
private int startTimeMin;
|
||||
private int startTimeMax;
|
||||
|
||||
private double startTimer;
|
||||
|
||||
protected bool isStarted;
|
||||
protected bool isFinished;
|
||||
|
||||
public string Name
|
||||
{
|
||||
get { return name; }
|
||||
}
|
||||
|
||||
public string Description
|
||||
{
|
||||
get { return description; }
|
||||
}
|
||||
|
||||
public int Commonness
|
||||
{
|
||||
get { return commonness; }
|
||||
}
|
||||
|
||||
public bool IsStarted
|
||||
{
|
||||
get { return isStarted; }
|
||||
}
|
||||
|
||||
public bool IsFinished
|
||||
{
|
||||
get { return isFinished; }
|
||||
}
|
||||
|
||||
public int Difficulty
|
||||
{
|
||||
get { return difficulty; }
|
||||
}
|
||||
|
||||
public ScriptedEvent(XElement element)
|
||||
{
|
||||
name = ToolBox.GetAttributeString(element, "name", "");
|
||||
description = ToolBox.GetAttributeString(element, "description", "");
|
||||
|
||||
difficulty = ToolBox.GetAttributeInt(element, "difficulty", 1);
|
||||
commonness = ToolBox.GetAttributeInt(element, "commonness", 1);
|
||||
|
||||
if (element.Attribute("starttime") != null)
|
||||
{
|
||||
startTimeMax = ToolBox.GetAttributeInt(element, "starttime", 1);
|
||||
startTimeMin = startTimeMax;
|
||||
}
|
||||
else
|
||||
{
|
||||
startTimeMax = ToolBox.GetAttributeInt(element, "starttimemax", 1);
|
||||
startTimeMin = ToolBox.GetAttributeInt(element, "starttimemin", 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static ScriptedEvent LoadRandom()
|
||||
{
|
||||
XDocument doc = ToolBox.TryLoadXml(configFile);
|
||||
if (doc == null) return null;
|
||||
|
||||
int eventCount = doc.Root.Elements().Count();
|
||||
//int[] commonness = new int[eventCount];
|
||||
float[] eventProbability = new float[eventCount];
|
||||
|
||||
float probabilitySum = 0.0f;
|
||||
|
||||
int i = 0;
|
||||
foreach (XElement element in doc.Root.Elements())
|
||||
{
|
||||
eventProbability[i] = ToolBox.GetAttributeInt(element, "commonness", 1);
|
||||
|
||||
//if the event has been previously selected, it's less likely to be selected now
|
||||
int previousEventIndex = previousEvents.FindIndex(x => x == i);
|
||||
if (previousEventIndex >= 0)
|
||||
{
|
||||
//how many shifts ago was the event last selected
|
||||
int eventDist = eventCount - previousEventIndex;
|
||||
|
||||
float weighting = (1.0f / eventDist) * PreviouslyUsedWeight;
|
||||
|
||||
eventProbability[i] *= weighting;
|
||||
}
|
||||
|
||||
probabilitySum += eventProbability[i];
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
float randomNumber = (float)Game1.random.NextDouble() * probabilitySum;
|
||||
|
||||
i = 0;
|
||||
foreach (XElement element in doc.Root.Elements())
|
||||
{
|
||||
if (randomNumber <= eventProbability[i])
|
||||
{
|
||||
Type t;
|
||||
string type = element.Name.ToString();
|
||||
|
||||
try
|
||||
{
|
||||
t = Type.GetType("Subsurface." + type + ", Subsurface", true, true);
|
||||
if (t == null)
|
||||
{
|
||||
DebugConsole.ThrowError("Error in " + configFile + "! Could not find a an event class of the type ''" + type + "''.");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
DebugConsole.ThrowError("Error in " + configFile + "! Could not find a an event class of the type ''" + type + "''.");
|
||||
continue;
|
||||
}
|
||||
|
||||
ConstructorInfo constructor = t.GetConstructor(new[] { typeof(XElement) });
|
||||
object instance = constructor.Invoke(new object[] { element });
|
||||
|
||||
previousEvents.Add(i);
|
||||
|
||||
return (ScriptedEvent)instance;
|
||||
}
|
||||
|
||||
randomNumber -= eventProbability[i];
|
||||
i++;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public virtual void Init()
|
||||
{
|
||||
isStarted = false;
|
||||
isFinished = false;
|
||||
startTimer = Game1.random.Next(startTimeMin, startTimeMax);
|
||||
}
|
||||
|
||||
protected virtual void Start()
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void Update(float deltaTime)
|
||||
{
|
||||
if (isStarted) return;
|
||||
|
||||
if (startTimer>0)
|
||||
{
|
||||
startTimer -= deltaTime;
|
||||
}
|
||||
else
|
||||
{
|
||||
Start();
|
||||
isStarted = true;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void Finished()
|
||||
{
|
||||
isFinished = true;
|
||||
//EventManager.EventFinished(this);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
namespace Subsurface
|
||||
{
|
||||
class ScriptedTask : Task
|
||||
{
|
||||
private ScriptedEvent scriptedEvent;
|
||||
|
||||
private bool prevStarted;
|
||||
|
||||
public ScriptedTask(TaskManager taskManager, ScriptedEvent scriptedEvent)
|
||||
: base(taskManager, scriptedEvent.Difficulty, scriptedEvent.Name)
|
||||
{
|
||||
this.scriptedEvent = scriptedEvent;
|
||||
scriptedEvent.Init();
|
||||
}
|
||||
|
||||
public override void Update(float deltaTime)
|
||||
{
|
||||
if (prevStarted == false && scriptedEvent.IsStarted)
|
||||
{
|
||||
taskManager.TaskStarted(this);
|
||||
prevStarted = true;
|
||||
}
|
||||
|
||||
scriptedEvent.Update(deltaTime);
|
||||
if (scriptedEvent.IsFinished) Finished();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
|
||||
namespace Subsurface
|
||||
{
|
||||
class Task
|
||||
{
|
||||
|
||||
protected string name;
|
||||
|
||||
private float priority;
|
||||
|
||||
protected TaskManager taskManager;
|
||||
|
||||
protected bool isFinished;
|
||||
|
||||
public string Name
|
||||
{
|
||||
get { return name; }
|
||||
}
|
||||
|
||||
public float Priority
|
||||
{
|
||||
get { return priority; }
|
||||
}
|
||||
|
||||
public bool IsFinished
|
||||
{
|
||||
get { return isFinished; }
|
||||
}
|
||||
|
||||
public Task(TaskManager taskManager, float priority, string name)
|
||||
{
|
||||
this.taskManager = taskManager;
|
||||
this.priority = priority;
|
||||
this.name = name;
|
||||
|
||||
taskManager.AddTask(this);
|
||||
}
|
||||
|
||||
public virtual void Update(float deltaTime)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected virtual void Finished()
|
||||
{
|
||||
taskManager.TaskFinished(this);
|
||||
isFinished = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
|
||||
namespace Subsurface
|
||||
{
|
||||
class TaskManager
|
||||
{
|
||||
private List<Task> tasks;
|
||||
|
||||
private GUIListBox taskListBox;
|
||||
|
||||
public TaskManager(GameSession session)
|
||||
{
|
||||
tasks = new List<Task>();
|
||||
|
||||
taskListBox = new GUIListBox(new Rectangle(Game1.GraphicsWidth - 250, 50, 250, 500), Color.Transparent);
|
||||
//taskListBox.ScrollBarEnabled = false;
|
||||
taskListBox.Padding = GUI.style.smallPadding;
|
||||
}
|
||||
|
||||
public void AddTask(Task newTask)
|
||||
{
|
||||
if (tasks.Contains(newTask)) return;
|
||||
|
||||
tasks.Add(newTask);
|
||||
}
|
||||
|
||||
public void StartShift(int scriptedEventCount)
|
||||
{
|
||||
CreateScriptedEvents(scriptedEventCount);
|
||||
|
||||
taskListBox.ClearChildren();
|
||||
}
|
||||
|
||||
|
||||
public void EndShift()
|
||||
{
|
||||
taskListBox.ClearChildren();
|
||||
tasks.Clear();
|
||||
}
|
||||
|
||||
private void CreateScriptedEvents(int scriptedEventCount)
|
||||
{
|
||||
for (int i = 0; i < scriptedEventCount; i++)
|
||||
{
|
||||
ScriptedEvent scriptedEvent = ScriptedEvent.LoadRandom();
|
||||
AddTask(new ScriptedTask(this, scriptedEvent));
|
||||
}
|
||||
}
|
||||
|
||||
public void TaskStarted(Task task)
|
||||
{
|
||||
Color color = Color.Red;
|
||||
int width = 250;
|
||||
if (task.Priority < 30.0f)
|
||||
{
|
||||
width = 200;
|
||||
color = Color.Yellow;
|
||||
}
|
||||
else if (task.Priority < 60)
|
||||
{
|
||||
width = 220;
|
||||
color = Color.Orange;
|
||||
}
|
||||
|
||||
GUIFrame frame = new GUIFrame(new Rectangle(0,0,width,40), Color.Transparent, Alignment.Right, taskListBox);
|
||||
frame.UserData = task;
|
||||
frame.Padding = new Vector4(0.0f, 5.0f, 5.0f, 5.0f);
|
||||
|
||||
GUIFrame colorFrame = new GUIFrame(new Rectangle(0, 0, 0, 0), color * 0.5f, Alignment.Right, frame);
|
||||
GUITextBlock textBlock = new GUITextBlock(new Rectangle(5, 5, 0, 20), task.Name, Color.Transparent, Color.Black, Alignment.Right, colorFrame);
|
||||
//textBlock.Padding = new Vector4(10.0f, 10.0f, 0.0f, 0.0f);
|
||||
|
||||
//colorFrame.AddChild(textBlock);
|
||||
|
||||
taskListBox.children.Sort((x, y) => ((Task)y.UserData).Priority.CompareTo(((Task)x.UserData).Priority));
|
||||
}
|
||||
|
||||
public void TaskFinished(Task task)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void Update(float deltaTime)
|
||||
{
|
||||
Task removeTask = null;
|
||||
GUIComponent removeComponent = null;
|
||||
foreach (Task task in tasks)
|
||||
{
|
||||
if (task.IsFinished)
|
||||
{
|
||||
foreach (GUIComponent comp in taskListBox.children)
|
||||
{
|
||||
if (comp.UserData as Task != task) continue;
|
||||
comp.Rect = new Rectangle(comp.Rect.X, comp.Rect.Y, comp.Rect.Width, comp.Rect.Height - 1);
|
||||
comp.children[0].ClearChildren();
|
||||
if (comp.Rect.Height < 1)
|
||||
{
|
||||
removeComponent = comp;
|
||||
removeTask = task;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
task.Update(deltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
if (removeComponent!= null)
|
||||
{
|
||||
taskListBox.RemoveChild(removeComponent);
|
||||
tasks.Remove(removeTask);
|
||||
}
|
||||
|
||||
//endShiftButton.Enabled = finished || Game1.server!=null;
|
||||
}
|
||||
|
||||
public void Draw(SpriteBatch spriteBatch)
|
||||
{
|
||||
taskListBox.Draw(spriteBatch);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user