Harvester
Introduction
The Harvesting System is a modular and extensible tool for procedurally generating and managing resources in virtual environments. It consists of various components such as the Harvester, Harvestable objects, Harvestable Colliders, and Harvestable Masks that work together to simulate the process of harvesting resources. The system also includes features such as network synchronization, real-time updating, and support for custom resources, making it flexible and adaptable to a variety of use cases. The Harvesting System provides a comprehensive solution for game developers and designers looking to add harvesting mechanics to their projects with ease.
The Harvesting System is a set of interconnected classes and components that enable players to harvest resources from the game world. It allows for the creation of a wide range of harvestable objects, including trees, rocks, and other environmental elements.
At the core of the system is the Harvester class, which manages the player’s interactions with the game world. The Harvester uses a variety of data structures to store information about the game world, including a list of all harvestable objects and their states, as well as a list of all the tools available to the player. It also provides methods for handling events, such as collisions between the player’s harvesting tool and a harvestable object.
The Harvester class makes use of a number of other components and classes to implement the various features of the Harvesting System. These include the HarvestableCollider, which defines the boundaries of a harvestable object and handles collisions between the player’s tool and the object; the HarvestableChild, which manages the visual representation of a harvestable object and provides information about its state; and the HarvesterTool, which defines the properties of the player’s harvesting tool, such as its durability and effectiveness.
The Harvesting System also includes components for managing the visual representation of harvested objects, such as the HarvesterObjectGenerator and the HarvesterObjectHealthBar, as well as components for managing the state of the game world, such as the HarvesterTerrainBackup and the HarvesterTerrainMask.
Together, these components and classes form a powerful and flexible system for implementing a wide range of harvesting mechanics in a game. By combining the various features of the system, game developers can create a dynamic and engaging gameplay experience that encourages players to explore and interact with the game world in new and exciting ways.
Overview
The Harvester
class contains functionality for harvesting vegetation from a terrain in Unity, including various settings and options for customizing the harvesting process. It includes features such as support for multiple terrains, timers for restoring harvested vegetation, and explosion effects.
Interface
Class | Description |
---|---|
HarvesterObjects |
Contains information about a type of harvestable object, including its name and prefab. |
HarvesterTreeInstance |
Contains information about a tree instance on the terrain, such as its position, width and height scales, and color. |
HarvesterTool |
Contains information about a harvester tool, including its damage and animation settings. |
HarvestableObject |
Represents a harvestable object in the scene, such as a tree or plant, and contains functionality for damaging and harvesting it. |
listChanged | This is the client side SyncList callback function that handles processing the addition and removal of the trees client side. |
explosionListChanged | This is similar to the listChanged method, but is specific to processing explosions. |
clientForceDelete | This method forces the deletion of vegetation using a physics overlap sphere. |
ClientAddHarvestableObject | This is the process which handles tree addition on the local client. Terrain trees are not networked, so we need a local process which will handle adding the tree and the associated collider. |
FillTreeValues(Terrain terrain, HarvesterTreeInstance[] currentTrees) |
Fills the tree values in the terrain with the tree values passed in as the second parameter. |
ServerUpdateHarvestableListuMMOCE(Vector3 position) |
Updates the server’s harvestable list for uMMO/CE with a tree object based on the terrain position. |
UpdateServersList(Color32 color, float height, Color32 lightmapColor, Vector3 position, int prototypeIndex, float rotation, float widthScale, string terrain, int terrainIndex, float restoreTime) |
Updates the server’s tree sync list with the information in this call. |
UpdateServersExplodeList(Color32 color, float height, Color32 lightmapColor, Vector3 position, int prototypeIndex, float rotation, float widthScale, Terrain terrain, int terrainIndex) |
Updates the server’s explode list with the information in this call. |
ClientAddVegetationInstance | This method handles tree addition on the local client. |
Parameters: SyncListVegetationInstance tree – The tree to be added. | |
ClientResetTrees | This method resets the trees on the client in the event that the list is cleared. |
ClientRemoveHarvestableObject | This method handles tree removal on the local client. |
Parameters: SyncListHarvesterTree tree – The tree to be removed. | |
GetTerrainFromName | This method gets a terrain instance from its name. |
RpcSpawnHarvestSoundEffect | This is a client command to tell them to spawn a sound effect at the harvest location. |
Parameters: Vector3 itemPosition – The position of the item. | |
int index – The index of the item. | |
RpcSpawnChopSoundEffect | This is a client command to tell them to spawn a sound effect at the chop location. |
Parameters: Vector3 itemPosition – The position of the item. | |
int index – The index of the item. | |
RpcSpawnHarvestChopEffect | This is a client command to tell them to spawn a harvesting effect at the harvest location. |
Parameters: Vector3 itemPosition – The position of the item. | |
Quaternion rotation – The rotation of the item. | |
int index – The index of the item. | |
DestroyEffect | This method destroys the spawned game object regardless of who needs to call it. |
Parameters: GameObject go – The game object to be destroyed. | |
float time – The time delay before the object is destroyed. | |
SetHarvestCursor | This method sets the harvest cursor. |
DamageObject | This method damages a harvestable object and spawns chop and sound effects. Parameters are a HarvestableObject, float itemDamage, Vector3 hitPoint, and Vector3 hitDirection. |
DestroyPrefab | This method destroys a given GameObject immediately. Parameter is a GameObject. |
ServerProcesstheExplosionObjects | This method processes explosion objects on the server. It finds HarvestableColliders and HarvestableObjects within the sphere of the explosion and destroys the colliders. Parameters are a Vector3 Position. |
Explode | This method adds a new tree to the server’s list of trees to be harvested. Parameters are a Color32 color, float height, Color32 lightmapColor, Vector3 position, int prototypeIndex, float rotation, float widthScale, and Vector3 explosionPosition. |
RemoveInstanceListJob | This method removes trees from the list of trees that have been harvested. |
AddInstanceListJob | This method adds a new tree to the list of trees to be harvested. Parameters are a Color32 color, float height, Color32 lightmapColor, Vector3 position, int prototypeIndex, float rotation, float widthScale, a Terrain, an int terrainInstanceId, and a float restoreTime. |
ServerUpdateHarvestableList | This method updates the server’s list of harvestable objects. Parameters are a HarvestableCollider. |
SpawnNetworkedExplodingTerrainObject(Vector3 position, string tag, float rotation, Vector3 scale) |
Spawns a networked exploding terrain object. The position parameter specifies the position of the object to be spawned, tag is a string that specifies the tag of the object, rotation is a float that specifies the rotation of the object, and scale is a Vector3 that specifies the scale of the object. This method instantiates a non-harvestable prefab and sets its mass and rotation before applying an explosion force to it. The object is then destroyed after a specified amount of time. |
GetBlastRange() |
Returns the blast range. |
GetBlastForce() |
Returns the blast force. |
SpawnHarvestEffect(Vector3 position, int index) |
Spawns a harvest effect. The position parameter specifies the position of the effect to be spawned and index is an integer that specifies the index of the object to be harvested. This method selects a random harvestable prefab and instantiates it before modifying its position, name, and tag. The object is then destroyed after a specified amount of time. |
SpawnNetworkedHarvestableTerrainObject(Vector3 position, string tag, float rotation, Vector3 scale, float toolDamage, string prefabName, SyncListHarvesterTree newSlot, int treeIndex=-1) |
Spawns a harvestable object at the specified position. The object is chosen from the harvester list based on the provided prefabName , which is the name of the object’s prefab. tag is the tag that will be applied to the spawned object. rotation and scale determine the orientation and size of the spawned object. toolDamage is the amount of damage that the tool that harvested the object caused. newSlot is a SyncListHarvesterTree that specifies the terrain instance on which to spawn the object, and treeIndex is the index of the tree to spawn. If treeIndex is not provided, it defaults to -1 . |
SpawnNetworkedExplodingTerrainObject(Vector3 position, string tag, float rotation, Vector3 scale, Vector3 explosionPosition, SyncListHarvesterTree newSlot, int treeIndex=-1) |
Spawns a non-harvestable object at the specified position that will explode. The object is chosen from the harvester list based on the provided tag , which is the tag of the object. rotation and scale determine the orientation and size of the spawned object. explosionPosition is the position at which the explosion will occur. newSlot is a SyncListHarvesterTree that specifies the terrain instance on which to spawn the object, and treeIndex is the index of the tree to spawn. If treeIndex is not provided, it defaults to -1 . |
SpawnNetworkedExplodingTerrainObject2(GameObject currentTree) |
Spawns a non-harvestable object at the position of the specified tree that will explode. The object is chosen from the harvester list based on the tag of the provided currentTree . |
SpawnNetworkedFallingTerrainObject(Vector3 position, string tag, float rotation, Vector3 scale, Vector3 hitDirection, int prototypeIndex = -1) |
Spawns a non-harvestable object at the specified position that will fall over. The object is chosen from the harvester list based on the provided tag , which is the tag of the object. rotation and scale determine the orientation and size of the spawned object. hitDirection is the direction in which the object should fall. prototypeIndex is the index of the object in the harvester list to spawn. If prototypeIndex is not provided, it defaults to -1 . |
Property Name | Type | Summary |
---|---|---|
harvesterObjectsSystemArray | HarvesterObjects[] | This list contains all of the items that should be harvested. |
originalTerainTrees | HarvesterTreeInstance[] | The tree objects on the terrain, this is used to restore the original trees back to the terrain if the application quits or crashes. |
runningTerrainTrees | HarvesterTreeInstance[] | The current tree instances on the terrain. |
RestoreVegetationTick | float | This is the amount of time that must pass before it will process the list. |
RestoreVegetationStartTick | float | This is the amount of time that must start before the system will start to process the list. |
harvestingRestoreTime | float | How long before the harvesting system will restore the harvested items. |
dropDestructionDestroyTime | float | The amount of time to destroy the spawned game object. |
nonHarvestableObjectPrefabTime | float | The amount of time to destroy the spawned game object. |
enableDropDestruction | bool | Enable the destruction of items spawned by the harvesting system. |
enableHarvestToolFx | bool | Enable tool harvesting FX. |
enableDirectHarvest | bool | Enable direct harvesting (no need for tools). |
ChangeCursor | bool | Do you require that the players have a harvester tool or will you allow them to damage by any means (ie punching it). |
EnableMouseCursorOption | bool | Allow the mouse cursor to change, only works if the mouse cursor is visible. |
enableDebug | bool | Enable debug logging. |
vegetationDetectionDistance | float | The amount of distance to detect vegetations around the player. |
AxeCursor | Texture2D | The sprite that the default cursor will change to when hovering over items. |
BlastRange | float | This is for the explosive tree functionality and represents the distance the blast will affect, this is really meant as a showcase and torture test for the system. |
BlastForce | float | This is for the explosive tree functionality and represents the amount of force placed on objects, this is really meant as a showcase and torture test for the system. |
toolDamage | int | The current damage that is being applied to the harvested object. This is overridden by the tool that is held, if you are using tools. |
harvesterOffset | Vector3 | This is used to offset the Y value higher or lower on the object, should not be needed. |
currentPosition | Vector3 | The current position of the player. |
currentSkillLevel | int | The current skill level of the player. |