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

View File

@@ -0,0 +1,53 @@
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Subsurface
{
class CargoManager
{
private List<MapEntityPrefab> purchasedItems;
public CargoManager()
{
purchasedItems = new List<MapEntityPrefab>();
}
public void AddItem(MapEntityPrefab item)
{
purchasedItems.Add(item);
}
public void CreateItems()
{
WayPoint wp = WayPoint.GetRandom(SpawnType.Cargo);
if (wp==null)
{
DebugConsole.ThrowError("The submarine must have a waypoint marked as Cargo for bought items to be placed correctly!");
return;
}
Hull cargoRoom = Hull.FindHull(wp.Position);
if (wp == null)
{
DebugConsole.ThrowError("A waypoint marked as Cargo must be placed inside a room!");
return;
}
foreach (MapEntityPrefab prefab in purchasedItems)
{
Vector2 position = new Vector2(
Rand.Range(cargoRoom.Rect.X + 20, cargoRoom.Rect.Right - 20),
Rand.Range(cargoRoom.Rect.Y - cargoRoom.Rect.Height + 20.0f, cargoRoom.Rect.Y));
new Item(prefab as ItemPrefab, wp.Position);
}
purchasedItems.Clear();
}
}
}

View File

@@ -0,0 +1,180 @@
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Xml.Linq;
namespace Subsurface
{
class CrewManager
{
public List<Character> characters;
public List<CharacterInfo> characterInfos;
//public static string mapFile;
//public string saveFile;
private int money;
private GUIFrame guiFrame;
private GUIListBox listBox;
public int Money
{
get { return money; }
set { money = (int)Math.Max(value, 0.0f); }
}
public CrewManager()
{
characters = new List<Character>();
characterInfos = new List<CharacterInfo>();
guiFrame = new GUIFrame(new Rectangle(0, 50, 150, 450), Color.Transparent);
listBox = new GUIListBox(new Rectangle(0, 0, 150, 0), Color.Transparent, null, guiFrame);
listBox.ScrollBarEnabled = false;
listBox.OnSelected = SelectCharacter;
money = 10000;
}
public CrewManager(XElement element)
: this()
{
money = ToolBox.GetAttributeInt(element, "money", 0);
foreach (XElement subElement in element.Elements())
{
if (subElement.Name.ToString().ToLower()!="character") continue;
characterInfos.Add(new CharacterInfo(subElement));
}
}
public bool SelectCharacter(object selection)
{
//listBox.Select(selection);
Character character = selection as Character;
if (character == null) return false;
if (characters.Contains(character))
{
Character.Controlled = character;
return true;
}
return false;
}
public void AddCharacter(Character character)
{
characters.Add(character);
if (!characterInfos.Contains(character.Info))
{
characterInfos.Add(character.Info);
}
GUIFrame frame = new GUIFrame(new Rectangle(0, 0, 0, 40), Color.Transparent, null, listBox);
frame.UserData = character;
frame.Padding = new Vector4(5.0f, 5.0f, 5.0f, 5.0f);
frame.HoverColor = Color.LightGray * 0.5f;
frame.SelectedColor = Color.Gold * 0.5f;
string name = character.Info.Name.Replace(' ', '\n');
GUITextBlock textBlock = new GUITextBlock(
new Rectangle(40, 0, 0, 25),
name,
Color.Transparent, Color.White,
Alignment.Left, Alignment.Left,
null, frame);
textBlock.Padding = new Vector4(5.0f, 0.0f, 5.0f, 0.0f);
new GUIImage(new Rectangle(-10, -10, 0, 0), character.AnimController.limbs[0].sprite, Alignment.Left, frame);
}
public void Update(float deltaTime)
{
guiFrame.Update(deltaTime);
}
public void KillCharacter(Character killedCharacter)
{
GUIComponent characterBlock = listBox.GetChild(killedCharacter) as GUIComponent;
if (characterBlock != null) characterBlock.Color = Color.DarkRed * 0.5f;
//if (characters.Find(c => !c.IsDead)==null)
//{
// Game1.GameSession.EndShift(null, null);
//}
}
public void StartShift()
{
listBox.ClearChildren();
characters.Clear();
WayPoint[] waypoints = WayPoint.SelectCrewSpawnPoints(characterInfos);
for (int i = 0; i < waypoints.Length; i++)
{
//WayPoint randomWayPoint = WayPoint.GetRandom(SpawnType.Human);
//Vector2 position = (randomWayPoint == null) ? Vector2.Zero : randomWayPoint.SimPosition;
Character character = new Character(characterInfos[i], waypoints[i]);
Character.Controlled = character;
if (!character.Info.StartItemsGiven)
{
character.GiveJobItems(waypoints[i]);
character.Info.StartItemsGiven = true;
}
AddCharacter(character);
}
if (characters.Count > 0) SelectCharacter(characters[0]);
}
public void EndShift()
{
foreach (Character c in characters)
{
if (!c.IsDead)
{
c.Info.UpdateCharacterItems();
continue;
}
CharacterInfo deadInfo = characterInfos.Find(x => c.Info == x);
if (deadInfo != null) characterInfos.Remove(deadInfo);
}
characters.Clear();
listBox.ClearChildren();
}
public void Draw(SpriteBatch spriteBatch)
{
guiFrame.Draw(spriteBatch);
}
public void Save(XElement parentElement)
{
XElement element = new XElement("crew");
element.Add(new XAttribute("money", money));
foreach (CharacterInfo ci in characterInfos)
{
ci.Save(element);
}
parentElement.Add(element);
}
}
}

View File

@@ -0,0 +1,154 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Subsurface
{
class GameModePreset
{
public static List<GameModePreset> list = new List<GameModePreset>();
public ConstructorInfo Constructor;
public string Name;
public bool IsSinglePlayer;
public GameModePreset(string name, Type type, bool isSinglePlayer = false)
{
this.Name = name;
//Constructor = constructor;
Constructor = type.GetConstructor(new Type[] { typeof(GameModePreset)});
IsSinglePlayer = isSinglePlayer;
list.Add(this);
}
public GameMode Instantiate()
{
object[] lobject = new object[] { this };
return(GameMode)Constructor.Invoke(lobject);
}
}
class GameMode
{
public static List<GameModePreset> presetList = new List<GameModePreset>();
TimeSpan duration;
protected DateTime startTime;
protected DateTime endTime;
//public readonly bool IsSinglePlayer;
private GUIProgressBar timerBar;
protected bool isRunning;
//protected string name;
protected GameModePreset preset;
private string endMessage;
public virtual Quest Quest
{
get { return null; }
}
public DateTime StartTime
{
get { return startTime; }
}
public DateTime EndTime
{
get { return endTime; }
}
public bool IsRunning
{
get { return isRunning; }
}
public bool IsSinglePlayer
{
get { return preset.IsSinglePlayer; }
}
public string Name
{
get { return preset.Name; }
}
public string EndMessage
{
get { return endMessage; }
}
public GameMode(GameModePreset preset)
{
this.preset = preset;
//list.Add(this);
}
public virtual void Draw(SpriteBatch spriteBatch)
{
if (timerBar != null) timerBar.Draw(spriteBatch);
}
public virtual void Start(TimeSpan duration)
{
startTime = DateTime.Now;
if (duration!=TimeSpan.Zero)
{
endTime = startTime + duration;
this.duration = duration;
timerBar = new GUIProgressBar(new Rectangle(Game1.GraphicsWidth - 120, 20, 100, 25), Color.Gold, 0.0f, null);
}
endMessage = "The round has ended!";
isRunning = true;
}
public virtual void Update(float deltaTime)
{
if (!isRunning) return;
if (duration != TimeSpan.Zero)
{
double elapsedTime = (DateTime.Now - startTime).TotalSeconds;
timerBar.BarSize = (float)(elapsedTime / duration.TotalSeconds);
}
//if (DateTime.Now >= endTime)
//{
// End(endMessage);
//}
}
public virtual void End(string endMessage = "")
{
isRunning = false;
if (endMessage != "" || this.endMessage == null) this.endMessage = endMessage;
Game1.GameSession.EndShift(endMessage);
}
public static void Init()
{
new GameModePreset("Single Player", typeof(SinglePlayerMode), true);
new GameModePreset("SandBox", typeof(GameMode), false);
new GameModePreset("Traitor", typeof(TraitorMode), false);
new GameModePreset("Quest", typeof(QuestMode), false);
}
}
}

View File

@@ -0,0 +1,203 @@
using System;
using System.IO;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Text;
using System.Collections.Generic;
using System.Xml.Linq;
namespace Subsurface
{
class GameSession
{
public readonly TaskManager taskManager;
//protected DateTime startTime;
//protected DateTime endTime;
public readonly GameMode gameMode;
private GUIFrame guiRoot;
//private GUIListBox chatBox;
//private GUITextBox textBox;
private string saveFile;
private Submarine submarine;
public Quest Quest
{
get
{
if (gameMode != null) return gameMode.Quest;
return null;
}
}
private Level level;
public Level Level
{
get { return level; }
}
public Map Map
{
get
{
SinglePlayerMode mode = (gameMode as SinglePlayerMode);
return (mode == null) ? null : mode.map;
}
}
public Submarine Submarine
{
get { return submarine; }
}
public string SaveFile
{
get { return saveFile; }
}
public GameSession(Submarine submarine, string saveFile, GameModePreset gameModePreset)
:this(submarine, saveFile, gameModePreset.Instantiate())
{
}
public GameSession(Submarine selectedSub, string saveFile, GameMode gameMode = null)
{
taskManager = new TaskManager(this);
this.saveFile = saveFile;
guiRoot = new GUIFrame(new Rectangle(0,0,Game1.GraphicsWidth,Game1.GraphicsWidth), Color.Transparent);
this.gameMode = gameMode;
this.submarine = selectedSub;
}
public GameSession(Submarine selectedSub, string saveFile, string filePath)
: this(selectedSub, saveFile)
{
XDocument doc = ToolBox.TryLoadXml(filePath);
if (doc == null) return;
foreach (XElement subElement in doc.Root.Elements())
{
if (subElement.Name.ToString().ToLower() != "gamemode") continue;
gameMode = new SinglePlayerMode(subElement);
}
}
public void StartShift(TimeSpan duration, string levelSeed)
{
Level level = Level.CreateRandom(levelSeed);
StartShift(duration, level);
}
public void StartShift(TimeSpan duration, Level level, bool reloadSub = true)
{
Game1.LightManager.LosEnabled = (Game1.Server==null);
this.level = level;
if (reloadSub || Submarine.Loaded != submarine) submarine.Load();
if (level != null)
{
level.Generate(submarine == null ? 100.0f : Math.Max(Submarine.Borders.Width, Submarine.Borders.Height));
submarine.SetPosition(level.StartPosition - new Vector2(0.0f, 2000.0f));
}
if (Quest!=null) Quest.Start(Level.Loaded);
if (gameMode!=null) gameMode.Start(duration);
taskManager.StartShift(level);
}
public void EndShift(string endMessage)
{
if (Quest != null) Quest.End();
if (Game1.Server!=null)
{
Game1.Server.EndGame(endMessage);
}
else if (Game1.Client==null)
{
Game1.LobbyScreen.Select();
}
taskManager.EndShift();
//gameMode.End();
//return true;
}
public void KillCharacter(Character character)
{
SinglePlayerMode singlePlayerMode = gameMode as SinglePlayerMode;
if (singlePlayerMode == null) return;
singlePlayerMode.crewManager.KillCharacter(character);
}
public bool LoadPrevious(GUIButton button, object obj)
{
SaveUtil.LoadGame(saveFile);
Game1.LobbyScreen.Select();
return true;
}
public void Update(float deltaTime)
{
taskManager.Update(deltaTime);
guiRoot.Update(deltaTime);
if (gameMode != null) gameMode.Update(deltaTime);
}
public void Draw(SpriteBatch spriteBatch)
{
guiRoot.Draw(spriteBatch);
taskManager.Draw(spriteBatch);
if (gameMode != null) gameMode.Draw(spriteBatch);
}
public void Save(string filePath)
{
XDocument doc = new XDocument(
new XElement((XName)"Gamesession"));
var now = DateTime.Now;
doc.Root.Add(new XAttribute("savetime", now.Hour + ":" + now.Minute + ", " + now.ToShortDateString()));
((SinglePlayerMode)gameMode).Save(doc.Root);
try
{
doc.Save(filePath);
}
catch
{
DebugConsole.ThrowError("Saving gamesession to ''" + filePath + "'' failed!");
}
}
}
}

View File

@@ -0,0 +1,25 @@
using System.Collections.Generic;
namespace Subsurface
{
class HireManager
{
public List<CharacterInfo> availableCharacters;
const int MaxAvailableCharacters = 10;
public HireManager()
{
availableCharacters = new List<CharacterInfo>();
}
public void GenerateCharacters(string file, int amount)
{
for (int i = 0 ; i<amount ; i++)
{
availableCharacters.Add(new CharacterInfo(file));
}
}
}
}

View File

@@ -0,0 +1,51 @@
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Subsurface
{
class QuestMode : GameMode
{
Quest quest;
public override Quest Quest
{
get
{
return quest;
}
}
public QuestMode(GameModePreset preset)
: base(preset)
{
Location[] locations = new Location[2];
Random rand = new Random(Game1.NetLobbyScreen.LevelSeed.GetHashCode());
for (int i = 0; i < 2; i++)
{
locations[i] = Location.CreateRandom(new Vector2((float)rand.NextDouble() * 10000.0f, (float)rand.NextDouble() * 10000.0f));
}
quest = Quest.LoadRandom(locations, rand);
}
public override void Start(TimeSpan duration)
{
base.Start(duration);
new GUIMessageBox(quest.Name, quest.Description, 400, 400);
quest.Start(Level.Loaded);
}
public override void End(string endMessage = "")
{
quest.End();
base.End(endMessage);
}
}
}

View File

@@ -0,0 +1,253 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace Subsurface
{
class SinglePlayerMode : GameMode
{
//private const int StartCharacterAmount = 3;
public readonly CrewManager crewManager;
//public readonly HireManager hireManager;
private GUIButton endShiftButton;
public readonly CargoManager CargoManager;
public Map map;
private bool crewDead;
private float endTimer;
private bool savedOnStart;
public override Quest Quest
{
get
{
return map.SelectedConnection.Quest;
}
}
public int Money
{
get { return crewManager.Money; }
set { crewManager.Money = value; }
}
public SinglePlayerMode(GameModePreset preset)
: base(preset)
{
crewManager = new CrewManager();
CargoManager = new CargoManager();
endShiftButton = new GUIButton(new Rectangle(Game1.GraphicsWidth - 220, 20, 200, 25), "End shift", Alignment.TopLeft, GUI.style);
endShiftButton.OnClicked = EndShift;
for (int i = 0; i < 3; i++)
{
JobPrefab jobPrefab = null;
switch (i)
{
case 0:
jobPrefab = JobPrefab.List.Find(jp => jp.Name == "Captain");
break;
case 1:
jobPrefab = JobPrefab.List.Find(jp => jp.Name == "Engineer");
break;
case 2:
jobPrefab = JobPrefab.List.Find(jp => jp.Name == "Mechanic");
break;
}
CharacterInfo characterInfo =
new CharacterInfo(Character.HumanConfigFile, "", Gender.None, jobPrefab);
crewManager.characterInfos.Add(characterInfo);
}
}
public SinglePlayerMode(XElement element)
: this(GameModePreset.list.Find(gm => gm.Name == "Single Player"))
{
string mapSeed = ToolBox.GetAttributeString(element, "mapseed", "a");
GenerateMap(mapSeed);
map.SetLocation(ToolBox.GetAttributeInt(element, "currentlocation", 0));
foreach (XElement subElement in element.Elements())
{
if (subElement.Name.ToString().ToLower() != "crew") continue;
crewManager = new CrewManager(subElement);
}
}
public void GenerateMap(string seed)
{
map = new Map(seed, 500);
}
public override void Start(TimeSpan duration)
{
CargoManager.CreateItems();
if (!savedOnStart)
{
SaveUtil.SaveGame(Game1.GameSession.SaveFile);
savedOnStart = true;
}
endTimer = 5.0f;
crewManager.StartShift();
}
public bool TryHireCharacter(HireManager hireManager, CharacterInfo characterInfo)
{
if (crewManager.Money < characterInfo.Salary) return false;
hireManager.availableCharacters.Remove(characterInfo);
crewManager.characterInfos.Add(characterInfo);
crewManager.Money -= characterInfo.Salary;
return true;
}
public string GetMoney()
{
return ("Money: " + crewManager.Money);
}
public override void Draw(SpriteBatch spriteBatch)
{
base.Draw(spriteBatch);
crewManager.Draw(spriteBatch);
if (Level.Loaded.AtEndPosition)
{
endShiftButton.Text = "Enter " + map.SelectedLocation.Name;
endShiftButton.Draw(spriteBatch);
}
else if (Level.Loaded.AtStartPosition)
{
endShiftButton.Text = "Enter " + map.CurrentLocation.Name;
endShiftButton.Draw(spriteBatch);
}
//chatBox.Draw(spriteBatch);
//textBox.Draw(spriteBatch);
//timerBar.Draw(spriteBatch);
//if (Game1.Client == null) endShiftButton.Draw(spriteBatch);
}
public override void Update(float deltaTime)
{
base.Update(deltaTime);
crewManager.Update(deltaTime);
endShiftButton.Update(deltaTime);
if (!crewDead)
{
if (crewManager.characters.Find(c => !c.IsDead) == null)
{
crewDead = true;
}
}
else
{
endTimer -= deltaTime;
if (endTimer <= 0.0f) End("");
}
}
public override void End(string endMessage = "")
{
isRunning = false;
//if (endMessage != "" || this.endMessage == null) this.endMessage = endMessage;
StringBuilder sb = new StringBuilder();
List<Character> casualties = crewManager.characters.FindAll(c => c.IsDead);
if (casualties.Count == crewManager.characters.Count)
{
sb.Append("Your entire crew has died!");
var msgBox = new GUIMessageBox("", sb.ToString(), new string[] { "Load game", "Quit" });
msgBox.Buttons[0].OnClicked += Game1.GameSession.LoadPrevious;
msgBox.Buttons[0].OnClicked += msgBox.Close;
msgBox.Buttons[1].OnClicked = Game1.LobbyScreen.QuitToMainMenu;
msgBox.Buttons[1].OnClicked += msgBox.Close;
}
else
{
if (casualties.Any())
{
sb.Append("Casualties: \n");
foreach (Character c in casualties)
{
sb.Append(" - " + c.Info.Name + "\n");
}
}
else
{
sb.Append("No casualties!");
}
if (Level.Loaded.AtEndPosition)
{
map.MoveToNextLocation();
}
SaveUtil.SaveGame(Game1.GameSession.SaveFile);
}
crewManager.EndShift();
for (int i = Character.CharacterList.Count - 1; i >= 0; i--)
{
Character.CharacterList[i].Remove();
}
Game1.GameSession.EndShift("");
}
private bool EndShift(GUIButton button, object obj)
{
End("");
return true;
}
public void Save(XElement element)
{
//element.Add(new XAttribute("day", day));
XElement modeElement = new XElement("gamemode");
modeElement.Add(new XAttribute("currentlocation", map.CurrentLocationIndex));
modeElement.Add(new XAttribute("mapseed", map.Seed));
crewManager.Save(modeElement);
element.Add(modeElement);
}
}
}

View File

@@ -0,0 +1,74 @@
using System;
using System.Linq;
using Subsurface.Networking;
namespace Subsurface
{
class TraitorMode : GameMode
{
Client traitor;
Client target;
public TraitorMode(GameModePreset preset)
: base(preset)
{
}
public override void Start(TimeSpan duration)
{
base.Start(duration);
traitor = null;
target = null;
}
public override void Update(float deltaTime)
{
if (Game1.Server == null) return;
base.Update(deltaTime);
if (!isRunning) return;
if (DateTime.Now >= endTime)
{
string endMessage = traitor.character.Info.Name + " was a traitor! ";
endMessage += (traitor.character.Info.Gender == Gender.Male) ? "His" : "Her";
endMessage += " task was to assassinate " + target.character.Info.Name + ". The task was unsuccesful.";
End(endMessage);
return;
}
if (traitor==null || target ==null)
{
int clientCount = Game1.Server.connectedClients.Count();
if (clientCount < 2) return;
int traitorIndex = Rand.Int(clientCount, false);
traitor = Game1.Server.connectedClients[traitorIndex];
int targetIndex = 0;
while (targetIndex == traitorIndex)
{
targetIndex = Rand.Int(clientCount, false);
}
target = Game1.Server.connectedClients[targetIndex];
Game1.Server.NewTraitor(traitor, target);
}
else
{
if (target.character.IsDead)
{
string endMessage = traitor.character.Info.Name + " was a traitor! ";
endMessage += (traitor.character.Info.Gender == Gender.Male) ? "his" : "her";
endMessage += " task was to assassinate " + target.character.Info.Name + ". The task was succesful.";
End(endMessage);
}
}
}
}
}