Fix Spaghetti Coding + Refactoring
This commit is contained in:
@@ -5,11 +5,11 @@ using UnityEngine;
|
||||
//Only one should exist at the same time
|
||||
public class BaseModelBehaviour : ModelBehaviour
|
||||
{
|
||||
public BaseModel baseModel
|
||||
public BaseModel BaseModel
|
||||
{
|
||||
get
|
||||
{
|
||||
if (model is BaseModel bModel)
|
||||
if (Model is BaseModel bModel)
|
||||
{
|
||||
return bModel;
|
||||
}
|
||||
@@ -17,7 +17,7 @@ public class BaseModelBehaviour : ModelBehaviour
|
||||
}
|
||||
set
|
||||
{
|
||||
model = value;
|
||||
Model = value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,15 +4,15 @@ using UnityEngine;
|
||||
|
||||
public class ChildModelBehaviour : ModelBehaviour
|
||||
{
|
||||
public ChildModel childModel
|
||||
public ChildModel ChildModel
|
||||
{
|
||||
get
|
||||
{
|
||||
if (model == null)
|
||||
if (Model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if (model is ChildModel cModel)
|
||||
if (Model is ChildModel cModel)
|
||||
{
|
||||
return cModel;
|
||||
}
|
||||
@@ -20,7 +20,7 @@ public class ChildModelBehaviour : ModelBehaviour
|
||||
}
|
||||
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
|
||||
public const string PORT_BIKE_SPROCKET = "bikeSprocket";
|
||||
@@ -10,6 +14,7 @@
|
||||
|
||||
|
||||
// ERLCAR PORTS
|
||||
// TODO: rename all important ones
|
||||
public const string PORT_ERLBACH_0 = "portErlbach0";
|
||||
public const string PORT_ERLBACH_1 = "portErlbach1";
|
||||
public const string PORT_ERLBACH_2 = "portErlbach2";
|
||||
|
||||
@@ -37,62 +37,13 @@ public class Model
|
||||
this.scale = Vector3.one;
|
||||
}
|
||||
|
||||
public bool hasPorts()
|
||||
public bool HasPorts()
|
||||
{
|
||||
return ports != null && ports.Count > 0;
|
||||
}
|
||||
|
||||
public void addPort(Port port)
|
||||
public void AddPort(Port 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;
|
||||
|
||||
internal Model model
|
||||
internal Model Model
|
||||
{
|
||||
get
|
||||
{
|
||||
@@ -18,68 +18,84 @@ public class ModelBehaviour : MonoBehaviour
|
||||
}
|
||||
set
|
||||
{
|
||||
_model = value;
|
||||
UpdateModel(value);
|
||||
_model = value;
|
||||
}
|
||||
} //should be verified
|
||||
internal List<GameObject> children;
|
||||
internal List<GameObject> Children = new List<GameObject>();
|
||||
internal MeshFilter meshFilter;
|
||||
internal MeshRenderer meshRenderer;
|
||||
|
||||
void Start()
|
||||
{
|
||||
var filter = gameObject.GetComponent<MeshFilter>();
|
||||
if (filter == null)
|
||||
{
|
||||
filter = gameObject.AddComponent<MeshFilter>();
|
||||
}
|
||||
meshFilter = filter;
|
||||
|
||||
var renderer = gameObject.GetComponent<MeshRenderer>();
|
||||
if (renderer == null)
|
||||
{
|
||||
renderer = gameObject.AddComponent<MeshRenderer>();
|
||||
}
|
||||
meshRenderer = renderer;
|
||||
Init();
|
||||
}
|
||||
|
||||
private void UpdateModel(Model _model)
|
||||
void Init() // unity my beloved edge case
|
||||
{
|
||||
//kill old children
|
||||
foreach (var go in GetComponentsInChildren<ChildModelBehaviour>())
|
||||
if(meshFilter == null)
|
||||
meshFilter = gameObject.GetOrAddComponent<MeshFilter>();
|
||||
if(meshRenderer == null)
|
||||
meshRenderer = gameObject.GetOrAddComponent<MeshRenderer>();
|
||||
}
|
||||
|
||||
private void UpdateModel(Model newModel)
|
||||
{
|
||||
if (newModel is BaseModel)
|
||||
{
|
||||
Debug.Log($"Destroying {go.name}");
|
||||
Destroy(go); // will be destroyed next frame
|
||||
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
|
||||
foreach (var compo in GetComponentsInChildren<ChildModelBehaviour>())
|
||||
{
|
||||
if (compo == this) // why does InChildren include the parent
|
||||
{
|
||||
continue;
|
||||
}
|
||||
Debug.Log($"Destroying {name}'s Child {compo.gameObject.name}");
|
||||
Destroy(compo.gameObject); // will be destroyed next frame
|
||||
}
|
||||
|
||||
//spawn new childPorts
|
||||
if (model.hasPorts())
|
||||
if (newModel.HasPorts())
|
||||
{
|
||||
Debug.Log($"Spawning {gameObject.name}'s ports");
|
||||
children = model.spawnAllPorts(gameObject);
|
||||
spawnChildPorts(newModel);
|
||||
}
|
||||
//change ourselves
|
||||
meshFilter.mesh = _model.mesh;
|
||||
meshFilter.sharedMesh = _model.mesh;
|
||||
meshRenderer.material = _model.material;
|
||||
meshRenderer.sharedMaterial = _model.material;
|
||||
transform.rotation = _model.rotation;
|
||||
transform.localScale = _model.scale;
|
||||
transform.localPosition += _model.offset;
|
||||
meshFilter.mesh = newModel.mesh;
|
||||
meshFilter.sharedMesh = newModel.mesh;
|
||||
meshRenderer.material = newModel.material;
|
||||
meshRenderer.sharedMaterial = newModel.material;
|
||||
transform.rotation = newModel.rotation;
|
||||
transform.localScale = newModel.scale;
|
||||
transform.localPosition += newModel.offset;
|
||||
}
|
||||
|
||||
public void UpdateChild(int childNum, string 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>();
|
||||
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);
|
||||
}
|
||||
@@ -87,25 +103,61 @@ public class ModelBehaviour : MonoBehaviour
|
||||
return list;
|
||||
}
|
||||
|
||||
public bool setFirstWithPort(string port, ChildModel _childModel)
|
||||
public bool SetFirstWithPort(string port, ChildModel childModel)
|
||||
{
|
||||
Debug.Log($"{this.name}: has {children.Count} children");
|
||||
for (int i = 0; i < children.Count; i++)
|
||||
Debug.Log($"{this.name}: has {Children.Count} children");
|
||||
for (int i = 0; i < Children.Count; i++)
|
||||
{
|
||||
var cmb = children[i].GetComponent<ChildModelBehaviour>();
|
||||
Debug.Log($"{this.name}: {i} name {children[i].name} - port {cmb.childModel.port}");
|
||||
if(cmb.childModel.port == port)
|
||||
var cmb = Children[i].GetComponent<ChildModelBehaviour>();
|
||||
Debug.Log($"{this.name}: {i} name {Children[i].name} - port {cmb.ChildModel.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");
|
||||
return false;
|
||||
}
|
||||
cmb.UpdateModel(model);
|
||||
cmb.UpdateModel(Model);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Debug.LogWarning($"{this.name}: Didnt find port {port}");
|
||||
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
|
||||
this.baseModel = new GameObject(baseModel.nameId)
|
||||
{
|
||||
name = baseModel.nameId
|
||||
name = baseModel.nameId,
|
||||
};
|
||||
this.baseModel.AddComponent<BaseModelBehaviour>();
|
||||
// Positionieren
|
||||
@@ -112,30 +112,17 @@ public class ModelManager : MonoBehaviour
|
||||
this.baseModel.SetActive(true);
|
||||
}
|
||||
|
||||
this.baseModel.GetComponent<BaseModelBehaviour>().baseModel = baseModel;
|
||||
this.baseModel.name = baseModel.nameId;
|
||||
this.baseModel.GetComponent<BaseModelBehaviour>().BaseModel = baseModel;
|
||||
this.baseModel.SetActive(true);
|
||||
Debug.Log($"Modell {baseModel.nameHuman} lock and loaded.");
|
||||
Debug.Log($"Model {baseModel.nameHuman} lock and loaded.");
|
||||
}
|
||||
|
||||
public void openSelector()
|
||||
{
|
||||
Debug.Log("Open Selector");
|
||||
|
||||
// Altes BaseModel zerstören und ein neues leeres erstellen
|
||||
|
||||
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
|
||||
baseModel.SetActive(false);
|
||||
baseModelSelector.Activate(); //actimel aktiviert auswahlkräfte
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -25,8 +25,7 @@ public class ReturnButtonBehaviour : MonoBehaviour
|
||||
{
|
||||
gameObject.SetActive(true);
|
||||
//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.rotation = Quaternion.LookRotation(gameObject.transform.position - cameraTf.position);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user