Fix Spaghetti Coding + Refactoring
This commit is contained in:
@@ -5,11 +5,11 @@ using UnityEngine;
|
|||||||
//Only one should exist at the same time
|
//Only one should exist at the same time
|
||||||
public class BaseModelBehaviour : ModelBehaviour
|
public class BaseModelBehaviour : ModelBehaviour
|
||||||
{
|
{
|
||||||
public BaseModel baseModel
|
public BaseModel BaseModel
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if (model is BaseModel bModel)
|
if (Model is BaseModel bModel)
|
||||||
{
|
{
|
||||||
return bModel;
|
return bModel;
|
||||||
}
|
}
|
||||||
@@ -17,7 +17,7 @@ public class BaseModelBehaviour : ModelBehaviour
|
|||||||
}
|
}
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
model = value;
|
Model = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,15 +4,15 @@ using UnityEngine;
|
|||||||
|
|
||||||
public class ChildModelBehaviour : ModelBehaviour
|
public class ChildModelBehaviour : ModelBehaviour
|
||||||
{
|
{
|
||||||
public ChildModel childModel
|
public ChildModel ChildModel
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if (model == null)
|
if (Model == null)
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (model is ChildModel cModel)
|
if (Model is ChildModel cModel)
|
||||||
{
|
{
|
||||||
return cModel;
|
return cModel;
|
||||||
}
|
}
|
||||||
@@ -20,7 +20,7 @@ public class ChildModelBehaviour : ModelBehaviour
|
|||||||
}
|
}
|
||||||
internal set
|
internal set
|
||||||
{
|
{
|
||||||
model = value;
|
Model = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,8 @@
|
|||||||
public class Definitions
|
/// <summary>
|
||||||
|
/// Class containing all PORT definitions
|
||||||
|
/// Can contain future IDs aswell
|
||||||
|
/// </summary>
|
||||||
|
public class Definitions
|
||||||
{
|
{
|
||||||
// BIKE PORTS
|
// BIKE PORTS
|
||||||
public const string PORT_BIKE_SPROCKET = "bikeSprocket";
|
public const string PORT_BIKE_SPROCKET = "bikeSprocket";
|
||||||
@@ -10,6 +14,7 @@
|
|||||||
|
|
||||||
|
|
||||||
// ERLCAR PORTS
|
// ERLCAR PORTS
|
||||||
|
// TODO: rename all important ones
|
||||||
public const string PORT_ERLBACH_0 = "portErlbach0";
|
public const string PORT_ERLBACH_0 = "portErlbach0";
|
||||||
public const string PORT_ERLBACH_1 = "portErlbach1";
|
public const string PORT_ERLBACH_1 = "portErlbach1";
|
||||||
public const string PORT_ERLBACH_2 = "portErlbach2";
|
public const string PORT_ERLBACH_2 = "portErlbach2";
|
||||||
|
|||||||
@@ -37,62 +37,13 @@ public class Model
|
|||||||
this.scale = Vector3.one;
|
this.scale = Vector3.one;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool hasPorts()
|
public bool HasPorts()
|
||||||
{
|
{
|
||||||
return ports != null && ports.Count > 0;
|
return ports != null && ports.Count > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addPort(Port port)
|
public void AddPort(Port port)
|
||||||
{
|
{
|
||||||
ports.Add(port);
|
ports.Add(port);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Spawns all Port-GO's, should be called only once
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="parent"><c>gameObject</c> in the MonoBehaviour</param>
|
|
||||||
/// <returns>All spawned GO's</returns>
|
|
||||||
public List<GameObject> spawnAllPorts(GameObject parent)
|
|
||||||
{
|
|
||||||
Debug.Log($"{this.nameId}: Spawning {parent.name} count of {ports.Count} Ports");
|
|
||||||
List<GameObject> ret = new List<GameObject>();
|
|
||||||
for (int i = 0; i < ports.Count; i++)
|
|
||||||
{
|
|
||||||
var port = ports[i];
|
|
||||||
Debug.Log(i + ". Creating port " + port.port);
|
|
||||||
// bike:wheel:num
|
|
||||||
GameObject child = new GameObject(parent.name + ":" + port.port + ":" + i);
|
|
||||||
ChildModelBehaviour cmb = child.AddComponent<ChildModelBehaviour>();
|
|
||||||
|
|
||||||
var mm = Object.FindAnyObjectByType<ModelManager>();
|
|
||||||
if (mm == null)
|
|
||||||
{
|
|
||||||
Debug.LogError("ModelManager not found");
|
|
||||||
}
|
|
||||||
var model = mm.getById(port.defaultId);
|
|
||||||
|
|
||||||
if (model.mesh == null)
|
|
||||||
{
|
|
||||||
Debug.LogError("Default Mesh Not Found, destoying child");
|
|
||||||
Object.Destroy(child);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
Debug.Log($"Loaded Default Mesh: {model.mesh.name}");
|
|
||||||
//set mesh
|
|
||||||
var meshFilter = child.AddComponent<MeshFilter>();
|
|
||||||
meshFilter.mesh = model.mesh;
|
|
||||||
meshFilter.sharedMesh = model.mesh;
|
|
||||||
//set material
|
|
||||||
var meshRenderer = child.AddComponent<MeshRenderer>();
|
|
||||||
meshRenderer.material = model.material;
|
|
||||||
|
|
||||||
child.transform.SetParent(parent.transform); //makes this an actual child
|
|
||||||
port.apply(child.transform); // move to correct position
|
|
||||||
ret.Add(child); // i debugged 1.5 hours for this
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -10,7 +10,7 @@ public class ModelBehaviour : MonoBehaviour
|
|||||||
{
|
{
|
||||||
private Model _model;
|
private Model _model;
|
||||||
|
|
||||||
internal Model model
|
internal Model Model
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
@@ -18,68 +18,84 @@ public class ModelBehaviour : MonoBehaviour
|
|||||||
}
|
}
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
_model = value;
|
|
||||||
UpdateModel(value);
|
UpdateModel(value);
|
||||||
|
_model = value;
|
||||||
}
|
}
|
||||||
} //should be verified
|
} //should be verified
|
||||||
internal List<GameObject> children;
|
internal List<GameObject> Children = new List<GameObject>();
|
||||||
internal MeshFilter meshFilter;
|
internal MeshFilter meshFilter;
|
||||||
internal MeshRenderer meshRenderer;
|
internal MeshRenderer meshRenderer;
|
||||||
|
|
||||||
void Start()
|
void Start()
|
||||||
{
|
{
|
||||||
var filter = gameObject.GetComponent<MeshFilter>();
|
Init();
|
||||||
if (filter == null)
|
|
||||||
{
|
|
||||||
filter = gameObject.AddComponent<MeshFilter>();
|
|
||||||
}
|
|
||||||
meshFilter = filter;
|
|
||||||
|
|
||||||
var renderer = gameObject.GetComponent<MeshRenderer>();
|
|
||||||
if (renderer == null)
|
|
||||||
{
|
|
||||||
renderer = gameObject.AddComponent<MeshRenderer>();
|
|
||||||
}
|
|
||||||
meshRenderer = renderer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void UpdateModel(Model _model)
|
void Init() // unity my beloved edge case
|
||||||
{
|
{
|
||||||
|
if(meshFilter == null)
|
||||||
|
meshFilter = gameObject.GetOrAddComponent<MeshFilter>();
|
||||||
|
if(meshRenderer == null)
|
||||||
|
meshRenderer = gameObject.GetOrAddComponent<MeshRenderer>();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void UpdateModel(Model newModel)
|
||||||
|
{
|
||||||
|
if (newModel is BaseModel)
|
||||||
|
{
|
||||||
|
this.name = "BaseModel:" + newModel.nameId;
|
||||||
|
} else if (newModel is ChildModel cModel)
|
||||||
|
{
|
||||||
|
this.name = cModel.port + ":" + newModel.nameId;
|
||||||
|
}
|
||||||
|
Init();
|
||||||
|
Debug.Log($"Model {name} update to new {newModel} Model.");
|
||||||
|
//unapply model offset
|
||||||
|
if (Model != null)
|
||||||
|
{
|
||||||
|
transform.localPosition -= Model.offset;
|
||||||
|
//rotation + scale is set, not modified
|
||||||
|
}
|
||||||
|
|
||||||
//kill old children
|
//kill old children
|
||||||
foreach (var go in GetComponentsInChildren<ChildModelBehaviour>())
|
foreach (var compo in GetComponentsInChildren<ChildModelBehaviour>())
|
||||||
{
|
{
|
||||||
Debug.Log($"Destroying {go.name}");
|
if (compo == this) // why does InChildren include the parent
|
||||||
Destroy(go); // will be destroyed next frame
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Debug.Log($"Destroying {name}'s Child {compo.gameObject.name}");
|
||||||
|
Destroy(compo.gameObject); // will be destroyed next frame
|
||||||
}
|
}
|
||||||
|
|
||||||
//spawn new childPorts
|
//spawn new childPorts
|
||||||
if (model.hasPorts())
|
if (newModel.HasPorts())
|
||||||
{
|
{
|
||||||
Debug.Log($"Spawning {gameObject.name}'s ports");
|
Debug.Log($"Spawning {gameObject.name}'s ports");
|
||||||
children = model.spawnAllPorts(gameObject);
|
spawnChildPorts(newModel);
|
||||||
}
|
}
|
||||||
//change ourselves
|
//change ourselves
|
||||||
meshFilter.mesh = _model.mesh;
|
meshFilter.mesh = newModel.mesh;
|
||||||
meshFilter.sharedMesh = _model.mesh;
|
meshFilter.sharedMesh = newModel.mesh;
|
||||||
meshRenderer.material = _model.material;
|
meshRenderer.material = newModel.material;
|
||||||
meshRenderer.sharedMaterial = _model.material;
|
meshRenderer.sharedMaterial = newModel.material;
|
||||||
transform.rotation = _model.rotation;
|
transform.rotation = newModel.rotation;
|
||||||
transform.localScale = _model.scale;
|
transform.localScale = newModel.scale;
|
||||||
transform.localPosition += _model.offset;
|
transform.localPosition += newModel.offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateChild(int childNum, string id)
|
public void UpdateChild(int childNum, string id)
|
||||||
{
|
{
|
||||||
var newChildModel = FindAnyObjectByType<ModelManager>().getById(id);
|
var newChildModel = FindAnyObjectByType<ModelManager>().getById(id);
|
||||||
children[childNum].GetComponent<ChildModelBehaviour>().UpdateModel(newChildModel);
|
Children[childNum].GetComponent<ChildModelBehaviour>().UpdateModel(newChildModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<int> getChildrenWithPort(string port)
|
public List<int> GetChildrenWithPort(string port)
|
||||||
{
|
{
|
||||||
List<int> list = new List<int>();
|
List<int> list = new List<int>();
|
||||||
for (int i = 0; i < children.Count; i++)
|
for (int i = 0; i < Children.Count; i++)
|
||||||
{
|
{
|
||||||
if (children[i].GetComponent<ChildModelBehaviour>().childModel.port == port)
|
if (Children[i].GetComponent<ChildModelBehaviour>().ChildModel.port == port)
|
||||||
{
|
{
|
||||||
list.Add(i);
|
list.Add(i);
|
||||||
}
|
}
|
||||||
@@ -87,25 +103,61 @@ public class ModelBehaviour : MonoBehaviour
|
|||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool setFirstWithPort(string port, ChildModel _childModel)
|
public bool SetFirstWithPort(string port, ChildModel childModel)
|
||||||
{
|
{
|
||||||
Debug.Log($"{this.name}: has {children.Count} children");
|
Debug.Log($"{this.name}: has {Children.Count} children");
|
||||||
for (int i = 0; i < children.Count; i++)
|
for (int i = 0; i < Children.Count; i++)
|
||||||
{
|
{
|
||||||
var cmb = children[i].GetComponent<ChildModelBehaviour>();
|
var cmb = Children[i].GetComponent<ChildModelBehaviour>();
|
||||||
Debug.Log($"{this.name}: {i} name {children[i].name} - port {cmb.childModel.port}");
|
Debug.Log($"{this.name}: {i} name {Children[i].name} - port {cmb.ChildModel.port}");
|
||||||
if(cmb.childModel.port == port)
|
if(cmb.ChildModel.port == port)
|
||||||
{
|
{
|
||||||
if (_childModel == null)
|
if (childModel == null)
|
||||||
{
|
{
|
||||||
Debug.LogWarning($"{this.name}: Cant replace port {port} with model, model is null");
|
Debug.LogWarning($"{this.name}: Cant replace port {port} with model, model is null");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
cmb.UpdateModel(model);
|
cmb.UpdateModel(Model);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Debug.LogWarning($"{this.name}: Didnt find port {port}");
|
Debug.LogWarning($"{this.name}: Didnt find port {port}");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Spawns all Port-GO's, should be called only on Model Switch <b>after</b> cleanup
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="newModel"><c>Model</c> the New Model to spawn ports from</param>
|
||||||
|
private void spawnChildPorts(Model newModel)
|
||||||
|
{
|
||||||
|
Debug.Log($"{newModel.nameId}: Spawning {this.name} count of {newModel.ports.Count} Ports");
|
||||||
|
|
||||||
|
for (int i = 0; i < newModel.ports.Count; i++)
|
||||||
|
{
|
||||||
|
var port = newModel.ports[i];
|
||||||
|
Debug.Log(i + ". Creating port " + port.port);
|
||||||
|
// bike:wheel:num
|
||||||
|
GameObject child = new GameObject(this.name + ":" + port.port + ":" + i);
|
||||||
|
child.transform.SetParent(this.transform); //makes this an actual child
|
||||||
|
ChildModelBehaviour cmb = child.AddComponent<ChildModelBehaviour>();
|
||||||
|
|
||||||
|
var mm = FindAnyObjectByType<ModelManager>();
|
||||||
|
if (mm == null)
|
||||||
|
{
|
||||||
|
Debug.LogError("ModelManager not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
var childModel = mm.getById(port.defaultId);
|
||||||
|
if (childModel.mesh == null)
|
||||||
|
{
|
||||||
|
Debug.LogError("Default Mesh Not Found, destoying child");
|
||||||
|
Destroy(child);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
cmb.ChildModel = childModel;
|
||||||
|
port.apply(child.transform); // move to correct position
|
||||||
|
Children.Add(child);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -103,7 +103,7 @@ public class ModelManager : MonoBehaviour
|
|||||||
// Neues GameObject für das BaseModel erstellen
|
// Neues GameObject für das BaseModel erstellen
|
||||||
this.baseModel = new GameObject(baseModel.nameId)
|
this.baseModel = new GameObject(baseModel.nameId)
|
||||||
{
|
{
|
||||||
name = baseModel.nameId
|
name = baseModel.nameId,
|
||||||
};
|
};
|
||||||
this.baseModel.AddComponent<BaseModelBehaviour>();
|
this.baseModel.AddComponent<BaseModelBehaviour>();
|
||||||
// Positionieren
|
// Positionieren
|
||||||
@@ -112,30 +112,17 @@ public class ModelManager : MonoBehaviour
|
|||||||
this.baseModel.SetActive(true);
|
this.baseModel.SetActive(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.baseModel.GetComponent<BaseModelBehaviour>().baseModel = baseModel;
|
this.baseModel.GetComponent<BaseModelBehaviour>().BaseModel = baseModel;
|
||||||
this.baseModel.name = baseModel.nameId;
|
|
||||||
this.baseModel.SetActive(true);
|
this.baseModel.SetActive(true);
|
||||||
Debug.Log($"Modell {baseModel.nameHuman} lock and loaded.");
|
Debug.Log($"Model {baseModel.nameHuman} lock and loaded.");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void openSelector()
|
public void openSelector()
|
||||||
{
|
{
|
||||||
Debug.Log("Open Selector");
|
Debug.Log("Open Selector");
|
||||||
|
|
||||||
// Altes BaseModel zerstören und ein neues leeres erstellen
|
baseModel.SetActive(false);
|
||||||
|
baseModelSelector.Activate(); //actimel aktiviert auswahlkräfte
|
||||||
if (baseModel != null)
|
|
||||||
{
|
|
||||||
Destroy(baseModel); // Altes BaseModel zerstören
|
|
||||||
|
|
||||||
|
|
||||||
baseModel = new GameObject("BaseModel");
|
|
||||||
baseModel.AddComponent<BaseModelBehaviour>();
|
|
||||||
baseModel.transform.position = new Vector3(0,0,1); // Optional: Position setzen
|
|
||||||
Debug.Log("Neues leeres BaseModel erstellt.");
|
|
||||||
}
|
|
||||||
|
|
||||||
baseModelSelector.Activate(); //actimel aktiviert ausw'hlkrafte
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -25,8 +25,7 @@ public class ReturnButtonBehaviour : MonoBehaviour
|
|||||||
{
|
{
|
||||||
gameObject.SetActive(true);
|
gameObject.SetActive(true);
|
||||||
//black magic
|
//black magic
|
||||||
var cameraTf = GameObject.Find("[BuildingBlock] Camera Rig").transform;
|
//var cameraTf = GameObject.Find("[BuildingBlock] Camera Rig").transform;
|
||||||
|
|
||||||
//gameObject.transform.position = cameraTf.position + new Vector3(5f,-0.5f,5f);
|
//gameObject.transform.position = cameraTf.position + new Vector3(5f,-0.5f,5f);
|
||||||
//gameObject.transform.rotation = Quaternion.LookRotation(gameObject.transform.position - cameraTf.position);
|
//gameObject.transform.rotation = Quaternion.LookRotation(gameObject.transform.position - cameraTf.position);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user