Client LuaCsForBarotrauma
TestGameMode.cs
1 using Microsoft.Xna.Framework;
2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
7 
8 namespace Barotrauma
9 {
11  {
12  public Action OnRoundEnd;
13 
14  public bool SpawnOutpost;
15 
18 
20 
21  private List<Event> scriptedEvent;
22 
23  private GUIButton createEventButton;
24 
26  {
27  foreach (JobPrefab jobPrefab in JobPrefab.Prefabs.OrderBy(p => p.Identifier))
28  {
29  for (int i = 0; i < jobPrefab.InitialCount; i++)
30  {
31  var variant = Rand.Range(0, jobPrefab.Variants);
32  CrewManager.AddCharacterInfo(new CharacterInfo(CharacterPrefab.HumanSpeciesName, jobOrJobPrefab: jobPrefab, variant: variant));
33  }
34  }
35  }
36 
37  public override void Start()
38  {
39  base.Start();
40 
42  foreach (Submarine submarine in Submarine.Loaded)
43  {
44  submarine.NeutralizeBallast();
45  //normally the body would be made static during level generation,
46  //but in the test mode we load the outpost/wreck/beacon as if it was a normal sub and need to do this manually
47  if (submarine.Info.Type == SubmarineType.Outpost ||
48  submarine.Info.Type == SubmarineType.OutpostModule ||
49  submarine.Info.Type == SubmarineType.Wreck ||
50  submarine.Info.Type == SubmarineType.BeaconStation)
51  {
52  submarine.PhysicsBody.BodyType = FarseerPhysics.BodyType.Static;
53  }
54  }
55 
56  if (SpawnOutpost)
57  {
58  GenerateOutpost(Submarine.MainSub);
59  }
60 
61  if (TriggeredEvent != null)
62  {
63  scriptedEvent = new List<Event> { TriggeredEvent.CreateInstance(GameMain.GameSession.EventManager.RandomSeed) };
64  GameMain.GameSession.EventManager.PinnedEvent = scriptedEvent.Last();
65 
66  createEventButton = new GUIButton(new RectTransform(new Point(128, 64), GUI.Canvas, Anchor.TopCenter) { ScreenSpaceOffset = new Point(0, 32) }, TextManager.Get("create"))
67  {
68  OnClicked = delegate
69  {
71  GameMain.GameSession.EventManager.PinnedEvent = scriptedEvent.Last();
72  return true;
73  }
74  };
75  }
76  }
77 
78  public override void AddToGUIUpdateList()
79  {
80  base.AddToGUIUpdateList();
81  createEventButton?.AddToGUIUpdateList();
82  }
83 
84  public override void End(CampaignMode.TransitionType transitionType = CampaignMode.TransitionType.None)
85  {
87  OnRoundEnd?.Invoke();
88  }
89 
90  public override void Update(float deltaTime)
91  {
92  base.Update(deltaTime);
93 
94  if (scriptedEvent != null)
95  {
96  foreach (Event sEvent in scriptedEvent.Where(sEvent => !sEvent.IsFinished))
97  {
98  sEvent.Update(deltaTime);
99  }
100  }
101  }
102 
103  private void GenerateOutpost(Submarine submarine)
104  {
105  Submarine outpost = OutpostGenerator.Generate(OutpostParams ?? OutpostGenerationParams.OutpostParams.GetRandomUnsynced(), OutpostType ?? LocationType.Prefabs.GetRandomUnsynced());
106  outpost.SetPosition(Vector2.Zero);
107 
108  float closestDistance = 0.0f;
109  DockingPort myPort = null, outPostPort = null;
110  foreach (DockingPort port in DockingPort.List)
111  {
112  if (port.IsHorizontal || port.Docked) { continue; }
113  if (port.Item.Submarine == outpost)
114  {
115  outPostPort = port;
116  continue;
117  }
118  if (port.Item.Submarine != submarine) { continue; }
119 
120  //the submarine port has to be at the top of the sub
121  if (port.Item.WorldPosition.Y < submarine.WorldPosition.Y) { continue; }
122 
123  float dist = Vector2.DistanceSquared(port.Item.WorldPosition, outpost.WorldPosition);
124  if ((myPort == null || dist < closestDistance || port.MainDockingPort) && !(myPort?.MainDockingPort ?? false))
125  {
126  myPort = port;
127  closestDistance = dist;
128  }
129  }
130 
131  if (myPort != null && outPostPort != null)
132  {
133  Vector2 portDiff = myPort.Item.WorldPosition - submarine.WorldPosition;
134  Vector2 spawnPos = (outPostPort.Item.WorldPosition - portDiff) - Vector2.UnitY * outPostPort.DockedDistance;
135 
136  submarine.SetPosition(spawnPos);
137  myPort.Dock(outPostPort);
138  myPort.Lock(true);
139  }
140 
141  if (Character.Controlled != null)
142  {
143  Character.Controlled.TeleportTo(outpost.GetWaypoints(false).GetRandomUnsynced(point => point.SpawnType == SpawnType.Human).WorldPosition);
144  }
145  }
146  }
147 }
Stores information about the Character that is needed between rounds in the menu etc....
static readonly Identifier HumanSpeciesName
Responsible for keeping track of the characters in the player crew, saving and loading their orders,...
Submarine Submarine
Definition: Entity.cs:53
virtual void Update(float deltaTime)
Definition: Event.cs:69
bool IsFinished
Definition: Event.cs:25
Event CreateInstance(int seed)
Definition: EventPrefab.cs:115
virtual void AddToGUIUpdateList(bool ignoreChildren=false, int order=0)
static GameSession?? GameSession
Definition: GameMain.cs:88
static readonly PrefabCollection< JobPrefab > Prefabs
static readonly PrefabCollection< LocationType > Prefabs
Definition: LocationType.cs:15
static readonly PrefabCollection< OutpostGenerationParams > OutpostParams
List< WayPoint > GetWaypoints(bool alsoFromConnectedSubs)
void SetPosition(Vector2 position, List< Submarine > checkd=null, bool forceUndockFromStaticSubmarines=true)
override void AddToGUIUpdateList()
Definition: TestGameMode.cs:78
EventPrefab TriggeredEvent
Definition: TestGameMode.cs:19
LocationType OutpostType
Definition: TestGameMode.cs:17
OutpostGenerationParams OutpostParams
Definition: TestGameMode.cs:16
override void End(CampaignMode.TransitionType transitionType=CampaignMode.TransitionType.None)
Definition: TestGameMode.cs:84
override void Start()
Definition: TestGameMode.cs:37
override void Update(float deltaTime)
Definition: TestGameMode.cs:90
TestGameMode(GameModePreset preset)
Definition: TestGameMode.cs:25