Skip to content

Commit

Permalink
Merge pull request #16 from prooheckcp/Version117
Browse files Browse the repository at this point in the history
RobloxStateMachine - V.1.1.7
  • Loading branch information
prooheckcp committed May 12, 2024
2 parents 87e3d73 + 9001e44 commit 2f7113a
Show file tree
Hide file tree
Showing 23 changed files with 500 additions and 87 deletions.
40 changes: 40 additions & 0 deletions .moonwave/custom.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
:root {
--primary-color: #86C232;
--secondary-color: #61892F;
--background-color: #222629;
}
*/

:root {
--primary-color: #53900F;
--secondary-color: #1F6521;
--background-color: rgba(34,38,41,1);
}

html[data-theme='dark']{
--ifm-background-color: var(--background-color);
--ifm-color-primary: var(--primary-color);
--ifm-color-primary-dark: var(--primary-color);
--ifm-color-primary-darker: var(--primary-color);
--ifm-color-primary-darkest: var(--primary-color);
--ifm-color-primary-light: var(--primary-color);
--ifm-color-primary-lighter: var(--primary-color);
--ifm-color-primary-lightest: var(--primary-color);
--ifm-background-color: var(--background-color);
--ifm-code-font-size: 95%;
--ifm-navbar-background-color: var(--secondary-color);
--ifm-footer-background-color: var(--secondary-color);
--ifm-background-color: var(--background-color);
--ifm-footer-background-color: var(--secondary-color);
--ifm-footer-color: var(--ifm-footer-link-color);
--ifm-footer-link-color: var(--ifm-color-secondary);
--ifm-footer-title-color: var(--ifm-color-white);
}

.footer--dark {
--ifm-footer-background-color: var(--secondary-color) !important;
--ifm-footer-color: var(--ifm-footer-link-color) !important;
--ifm-footer-link-color: var(--ifm-color-secondary) !important;
--ifm-footer-title-color: var(--ifm-color-white) !important;
}
Binary file added .moonwave/static/RobloxStateMachineIcon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![](https://cdn.discordapp.com/attachments/670023265455964198/1170892030101159946/RobloxStateMachine.png?ex=655ab12e&is=65483c2e&hm=60cad6920fb0d720cd6bd6d23c4f73190f20e39f8d15b755b00dbbaf1d600ae2&)
![](./gh-assets/RobloxStateMachine.png)

RobloxStateMachine is a lightweight library that simplifies the creation and management of state machines. It allows you create States and Transitions which are reusable and can contain/interact with different data.

Expand Down
30 changes: 30 additions & 0 deletions docs/AdvancedConcepts/accesInternals.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
sidebar_position: 4
sidebar_label: "🔍 Access internals"
---

# 🔍 Access internals

While inside a state or transition you can actually access to a lot of information about our state machine!
To do this all you have to do is call **self** from within any of the virtual methods.

Here's an example of what you can access from within the state
```lua
-- Blue.Lua
local GoToRed = require(script.Parent.Parent.Transitions.GoToRed)
local ChangeColorState = require(script.Parent.ChangeColorState)

local Blue = ChangeColorState:Extend("Blue")
Blue.Transitions = {GoToRed}

function Blue:OnEnter()
print(self.Data) -- Get state machine data
self:ChangeState("Red") -- Changes the current state to Red
self:GetState() -- "Blue"
self:GetPreviousState() -- Gets the previous state this state machine was in before Blue
self:ChangeData() -- Changes data from the state machine
end


return Blue
```
71 changes: 71 additions & 0 deletions docs/AdvancedConcepts/extendStates.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
sidebar_position: 2
sidebar_label: "🚀 Extend States/Transitions"
---

# 🚀 Extend States/Transitions

We can extend classes that share behavior with one another. In fact we do this in one of our examples!

Let's see how this works in practice. First you start by creating a Parent State. This is the state from which other states will be created.


```lua
--ChangeColorState.lua
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local RobloxStateMachine = require(ReplicatedStorage.RobloxStateMachine)

local State = RobloxStateMachine.State

local ChangeColorState = State "ChangeColorState"

function ChangeColorState:OnInit()
self.Color = Color3.fromRGB(255, 255, 255)
end

function ChangeColorState:OnEnter(data)
data.part.Color = self.Color
end

function ChangeColorState:OnDestroy()
print("Destroy Instance")
end

return ChangeColorState
```

Now that we have our parent class that defines are behavior on which we will be changing the part color to what has been defined in the
init function we can now make new states! Let's create a blue and red state

```lua
--Red.lua
local GoToBlue = require(script.Parent.Parent.Transitions.GoToBlue)
local ChangeColorState = require(script.Parent.ChangeColorState)

local Red = ChangeColorState:Extend("Red")
Red.Transitions = {GoToBlue}

function Red:OnInit()
self.Color = Color3.fromRGB(255, 0, 0)
end

return Red
```

```lua
--Blue.lua
local GoToRed = require(script.Parent.Parent.Transitions.GoToRed)
local ChangeColorState = require(script.Parent.ChangeColorState)

local Blue = ChangeColorState:Extend("Blue")
Blue.Transitions = {GoToRed}

function Blue:OnInit()
self.Color = Color3.fromRGB(0, 0, 255)
end

return Blue
```

By doing this we didn't have to define the changing color behavior on both blue and red!
42 changes: 42 additions & 0 deletions docs/AdvancedConcepts/stopStateTransition.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
sidebar_position: 3
sidebar_label: "🛑 Stop State Transition"
---

# 🛑 Stop State Transition

There's specific situations where we might want to "stuck" a state machine on a specific state.
For this purpose there's a virtual method specifically for this!

```lua
--Blue.lua
local GoToRed = require(script.Parent.Parent.Transitions.GoToRed)
local ChangeColorState = require(script.Parent.ChangeColorState)

local Blue = ChangeColorState:Extend("Blue")
Blue.Transitions = {GoToRed}

function Blue:OnInit()
self.Color = Color3.fromRGB(0, 0, 255)
end

function Blue:CanChangeState(newState: string)
return newState == "Red"
end

return Blue
```

By doing this we ensure that the state blue can only be changed to the new state **IF** the new state is equal to "Red".

We could also block the player into this state entirely by simply returning false.

```lua
function Blue:CanChangeState(newState: string)
return false
end
```

This can be specially useful to per e.g to stuck an NPC into the dead state since we don't want him to magically revive!

In case you're wondering what would happen if didn't declare this method. :CanChangeState returns **true** by default
4 changes: 4 additions & 0 deletions moonwave.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[docusaurus]
onBrokenLinks = "throw"
onBrokenMarkdownLinks = "warn"
favicon = "RobloxStateMachineIcon.png"
Binary file added pages/gh-assets/RobloxStateMachine.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local Transition = require(script.Parent.Transition)
local mergeTables = require(script.Parent.Parent.Functions.mergeTables)

--[=[
@class State
Expand Down Expand Up @@ -119,6 +120,31 @@ function State.new(stateName: string?): State
return self
end

--[=[
Extends a state inheriting the behavior from the parent state
```lua
local state = State.new("Blue")
function state:Print()
print("Hello World!")
end
local extendedState = state:Extend("Red")
function extendedState:OnInit()
self:Print() -- Will print "Hello World!"
end
```
@param stateName string
@return State
]=]
function State:Extend(stateName: string): State
return mergeTables(State.new(stateName), self)
end

--[=[
Forcelly changes the current state of our state machine to a new one
Expand Down Expand Up @@ -208,6 +234,25 @@ function State:OnInit(_data: {[string]: any}): ()

end

--[=[
:::info
This is a **Virtual Method**. Virtual Methods are meant to be overwritten
:::
```lua
function State:CanChangeState(targetState: string)
return targetState == "Blue" -- If the target state is blue, we can change to it
end
```
@param _targetState string -- The state we are trying to change to
@return boolean -- Whether or not we can change to the given state
]=]
function State:CanChangeState(_targetState: string): boolean
return true
end

--[=[
:::info
This is a **Virtual Method**. Virtual Methods are meant to be overwritten
Expand Down Expand Up @@ -302,6 +347,29 @@ function State:OnLeave(_data: {[string]: any}): ()

end

--[=[
:::info
This is a **Virtual Method**. Virtual Methods are meant to be overwritten
:::
Called whenever the state machine is destroyed
```lua
function State:OnDestroy()
print("I was destroyed!")
end
```
@return ()
]=]
function State:OnDestroy(): ()

end

export type State = typeof(State)

return State
return setmetatable(State, {
__call = function(_, properties): State
return State.new(properties)
end
})
Loading

0 comments on commit 2f7113a

Please sign in to comment.