Skip to content

Commit f3df310

Browse files
committed
add enemy navmesh and workshop
1 parent 3ef8330 commit f3df310

14 files changed

+856
-80
lines changed

Enemy_ProjectPos.cs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using UnityEngine;
7+
using UnityEngine.AI;
8+
9+
namespace KarlsonMapEditor
10+
{
11+
public class Enemy_ProjectPos : MonoBehaviour
12+
{
13+
private NavMeshAgent agent;
14+
public float delta = 0;
15+
16+
public void Start()
17+
{
18+
agent = GetComponent<NavMeshAgent>();
19+
agent.updatePosition = false;
20+
}
21+
22+
public void Update()
23+
{
24+
transform.position = agent.nextPosition - new Vector3(0, delta, 0);
25+
}
26+
}
27+
}

LevelEditor.cs

+22-9
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public static void _ongui()
201201
if (!editorMode) return;
202202
if (dg_screenshot)
203203
{
204-
GUI.Box(new Rect(Screen.width / 2 - 200, 10, 400, 25), "Point your camera to set the Thumbnail and press ENTER");
204+
GUI.Box(new Rect(Screen.width / 2 - 200, 10, 400, 25), "Point your camera to set the Thumbnail and press <b>ENTER</b>");
205205
return;
206206
}
207207
if (dg_enabled) wir[(int)WindowId.Prompt] = GUI.Window(wid[(int)WindowId.Prompt], wir[(int)WindowId.Prompt], (windowId) => {
@@ -274,7 +274,7 @@ void recurence(string file)
274274
GUI.Box(new Rect(0, 20, 150, 80), "");
275275
if (GUI.Button(new Rect(0, 20, 150, 20), "Save Map")) { SaveLevel(); dd_file = false; }
276276
if (GUI.Button(new Rect(0, 40, 150, 20), "Close Map")) { StartEdit(); dd_file = false; }
277-
if (GUI.Button(new Rect(0, 60, 150, 20), "Upload to Workshop")) Dialog("TBA", (_) => { }, "TBA");
277+
if (GUI.Button(new Rect(0, 60, 150, 20), "Upload to Workshop")) dg_screenshot = true;
278278
if (GUI.Button(new Rect(0, 80, 150, 20), "Exit Editor")) { ExitEditor(); dd_file = false; }
279279
}
280280

@@ -778,6 +778,16 @@ public static void _onupdate()
778778
{
779779
if (!editorMode || Camera.main == null) return;
780780

781+
if(dg_screenshot && Input.GetKey(KeyCode.Return))
782+
{
783+
dg_screenshot = false;
784+
var ss = MakeScreenshot();
785+
Dialog("Enter level name:", (name) =>
786+
{
787+
Workshop_API.Core.UploadLevel(new Workshop_API.KWM_Convert.KWM(name, ss, SaveLevelData()));
788+
});
789+
}
790+
781791
if (!Input.GetButton("Fire2") && Input.GetMouseButtonDown(2))
782792
{
783793
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
@@ -961,11 +971,10 @@ public static byte[] MakeScreenshot()
961971
UnityEngine.Object.Destroy(GOcam);
962972
return screenShot.EncodeToPNG();
963973
}
964-
965-
private static void SaveLevel()
974+
private static byte[] SaveLevelData()
966975
{
967-
using(MemoryStream ms = new MemoryStream())
968-
using(BinaryWriter bw = new BinaryWriter(ms))
976+
using (MemoryStream ms = new MemoryStream())
977+
using (BinaryWriter bw = new BinaryWriter(ms))
969978
{
970979
bw.Write(1);
971980
bw.Write(gridAlign);
@@ -984,13 +993,13 @@ private static void SaveLevel()
984993
foreach (var obj in objects)
985994
if (obj.internalObject) internalCount++;
986995
bw.Write(objects.Count - internalCount);
987-
foreach(var obj in objects)
996+
foreach (var obj in objects)
988997
{
989998
if (obj.internalObject) continue; // don't write internal objects
990999
bw.Write(obj.data.IsPrefab);
9911000
bw.Write(obj.go.name);
9921001
bw.Write(obj.data.GroupName);
993-
if(obj.data.IsPrefab)
1002+
if (obj.data.IsPrefab)
9941003
{
9951004
bw.Write(obj.data.PrefabId);
9961005
bw.Write(obj.aPosition);
@@ -1012,8 +1021,12 @@ private static void SaveLevel()
10121021
}
10131022
}
10141023
bw.Flush();
1015-
File.WriteAllBytes(Path.Combine(Main.directory, "Levels", levelName + ".kme"), SevenZipHelper.Compress(ms.ToArray()));
1024+
return SevenZipHelper.Compress(ms.ToArray());
10161025
}
1026+
}
1027+
private static void SaveLevel()
1028+
{
1029+
File.WriteAllBytes(Path.Combine(Main.directory, "Levels", levelName + ".kme"), SaveLevelData());
10171030
List<string> oldList = Main.prefs["edit_recent"].Split(';').ToList();
10181031
if (oldList.Contains(levelName + ".kme"))
10191032
oldList.Remove(levelName + ".kme");

LevelPlayer.cs

+64-30
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
using SevenZip.Compression.LZMA;
44
using System;
55
using System.CodeDom;
6+
using System.Collections;
67
using System.Collections.Generic;
78
using System.IO;
89
using System.Linq;
910
using System.Text;
1011
using System.Threading.Tasks;
1112
using UnityEngine;
13+
using UnityEngine.AI;
1214
using UnityEngine.Events;
1315
using UnityEngine.SceneManagement;
1416

@@ -48,46 +50,77 @@ public static void LoadLevelData(Scene arg0, LoadSceneMode arg1)
4850

4951
foreach (var obj in levelData.Objects)
5052
{
53+
if (obj.IsPrefab) continue;
5154
GameObject go;
52-
if (obj.IsPrefab)
55+
if(obj.Lava)
5356
{
54-
go = LevelData.MakePrefab(obj.PrefabId);
55-
if(obj.PrefabId == 11)
57+
go = LoadsonAPI.PrefabManager.NewGlass();
58+
UnityEngine.Object.Destroy(go.GetComponent<Glass>());
59+
go.AddComponent<Lava>();
60+
}
61+
else if (obj.Glass)
62+
go = LoadsonAPI.PrefabManager.NewGlass();
63+
else
64+
go = LoadsonAPI.PrefabManager.NewCube();
65+
if (obj.TextureId < Main.gameTex.Length)
66+
go.GetComponent<MeshRenderer>().material.mainTexture = Main.gameTex[obj.TextureId];
67+
else
68+
go.GetComponent<MeshRenderer>().material.mainTexture = levelData.Textures[obj.TextureId - Main.gameTex.Length];
69+
go.GetComponent<MeshRenderer>().material.color = obj._Color;
70+
if (obj.Bounce)
71+
go.GetComponent<BoxCollider>().material = LoadsonAPI.PrefabManager.BounceMaterial();
72+
if (obj.DisableTrigger)
73+
go.GetComponent<BoxCollider>().isTrigger = false;
74+
go.transform.position = obj.Position;
75+
go.transform.rotation = Quaternion.Euler(obj.Rotation);
76+
go.transform.localScale = obj.Scale;
77+
}
78+
// create navmesh
79+
GameObject navmeshGO = new GameObject("NavMesh Surface");
80+
navmeshGO.transform.position = levelData.startPosition;
81+
var navmeshs = navmeshGO.AddComponent<NavMeshSurface>();
82+
navmeshs.useGeometry = NavMeshCollectGeometry.RenderMeshes;
83+
navmeshs.collectObjects = CollectObjects.All;
84+
navmeshs.BuildNavMesh();
85+
Loadson.Console.Log("navmesh pos: " + navmeshs.navMeshData.position);
86+
87+
foreach (var obj in levelData.Objects)
88+
{
89+
if (!obj.IsPrefab) continue;
90+
GameObject go;
91+
go = LevelData.MakePrefab(obj.PrefabId);
92+
go.transform.position = obj.Position;
93+
go.transform.rotation = Quaternion.Euler(obj.Rotation);
94+
go.transform.localScale = obj.Scale;
95+
if (obj.PrefabId == 11)
96+
{
97+
Enemy e = go.GetComponent<Enemy>();
98+
if (obj.PrefabData != 0)
5699
{
57-
Enemy e = go.GetComponent<Enemy>();
58-
if(obj.PrefabData != 0)
59-
{
60-
e.startGun = LevelData.MakePrefab(obj.PrefabData - 1);
61-
typeof(Enemy).GetMethod("GiveGun", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).Invoke(e, Array.Empty<object>());
62-
}
100+
e.startGun = LevelData.MakePrefab(obj.PrefabData - 1);
101+
typeof(Enemy).GetMethod("GiveGun", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).Invoke(e, Array.Empty<object>());
102+
103+
go.GetComponent<NavMeshAgent>().enabled = true;
104+
go.GetComponent<NavMeshAgent>().enabled = false;
105+
go.GetComponent<NavMeshAgent>().enabled = true;
63106
}
107+
obj._enemyFix = go;
64108
}
65-
else
109+
}
110+
IEnumerator enemyFix()
111+
{
112+
yield return new WaitForEndOfFrame();
113+
foreach (var obj in levelData.Objects)
66114
{
67-
if(obj.Lava)
115+
if (obj.PrefabId == 11)
68116
{
69-
go = LoadsonAPI.PrefabManager.NewGlass();
70-
UnityEngine.Object.Destroy(go.GetComponent<Glass>());
71-
go.AddComponent<Lava>();
117+
float deltaY = obj._enemyFix.GetComponent<NavMeshAgent>().nextPosition.y - obj.Position.y;
118+
Loadson.Console.Log("delta: " + deltaY);
119+
obj._enemyFix.AddComponent<Enemy_ProjectPos>().delta = deltaY;
72120
}
73-
else if (obj.Glass)
74-
go = LoadsonAPI.PrefabManager.NewGlass();
75-
else
76-
go = LoadsonAPI.PrefabManager.NewCube();
77-
if (obj.TextureId < Main.gameTex.Length)
78-
go.GetComponent<MeshRenderer>().material.mainTexture = Main.gameTex[obj.TextureId];
79-
else
80-
go.GetComponent<MeshRenderer>().material.mainTexture = levelData.Textures[obj.TextureId - Main.gameTex.Length];
81-
go.GetComponent<MeshRenderer>().material.color = obj._Color;
82-
if (obj.Bounce)
83-
go.GetComponent<BoxCollider>().material = LoadsonAPI.PrefabManager.BounceMaterial();
84-
if (obj.DisableTrigger)
85-
go.GetComponent<BoxCollider>().isTrigger = false;
86121
}
87-
go.transform.position = obj.Position;
88-
go.transform.rotation = Quaternion.Euler(obj.Rotation);
89-
go.transform.localScale = obj.Scale;
90122
}
123+
Coroutines.StartCoroutine(enemyFix());
91124
}
92125

93126
public class LevelData
@@ -193,6 +226,7 @@ public LevelObject(Vector3 position, Vector3 rotation, Vector3 scale, int textur
193226
public bool DisableTrigger;
194227

195228
public int PrefabData;
229+
public GameObject _enemyFix;
196230

197231
public override string ToString()
198232
{

LoadsonMod.csproj

+5
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@
248248
<Compile Include="7z\SevenZipHelper.cs" />
249249
<Compile Include="BinaryExtensions.cs" />
250250
<Compile Include="ColorPicker.cs" />
251+
<Compile Include="Enemy_ProjectPos.cs" />
251252
<Compile Include="FilePicker.cs" />
252253
<Compile Include="GUIex.cs" />
253254
<Compile Include="LevelEditor.cs" />
@@ -256,6 +257,10 @@
256257
<Compile Include="LobbyPatch.cs" />
257258
<Compile Include="Main.cs" />
258259
<Compile Include="MilkHooks.cs" />
260+
<Compile Include="NavMeshComponents\NavMeshLink.cs" />
261+
<Compile Include="NavMeshComponents\NavMeshModifier.cs" />
262+
<Compile Include="NavMeshComponents\NavMeshModifierVolume.cs" />
263+
<Compile Include="NavMeshComponents\NavMeshSurface.cs" />
259264
<Compile Include="PlayerMovementPatch.cs" />
260265
<Compile Include="Properties\AssemblyInfo.cs" />
261266
<Compile Include="Workshop API\Core.cs" />

LobbyPatch.cs

+2-16
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,7 @@ public static void RenderMenuPage(int page)
137137
text.transform.localPosition = new Vector3(13.8f, -17.8543f, 0f);
138138
text.transform.rotation = Quaternion.Euler(0f, -90f, 0f);
139139
text.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
140-
InterceptButton(go.GetComponent<Button>(), () =>
141-
{
142-
Loadson.Console.Log("Loading level idx: " + idx);
143-
Loadson.Console.Log("Loading level: " + levelList[idx]);
144-
LevelPlayer.LoadLevel(levelList[idx]);
145-
});
146-
Loadson.Console.Log("[r 10");
140+
InterceptButton(go.GetComponent<Button>(), () => LevelPlayer.LoadLevel(levelList[idx]));
147141
continue;
148142
}
149143
// read kwm
@@ -153,18 +147,10 @@ public static void RenderMenuPage(int page)
153147
Texture2D tex = new Texture2D(1, 1);
154148
tex.LoadImage(level.Thumbnail);
155149
go.GetComponent<Image>().sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0, 0));
156-
InterceptButton(go.GetComponent<Button>(), () =>
157-
{
158-
Loadson.Console.Log("Loading level idx: " + idx);
159-
Loadson.Console.Log("[WML] Loading level: " + levelList[idx]);
160-
LevelPlayer.LoadLevel(Path.GetFileName(levelList[idx]), level.LevelData);
161-
});
150+
InterceptButton(go.GetComponent<Button>(), () => LevelPlayer.LoadLevel(Path.GetFileName(levelList[idx]), level.LevelData));
162151
}
163-
Loadson.Console.Log("[r 1");
164152
GameObject.Find("/UI/Custom/PrevPage").GetComponent<Button>().interactable = page != 1;
165-
Loadson.Console.Log("[r 2");
166153
GameObject.Find("/UI/Custom/NextPage").GetComponent<Button>().interactable = (levelList.Length - 1) / 12 >= page;
167-
Loadson.Console.Log("[r 3");
168154
}
169155
}
170156
}

Main.cs

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.Diagnostics;
45
using System.IO;
56
using System.Linq;
67
using System.Linq.Expressions;
@@ -76,6 +77,11 @@ public override void OnEnable()
7677
WorkshopGUI.OpenWorkshop();
7778
}),
7879
("Editor", () => LevelEditor.StartEdit()),
80+
("<size=25>Open Maps\nFolder</size>", () => Process.Start(Path.Combine(directory, "Levels"))),
81+
/*("<size=25>Load MLL\nlevel</size>", () => {
82+
string file = FilePicker.PickFile("Open a MLL file", "MLL level (.mll)\0*.mll\0All files (*.*)\0*.*\0\0");
83+
if (file == "null") return;
84+
}),*/
7985
}, "Map Editor");
8086

8187
AddAPIFunction("KarlsonMapEditor.PickFile", (args) =>
@@ -92,21 +98,13 @@ public override void OnEnable()
9298
ColorPicker.imCircle = LoadAsset<Texture2D>("imCircle");
9399

94100
if (!DiscordAPI.HasDiscord)
95-
Loadson.Console.Log("Discord not found. You will not be able to upload levels to the workshop");
101+
Loadson.Console.Log("Discord not found. You will not be able to like/upload levels to the workshop");
96102
else
97103
{
98104
new Thread(() =>
99105
{
100-
Loadson.Console.Log("Awaiting discord..");
101106
while (DiscordAPI.User.Id == 0) Thread.Sleep(200);
102-
Loadson.Console.Log("Got discord user id " + DiscordAPI.User.Id);
103-
while (DiscordAPI.Bearer.Length < 2)
104-
{
105-
Loadson.Console.Log("Waiting for bearer (" + DiscordAPI.Bearer + ")");
106-
Thread.Sleep(200);
107-
}
108-
Loadson.Console.Log("Got discord bearer " + DiscordAPI.Bearer);
109-
Loadson.Console.Log("[WAPI] Logging in as " + DiscordAPI.User.Id + " " + DiscordAPI.Bearer);
107+
while (DiscordAPI.Bearer == "") Thread.Sleep(200);
110108
int[] ta;
111109
(workshopToken, ta) = Core.Login(DiscordAPI.User.Id, DiscordAPI.Bearer);
112110
workshopLikes = ta.ToList();
@@ -122,10 +120,13 @@ public override void OnGUI()
122120
{
123121
LevelEditor._ongui();
124122
WorkshopGUI._OnGUI();
125-
if(loginWid != -1 && workshopToken == "")
123+
if(loginWid != -1 && workshopToken == "" && !noDiscordAck)
126124
GUI.Window(loginWid, loginRect, (_) => {
127125
if (!LoadsonAPI.DiscordAPI.HasDiscord)
126+
{
128127
GUI.Label(new Rect(5, 20, 200, 30), "Discord was not detected");
128+
if (GUI.Button(new Rect(160, 20, 35, 20), "Ok")) noDiscordAck = true;
129+
}
129130
else if (LoadsonAPI.DiscordAPI.User.Id == 0)
130131
GUI.Label(new Rect(5, 20, 200, 30), "Awaiting Discord User");
131132
else if (DiscordAPI.Bearer.Length < 2)
@@ -150,7 +151,8 @@ public override void Update(float deltaTime)
150151
public static string directory;
151152
public static Texture2D[] gameTex;
152153
public static string workshopToken = "";
153-
public static List<int> workshopLikes;
154+
public static List<int> workshopLikes = new List<int>();
154155
public static List<Action> runOnMain = new List<Action>();
156+
static bool noDiscordAck = false;
155157
}
156158
}

0 commit comments

Comments
 (0)