Skip to content

Commit 77d9835

Browse files
committed
v1.6 - QOL
1 parent b8f4be1 commit 77d9835

6 files changed

+158
-88
lines changed

BinaryExtensions.cs

+47
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,51 @@ public static void Write(this BinaryWriter bw, Color c)
4242
bw.Write(c.a);
4343
}
4444
}
45+
46+
public static class Vector3Extensions
47+
{
48+
public static float DistanceOnDirection(Vector3 origin, Vector3 point, Vector3 direction)
49+
{
50+
Vector3 translated = point - origin;
51+
if (direction.x != 0)
52+
return translated.x / direction.x;
53+
if (direction.y != 0)
54+
return translated.y / direction.y;
55+
if (direction.z != 0)
56+
return translated.z / direction.z;
57+
return 0f; // something went wrong. or point == origin
58+
}
59+
public static Vector3 Snap(Vector3 original, float snap)
60+
{
61+
Vector3 result = new Vector3();
62+
// 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;
70+
71+
// 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;
79+
80+
// 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;
88+
89+
return result;
90+
}
91+
}
4592
}

LevelEditor.cs

+47-30
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.IO;
88
using System.Linq;
99
using System.Management.Instrumentation;
10+
using System.Reflection;
1011
using System.Runtime.InteropServices;
1112
using System.Text;
1213
using System.Threading.Tasks;
@@ -79,11 +80,11 @@ public static void StartEdit()
7980
SceneManager.sceneLoaded += InitEditor;
8081
SceneManager.LoadScene(6);
8182
}
83+
const float clickGizmoScaleFactor = 0.1f;
8284
static List<GameObject> clickGizmo;
8385
static Camera gizmoCamera;
8486
static Vector3 gizmoMovePos;
8587
static Vector3 gizmoMoveDirection = Vector3.zero;
86-
static float oldGizmoDistance;
8788

8889
static string targetGroup = "";
8990
static GUIex.Dropdown enemyGun;
@@ -194,6 +195,7 @@ public static void Dialog(string caption, Action<string> action, string input =
194195

195196
private static int selObj = -1;
196197
private static float moveStep = 0.05f;
198+
private static bool aligned = false;
197199
private static Vector2 object_browser_scroll = new Vector2(0, 0);
198200

199201
public static void _ongui()
@@ -733,7 +735,7 @@ void recurence(string file)
733735
wir[(int)WindowId.LevelData] = GUI.Window(wid[(int)WindowId.LevelData], wir[(int)WindowId.LevelData], (windowId) => {
734736
GUI.DragWindow(new Rect(0, 0, 200, 20));
735737
GUI.Label(new Rect(5, 20, 100, 20), "Grid Align");
736-
GUI.TextField(new Rect(95, 20, 50, 20), "TBA");
738+
gridAlign = float.Parse(GUI.TextField(new Rect(95, 20, 50, 20), gridAlign.ToString("0.00")));
737739

738740
GUI.Label(new Rect(5, 40, 100, 20), "Starting Gun");
739741
startGunDD.Draw(new Rect(95, 40, 100, 20));
@@ -778,6 +780,7 @@ void recurence(string file)
778780
public static void _onupdate()
779781
{
780782
if (!editorMode || Camera.main == null) return;
783+
if (levelName == "") return;
781784

782785
if(dg_screenshot && Input.GetKey(KeyCode.Return))
783786
{
@@ -841,10 +844,27 @@ public static void _onupdate()
841844

842845
if (selObj != -1)
843846
{
847+
float distanceToGizmo = Vector3.Distance(Camera.main.transform.position, clickGizmo[0].transform.position);
844848
clickGizmo[0].transform.position = objects[selObj].go.transform.position;
845849
clickGizmo[1].transform.position = objects[selObj].go.transform.position + new Vector3(0, 0, 1);
850+
clickGizmo[1].transform.rotation = Quaternion.Euler(90, 0, 0);
846851
clickGizmo[2].transform.position = objects[selObj].go.transform.position + new Vector3(0, 1, 0);
852+
clickGizmo[2].transform.rotation = Quaternion.Euler(0, 0, 0);
847853
clickGizmo[3].transform.position = objects[selObj].go.transform.position + new Vector3(1, 0, 0);
854+
clickGizmo[3].transform.rotation = Quaternion.Euler(0, 0, 90);
855+
if (Input.GetKey(KeyCode.LeftShift))
856+
{
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);
867+
}
848868
// check for hovering gizmo
849869

850870
Ray ray = gizmoCamera.ScreenPointToRay(Input.mousePosition);
@@ -885,24 +905,23 @@ public static void _onupdate()
885905
{
886906
gizmoMovePos = ray.GetPoint(hit.distance);
887907
if (clickGizmo[1] == hit.transform.gameObject)
888-
gizmoMoveDirection = new Vector3(0, 0, 1);
908+
gizmoMoveDirection = clickGizmo[1].transform.up;
889909
if (clickGizmo[2] == hit.transform.gameObject)
890-
gizmoMoveDirection = new Vector3(0, 1, 0);
910+
gizmoMoveDirection = clickGizmo[2].transform.up;
891911
if (clickGizmo[3] == hit.transform.gameObject)
892-
gizmoMoveDirection = new Vector3(1, 0, 0);
912+
gizmoMoveDirection = -clickGizmo[3].transform.up;
893913
}
894-
oldGizmoDistance = hit.distance;
895914
}
896915
if (Input.GetMouseButton(0) && gizmoMoveDirection != Vector3.zero)
897916
{
898-
Vector3 delta = ray.GetPoint(oldGizmoDistance) - gizmoMovePos;
899-
delta.Scale(gizmoMoveDirection);
917+
Vector3 delta = gizmoMoveDirection * Vector3Extensions.DistanceOnDirection(gizmoMovePos, ray.GetPoint(distanceToGizmo), gizmoMoveDirection);
900918
objects[selObj].aPosition += delta;
901-
gizmoMovePos = ray.GetPoint(oldGizmoDistance);
919+
gizmoMovePos = ray.GetPoint(distanceToGizmo);
902920
}
903921
if (Input.GetMouseButtonUp(0))
904922
{
905923
gizmoMoveDirection = Vector3.zero;
924+
aligned = false;
906925
}
907926
}
908927
else
@@ -915,30 +934,27 @@ public static void _onupdate()
915934

916935
startPosition = objects[0].aPosition;
917936
startOrientation = objects[0].aRotation.y;
918-
919-
// update grid align
937+
938+
// grid align
920939
if(gridAlign != 0)
921940
{
922-
if (gridAlign < 0) gridAlign = 0;
923-
foreach (var obj in objects)
941+
if(gridAlign < 0)
924942
{
925-
Vector3 delta = obj.aPosition;
926-
while (delta.x >= gridAlign)
927-
delta.x -= gridAlign;
928-
if (2 * delta.x / gridAlign >= 1)
929-
delta.x = gridAlign - delta.x;
930-
931-
while (delta.y >= gridAlign)
932-
delta.y -= gridAlign;
933-
if (2 * delta.y / gridAlign >= 1)
934-
delta.y = gridAlign - delta.y;
935-
936-
while (delta.z >= gridAlign)
937-
delta.z -= gridAlign;
938-
if (2 * delta.z / gridAlign >= 1)
939-
delta.z = gridAlign - delta.z;
940-
941-
obj.aPosition -= delta;
943+
gridAlign = 0;
944+
}
945+
else
946+
{
947+
if (!aligned)
948+
{
949+
Loadson.Console.Log("Aligning objects");
950+
foreach (var obj in objects)
951+
{
952+
obj.aPosition += Vector3Extensions.Snap(obj.aPosition, gridAlign) - obj.aPosition;
953+
obj.aRotation += Vector3Extensions.Snap(obj.aRotation, gridAlign) - obj.aRotation;
954+
obj.aScale += Vector3Extensions.Snap(obj.aScale, gridAlign) - obj.aScale;
955+
}
956+
aligned = true;
957+
}
942958
}
943959
}
944960
}
@@ -1056,6 +1072,7 @@ private static void LoadLevel(string path)
10561072
globalPhysics = true; ToggleGlobalPhysics();
10571073
targetGroup = "";
10581074
selObj = -1;
1075+
aligned = false;
10591076
}
10601077

10611078
private static IEnumerator identifyObject(GameObject go)

LevelPlayer.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public LevelData(byte[] _data)
147147
Loadson.Console.Log("Loading level version " + version);
148148
if (version == 1)
149149
LoadLevel_Version1(br);
150-
else
150+
else if(version == 2)
151151
{
152152
gridAlign = br.ReadSingle();
153153
startingGun = br.ReadInt32();
@@ -179,6 +179,11 @@ public LevelData(byte[] _data)
179179
}
180180
Objects = objects.ToArray();
181181
}
182+
else
183+
{
184+
Loadson.Console.Log("<color=red>Unknown level version " + version + "</color>");
185+
Loadson.Console.Log("Try to update KME to the latest version.");
186+
}
182187
}
183188
}
184189

_lmr.tracker

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
KarlsonMapEditor|v1.5|https://github.com/karlsonmodding/KarlsonMapEditor/releases/download/v1/KarlsonMapEditor.klm
1+
KarlsonMapEditor|v1.6|https://github.com/karlsonmodding/KarlsonMapEditor/releases/download/v1/KarlsonMapEditor.klm

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.5</LMR>
3+
description=Create, Play and Download custom Karlson Maps\nA whole workshop awaits you!\n\n<LMR>v1.6</LMR>
44
deps=

z_file format.txt

+56-55
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
conventions:
22
_data [
3-
int32 - number of bytes
4-
byte[] - bytes
5-
6-
Write with:
7-
bw.Write(data.Length);
8-
bw.Write(data);
3+
int32 - number of bytes
4+
byte[] - bytes
5+
6+
Write with:
7+
bw.Write(data.Length);
8+
bw.Write(data);
99

10-
Read with:
11-
int _len = bw.ReadInt32();
12-
byte[] data = bw.ReadBytes(_len);
10+
Read with:
11+
int _len = bw.ReadInt32();
12+
byte[] data = bw.ReadBytes(_len);
1313
]
1414

1515
_vector3 [
1616
float(single) - x
1717
float(single) - y
1818
float(single) - z
1919

20-
Write with:
21-
[BinaryExtensions]
22-
bw.Write(v);
23-
[Default]
24-
bw.Write(v.x);
25-
bw.Write(v.y);
26-
bw.Write(v.z);
20+
Write with:
21+
[BinaryExtensions]
22+
bw.Write(v);
23+
[Default]
24+
bw.Write(v.x);
25+
bw.Write(v.y);
26+
bw.Write(v.z);
2727

28-
Read with:
29-
[BinaryExtensions]
30-
Vector3 v = bw.ReadVector3();
31-
[Default]
32-
Vector3 v = new Vector3(bw.ReadSingle(), bw.ReadSingle(), bw.ReadSingle());
28+
Read with:
29+
[BinaryExtensions]
30+
Vector3 v = bw.ReadVector3();
31+
[Default]
32+
Vector3 v = new Vector3(bw.ReadSingle(), bw.ReadSingle(), bw.ReadSingle());
3333
]
3434

3535
_color [
@@ -38,56 +38,57 @@ _color [
3838
float(single) - b
3939
float(single) - a
4040

41-
Write with:
42-
[BinaryExtensions]
43-
bw.Write(c);
44-
[Default]
45-
bw.Write(c.r);
46-
bw.Write(c.g);
47-
bw.Write(c.b);
48-
bw.Write(c.a);
41+
Write with:
42+
[BinaryExtensions]
43+
bw.Write(c);
44+
[Default]
45+
bw.Write(c.r);
46+
bw.Write(c.g);
47+
bw.Write(c.b);
48+
bw.Write(c.a);
4949

50-
Read with:
51-
[BinaryExtensions]
52-
Vector3 v = bw.ReadColor();
53-
[Default]
54-
Vector3 v = new Color(bw.ReadSingle(), bw.ReadSingle(), bw.ReadSingle(), bw.ReadSingle());
50+
Read with:
51+
[BinaryExtensions]
52+
Vector3 v = bw.ReadColor();
53+
[Default]
54+
Vector3 v = new Color(bw.ReadSingle(), bw.ReadSingle(), bw.ReadSingle(), bw.ReadSingle());
5555
]
5656

5757

5858
[.kme] Karlson Map Editor
5959
Encoded with LZMA: Decompress before reading, Compress before writing.
6060
Written with BinaryWriter / Read with BinaryReader
61-
int32 - Level Data Version
61+
int32 - Level Data Version (currently 2)
6262
float(single) - grid align
6363
int32 - starting gun
6464
_vector3 - spawn position
6565
float(single) - spawn orientation
6666
int32 - number of custom textures
6767
foreach [number of custom textures]
68-
string - texture name
68+
string - texture name
6969
_data - texture data (Texture2D.EncodeToPNG())
7070
int32 - number of objects
7171
foreach [number of objects]
72-
bool - prefab
73-
string - name
74-
string - group
75-
if [prefab]
76-
int32 - prefab id
77-
_vector3 - position
78-
_vector3 - rotation (euler)
79-
_vector3 - scale
80-
int32 - prefab data (used for enemies, gun id)
81-
else
82-
_vector3 - position
83-
_vector3 - rotation (euler)
84-
_vector3 - scale
85-
int32 - texture index
86-
_color - mesh renderer material color
87-
boolean - bounce
88-
boolean - glass
89-
boolean - lava
90-
boolean - coll disable trigger
72+
bool - prefab
73+
string - name
74+
string - group
75+
if [prefab]
76+
int32 - prefab id
77+
_vector3 - position
78+
_vector3 - rotation (euler)
79+
_vector3 - scale
80+
int32 - prefab data (used for enemies, gun id)
81+
else
82+
_vector3 - position
83+
_vector3 - rotation (euler)
84+
_vector3 - scale
85+
int32 - texture index
86+
_color - mesh renderer material color
87+
boolean - bounce
88+
boolean - glass
89+
boolean - lava
90+
boolean - coll disable trigger
91+
boolean - mark as object
9192

9293
[.kwm] Karlson Workshop Map
9394
Encoded with LZMA: Decompress before reading, Compress before writing.

0 commit comments

Comments
 (0)