diff --git a/README.md b/README.md index 17de6c0..b958cbd 100644 --- a/README.md +++ b/README.md @@ -39,8 +39,8 @@ RegionState:SetVisibleWhenOutsideRegions("Region3") --Make it so region 1 can see 2 and 3. --ConnectRegions makes it so both regions can see each other. -RegionStateObject:ConnectRegions("Region1", "Region2") -RegionStateObject:ConnectRegions("Region1", "Region3") +RegionState:ConnectRegions("Region1", "Region2") +RegionState:ConnectRegions("Region1", "Region3") --Set up the models (next section) @@ -67,6 +67,28 @@ local IsVisible = RegionState:IsRegionVisible("Region1") --Bool for if a region local InRegion = RegionState:IsInRegion("Region1", Vector3.new()) --Bool for if a point is in a region ``` +#### Secondary Region Setup +`RegionState` offers an additional method that makes it easier to create a centralized folder of "Regions" that you can easily customize, expand, rotate, resize, and re-position. +Depending on use cases, a variety of ways to setup regions is possible. One way is by storing additional regions that can allow you to expand a previous region to an easily customizable size. +When rooms are not entire models this may be of use as it allows such behavior. +``` +Regions : Instance +|- Region1 : Instance +| |-- ZonePart : BasePart +| |-- ZonePart : BasePart +|- Region2 : Instance +| |-- ZonePart : BasePart +``` +Using the instance above as an example, +```lua +local ReplicatedStorage = game:GetService("ReplicatedStorage") +local RegionCulling = require(ReplicatedStorage:WaitForChild("RegionCulling")) +local RegionState = RegionCulling.RegionState + +RegionState:InsertRegionsFromInstance(ReplicatedStorage:WaitForChild("Regions")) +--// RegionCulling will generate all regions based on the provided instances. +``` + ## ModelCulling `ModelCulling` controls the hiding of models based on `RegionState`. Almost all API calls will be to `BindModelToRegion`, which returns @@ -159,4 +181,4 @@ before retrying. Waiting may be required before getting a proper reading. # License This project is available under the terms of the MIT License. -See [LICENSE](LICENSE) for details. \ No newline at end of file +See [LICENSE](LICENSE) for details. diff --git a/src/State/BufferedRegionState.lua b/src/State/BufferedRegionState.lua index 437493e..704b3c7 100644 --- a/src/State/BufferedRegionState.lua +++ b/src/State/BufferedRegionState.lua @@ -92,6 +92,15 @@ function BufferedRegionState:AddRegion(RegionName: string, Center: CFrame, Size: self.WrappedRegionState:AddRegion(RegionName, Center, Size) end +--[[ +Creates regions based on a pre-determined model/folder, provides for quicker setup & +easier customization. +Name each region with the appropriate title and then add parts to determine their region. +--]] +function BufferedRegionState:InsertRegionsFromInstance(Instances: Model | Folder | Instance): () + self.WrappedRegionState:InsertRegionsFromInstance(Instances) +end + --[[ Marks a region as visible to another. --]] @@ -115,4 +124,4 @@ end -return (BufferedRegionState :: any) :: Types.BufferedRegionState \ No newline at end of file +return (BufferedRegionState :: any) :: Types.BufferedRegionState diff --git a/src/State/RegionState.lua b/src/State/RegionState.lua index 44c215c..a2fcbc7 100644 --- a/src/State/RegionState.lua +++ b/src/State/RegionState.lua @@ -95,6 +95,28 @@ function RegionState:AddRegion(RegionName: string, Center: CFrame, Size: Vector3 }) end +--[[ +Creates regions based on a pre-determined model/folder, provides for quicker setup & +easier customization. +Name each region with the appropriate title and then add parts to determine their region. +--]] +function RegionState:InsertRegionsFromInstance(Instances: Model | Folder | Instance): () + if #Instances:GetChildren() == 0 then + error("Instance \"" .. Instances:GetFullName() .. "\" has no child instances, unable to build Regions from it.") + end + for _, RegionParent in Instances:GetChildren() do + --Get the region name from the child name + local RegionName = RegionParent.Name + for _, Inst in RegionParent:GetChildren() do + if not Inst:IsA("BasePart") then + continue + end + local RegionPart : BasePart = Inst :: BasePart + self:AddRegion(RegionName, RegionPart.CFrame, RegionPart.Size) + end + end +end + --[[ Marks a region as visible to another. --]] @@ -162,4 +184,4 @@ end -return (RegionState :: any) :: Types.RegionState \ No newline at end of file +return (RegionState :: any) :: Types.RegionState diff --git a/src/Types.lua b/src/Types.lua index 6380137..9bf478b 100644 --- a/src/Types.lua +++ b/src/Types.lua @@ -15,6 +15,7 @@ export type BaseRegionState = { GetCurrentVisibleRegions: (self: BaseRegionState) -> ({string}), IsRegionVisible: (self: BaseRegionState, RegionName: string) -> (boolean), AddRegion: (self: BaseRegionState, RegionName: string, Center: CFrame, Size: Vector3) -> (), + InsertRegionsFromInstance: (self: BaseRegionState, Instances: Instance) -> (), ConnectRegions: (self: BaseRegionState, RegionName1: string, RegionName2: string) -> (), SetVisibleWhenOutsideRegions: (self: BaseRegionState, RegionName: string) -> (), StartUpdating: (self: BaseRegionState) -> (), @@ -99,4 +100,4 @@ export type ModelCulling = { StartModelFlattening: (self: ModelCulling) -> (), } -return true \ No newline at end of file +return true diff --git a/test/State/RegionState.spec.lua b/test/State/RegionState.spec.lua index f6103a1..e314d5f 100644 --- a/test/State/RegionState.spec.lua +++ b/test/State/RegionState.spec.lua @@ -142,5 +142,19 @@ return function() expect(RegionStateObject:IsRegionVisible("Region2")).to.equal(false) expect(RegionStateObject:IsRegionVisible("Region3")).to.equal(false) end) + + it("should build regions from parts", function() + local InstanceTree = Instance.new("Folder") + InstanceTree.Name = "Regions" + local Region4 = Instance.new("Model", InstanceTree) + Region4.Name = "Region4" + local BasePart = Instance.new("Part", Region4) + BasePart.CFrame = CFrame.new(0, 4, 0) + BasePart.Size = Vector3.new(2, 2, 2) + + RegionStateObject:InsertRegionsFromInstance(InstanceTree) + expect(RegionStateObject:IsInRegion("Region4", Vector3.new(0, 1, 0))).to.equal(false) + expect(RegionStateObject:IsInRegion("Region4", Vector3.new(0, 3, 0))).to.equal(true) + end) end) -end \ No newline at end of file +end