public void AddSector(Address host, ShardID idk, Action <Sector> onCreate) { Sector rs; if (sectors.TryGetValue(host, out rs)) { onCreate?.Invoke(rs); return; } OnCreateObject(idk.ToString(), sec => { sec.transform.parent = transform; Sector s = sec.AddComponent <Sector>(); s.CubePrototype = cubePrototype; s.EntityPrototype = entityPrototype; s.Host = host; s.ExpectedID = idk; sectors.Add(host, s); s.onNewID = id => { }; s.OnNewNeighbor = (id, addr) => { AddSector(addr, id); }; onCreate?.Invoke(s); }); }
// Update is called once per frame public void Update() { { FullShardAddress t; while (newNeighbors.TryTake(out t)) { Debug.Log(name + ": received neighbor update: " + t); OnNewNeighbor(t.ShardID, t.ObserverAddress); } } if (sdsChanged) { sdsChanged = false; { var id = privateID; if (id != publicID) { Debug.Log("ID change detected: " + id); publicID = id; name = publicID.ToString(); transform.name = publicID.ToString(); onNewID?.Invoke(id); if (cube != null) { cube.transform.position = Convert(publicID.XYZ) * Scale; } } } //Debug.Log("Sector: processing change"); updateNo++; SDS source = SDS; //Debug.Log("Sector: got "+transform.childCount+" children"); LazyList <GameObject> toDestroy = new LazyList <GameObject>(); foreach (Transform child in transform) { var obj = child.gameObject; if (obj.name == "cube") { //Debug.Log("Sector: got cube"); continue; } if (obj.hideFlags == HideFlags.HideAndDontSave) { continue; } obj.hideFlags = HideFlags.HideAndDontSave; obj.GetComponent <Renderer>().enabled = false; if (!availableEntityObjects.ContainsKey(obj.name)) { availableEntityObjects.Add(obj.name, obj); } else { toDestroy.Add(obj); } } foreach (var obj in toDestroy) { if (availableEntityObjects.ContainsValue(obj)) { Debug.LogError("Object " + obj.name + " still in use"); } else { Destroy(obj); } } //Debug.Log("Sector: recovered " + availableEntityObjects.Count + " objects"); int reused = 0; foreach (var e in source.FinalEntities) { GameObject obj; var next = Convert(e.ID.Position) * Scale; Vector3 prev = next; string key = e.ID.Guid.ToString(); if (!availableEntityObjects.ContainsKey(key)) { obj = entityPrototype != null?Instantiate(entityPrototype, transform) : new GameObject(); obj.GetComponent <Renderer>().material.color = myColor; obj.transform.parent = transform; obj.name = key; } else { obj = availableEntityObjects[key]; availableEntityObjects.Remove(key); if (obj == null) { Debug.LogError("Object " + key + " is null. Bad shit will happen"); } obj.hideFlags = HideFlags.None; obj.GetComponent <Renderer>().enabled = true; prev = obj.transform.position; reused++; } var c = obj.GetComponent <EntityComponent>(); if (c == null) { c = obj.AddComponent <EntityComponent>(); } c.SetState(next - Convert(e.Velocity) * Scale, next, secondsPerTLG); obj.transform.position = next; } //Debug.Log("Sector: got " + transform.childCount + " children, reusing "+reused); } }