-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomGame.cs
260 lines (214 loc) · 11 KB
/
CustomGame.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
using System.Collections.Generic;
using TaleWorlds.Core;
using TaleWorlds.Engine;
using TaleWorlds.Engine.Screens;
using TaleWorlds.InputSystem;
using TaleWorlds.Library;
using TaleWorlds.MountAndBlade;
using TaleWorlds.MountAndBlade.View.Screens;
using TaleWorlds.ObjectSystem;
using TaleWorlds.ScreenSystem;
namespace BannerLord_CustomMission {
public class CustomGame {
public class GameLoader : MBGameManager {
protected override void DoLoadingForGameManager(GameManagerLoadingSteps gameManagerLoadingStep, out GameManagerLoadingSteps nextStep) {
nextStep = GameManagerLoadingSteps.None;
switch (gameManagerLoadingStep) {
case GameManagerLoadingSteps.PreInitializeZerothStep:
MBGameManager.LoadModuleData(false);
MBGlobals.InitializeReferences();
/* Load our custom game type! */
Game.CreateGame(new GameType(), this).DoLoading();
nextStep = GameManagerLoadingSteps.FirstInitializeFirstStep;
return;
case GameManagerLoadingSteps.FirstInitializeFirstStep:
bool loadingComplete = true;
foreach (MBSubModuleBase mbsubModuleBase in Module.CurrentModule.SubModules)
loadingComplete &= mbsubModuleBase.DoLoading(Game.Current);
nextStep = (loadingComplete ? GameManagerLoadingSteps.WaitSecondStep : GameManagerLoadingSteps.FirstInitializeFirstStep);
return;
case GameManagerLoadingSteps.WaitSecondStep:
MBGameManager.StartNewGame();
nextStep = GameManagerLoadingSteps.SecondInitializeThirdState;
return;
case GameManagerLoadingSteps.SecondInitializeThirdState:
nextStep = (Game.Current.DoLoading() ? GameManagerLoadingSteps.PostInitializeFourthState : GameManagerLoadingSteps.SecondInitializeThirdState);
return;
case GameManagerLoadingSteps.PostInitializeFourthState:
nextStep = GameManagerLoadingSteps.FinishLoadingFifthStep;
return;
case GameManagerLoadingSteps.FinishLoadingFifthStep:
nextStep = GameManagerLoadingSteps.None;
return;
default:
return;
}
}
public override void OnLoadFinished() {
base.OnLoadFinished();
/* Push our GameState! */
GameStateManager.Current.CleanAndPushState(GameStateManager.Current.CreateState<GameState>());
}
}
public class GameType : TaleWorlds.Core.GameType {
public override bool IsCoreOnlyGameMode => true;
public override bool IsDevelopment => false;
public string TypeString => "Campaign";
public GameType() { }
protected override void OnInitialize() {
IGameStarter gameStarter = new BasicGameStarter();
this.RegisterGameModels(gameStarter);
this.LoadGameText(CurrentGame.GameTextManager);
GameManager.InitializeGameStarter(CurrentGame, gameStarter);
GameManager.OnGameStart(CurrentGame, gameStarter);
CurrentGame.SetBasicModels(gameStarter.Models);
CurrentGame.CreateGameManager();
GameManager.BeginGameStart(CurrentGame);
CurrentGame.InitializeDefaultGameObjects();
this.LoadGameXml();
CurrentGame.ObjectManager.UnregisterNonReadyObjects();
CurrentGame.SetDefaultEquipments(new Dictionary<string, Equipment>());
CurrentGame.ObjectManager.UnregisterNonReadyObjects();
GameManager.OnNewCampaignStart(CurrentGame, null);
GameManager.OnAfterCampaignStart(CurrentGame);
GameManager.OnGameInitializationFinished(CurrentGame);
}
protected override void DoLoadingForGameType(GameTypeLoadingStates gameTypeLoadingState, out GameTypeLoadingStates nextState) {
nextState = GameTypeLoadingStates.None;
switch (gameTypeLoadingState) {
case GameTypeLoadingStates.InitializeFirstStep:
base.CurrentGame.Initialize();
nextState = GameTypeLoadingStates.WaitSecondStep;
return;
case GameTypeLoadingStates.WaitSecondStep:
nextState = GameTypeLoadingStates.LoadVisualsThirdState;
return;
case GameTypeLoadingStates.LoadVisualsThirdState:
nextState = GameTypeLoadingStates.PostInitializeFourthState;
break;
case GameTypeLoadingStates.PostInitializeFourthState:
break;
default:
return;
}
}
private void RegisterGameModels(IGameStarter basicGameStarter) {
basicGameStarter.AddModel(new DefaultAgentApplyDamageModel());
basicGameStarter.AddModel(new DefaultDamageParticleModel());
basicGameStarter.AddModel(new DefaultMissionDifficultyModel());
basicGameStarter.AddModel(new DefaultRidingModel());
basicGameStarter.AddModel(new DefaultStrikeMagnitudeModel());
basicGameStarter.AddModel(new SandBox.GameComponents.SandboxAutoBlockModel());
basicGameStarter.AddModel(new CustomBattleAgentStatCalculateModel());
basicGameStarter.AddModel(new CustomBattleApplyWeatherEffectsModel());
basicGameStarter.AddModel(new CustomBattleInitializationModel());
basicGameStarter.AddModel(new CustomBattleMoraleModel());
}
private void LoadXML(string id) {
ObjectManager.LoadXML(id, IsDevelopment, TypeString);
}
private void LoadGameText(GameTextManager gameTextManager) {
gameTextManager.LoadGameTexts();
}
private void LoadGameXml() {
// Native files:
LoadXML("Monsters");
LoadXML("SkeletonScales");
LoadXML("ItemModifiers");
LoadXML("ItemModifierGroups");
LoadXML("CraftingPieces");
LoadXML("WeaponDescriptions");
LoadXML("CraftingTemplates");
LoadXML("BodyProperties");
LoadXML("SkillSets");
// Sandbox files:
LoadXML("Items");
LoadXML("EquipmentRosters");
LoadXML("NPCCharacters");
LoadXML("SPCultures");
}
protected override void OnRegisterTypes(MBObjectManager objectManager) {
objectManager.RegisterType<BasicCharacterObject>("NPCCharacter", "NPCCharacters", 43U, true, false);
objectManager.RegisterType<BasicCultureObject>("Culture", "SPCultures", 17U, true, false);
}
public override void OnStateChanged(TaleWorlds.Core.GameState oldState) { }
protected override void BeforeRegisterTypes(MBObjectManager objectManager) { }
public override void OnDestroy() { }
}
public class GameState : TaleWorlds.Core.GameState {
private Color _debugGreen;
public Color DebugGreen {
get {
if (_debugGreen == null) {
_debugGreen = Color.ConvertStringToColor("#527A2D");
}
return this._debugGreen;
}
}
protected override void OnTick(float dt) {
if (Input.IsKeyReleased(InputKey.F1)) {
CustomMission.MissionManager.OpenSceneEditor();
}
}
}
[GameStateScreen(typeof(BannerLord_CustomMission.CustomGame.GameState))]
public class CustomGameScene : ScreenBase, IGameStateListener {
private TaleWorlds.Core.GameState State;
private Scene Scene;
private Camera Camera;
private SceneLayer SceneLayer;
public CustomGameScene(TaleWorlds.Core.GameState state) {
this.State = state;
}
protected override void OnInitialize() {
base.OnInitialize();
this.SceneLayer = new SceneLayer("SceneLayer");
base.AddLayer(this.SceneLayer);
this.SceneLayer.SceneView.SetResolutionScaling(true);
this.Camera = Camera.CreateCamera();
Common.MemoryCleanupGC();
}
protected override void OnFinalize() {
base.OnFinalize();
}
protected override void OnActivate() {
this.Scene = Scene.CreateNewScene();
this.Scene.SetPlaySoundEventsAfterReadyToRender(true);
/*
* Note: If you load a scene with some scripting objects, ensure the game is in proper state, otherwise you'll crash the engine.
* I.e., if you try to load a castle defense scene without having an active MissionState, kaboom! This is handled through
* engine callbacks so, we can't do much to protect against this.
*/
this.Scene.Read("empty_scene");
for (int i = 0; i < 40; i++) {
this.Scene.Tick(0.1f);
}
Vec3 vec = default(Vec3);
GameEntity cameraInstance = this.Scene.FindEntityWithTag("camera_instance");
cameraInstance.GetCameraParamsFromCameraScript(this.Camera, ref vec);
SoundManager.SetListenerFrame(this.Camera.Frame);
if (this.SceneLayer != null) {
this.SceneLayer.SetScene(this.Scene);
this.SceneLayer.SceneView.SetEnable(true);
this.SceneLayer.SceneView.SetSceneUsesShadows(true);
}
LoadingWindow.DisableGlobalLoadingWindow();
base.OnActivate();
}
protected override void OnFrameTick(float dt) {
base.OnFrameTick(dt);
if (this.SceneLayer != null && this.SceneLayer.SceneView.ReadyToRender()) {
this.SceneLayer.SetCamera(this.Camera);
}
this.Scene.Tick(dt);
}
protected override void OnDeactivate() {
base.OnDeactivate();
}
void IGameStateListener.OnActivate() { }
void IGameStateListener.OnDeactivate() { }
void IGameStateListener.OnInitialize() { }
void IGameStateListener.OnFinalize() { }
}
}
}