Ejemplo n.º 1
0
 internal void UpdateTextures()
 {
     ShaderHelper.UpdateTextures(gameObject, GetShaderState());
 }
Ejemplo n.º 2
0
        /// <summary>
        ///     Instantiate all pieces from this prefab under the stub piece
        ///     to generate a new piece representing the blueprint
        /// </summary>
        /// <param name="baseObject"></param>
        /// <returns></returns>
        private bool GhostInstantiate(GameObject baseObject)
        {
            var ret = true;

            ZNetView.m_forceDisableInit = true;

            try
            {
                // Get max x/z from blueprint pieces
                var pieces = new List <PieceEntry>(m_pieceEntries);
                var maxX   = pieces.Max(x => x.posX);
                var maxZ   = pieces.Max(x => x.posZ);

                // Get rotation relative to the camera
                var tf = baseObject.transform;
                tf.rotation = Camera.main.transform.rotation;
                var quat = new Quaternion();
                quat.eulerAngles = new Vector3(0, tf.rotation.eulerAngles.y, 0);
                tf.SetPositionAndRotation(tf.position, quat);
                tf.position -= tf.right * (maxX / 2f);
                tf.position += tf.forward * 5f;

                // Get all distinct pieces
                var prefabs = new Dictionary <string, GameObject>();
                foreach (var piece in pieces.GroupBy(x => x.name).Select(x => x.FirstOrDefault()))
                {
                    var go = PrefabManager.Instance.GetPrefab(piece.name);
                    go.transform.SetPositionAndRotation(go.transform.position, quat);
                    prefabs.Add(piece.name, go);
                }

                // Check if all distinct pieces are found
                var nulls = prefabs.Values.Count(x => x == null);
                if (nulls > 0)
                {
                    throw new Exception($"{nulls} nulls found");
                }

                // Instantiate all pieces for the ghost prefab
                foreach (var piece in pieces)
                {
                    var pos = tf.position + tf.right * piece.GetPosition().x + tf.forward * piece.GetPosition().z +
                              new Vector3(0, piece.GetPosition().y, 0);

                    var q = new Quaternion();
                    q.eulerAngles = new Vector3(0, tf.transform.rotation.eulerAngles.y + piece.GetRotation().eulerAngles.y);

                    var child = Object.Instantiate(prefabs[piece.name], pos, q);
                    child.transform.SetParent(baseObject.transform);

                    // A Ghost doesn't need fancy scripts
                    foreach (var component in child.GetComponentsInChildren <MonoBehaviour>())
                    {
                        Object.Destroy(component);
                    }

                    // A Ghost also has to look like one
                    ShaderHelper.UpdateTextures(child, ShaderHelper.ShaderState.Floating);
                }
            }
            catch (Exception ex)
            {
                Logger.LogError($"Error while instantiating: {ex}");
                ret = false;
            }
            finally
            {
                ZNetView.m_forceDisableInit = false;
            }

            return(ret);
        }