First commit
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
|
||||
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(GameSession session)
|
||||
{
|
||||
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, 400), Color.Transparent, guiFrame);
|
||||
listBox.ScrollBarEnabled = false;
|
||||
listBox.OnSelected = SelectCharacter;
|
||||
|
||||
money = 10000;
|
||||
}
|
||||
|
||||
private string CreateSaveFile(string mapName)
|
||||
{
|
||||
string path = "Content/Data/Saves/";
|
||||
|
||||
string name = Path.GetFileNameWithoutExtension(mapName);
|
||||
|
||||
int i = 0;
|
||||
while (File.Exists(path+name + i))
|
||||
{
|
||||
i++;
|
||||
}
|
||||
|
||||
return path + name + i;
|
||||
}
|
||||
|
||||
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, 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.Black,
|
||||
Alignment.Left,
|
||||
Alignment.Left,
|
||||
frame);
|
||||
textBlock.Padding = new Vector4(5.0f, 0.0f, 5.0f, 0.0f);
|
||||
|
||||
GUIImage face = new GUIImage(new Rectangle(-10,-10,0,0), character.animController.limbs[0].sprite, Alignment.Left, frame);
|
||||
}
|
||||
|
||||
public void KillCharacter(Character killedCharacter)
|
||||
{
|
||||
GUIComponent characterBlock = listBox.GetChild(killedCharacter) as GUIComponent;
|
||||
if (characterBlock != null) characterBlock.Color = Color.DarkRed * 0.5f;
|
||||
|
||||
}
|
||||
|
||||
public void StartShift()
|
||||
{
|
||||
foreach (CharacterInfo ci in characterInfos)
|
||||
{
|
||||
WayPoint randomWayPoint = WayPoint.GetRandom(WayPoint.SpawnType.Human);
|
||||
Vector2 position = (randomWayPoint == null) ? Vector2.Zero : randomWayPoint.SimPosition;
|
||||
|
||||
Character character = new Character(ci.file, position, ci);
|
||||
Character.Controlled = character;
|
||||
AddCharacter(character);
|
||||
}
|
||||
|
||||
if (characters.Count>0) SelectCharacter(characters[0]);
|
||||
}
|
||||
|
||||
public void EndShift()
|
||||
{
|
||||
foreach (Character c in characters)
|
||||
{
|
||||
if (!c.IsDead) 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Subsurface
|
||||
{
|
||||
class GameMode
|
||||
{
|
||||
public static List<GameMode> list = new List<GameMode>();
|
||||
|
||||
//TimeSpan duration;
|
||||
protected DateTime startTime;
|
||||
protected DateTime endTime;
|
||||
|
||||
protected bool isRunning;
|
||||
|
||||
protected string name;
|
||||
|
||||
private string endMessage;
|
||||
|
||||
public DateTime StartTime
|
||||
{
|
||||
get { return startTime; }
|
||||
}
|
||||
|
||||
public DateTime EndTime
|
||||
{
|
||||
get { return endTime; }
|
||||
}
|
||||
|
||||
public bool IsRunning
|
||||
{
|
||||
get { return isRunning; }
|
||||
}
|
||||
|
||||
public string Name
|
||||
{
|
||||
get { return name; }
|
||||
}
|
||||
|
||||
public string EndMessage
|
||||
{
|
||||
get { return endMessage; }
|
||||
}
|
||||
|
||||
public GameMode(string name)
|
||||
{
|
||||
this.name = name;
|
||||
|
||||
list.Add(this);
|
||||
}
|
||||
|
||||
public virtual void Start(TimeSpan duration)
|
||||
{
|
||||
startTime = DateTime.Now;
|
||||
endTime = startTime + duration;
|
||||
|
||||
endMessage = "The round has ended!";
|
||||
|
||||
isRunning = true;
|
||||
}
|
||||
|
||||
public virtual void Update()
|
||||
{
|
||||
if (!isRunning) return;
|
||||
|
||||
if (DateTime.Now >= endTime)
|
||||
{
|
||||
End(endMessage);
|
||||
}
|
||||
}
|
||||
|
||||
public void End(string endMessage = "")
|
||||
{
|
||||
isRunning = false;
|
||||
|
||||
if (endMessage != "" || this.endMessage == null) this.endMessage = endMessage;
|
||||
|
||||
Game1.gameSession.EndShift(null, null);
|
||||
}
|
||||
|
||||
public static void Init()
|
||||
{
|
||||
new GameMode("Sandbox");
|
||||
new TraitorMode("Traitor");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,234 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
|
||||
namespace Subsurface
|
||||
{
|
||||
class GameSession
|
||||
{
|
||||
public readonly TaskManager taskManager;
|
||||
public readonly CrewManager crewManager;
|
||||
public readonly HireManager hireManager;
|
||||
|
||||
protected DateTime startTime;
|
||||
protected DateTime endTime;
|
||||
|
||||
public readonly GameMode gameMode;
|
||||
|
||||
private GUIListBox chatBox;
|
||||
private GUITextBox textBox;
|
||||
|
||||
private GUIProgressBar timerBar;
|
||||
|
||||
private GUIButton endShiftButton;
|
||||
|
||||
string saveFile;
|
||||
|
||||
private int day;
|
||||
|
||||
public string SaveFile
|
||||
{
|
||||
get { return saveFile; }
|
||||
}
|
||||
|
||||
public int Day
|
||||
{
|
||||
get { return day; }
|
||||
}
|
||||
|
||||
public GameSession(string selectedMapFile, bool save, TimeSpan gameDuration, GameMode gameMode = null)
|
||||
{
|
||||
taskManager = new TaskManager(this);
|
||||
crewManager = new CrewManager(this);
|
||||
hireManager = new HireManager();
|
||||
|
||||
hireManager.GenerateCharacters("Content/Characters/Human/human.xml", 10);
|
||||
|
||||
int width = 350, height = 100;
|
||||
chatBox = new GUIListBox(new Rectangle(
|
||||
Game1.GraphicsWidth - (int)GUI.style.smallPadding.X - width,
|
||||
Game1.GraphicsHeight - (int)GUI.style.smallPadding.W*2 - 25 - height,
|
||||
width, height),
|
||||
Color.White * 0.5f);
|
||||
|
||||
endShiftButton = new GUIButton(new Rectangle(Game1.GraphicsWidth - 240, 20, 100, 25), "End shift", Color.White, Alignment.CenterX, null);
|
||||
endShiftButton.OnClicked = EndShift;
|
||||
|
||||
timerBar = new GUIProgressBar(new Rectangle(Game1.GraphicsWidth - 120, 20, 100, 25), Color.Gold, 0.0f, null);
|
||||
|
||||
textBox = new GUITextBox(
|
||||
new Rectangle(chatBox.Rect.X, chatBox.Rect.Y + chatBox.Rect.Height + (int)GUI.style.smallPadding.W, chatBox.Rect.Width, 25),
|
||||
Color.White * 0.5f, Color.Black, Alignment.Bottom, Alignment.Left);
|
||||
textBox.OnEnter = EnterChatMessage;
|
||||
|
||||
this.gameMode = gameMode;
|
||||
if (this.gameMode != null) this.gameMode.Start(Game1.netLobbyScreen.GameDuration);
|
||||
|
||||
startTime = DateTime.Now;
|
||||
endTime = startTime + gameDuration;
|
||||
|
||||
if (!save) return;
|
||||
|
||||
CreateSaveFile(selectedMapFile);
|
||||
|
||||
day = 1;
|
||||
|
||||
}
|
||||
|
||||
public bool TryHireCharacter(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 void StartShift(int scriptedEventCount = 1)
|
||||
{
|
||||
crewManager.StartShift();
|
||||
taskManager.StartShift(scriptedEventCount);
|
||||
}
|
||||
|
||||
public bool EndShift(GUIButton button, object obj)
|
||||
{
|
||||
if (Game1.server!=null)
|
||||
{
|
||||
string endMessage = gameMode.EndMessage;
|
||||
|
||||
Game1.server.EndGame(endMessage);
|
||||
|
||||
}
|
||||
else if (Game1.client==null)
|
||||
{
|
||||
if (saveFile == null) return false;
|
||||
|
||||
Map.Save(Path.GetDirectoryName(saveFile) + "/", Path.GetFileName(saveFile));
|
||||
|
||||
crewManager.EndShift();
|
||||
|
||||
Game1.lobbyScreen.Select();
|
||||
|
||||
day++;
|
||||
}
|
||||
|
||||
taskManager.EndShift();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void CreateSaveFile(string mapName)
|
||||
{
|
||||
string path = "Content/Data/Saves/";
|
||||
|
||||
if (!Directory.Exists(path))
|
||||
{
|
||||
Directory.CreateDirectory(path);
|
||||
}
|
||||
|
||||
string name = Path.GetFileNameWithoutExtension(mapName);
|
||||
string extension = Path.GetExtension(mapName);
|
||||
|
||||
int i = 0;
|
||||
while (File.Exists(path + name + i + extension))
|
||||
{
|
||||
i++;
|
||||
}
|
||||
|
||||
saveFile = path + name + i+extension;
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
File.Copy(mapName, saveFile);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
DebugConsole.ThrowError("Copying map file ''" + mapName + "'' to ''" + saveFile + "'' failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
public bool EnterChatMessage(GUITextBox textBox, string message)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(message)) return false;
|
||||
|
||||
if (Game1.server!=null)
|
||||
{
|
||||
Game1.server.SendChatMessage(message);
|
||||
}
|
||||
else if (Game1.client!=null)
|
||||
{
|
||||
Game1.client.SendChatMessage(Game1.client.Name + ": " + message);
|
||||
}
|
||||
|
||||
textBox.Deselect();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void NewChatMessage(string text, Color color)
|
||||
{
|
||||
GUITextBlock msg = new GUITextBlock(new Rectangle(0, 0, 0, 20), text,
|
||||
((chatBox.CountChildren % 2) == 0) ? Color.Transparent : Color.Black * 0.1f, color,
|
||||
Alignment.Left, null, true);
|
||||
|
||||
msg.Padding = new Vector4(GUI.style.smallPadding.X, 0, 0, 0);
|
||||
chatBox.AddChild(msg);
|
||||
|
||||
while (chatBox.CountChildren > 20)
|
||||
{
|
||||
chatBox.RemoveChild(chatBox.children.First());
|
||||
}
|
||||
}
|
||||
|
||||
public void Update(float deltaTime)
|
||||
{
|
||||
taskManager.Update(deltaTime);
|
||||
|
||||
textBox.Update(deltaTime);
|
||||
|
||||
if (gameMode != null) gameMode.Update();
|
||||
|
||||
double duration = (endTime - startTime).TotalSeconds;
|
||||
double elapsedTime = (DateTime.Now-startTime).TotalSeconds;
|
||||
timerBar.BarSize = (float)(elapsedTime / Math.Max(duration, 1.0));
|
||||
|
||||
if (PlayerInput.KeyHit(Keys.Tab))
|
||||
{
|
||||
if (textBox.Selected)
|
||||
{
|
||||
textBox.Deselect();
|
||||
textBox.Text = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
textBox.Select();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Draw(SpriteBatch spriteBatch)
|
||||
{
|
||||
crewManager.Draw(spriteBatch);
|
||||
taskManager.Draw(spriteBatch);
|
||||
|
||||
chatBox.Draw(spriteBatch);
|
||||
textBox.Draw(spriteBatch);
|
||||
|
||||
timerBar.Draw(spriteBatch);
|
||||
|
||||
if (Game1.client == null) endShiftButton.Draw(spriteBatch);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
|
||||
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));
|
||||
Debug.Write(availableCharacters.Last().name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Subsurface.Networking;
|
||||
|
||||
namespace Subsurface
|
||||
{
|
||||
class TraitorMode : GameMode
|
||||
{
|
||||
Client traitor;
|
||||
Client target;
|
||||
|
||||
public TraitorMode(string name)
|
||||
: base(name)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override void Start(TimeSpan duration)
|
||||
{
|
||||
base.Start(duration);
|
||||
|
||||
traitor = null;
|
||||
target = null;
|
||||
}
|
||||
|
||||
public override void Update()
|
||||
{
|
||||
|
||||
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 = Game1.localRandom.Next(clientCount);
|
||||
traitor = Game1.server.connectedClients[traitorIndex];
|
||||
|
||||
int targetIndex = 0;
|
||||
while (targetIndex==traitorIndex)
|
||||
{
|
||||
targetIndex = Game1.localRandom.Next(clientCount);
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user