Elin Decompiled Documentation EA 23.102 Nightly
Loading...
Searching...
No Matches
MapPiece.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.IO;
4using UnityEngine;
5
6public class MapPiece : EScriptable
7{
8 public class MapPath
9 {
10 public string path;
11
12 public string tag;
13 }
14
15 public enum Type
16 {
17 Any,
18 Deco,
20 Trap,
21 Concert,
22 Farm
23 }
24
25 [Serializable]
26 public class Item
27 {
28 public string id;
29
30 public float chance;
31
32 [NonSerialized]
33 [HideInInspector]
34 public List<MapPath> paths = new List<MapPath>();
35 }
36
37 private static MapPiece _Instance;
38
39 public static bool initialized;
40
41 public List<Item> items;
42
43 public static Dictionary<string, PartialMap> CacheMap = new Dictionary<string, PartialMap>();
44
45 public static MapPiece Instance => _Instance ?? (_Instance = Resources.Load<MapPiece>("World/Map/MapPiece"));
46
48
49 public PartialMap GetMap(Type type, string tag, float ruin)
50 {
51 Init();
52 Item item = items.RandomItemWeighted((Item a) => (a.chance != 0f || !(type.ToString() == a.id)) ? (a.chance * (float)((type == Type.Any || type.ToString() == a.id) ? 1 : 0)) : 1f);
53 if (item.paths.Count == 0)
54 {
55 Debug.Log("no path");
56 return null;
57 }
58 string[] array = (tag.IsEmpty() ? null : tag.Split(','));
59 List<MapPath> list = new List<MapPath>();
60 foreach (MapPath path2 in item.paths)
61 {
62 if (path2.tag.IsEmpty() || (array != null && array.Contains(tag)))
63 {
64 list.Add(path2);
65 }
66 }
67 if (list.Count == 0)
68 {
69 return null;
70 }
71 string path = list.RandomItem().path;
72 Debug.Log("Loading PartialMap:" + path);
73 PartialMap partialMap = CacheMap.TryGetValue(path);
74 if (partialMap == null)
75 {
76 partialMap = PartialMap.Load(path);
77 CacheMap.Add(path, partialMap);
78 }
79 if (partialMap.allowRotate)
80 {
81 partialMap.dir = EScriptable.rnd(4);
82 }
83 partialMap.procedural = true;
84 partialMap.ruinChance = ruin;
85 return partialMap;
86 }
87
88 public void Init()
89 {
90 if (initialized)
91 {
92 return;
93 }
94 foreach (Item item in items)
95 {
96 item.paths.Clear();
97 string[] files = Directory.GetFiles(CorePath.MapPieceSave + item.id, "*", SearchOption.AllDirectories);
98 foreach (string text in files)
99 {
100 if (text.EndsWith("mp"))
101 {
102 DirectoryInfo directory = new FileInfo(text).Directory;
103 string tag = ((directory.Parent.Name != item.id) ? "" : directory.Name);
104 item.paths.Add(new MapPath
105 {
106 path = text,
107 tag = tag
108 });
109 }
110 }
111 }
112 initialized = true;
113 }
114
115 public void Reset()
116 {
117 initialized = false;
118 }
119}
bool enableMapPieceEditor
Definition: CoreDebug.cs:212
static string MapPieceSave
Definition: CorePath.cs:200
Definition: EClass.cs:5
static CoreDebug debug
Definition: EClass.cs:48
static int rnd(int a)
Definition: EScriptable.cs:5
float chance
Definition: MapPiece.cs:30
string id
Definition: MapPiece.cs:28
List< MapPath > paths
Definition: MapPiece.cs:34
static MapPiece Instance
Definition: MapPiece.cs:45
static Dictionary< string, PartialMap > CacheMap
Definition: MapPiece.cs:43
static bool initialized
Definition: MapPiece.cs:39
static MapPiece _Instance
Definition: MapPiece.cs:37
void Init()
Definition: MapPiece.cs:88
List< Item > items
Definition: MapPiece.cs:41
PartialMap GetMap(Type type, string tag, float ruin)
Definition: MapPiece.cs:49
static bool IsEditor
Definition: MapPiece.cs:47
void Reset()
Definition: MapPiece.cs:115
bool allowRotate
Definition: PartialMap.cs:55
static PartialMap Load(string path=null)
Definition: PartialMap.cs:448