Example #1
0
        public void PlanPieceRemovedFromBlueprint(PlanPiece planPiece)
        {
            ZDOID blueprintID = planPiece.GetBlueprintID();

            if (blueprintID == ZDOID.None)
            {
                return;
            }

            ZDO blueprintZDO = ZDOMan.instance.GetZDO(blueprintID);

            if (blueprintZDO == null)
            {
                return;
            }
            ZDOIDSet planPieces = GetPlanPieces(blueprintZDO);

            planPieces?.Remove(planPiece.GetPlanPieceID());
            if (planPieces == null || planPieces.Count() == 0)
            {
                GameObject blueprintObject = ZNetScene.instance.FindInstance(blueprintID);
                if (blueprintObject)
                {
                    ZNetScene.instance.Destroy(blueprintObject);
                }
            }
            else
            {
                blueprintZDO.Set(PlanPiece.zdoBlueprintPiece, planPieces.ToZPackage().GetArray());
            }
        }
Example #2
0
 private static ZDOIDSet GetPlanPieces(ZDO blueprintZDO)
 {
     byte[] data = blueprintZDO.GetByteArray(PlanPiece.zdoBlueprintPiece);
     if (data == null)
     {
         return(null);
     }
     return(ZDOIDSet.From(new ZPackage(data)));
 }
Example #3
0
        public static ZDOIDSet From(ZPackage package)
        {
            ZDOIDSet result = new ZDOIDSet();
            int      size   = package.ReadInt();

            for (int i = 0; i < size; i++)
            {
                result.Add(package.ReadZDOID());
            }
            return(result);
        }
Example #4
0
        private List <PlanPiece> GetPlanPiecesForBlueprint(ZDOID blueprintID)
        {
            List <PlanPiece> result = new List <PlanPiece>();
            ZDO blueprintZDO        = ZDOMan.instance.GetZDO(blueprintID);

            if (blueprintZDO == null)
            {
                return(result);
            }
            ZDOIDSet planPieces = GetPlanPieces(blueprintZDO);

            foreach (ZDOID pieceZDOID in planPieces)
            {
                GameObject pieceObject = ZNetScene.instance.FindInstance(pieceZDOID);
                if (pieceObject && pieceObject.TryGetComponent(out PlanPiece planPiece))
                {
                    result.Add(planPiece);
                }
            }
            return(result);
        }
Example #5
0
        private static bool PlaceBlueprint(Player player, Piece piece)
        {
            Blueprint bp        = Instance.m_blueprints[piece.m_name];
            var       transform = player.m_placementGhost.transform;
            var       position  = player.m_placementGhost.transform.position;
            var       rotation  = player.m_placementGhost.transform.rotation;

            bool placeDirect = ZInput.GetButton("Crouch");

            if (placeDirect && !allowDirectBuildConfig.Value)
            {
                MessageHud.instance.ShowMessage(MessageHud.MessageType.Center, "$msg_direct_build_disabled");
                return(false);
            }

            if (ZInput.GetButton("AltPlace"))
            {
                Vector2 extent = bp.GetExtent();
                FlattenTerrain.FlattenForBlueprint(transform, extent.x, extent.y, bp.m_pieceEntries);
            }

            uint cntEffects = 0u;
            uint maxEffects = 10u;

            GameObject blueprintPrefab = PrefabManager.Instance.GetPrefab(Blueprint.BlueprintPrefabName);
            GameObject blueprintObject = Object.Instantiate(blueprintPrefab, position, rotation);

            ZDO blueprintZDO = blueprintObject.GetComponent <ZNetView>().GetZDO();

            blueprintZDO.Set(ZDOBlueprintName, bp.m_name);
            ZDOIDSet createdPlans = new ZDOIDSet();

            for (int i = 0; i < bp.m_pieceEntries.Length; i++)
            {
                PieceEntry entry = bp.m_pieceEntries[i];
                // Final position
                Vector3 entryPosition = position + transform.forward * entry.posZ + transform.right * entry.posX + new Vector3(0, entry.posY, 0);

                // Final rotation
                Quaternion entryQuat = new Quaternion(entry.rotX, entry.rotY, entry.rotZ, entry.rotW);
                entryQuat.eulerAngles += rotation.eulerAngles;

                // Get the prefab of the piece or the plan piece
                string prefabName = entry.name;
                if (!placeDirect)
                {
                    prefabName += PlanPiecePrefab.PlannedSuffix;
                }

                GameObject prefab = PrefabManager.Instance.GetPrefab(prefabName);
                if (!prefab)
                {
                    Jotunn.Logger.LogWarning(entry.name + " not found, you are probably missing a dependency for blueprint " + bp.m_name + ", not placing @ " + entryPosition);
                    continue;
                }

                // Instantiate a new object with the new prefab
                GameObject gameObject = Object.Instantiate(prefab, entryPosition, entryQuat);

                ZNetView zNetView = gameObject.GetComponent <ZNetView>();
                if (!zNetView)
                {
                    Jotunn.Logger.LogWarning("No ZNetView for " + gameObject + "!!??");
                }
                else if (gameObject.TryGetComponent(out PlanPiece planPiece))
                {
                    planPiece.PartOfBlueprint(blueprintZDO.m_uid, entry);
                    createdPlans.Add(planPiece.GetPlanPieceID());
                }

                // Register special effects
                CraftingStation craftingStation = gameObject.GetComponentInChildren <CraftingStation>();
                if (craftingStation)
                {
                    player.AddKnownStation(craftingStation);
                }
                Piece newpiece = gameObject.GetComponent <Piece>();
                if (newpiece)
                {
                    newpiece.SetCreator(player.GetPlayerID());
                }
                PrivateArea privateArea = gameObject.GetComponent <PrivateArea>();
                if (privateArea)
                {
                    privateArea.Setup(Game.instance.GetPlayerProfile().GetName());
                }
                WearNTear wearntear = gameObject.GetComponent <WearNTear>();
                if (wearntear)
                {
                    wearntear.OnPlaced();
                }
                TextReceiver textReceiver = gameObject.GetComponent <TextReceiver>();
                if (textReceiver != null)
                {
                    textReceiver.SetText(entry.additionalInfo);
                }

                // Limited build effects
                if (cntEffects < maxEffects)
                {
                    newpiece.m_placeEffect.Create(gameObject.transform.position, rotation, gameObject.transform, 1f);
                    player.AddNoise(50f);
                    cntEffects++;
                }

                // Count up player builds
                Game.instance.GetPlayerProfile().m_playerStats.m_builds++;
            }

            blueprintZDO.Set(PlanPiece.zdoBlueprintPiece, createdPlans.ToZPackage().GetArray());

            // Dont set the blueprint piece and clutter the world with it
            return(false);
        }