Skip to content

Full code documentation

Bas de Reus edited this page Mar 26, 2024 · 49 revisions

This is a wiki page with the full documentation of every piece of code. Any class can be found here.

Animation Controller

The animation controller is a simple script that controls the animations of either the player or the NPC. This works by creating triggers that will decide wether an animation gets played.

Written by William

Usage

Flowchart

---
title: Animation Controller
---
graph TD;
    A[Start] --> B{Animator Assigned?};
    B -->|Yes| C[Play Animation];
    B -->|No| D(Error: No Animator Assigned);
    C --> E{Valid Animation?};
    E -->|Yes| F(Set Trigger);
    E -->|No| G(Error: Invalid Animation);
Loading

The class

The script requires a component of the type Animator. Otherwise it will not function.

Methods Return Description
+ PlayAnimation(string animation) void Play's an animation by activating the trigger given as a string
- IsValidAnimation(string animationName) bool Checks if the animation trigger exists.

Source

If u wish to take a closer look at the script Animation Controller.

Enum extensions

This is a static class that improve the usage of enum with custom attributes.

Write by Bas

Usage

Every method it public.

Methods Return Description
GetString(Enum) string Get the string of an enum type. If not existing returns empty string
GetChar(Enum) char Get the char of an enum type. If not existing returns empty char ('\0')
GetVector2(Enum) Vector2 Get the Vector2 of an enum type. If not existing returns vector2.zero
GetVector3(Enum) Vector3 Get the Vector3 of an enum type. If not existing returns vector3.zero
GetRandomEnumValue T Retrieves a random enum value of the specified type. Returns a random enum value of type T
Vector3 use case

Vector 3 attributes

image

Enum with Vector3

image

Using the enums Vector3

image

GetRandomEnumValue use case

image

Source

Static class

EnumExtensions.cs

Attributes

  1. CharValue
  2. StringValue
  3. Vector2Value
  4. Vector3Value

Menu's

The UI scripts I made serve 2 purposes. The settings script's purpose is loading, saving and creating the settings. The MenuSwitcher's purpose is switching from one menu to another without too much hassle.

Usage

Settings

Methods Return Description
+LoadSettings() void LoadSettings is a function that will check if there are any settings that have been changed from it's default value. If there weren't any changes it will load the default values. If there have been changes it will load those settings.
+ApplySettings() void Apply settings is linked to the apply button. It saves a boolean into a playerpref which says that settings have been changed from it's default value
+SetVolume(float) void This changes the volume and saves the value of the float into a playerpref. This is hooked up to a slider.
+SetQuality(int) void This changes the quality of the game to the int that is selected. This is hooked up to a dropdown. The int being the value of the dropdown.
+SetFullscreen(bool) void This sets the game to either fullscreen, or windowed. This is hooked up to a button
+SetResolution(int) void This changes the Resolution of the game to the int that is selected. The int is the index of the resolution dropdown
-SetHighestQuality() void This sets the game to the highest quality
-GetDefaultResolution() void This gets the highest possible resolution it can get and then sets that as its default
-PopulateResolutions() void This will fill the Resolution Dropdown with all the possible Resolutions it can find that are on the device.

Menu Switcher

Method Return Description
+HideMenu(Gameobject) void This will hide a specific Gameobject that is given through a unity event
+ActivateMenu(Gameobject) void This will reveal/activate a specific Gameobject that is given through a unity event.
+TogglePauseGame() void This will check if the game is paused, if it is not it will stop the timescale
+PausingEvent() void This will invoke the PausingGame event which allows us to easily add what should happen once the game is paused.

Source

If u wish to take a closer look at the scripts:

Object Interaction Ray

The Object Interaction Ray is a versatile tool designed to enhance gameplay across various platforms, including PC, mobile, and tablet devices. With its universal functionality, players can seamlessly click and interact with in-game elements, ensuring a smooth and immersive gaming experience regardless of the device they're using. This intuitive feature adds depth and engagement to gameplay, allowing users to effortlessly navigate and engage with the virtual world at their fingertips.

Written by Martijn

Usage

Methods Return Description
- GetMousePosition Vector3 Gets the position of the mouse / finger on the touchscreen
- Interact void Shoots out a raycast in the direction of GetMousePosition, that when it hits an object with the INTERACTABLE_TAG invokes an event
Events Description
onInteract The event that triggers when the ray from Interact collides with this object.
Interaction Ray Preview

InteractRaygif

Source

Scene switcher

This is a simple class to make the switch and loading of scene simple.

Writen by Bas

Usage

Method Return Description
+ LoadScene() void Load the scene that is set (sceneToLoad property)
+ LoadSceneAsync() Ienumerator This is a coroutine to load a new scene asynchronously. This allows for the progress of scene loading to be logged and displayed.
+ SetAndLoadScene() void Set the sceneToLoad property to a new scene, if this succeeds it will load it, otherwise it will give an error
+SetAndLoadSceneAsync() void Set the sceneToLoad property to a new scene, if the scene exists it will load it asynchronously. Otherwise it will give an error
+ SetSceneToLoad() bool Set the sceneToLoad property to a new scene, if it's not in the build settings you will get an error
- SceneExists(string) bool Looks at the build settings if the given scene is in it.

Source

You want to see how it works SceneSwitcher.cs.

Timer

A easy to use timer class. With multiple events that can be used. There is an editor script that inhance the usage of the timer class.

Writen by Bas

Usage

Methods Return Description
+ ResetTimer void Reset the timer to startingTime and calls the onReset event
+ SetCanCount(bool) void Set the canCount property, when setting it to true it will start counting, otherwise is stops
+ SetTimerLenght(float) void Set the timer lenght, when resetting it will use this number
+ GetCurrentTimerLenght float Get the timer it's current lenght
+ GetCurrentTimerPercentage float Calculates the percentage of the current timer relative to the progress. Return a number between 0-1
- Counting void The function that counts the timer. Calls events when it's needs to
Events Description
onTimerDone An event for when the timer is done with counting
onTimerPassedThreshold An event for when the timer is has passed the threshold when counting up
onStart An event for when starting counting
onReset An event for when reseting the timer
Timer class Unity inspector

image image

Source

You want to see how it works Timer.cs, there is also a custom editor script for it.

Vector extensions

Provides versatile extensions for Vector2 and Vector3, enhancing functionality for various operations.

A importend note, the following examples and explanmations are for both Vector2 and Vector3 except the Z axis.

Writen by Bas

Useage

Every method is public.

When a '*' is added, it means it's only for Vector3.

When a '?' is added, it means that it's optional.

Set

Set method Return Description
SetX(float) This Vector3 / Vector2 Change the X vector of this Vector3 / Vector2
SetY(float) This Vector3 / Vector2 Change the Y vector of this Vector3 / Vector2
SetZ(float) This Vector3 Change the Z vector of this Vector3

Add

Add method Return Description
Add(Vector3 / Vector2) This Vector3 / Vector2 Add the Axis of two Vector3 / Vector2 into one.
AddX(Vector3 / Vector2) This Vector3 / Vector2 Add the X Axis of two Vector3 / Vector2 into one
AddY(Vector3 / Vector2) This Vector3 / Vector2 Add the Y Axis of two Vector3 / Vector2 into one
AddZ(Vector3) This Vector3 Add the Z Axis of two Vector3 into one
Add(float, float, *float) This Vector3 / Vector2 Add the specified values to the Axis of this Vector3 / Vector2
AddX(float) This Vector3 / Vector2 Add the specified value to the X Axis of this Vector3 / Vector2
AddY(float) This Vector3 / Vector2 Add the specified value to the Y Axis of this Vector3 / Vector2
AddZ(float) This Vector3 Add the specified value to the Z Axis of this Vector3

Subtract

Subtract method Return Description
Subtract(Vector3 / Vector2) This Vector3 / Vector2 Subtract the Axis of the second Vector3 / Vector2 from the first Vector3 / Vector2and return the result
SubtractX(Vector3 / Vector2) This Vector3 / Vector2 Subtract the X Axis of the second Vector3 / Vector2 from the first Vector3 and return the result
SubtractY(Vector3 / Vector2) This Vector3 / Vector2 Subtract the Y Axis of the second Vector3 / Vector2 from the first Vector3 and return the result
SubtractZ(Vector3) This Vector3 Subtract the Z Axis of the second Vector3 from the first Vector3 and return the result
Subtract(float, float, *float) This Vector3 / Vector2 Subtract the specified values from the Axis of this Vector3 / Vector2
SubtractX(float) This Vector3 / Vector2 Subtract the specified value from the X Axis of this Vector3 / Vector2
SubtractY(float) This Vector3 / Vector2 Subtract the specified value from the Y Axis of this Vector3 / Vector2
SubtractZ(float) This Vector3 Subtract the specified value from the Z Axis of this Vector3

Multiply

Multiply method Return Description
Multiply(Vector3 / Vector2) This Vector3 / Vector2 Multiply the Axis of this Vector3 / Vector2 with the corresponding Axis of another Vector3 / Vector2 and return the result
MultiplyX(Vector3 / Vector2) This Vector3 / Vector2 Multiply the X Axis of this Vector3 / Vector2 with the X Axis of another Vector3 and return the result
MultiplyY(Vector3 / Vector2) This Vector3 / Vector2 Multiply the Y Axis of this Vector3 / Vector2 with the Y Axis of another Vector3 and return the result
MultiplyZ(Vector3) This Vector3 Multiply the Z Axis of this Vector3 with the Z Axis of another Vector3 and return the result
Multiply(float, float, *float) This Vector3 / Vector2 Multiply the specified values with the Axis of this Vector3 / Vector2
MultiplyX(float) This Vector3 / Vector2 Multiply the specified value with the X Axis of this Vector3 / Vector2
MultiplyY(float) This Vector3 / Vector2 Multiply the specified value from the Y Axis of this Vector3 / Vector2
MultiplyZ(float) This Vector3 Multiply the specified value from the Z Axis of this Vector3

Divide

Divide method Return Description
Divide(Vector3 / Vector2) This Vector3 / Vector2 Divide the Axis of this Vector3 / Vector2 by the corresponding Axis of another Vector3 / Vector2 and return the result
DivideX(Vector3 / Vector2) This Vector3 / Vector2 Divide the X Axis of this Vector3 / Vector2 by the X Axis of another Vector3 / Vector2 and return the result
DivideY(Vector3 / Vector2) This Vector3 / Vector2 Divide the X Axis of this Vector3 / Vector2 by the X Axis of another Vector3 / Vector2 and return the result
DivideZ(Vector3) This Vector3 Divide the X Axis of this Vector3 by the X Axis of another Vector3 and return the result
Dividey(float, float, *float) This Vector3 / Vector2 Divide the specified values by the Axis of this Vector3 / Vector2
DivideX(float) This Vector3 / Vector2 Divide the specified value from the X Axis of this Vector3 / Vector2
DivideY(float) This Vector3 / Vector2 Divide the specified value from the Y Axis of this Vector3 / Vector2
DivideZ(float) This Vector3 Divide the specified value from the Z Axis of this Vector3

Compare

Compare method Return Description
Compare(Vector3 / Vector2, ?float) Bool Compare two Vector3 / Vector2 with an optional margin for floating-point precision
CompareX(Vector3 / Vector2, ?float) Bool Compare the X axis of two Vector3 / Vector2 with an optional margin for floating-point precision
CompareY(Vector3 / Vector2, ?float) Bool Compare the Y axis of two Vector3 / Vector2 with an optional margin for floating-point precision
CompareZ(Vector3, ?float) Bool Compare the Z axis of two Vector3 / Vector2 with an optional margin for floating-point precision
CompareX(float, ?float) Bool Compare a float value with the X Axis of this Vector3 / Vector2 with an optional margin for floating-point precision
CompareY(float, ?float) Bool Compare a float value with the Y Axis of this Vector3 / Vector2 with an optional margin for floating-point precision
CompareZ(float, ?float) Bool Compare a float value with the Z Axis of this Vector3 / Vector2 with an optional margin for floating-point precision

Randomize

Randomize method Return Description
Randomize(Vector2, Vector2, Vector2) This Vector3 / Vector2 Randomize the X & Y (& Z) axis of this Vector3 / Vector2 within specified ranges
Randomize(Vector2) This Vector3 / Vector2 Randomize the X & Y (& Z) axis of this Vector3 / Vector2 within the specified range
RandomizeUniform(Vector2) This Vector3 / Vector2 Randomize all axis of this Vector3 / Vector2 with the same random number within the specified range
RandomizeX(Vector2) This Vector3 / Vector2 Randomize the X axis of this Vector3 / Vector2 within a specified range
RandomizeY(Vector2) This Vector3 / Vector2 Randomize the Y axis of this Vector3 / Vector2 within a specified range
RandomizeZ(Vector2) This Vector3 / Vector2 Randomize the Z axis of this Vector3 within a specified range

Invert

Invert method Return Description
Invert() This Vector3 / Vector2 Invert all axis of this Vector3 / Vector2
InvertX() This Vector3 / Vector2 Invert the X axis of this Vector3 / Vector2
InvertY() This Vector3 / Vector2 Invert the Y axis of this Vector3 / Vector2
InvertZ() This Vector3 / Vector2 Invert the X axis of this Vector3

Midpoint

Midpoint method Return Description
Midpoint(Vector3 / Vector2) New Vector3 / Vector2 Calculate the midpoint between two Vector3 / Vector2
WeightedAverage(Vector3 / Vector2, float) New Vector3 / Vector2 Calculate the weighted average of two Vector3 / Vector2 based on specified weight

IsWithinRange

IsWithinRange method Return Description
IsWithinRange(Vector3 / Vector2, float) Bool Checks if this Vector3 / Vector2 is within the specified range of another Vector3 / Vector2
IsXWithinRange(Vector3 / Vector2, float) Bool Checks if the X Axis of this Vector3 / Vector2 is within the specified range of the X Axis of another Vector3 / Vector2
IsYWithinRange(Vector3 / Vector2, float) Bool Checks if the Y Axis of this Vector3 / Vector2 is within the specified range of the X Axis of another Vector3 / Vector2
IsZWithinRange(Vector3, float) Bool Checks if the Z Axis of this Vector3 is within the specified range of the X Axis of another Vector3

Source

You want to see how it works VectorExtensions.cs.

Clone this wiki locally