/// <summary> /// Inserts the given matter object into this equipment. /// Checks whether a recipe matches the inserted elements and starts the production process, shows the progress bar and enables effects if a matching recipe has been found. /// </summary> /// <param name="matterObject"></param> private void InsertMatterObject(MatterObject matterObject) { if (matterObject != null) { matterObject.DisablePhysics(); matterObject.transform.SetParent(this.inputContainer.transform, false); matterObject.transform.localPosition = Vector3.zero; matterObject.transform.localRotation = Quaternion.identity; this.contentsUI?.AddMatter(matterObject.Matter); this.insertedObjects.Add(matterObject); this.insertedMatter.Add(matterObject.Matter); foreach (Recipe recipe in this.allRecipes) { if (recipe.MatchInputs(this.insertedMatter, this.allowForeignMattersInRecipes) == RecipeMatchState.FullMatch) { this.recipeInProgress = recipe; break; } } if (this.recipeInProgress != null) { this.fireLight.enabled = true; this.fireParticles.Play(); this.IsActivated = false; this.IsFinished = false; this.OnTimerStart(null); } } }
/// <summary> /// Interacts with this object. /// Attempts to insert a matter object held by <paramref name="interactor"/> or take a matter object from the output slot. /// If the interactor does not hold anything and the output slot is empty, this method will try take the last inserted object and give it to the interactor. /// </summary> /// <param name="interactor">The initiator of this interaction.</param> public override void Interact(Interactor interactor) { if (this.IsActivated) { return; } PickableObject heldObject = interactor.HeldObject; if (heldObject != null) { if (this.outputObjects.Count == 0) { MatterObject matterObject = heldObject.GetComponent <MatterObject>(); if (matterObject != null) { interactor.SetHeldObject(null); this.InsertMatterObject(matterObject); } } } else if (interactor.HeldObject == null) { MatterObject toTake = this.outputObjects.Count != 0 ? this.TakeOutputObject() : this.insertedObjects.Count > 0 ? this.TakeLastInsertedObject() : null; if (toTake != null) { interactor.SetHeldObject(toTake.GetComponent <PickableObject>()); } } }
/// <summary> /// Checks if interactor is server /// Checks if interactor is already holding an object - /// if it is not holding an object the respective matter object is created, <see cref="Interactor.HeldObject"/> is called /// and then <see cref="RpcGiveMatterObjectToInteractor(NetworkIdentity, NetworkIdentity)"/> is called /// with the given interactor and the created matter as parameters. /// If it is holding an object and the container can take this object back the matter object will be destroyed /// and the interactor will not hold an object anymore. /// </summary> /// <param name="interactor"></param> public void Interact(Interactor interactor) { if (this.isServer) { if (!interactor.IsHoldingObject) { if (this.containedMatter != null) { GameObject instantiatedMatter = GameObject.Instantiate(this.containedMatter.GetPrefab(), this.transform.position, Quaternion.identity); NetworkServer.Spawn(instantiatedMatter); interactor.SetHeldObject(instantiatedMatter.GetComponent <PickableObject>()); this.RpcGiveMatterObjectToInteractor(instantiatedMatter.GetComponent <NetworkIdentity>(), interactor.GetComponent <NetworkIdentity>()); } } else if (this.canTakeBackItems) { MatterObject matterObject = interactor.HeldObject.GetComponent <MatterObject>(); if (matterObject != null && (this.containedMatter == null || this.containedMatter.Equals(matterObject.Matter))) { interactor.SetHeldObject(null); this.RpcGiveMatterObjectToInteractor(null, interactor.GetComponent <NetworkIdentity>()); NetworkServer.Destroy(matterObject.gameObject); } } } }
/// <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); } } }
/// <summary> /// Ejects the matter object inside the output slot and returns it. /// </summary> /// <returns>The output matter object or `null` if none.</returns> private MatterObject TakeOutputObject() { if (this.outputObjects.Count != 0) { MatterObject toTake = this.outputObjects[this.outputObjects.Count - 1]; toTake.EnablePhysics(); toTake.transform.SetParent(this.transform, false); this.contentsUI?.RemoveMatter(toTake.Matter); this.outputObjects.RemoveAt(this.outputObjects.Count - 1); return(toTake); } return(null); }
/// <summary> /// Adds an output object to this equipment. /// Synchronizes it with the clients if run on the server. /// </summary> /// <param name="matterObject">The object to add to the output.</param> private void AddOutputObject(MatterObject matterObject) { this.outputObjects.Add(matterObject); this.contentsUI?.AddMatter(matterObject.Matter); matterObject.DisablePhysics(); matterObject.transform.SetParent(this.outputContainer.transform, false); matterObject.transform.localPosition = Vector3.zero; matterObject.transform.localRotation = Quaternion.identity; if (this.isServer) { this.RpcAddOutput(matterObject.GetComponent <NetworkIdentity>()); } }
/// <summary> /// Ejects the last inserted matter object, removes it from the input and returns it. /// </summary> /// <returns>The last inserted matter object or `null` if none.</returns> private MatterObject TakeLastInsertedObject() { if (this.insertedObjects.Count > 0) { int index = this.insertedObjects.Count - 1; MatterObject toTake = this.insertedObjects[index]; toTake.EnablePhysics(); toTake.transform.SetParent(this.transform, false); this.contentsUI?.RemoveMatter(toTake.Matter); this.insertedMatter.RemoveAt(index); this.insertedObjects.RemoveAt(index); return(toTake); } return(null); }