-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRodManager.lua
More file actions
131 lines (106 loc) · 2.98 KB
/
RodManager.lua
File metadata and controls
131 lines (106 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
local RodManager = {}
--// Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
--// Variables
local rodConfigs = {}
local rodInstances = {}
--// Initialize the Rod Manager
function RodManager.Init(rodConfigTable)
assert(typeof(rodConfigTable) == "table", "Rod config must be a table")
rodConfigs = rodConfigTable
end
--// Create a new rod instance
function RodManager.CreateRod(rodId)
if not rodConfigs[rodId] then
warn("Rod ID not found in config: " .. rodId)
return nil
end
local config = rodConfigs[rodId]
local rodModel = ServerStorage:FindFirstChild("FishingRods") and
ServerStorage.FishingRods:FindFirstChild(config.Model)
if not rodModel then
rodModel = ReplicatedStorage:FindFirstChild("FishingRods") and
ReplicatedStorage.FishingRods:FindFirstChild(config.Model)
if not rodModel then
warn("Rod model not found: " .. config.Model)
return nil
end
end
local rod = {
Id = rodId,
Name = config.Name,
ModelName = config.Model,
Rarity = config.Rarity,
CastRange = config.CastRange,
ReelSpeed = config.ReelSpeed,
CatchChance = config.CatchChance,
RareFishChance = config.RareFishChance,
MaxDurability = config.Durability,
CurrentDurability = config.Durability,
Model = rodModel:Clone(),
IsFishing = false,
CastPosition = nil,
FishingStartTime = nil,
Connections = {},
}
function rod:Cast(position)
if self.IsFishing then return false, "Already fishing" end
if self.CurrentDurability <= 0 then return false, "Rod is broken" end
self.IsFishing = true
self.CastPosition = position
self.FishingStartTime = tick()
return true
end
function rod:ReelIn()
if not self.IsFishing then return false, "Not currently fishing" end
self.IsFishing = false
self.CurrentDurability = math.max(0, self.CurrentDurability - 1)
local catchSuccess = math.random() < self.CatchChance
return catchSuccess
end
function rod:Repair(amount)
self.CurrentDurability = math.min(self.MaxDurability, self.CurrentDurability + amount)
return self.CurrentDurability
end
function rod:Destroy()
for _, connection in pairs(self.Connections) do
if typeof(connection) == "RBXScriptConnection" and connection.Connected then
connection:Disconnect()
end
end
self.Connections = {}
if self.Model then
self.Model:Destroy()
self.Model = nil
end
end
local rodId = rodId .. "_" .. tostring(tick())
rodInstances[rodId] = rod
return rod
end
--// Get rod by ID
function RodManager.GetRod(rodId)
return rodInstances[rodId]
end
--// Get all available rod types
function RodManager.GetRodTypes()
local types = {}
for rodId, config in pairs(rodConfigs) do
table.insert(types, {
Id = rodId,
Name = config.Name,
Rarity = config.Rarity
})
end
return types
end
--// Clean up resources
function RodManager.DestroyRod(rodId)
local rod = rodInstances[rodId]
if rod then
rod:Destroy()
rodInstances[rodId] = nil
end
end
return RodManager