Skip to content

Commit 5c17378

Browse files
committed
hotfix
1 parent 77d9835 commit 5c17378

File tree

3 files changed

+97
-46
lines changed

3 files changed

+97
-46
lines changed

BinaryExtensions.cs

+39-24
Original file line numberDiff line numberDiff line change
@@ -48,43 +48,58 @@ public static class Vector3Extensions
4848
public static float DistanceOnDirection(Vector3 origin, Vector3 point, Vector3 direction)
4949
{
5050
Vector3 translated = point - origin;
51-
if (direction.x != 0)
51+
if (direction.x > 0.01f)
5252
return translated.x / direction.x;
53-
if (direction.y != 0)
53+
if (direction.y > 0.01f)
5454
return translated.y / direction.y;
55-
if (direction.z != 0)
55+
if (direction.z > 0.01f)
5656
return translated.z / direction.z;
5757
return 0f; // something went wrong. or point == origin
5858
}
5959
public static Vector3 Snap(Vector3 original, float snap)
6060
{
6161
Vector3 result = new Vector3();
6262
// x axis
63-
float comp = original.x;
64-
if (original.x < 0)
65-
comp = -comp;
66-
while (comp >= snap) comp -= snap;
67-
result.x = original.x - comp;
68-
if (original.x < 0)
69-
result.x = original.x + comp;
63+
if (Mathf.Abs(original.x) < 0.01f)
64+
result.x = 0f;
65+
else
66+
{
67+
float comp = original.x + snap / 2f;
68+
if (original.x < 0)
69+
comp = -comp;
70+
while (comp >= snap) comp -= snap;
71+
result.x = original.x + snap / 2f - comp;
72+
if (original.x < 0)
73+
result.x = original.x - snap / 2f + comp;
74+
}
7075

7176
// y axis
72-
comp = original.y;
73-
if (original.y < 0)
74-
comp = -comp;
75-
while (comp >= snap) comp -= snap;
76-
result.y = original.y - comp;
77-
if (original.y < 0)
78-
result.y = original.y + comp;
77+
if (Mathf.Abs(original.y) < 0.01f)
78+
result.y = 0f;
79+
else
80+
{
81+
float comp = original.y + snap / 2f;
82+
if (original.y < 0)
83+
comp = -comp;
84+
while (comp >= snap) comp -= snap;
85+
result.y = original.y + snap / 2f - comp;
86+
if (original.y < 0)
87+
result.y = original.y - snap / 2f + comp;
88+
}
7989

8090
// z axis
81-
comp = original.z;
82-
if (original.z < 0)
83-
comp = -comp;
84-
while (comp >= snap) comp -= snap;
85-
result.z = original.z - comp;
86-
if (original.z < 0)
87-
result.z = original.z + comp;
91+
if (Mathf.Abs(original.z) < 0.01f)
92+
result.z = 0f;
93+
else
94+
{
95+
float comp = original.z + snap / 2f;
96+
if (original.z < 0)
97+
comp = -comp;
98+
while (comp >= snap) comp -= snap;
99+
result.z = original.z + snap / 2f - comp;
100+
if (original.z < 0)
101+
result.z = original.z - snap / 2f + comp;
102+
}
88103

89104
return result;
90105
}

LevelEditor.cs

+57-21
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections;
66
using System.Collections.Generic;
7+
using System.Diagnostics;
78
using System.IO;
89
using System.Linq;
910
using System.Management.Instrumentation;
@@ -20,6 +21,8 @@ namespace KarlsonMapEditor
2021
{
2122
public static class LevelEditor
2223
{
24+
private delegate bool skipObject(EditorObject obj);
25+
2326
private static bool _initd = false;
2427
private static void _init()
2528
{
@@ -85,6 +88,7 @@ public static void StartEdit()
8588
static Camera gizmoCamera;
8689
static Vector3 gizmoMovePos;
8790
static Vector3 gizmoMoveDirection = Vector3.zero;
91+
static float oldGizmoDistance;
8892

8993
static string targetGroup = "";
9094
static GUIex.Dropdown enemyGun;
@@ -269,15 +273,16 @@ void recurence(string file)
269273
if (GUI.Button(new Rect(0, 0, 100, 20), "File")) { dd_file = !dd_file; dd_level = false; }
270274
if (GUI.Button(new Rect(100, 0, 100, 20), "Map")) { dd_level = !dd_level; dd_file = false; }
271275
if (GUI.Button(new Rect(205, 0, 100, 20), "Tex Browser")) tex_browser_enabled = !tex_browser_enabled;
272-
GUI.Label(new Rect(310, 0, 1000, 20), $"<b>Karlson Map Editor</b> | Current map: <b>{levelName}</b> | Object count: <b>{objects.Count}</b> | Hold <b>right click</b> down to move and look around | Select an object by <b>middle clicking</b> it");
276+
GUI.Label(new Rect(310, 0, 1000, 20), $"<b>Karlson Map Editor</b> v1.7 | Current map: <b>{levelName}</b> | Object count: <b>{objects.Count}</b> | Hold <b>right click</b> down to move and look around | Select an object by <b>middle clicking</b> it");
273277

274278
if (dd_file)
275279
{
276-
GUI.Box(new Rect(0, 20, 150, 80), "");
280+
GUI.Box(new Rect(0, 20, 150, 100), "");
277281
if (GUI.Button(new Rect(0, 20, 150, 20), "Save Map")) { SaveLevel(); dd_file = false; }
278282
if (GUI.Button(new Rect(0, 40, 150, 20), "Close Map")) { StartEdit(); dd_file = false; }
279283
if (GUI.Button(new Rect(0, 60, 150, 20), "Upload to Workshop")) dg_screenshot = true;
280-
if (GUI.Button(new Rect(0, 80, 150, 20), "Exit Editor")) { ExitEditor(); dd_file = false; }
284+
if (GUI.Button(new Rect(0, 80, 150, 20), "Export for KMP")) { ExportKMP(); dd_file = false; }
285+
if (GUI.Button(new Rect(0, 100, 150, 20), "Exit Editor")) { ExitEditor(); dd_file = false; }
281286
}
282287

283288
if(dd_level)
@@ -844,7 +849,6 @@ public static void _onupdate()
844849

845850
if (selObj != -1)
846851
{
847-
float distanceToGizmo = Vector3.Distance(Camera.main.transform.position, clickGizmo[0].transform.position);
848852
clickGizmo[0].transform.position = objects[selObj].go.transform.position;
849853
clickGizmo[1].transform.position = objects[selObj].go.transform.position + new Vector3(0, 0, 1);
850854
clickGizmo[1].transform.rotation = Quaternion.Euler(90, 0, 0);
@@ -854,19 +858,20 @@ public static void _onupdate()
854858
clickGizmo[3].transform.rotation = Quaternion.Euler(0, 0, 90);
855859
if (Input.GetKey(KeyCode.LeftShift))
856860
{
857-
// rotate gizmo
858-
void rotateX(int x)
859-
{
860-
clickGizmo[x].transform.RotateAround(clickGizmo[0].transform.position, new Vector3(1, 0, 0), objects[selObj].aRotation.x);
861-
clickGizmo[x].transform.RotateAround(clickGizmo[0].transform.position, new Vector3(0, 1, 0), objects[selObj].aRotation.y);
862-
clickGizmo[x].transform.RotateAround(clickGizmo[0].transform.position, new Vector3(0, 0, 1), objects[selObj].aRotation.z);
863-
}
864-
rotateX(1);
865-
rotateX(2);
866-
rotateX(3);
861+
if (clickGizmo[1].gameObject.activeSelf)
862+
clickGizmo[1].gameObject.SetActive(false);
863+
clickGizmo[2].transform.position = objects[selObj].go.transform.position + objects[selObj].go.transform.up;
864+
clickGizmo[2].transform.rotation = objects[selObj].go.transform.rotation;
865+
clickGizmo[3].transform.position = objects[selObj].go.transform.position + objects[selObj].go.transform.right;
866+
clickGizmo[3].transform.rotation = Quaternion.Euler(objects[selObj].go.transform.rotation.eulerAngles + new Vector3(0f, 0f, 90f));
867+
}
868+
else
869+
{
870+
if (!clickGizmo[1].gameObject.activeSelf)
871+
clickGizmo[1].gameObject.SetActive(true);
867872
}
868-
// check for hovering gizmo
869873

874+
// check for hovering gizmo
870875
Ray ray = gizmoCamera.ScreenPointToRay(Input.mousePosition);
871876
RaycastHit hit;
872877
bool onGizmo = false;
@@ -911,12 +916,13 @@ void rotateX(int x)
911916
if (clickGizmo[3] == hit.transform.gameObject)
912917
gizmoMoveDirection = -clickGizmo[3].transform.up;
913918
}
919+
oldGizmoDistance = hit.distance;
914920
}
915921
if (Input.GetMouseButton(0) && gizmoMoveDirection != Vector3.zero)
916922
{
917-
Vector3 delta = gizmoMoveDirection * Vector3Extensions.DistanceOnDirection(gizmoMovePos, ray.GetPoint(distanceToGizmo), gizmoMoveDirection);
923+
Vector3 delta = gizmoMoveDirection * Vector3Extensions.DistanceOnDirection(gizmoMovePos, ray.GetPoint(oldGizmoDistance), gizmoMoveDirection);
918924
objects[selObj].aPosition += delta;
919-
gizmoMovePos = ray.GetPoint(distanceToGizmo);
925+
gizmoMovePos = ray.GetPoint(oldGizmoDistance);
920926
}
921927
if (Input.GetMouseButtonUp(0))
922928
{
@@ -988,7 +994,7 @@ public static byte[] MakeScreenshot()
988994
UnityEngine.Object.Destroy(GOcam);
989995
return screenShot.EncodeToPNG();
990996
}
991-
private static byte[] SaveLevelData()
997+
private static byte[] SaveLevelDataRaw(skipObject skipIfTrue)
992998
{
993999
using (MemoryStream ms = new MemoryStream())
9941000
using (BinaryWriter bw = new BinaryWriter(ms))
@@ -1008,11 +1014,11 @@ private static byte[] SaveLevelData()
10081014
}
10091015
int internalCount = 0;
10101016
foreach (var obj in objects)
1011-
if (obj.internalObject) internalCount++;
1017+
if (obj.internalObject || skipIfTrue(obj)) internalCount++;
10121018
bw.Write(objects.Count - internalCount);
10131019
foreach (var obj in objects)
10141020
{
1015-
if (obj.internalObject) continue; // don't write internal objects
1021+
if (obj.internalObject || skipIfTrue(obj)) continue; // don't write internal objects
10161022
bw.Write(obj.data.IsPrefab);
10171023
bw.Write(obj.go.name);
10181024
bw.Write(obj.data.GroupName);
@@ -1039,9 +1045,11 @@ private static byte[] SaveLevelData()
10391045
}
10401046
}
10411047
bw.Flush();
1042-
return SevenZipHelper.Compress(ms.ToArray());
1048+
return ms.ToArray();
10431049
}
10441050
}
1051+
private static byte[] SaveLevelDataRaw() => SaveLevelDataRaw((_) => false);
1052+
private static byte[] SaveLevelData() => SevenZipHelper.Compress(SaveLevelDataRaw());
10451053
private static void SaveLevel()
10461054
{
10471055
File.WriteAllBytes(Path.Combine(Main.directory, "Levels", levelName + ".kme"), SaveLevelData());
@@ -1054,6 +1062,34 @@ private static void SaveLevel()
10541062
Main.prefs["edit_recent"] = string.Join(";", oldList);
10551063
}
10561064

1065+
1066+
private static void ExportKMP()
1067+
{
1068+
Directory.CreateDirectory(Path.Combine(Main.directory, "KMP_Export"));
1069+
File.WriteAllBytes(Path.Combine(Main.directory, "KMP_Export", levelName + ".kme_raw"), SaveLevelDataRaw((obj) => obj.go.name.StartsWith("KMPSpawn_")));
1070+
// lookup kmp spawnpoints
1071+
List<(string, Vector3, float)> spawnPos = new List<(string, Vector3, float)>();
1072+
foreach(var obj in objects)
1073+
{
1074+
if (obj.go.name.StartsWith("KMPSpawn_"))
1075+
spawnPos.Add((obj.go.name.Substring(9), obj.aPosition, obj.aRotation.y));
1076+
}
1077+
if (spawnPos.Count == 0)
1078+
spawnPos.Add(("default", objects[0].aPosition, objects[0].aRotation.y));
1079+
using(FileStream fs = File.OpenWrite(Path.Combine(Main.directory, "KMP_Export", levelName + ".kme_data")))
1080+
using(BinaryWriter bw = new BinaryWriter(fs))
1081+
{
1082+
bw.Write(spawnPos.Count);
1083+
foreach (var x in spawnPos)
1084+
{
1085+
bw.Write(x.Item1);
1086+
bw.Write(x.Item2);
1087+
bw.Write(x.Item3);
1088+
}
1089+
}
1090+
Process.Start(Path.Combine(Path.Combine(Main.directory, "KMP_Export")));
1091+
}
1092+
10571093
private static void LoadLevel(string path)
10581094
{
10591095
LevelPlayer.LevelData data = new LevelPlayer.LevelData(File.ReadAllBytes(path));

metadata.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
displayname=Karlson Map Editor
22
author=devilExE
3-
description=Create, Play and Download custom Karlson Maps\nA whole workshop awaits you!\n\n<LMR>v1.6</LMR>
3+
description=Create, Play and Download custom Karlson Maps\nA whole workshop awaits you!\n\n<LMR>v1.7</LMR>
44
deps=

0 commit comments

Comments
 (0)