4
4
using System ;
5
5
using System . Collections ;
6
6
using System . Collections . Generic ;
7
+ using System . Diagnostics ;
7
8
using System . IO ;
8
9
using System . Linq ;
9
10
using System . Management . Instrumentation ;
@@ -20,6 +21,8 @@ namespace KarlsonMapEditor
20
21
{
21
22
public static class LevelEditor
22
23
{
24
+ private delegate bool skipObject ( EditorObject obj ) ;
25
+
23
26
private static bool _initd = false ;
24
27
private static void _init ( )
25
28
{
@@ -85,6 +88,7 @@ public static void StartEdit()
85
88
static Camera gizmoCamera ;
86
89
static Vector3 gizmoMovePos ;
87
90
static Vector3 gizmoMoveDirection = Vector3 . zero ;
91
+ static float oldGizmoDistance ;
88
92
89
93
static string targetGroup = "" ;
90
94
static GUIex . Dropdown enemyGun ;
@@ -269,15 +273,16 @@ void recurence(string file)
269
273
if ( GUI . Button ( new Rect ( 0 , 0 , 100 , 20 ) , "File" ) ) { dd_file = ! dd_file ; dd_level = false ; }
270
274
if ( GUI . Button ( new Rect ( 100 , 0 , 100 , 20 ) , "Map" ) ) { dd_level = ! dd_level ; dd_file = false ; }
271
275
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") ;
273
277
274
278
if ( dd_file )
275
279
{
276
- GUI . Box ( new Rect ( 0 , 20 , 150 , 80 ) , "" ) ;
280
+ GUI . Box ( new Rect ( 0 , 20 , 150 , 100 ) , "" ) ;
277
281
if ( GUI . Button ( new Rect ( 0 , 20 , 150 , 20 ) , "Save Map" ) ) { SaveLevel ( ) ; dd_file = false ; }
278
282
if ( GUI . Button ( new Rect ( 0 , 40 , 150 , 20 ) , "Close Map" ) ) { StartEdit ( ) ; dd_file = false ; }
279
283
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 ; }
281
286
}
282
287
283
288
if ( dd_level )
@@ -844,7 +849,6 @@ public static void _onupdate()
844
849
845
850
if ( selObj != - 1 )
846
851
{
847
- float distanceToGizmo = Vector3 . Distance ( Camera . main . transform . position , clickGizmo [ 0 ] . transform . position ) ;
848
852
clickGizmo [ 0 ] . transform . position = objects [ selObj ] . go . transform . position ;
849
853
clickGizmo [ 1 ] . transform . position = objects [ selObj ] . go . transform . position + new Vector3 ( 0 , 0 , 1 ) ;
850
854
clickGizmo [ 1 ] . transform . rotation = Quaternion . Euler ( 90 , 0 , 0 ) ;
@@ -854,19 +858,20 @@ public static void _onupdate()
854
858
clickGizmo [ 3 ] . transform . rotation = Quaternion . Euler ( 0 , 0 , 90 ) ;
855
859
if ( Input . GetKey ( KeyCode . LeftShift ) )
856
860
{
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 ) ;
867
872
}
868
- // check for hovering gizmo
869
873
874
+ // check for hovering gizmo
870
875
Ray ray = gizmoCamera . ScreenPointToRay ( Input . mousePosition ) ;
871
876
RaycastHit hit ;
872
877
bool onGizmo = false ;
@@ -911,12 +916,13 @@ void rotateX(int x)
911
916
if ( clickGizmo [ 3 ] == hit . transform . gameObject )
912
917
gizmoMoveDirection = - clickGizmo [ 3 ] . transform . up ;
913
918
}
919
+ oldGizmoDistance = hit . distance ;
914
920
}
915
921
if ( Input . GetMouseButton ( 0 ) && gizmoMoveDirection != Vector3 . zero )
916
922
{
917
- Vector3 delta = gizmoMoveDirection * Vector3Extensions . DistanceOnDirection ( gizmoMovePos , ray . GetPoint ( distanceToGizmo ) , gizmoMoveDirection ) ;
923
+ Vector3 delta = gizmoMoveDirection * Vector3Extensions . DistanceOnDirection ( gizmoMovePos , ray . GetPoint ( oldGizmoDistance ) , gizmoMoveDirection ) ;
918
924
objects [ selObj ] . aPosition += delta ;
919
- gizmoMovePos = ray . GetPoint ( distanceToGizmo ) ;
925
+ gizmoMovePos = ray . GetPoint ( oldGizmoDistance ) ;
920
926
}
921
927
if ( Input . GetMouseButtonUp ( 0 ) )
922
928
{
@@ -988,7 +994,7 @@ public static byte[] MakeScreenshot()
988
994
UnityEngine . Object . Destroy ( GOcam ) ;
989
995
return screenShot . EncodeToPNG ( ) ;
990
996
}
991
- private static byte [ ] SaveLevelData ( )
997
+ private static byte [ ] SaveLevelDataRaw ( skipObject skipIfTrue )
992
998
{
993
999
using ( MemoryStream ms = new MemoryStream ( ) )
994
1000
using ( BinaryWriter bw = new BinaryWriter ( ms ) )
@@ -1008,11 +1014,11 @@ private static byte[] SaveLevelData()
1008
1014
}
1009
1015
int internalCount = 0 ;
1010
1016
foreach ( var obj in objects )
1011
- if ( obj . internalObject ) internalCount ++ ;
1017
+ if ( obj . internalObject || skipIfTrue ( obj ) ) internalCount ++ ;
1012
1018
bw . Write ( objects . Count - internalCount ) ;
1013
1019
foreach ( var obj in objects )
1014
1020
{
1015
- if ( obj . internalObject ) continue ; // don't write internal objects
1021
+ if ( obj . internalObject || skipIfTrue ( obj ) ) continue ; // don't write internal objects
1016
1022
bw . Write ( obj . data . IsPrefab ) ;
1017
1023
bw . Write ( obj . go . name ) ;
1018
1024
bw . Write ( obj . data . GroupName ) ;
@@ -1039,9 +1045,11 @@ private static byte[] SaveLevelData()
1039
1045
}
1040
1046
}
1041
1047
bw . Flush ( ) ;
1042
- return SevenZipHelper . Compress ( ms . ToArray ( ) ) ;
1048
+ return ms . ToArray ( ) ;
1043
1049
}
1044
1050
}
1051
+ private static byte [ ] SaveLevelDataRaw ( ) => SaveLevelDataRaw ( ( _ ) => false ) ;
1052
+ private static byte [ ] SaveLevelData ( ) => SevenZipHelper . Compress ( SaveLevelDataRaw ( ) ) ;
1045
1053
private static void SaveLevel ( )
1046
1054
{
1047
1055
File . WriteAllBytes ( Path . Combine ( Main . directory , "Levels" , levelName + ".kme" ) , SaveLevelData ( ) ) ;
@@ -1054,6 +1062,34 @@ private static void SaveLevel()
1054
1062
Main . prefs [ "edit_recent" ] = string . Join ( ";" , oldList ) ;
1055
1063
}
1056
1064
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
+
1057
1093
private static void LoadLevel ( string path )
1058
1094
{
1059
1095
LevelPlayer . LevelData data = new LevelPlayer . LevelData ( File . ReadAllBytes ( path ) ) ;
0 commit comments