/// <summary> /// Attempts to deliver the given matter object. /// Checks the demand queue whether the given matter is demanded, destroys it and increments the player score. /// Does nothing if the given matter is not inside the demand queue. /// Only has effect when called on the server. /// </summary> /// <param name="matterObject">The matter object to deliver.</param> public void DeliverObject(MatterObject matterObject) { Matter matter = matterObject != null ? matterObject.Matter : null; if (this.isServer && matter != null) { Demand deliveredDemand = this.DemandQueue.Deliver(matter); if (deliveredDemand != null) { int bonusPoints = 0; float timeLeftPercent = 0.0F; // Calculate bonus points if the demand has a time limit and it was delivered before the threshold if (deliveredDemand.HasTimeLimit) { timeLeftPercent = Mathf.Clamp01(deliveredDemand.TimeLeft / deliveredDemand.TimeLimit); if (timeLeftPercent >= this.bonusScoreThreshold) { bonusPoints = Mathf.CeilToInt(((timeLeftPercent - this.bonusScoreThreshold) / (1.0F - this.bonusScoreThreshold)) * this.bonusScore); } } this.IncrementPlayerScore(matter.GetScoreReward() + bonusPoints); this.IncrementDeliveredScore(matter.GetScoreReward() + bonusPoints); this.IncrementDeliveredCounter(); NetworkServer.Destroy(matterObject.gameObject); } } }
private void RpcRemoveFromInput(int[] indices) { if (this.isClientOnly) { try { foreach (int index in indices) { Matter target = this.insertedMatter[index]; this.contentsUI?.RemoveMatter(target); this.insertedMatter.RemoveAt(index); this.insertedObjects.RemoveAt(index); } } catch (System.Exception ex) { Debug.LogError("Cannot remove given indices from input list. Clearing entire list instead.", this); Debug.LogException(ex, this); this.contentsUI?.Clear(); this.insertedObjects.Clear(); this.insertedMatter.Clear(); } } }
/// <summary> /// Sets the given matter as the contained matter of this supplybox. /// Updates the displayed matter texture to show the new matter icon. /// </summary> /// <param name="matter">Matter to be contained in this supplybox or `null`.</param> public void SetContainedMatter(Matter matter) { this.containedMatter = matter; if (this.matterDisplayMaterial != null) { this.matterDisplayMaterial.SetTexture(this.matterDisplayTextureField, matter != null ? matter.GetIcon().texture : null); } }
/// <summary> /// Creates a new demand with the given matter and time limit. /// </summary> /// <param name="id">The ID for this demand. Usually generated by the server's demand queue and then sent to the client.</param> /// <param name="matter">The matter for this demand.</param> /// <param name="timeLimit">The time limit for this demand in seconds. `0` for infinite.</param> public Demand(int id, Matter matter, float timeLimit = 0) { this.ID = id; this.Matter = matter; this.TimeLimit = timeLimit; this.TimeLeft = timeLimit; this.IsDelivered = false; }
public void AddDemand(Matter matter, float timeLimit = 0) { Demand demand = new Demand(this.nextDemandID++, matter, timeLimit); demand.OnExpired += this.Demand_OnExpired_Server; this.AcceptDemand(demand); }
/// <summary> /// Sets the matter and quantity to display. /// </summary> /// <param name="matter">The matter to display.</param> /// <param name="quantity">The quantity to display.</param> public void SetDisplay(Matter matter, int quantity) { this.matterToDisplay = matter; this.quantityToDisplay = quantity; this.matterIcon.enabled = matter != null; this.matterIcon.sprite = matter != null?matter.GetIcon() : null; this.quantityGameObject.SetActive(quantity > 1); this.quantityText.text = quantity.ToString(); }
/// <summary> /// Looks up a matter by the given ID. /// </summary> /// <param name="matterID">The ID to look up.</param> /// <returns>The matter associated with <paramref name="matterID"/> or `null` if none found.</returns> public static Matter GetByID(string matterID) { Matter target = null; if (matters.TryGetValue(matterID, out target)) { return(target); } Debug.LogError($"MatterID '{matterID}' not found."); return(null); }
public Demand Deliver(Matter matter) { Demand demand = this.GetDemand(matter); if (demand != null) { demand.SetDelivered(); this.RemoveDemand(demand); return(demand); } return(null); }
private void RpcAcceptDemand(int demandID, string matterID, float timeLimit) { if (this.isClientOnly) { Matter targetMatter = Matter.GetByID(matterID); if (targetMatter != null) { this.AcceptDemand(new Demand(demandID, targetMatter, timeLimit)); } else { Debug.LogError($"Cannot accept nonexisting matter with id '{matterID}'."); } } }
public override void OnStartServer() { // Register the spawnable prefabs on server Matter.RegisterSpawnablePrefabs(); // Register message handlers NetworkServer.RegisterHandler <ConnectionRequestMessage>(this.OnConnectionRequestMessage, false); // Register server events GameManager.OnLevelLoaded += GameManager_OnLevelLoaded_Server; // Load the first level if no level is currently loaded (e.g. when launching from the Unity Editor while having a level scene open) if (GameManager.CurrentLevel == null) { GameManager.Instance.LoadLevel(1); } }
/// <summary> /// Returns the oldest demand for the given matter. /// </summary> /// <param name="matter">The matter to find a demand for.</param> /// <returns>The oldest demand for <paramref name="matter"/> or `null` if none found.</returns> public Demand GetDemand(Matter matter) { if (matter != null) { var enumerator = this.currentDemands.GetEnumerator(); while (enumerator.MoveNext()) { if (enumerator.Current.Value.Matter.Equals(matter) && !this.demandsToRemove.Contains(enumerator.Current.Value)) { return(enumerator.Current.Value); } } } return(null); }
/// <summary> /// Removes the given matter from this UI. /// </summary> /// <param name="matter">The matter to remove.</param> /// <param name="quantity">The amount of that matter to remove (optional, defaults to 1).</param> public void RemoveMatter(Matter matter, int quantity = 1) { if (matter != null && this.displayedMatter.ContainsKey(matter)) { MatterIcon matterIcon = this.displayedMatter[matter]; if (matterIcon.DisplayedQuantity - quantity <= 0) { GameObject.Destroy(matterIcon.gameObject); this.displayedMatter.Remove(matter); } else { matterIcon.SetDisplay(matterIcon.DisplayedMatter, matterIcon.DisplayedQuantity - quantity); } } }
/// <summary> /// Sets the matter to display on this UI element. /// </summary> /// <param name="matter">The matter to display. `null` will hide the icon.</param> /// <param name="showRequiredComponents">Whether to display the required components for the given <paramref name="matter"/>.</param> public void SetMatter(Matter matter, bool showRequiredComponents = true) { if (matter != null) { this.iconUI.sprite = matter.GetIcon(); this.iconUI.enabled = true; if (showRequiredComponents && matter is MatterCompound) { this.ClearRequiredComponents(); this.requiredMatterContainer.SetActive(true); Dictionary <Matter, int> requiredMatters = new Dictionary <Matter, int>(); foreach (Matter component in ((MatterCompound)matter).Components) { if (!requiredMatters.ContainsKey(component)) { requiredMatters.Add(component, 1); } else { requiredMatters[component]++; } } var enumerator = requiredMatters.GetEnumerator(); while (enumerator.MoveNext()) { GameObject uiElement = GameObject.Instantiate(this.matterIconPrefab, Vector3.zero, Quaternion.identity, this.requiredMatterContainer.transform); MatterIcon matterIcon = uiElement.GetComponent <MatterIcon>(); matterIcon?.SetDisplay(enumerator.Current.Key, enumerator.Current.Value); } } else { this.requiredMatterContainer.SetActive(false); } } else { this.iconUI.enabled = false; } }
public override void OnStartClient() { // Register the spawnable prefabs on client (if not registered already because we are running a server) if (!NetworkServer.active) { Matter.RegisterSpawnablePrefabs(); } // Register message handlers NetworkClient.RegisterHandler <ServerStateMessage>(this.OnServerStateMessage, false); // Register client events GameManager.OnLevelLoaded += GameManager_OnLevelLoaded_Client; // Unload the currently loaded level if there is one (e.g. when launching from the Unity Editor while having a level scene open) // and we are not running a server. // The client should always load the level that is currently running on the server if (!NetworkServer.active) { GameManager.Instance.UnloadCurrentLevel(); } }
public override void OnDeserialize(NetworkReader reader, bool initialState) { base.OnDeserialize(reader, initialState); if (initialState) { int amountOfDemands = reader.ReadInt32(); Demand demand; for (int i = 0; i < amountOfDemands; i++) { demand = new Demand(reader.ReadInt32(), Matter.GetByID(reader.ReadString()), reader.ReadSingle()); if (demand.HasTimeLimit) { demand.SetTimeLeft(reader.ReadSingle()); } this.AcceptDemand(demand); } } }
/// <summary> /// Registers a matter and adds it to the <see cref="matters"/> dictionary. /// It only adds it if no matter with the same ID had been added before. /// </summary> /// <param name="matter">The matter to register.</param> private static void RegisterMatter(Matter matter) { if (!matters.ContainsKey(matter.GetID())) { if (matter.GetPrefab() != null) { if (matter.GetPrefab().GetComponent <MatterObject>().Matter == matter) { matters.Add(matter.GetID(), matter); ClientScene.RegisterPrefab(matter.GetPrefab()); } else { Debug.LogWarning($"Matter {matter.GetFullName()}'s prefab does not have its 'Matter' property set correctly. The matter cannot be registered!"); } } else { Debug.LogWarning($"Matter {matter.GetFullName()} does not have a prefab assigned to it and thus cannot be registered!"); } } }
/// <summary> /// Adds the given matter to this UI. /// </summary> /// <param name="matter">The matter to add.</param> /// <param name="quantity">The amount of that matter to add (optional, defaults to 1).</param> public void AddMatter(Matter matter, int quantity = 1) { if (matter != null) { MatterIcon matterIcon = null; int newQuantity = quantity; if (!this.displayedMatter.ContainsKey(matter)) { GameObject matterIconObject = GameObject.Instantiate(this.matterIconPrefab.gameObject, this.transform); matterIcon = matterIconObject.GetComponent <MatterIcon>(); this.displayedMatter.Add(matter, matterIcon); } else { matterIcon = this.displayedMatter[matter]; newQuantity += matterIcon.DisplayedQuantity; } matterIcon.SetDisplay(matter, newQuantity); } }
protected virtual void OnEnable() { Matter.RegisterMatter(this); }