-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShootingRange.lua
204 lines (146 loc) · 6.66 KB
/
ShootingRange.lua
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local StatCalculationUtils = require(ReplicatedStorage.Shared.Utils.StatCalculationUtils)
local NumberUtils = require(ReplicatedStorage.Shared.Utils.NumberUtils)
local AttributeUtils = require(ReplicatedStorage.Shared.Utils.AttributeUtils)
local ItemInfo = require(ReplicatedStorage.Shared.ItemInfo)
local RangeInfo = require(ReplicatedStorage.Shared.RangeInfo)
local EnemyInfo = require(ReplicatedStorage.Shared.EnemyInfo)
local Trove = require(ReplicatedStorage.Shared.Packages.Trove)
local ShootingRange = {}
local rangeTroves = {}
local randomObject
function ShootingRange.SetupPlayer(player)
local enemyFolder = Instance.new("Folder")
enemyFolder.Name = "Enemies"
enemyFolder.Parent = player
end
function ShootingRange.StartShootingRange(player, range)
if AttributeUtils.GetAttribute(player, "ActiveRange") then
warn("Player is already in a range")
return
end
local unlockedRanges = AttributeUtils.GetAttribute(player, "unlockedRanges", {})
if table.find(unlockedRanges, range) == nil then
warn("Player does not own range")
return
end
AttributeUtils.SetAttribute(player, "ActiveRange", range)
AttributeUtils.SetAttribute(player, "ActiveRangeHealth", 100)
AttributeUtils.SetAttribute(player, "ActiveRangeSpecialEnergy", 0)
AttributeUtils.SetAttribute(player, "ActiveLaneRangeIndex", 2)
AttributeUtils.SetAttribute(player, "RangeEnemiesDefeated", 0)
AttributeUtils.SetAttribute(player, "rangeRandomSeed", math.random(1, 1000))
local amountOfEnemies = RangeInfo[range].enemyKillRequirement
randomObject = Random.new(AttributeUtils.GetAttribute(player, "rangeRandomSeed"))
local trove = Trove.new()
rangeTroves[player.UserId] = trove
task.spawn(function()
local enemyIndex = 1
while AttributeUtils.GetAttribute(player, "ActiveRange") ~= nil and enemyIndex <= amountOfEnemies do
local randomEnemyTypeIndex = randomObject:NextInteger(1, #RangeInfo[range].enemies)
local enemyType = RangeInfo[range].enemies[randomEnemyTypeIndex]
local info = EnemyInfo[range][enemyType]
local enemyData = {
id = enemyIndex,
type = enemyType,
health = randomObject:NextNumber(info.healthRange[1], info.healthRange[2]),
damage = info.damage,
speed = info.speed,
}
local healthDecrement = info.damage
local selectedSpawn = randomObject:NextInteger(1, 3)
local enemyObject = Instance.new("StringValue")
enemyObject.Name = enemyIndex
trove:Add(enemyObject)
AttributeUtils.SetAttribute(enemyObject, "health", enemyData.health)
AttributeUtils.SetAttribute(enemyObject, "damage", enemyData.damage)
AttributeUtils.SetAttribute(enemyObject, "lastDamage", enemyData.speed)
AttributeUtils.SetAttribute(enemyObject, "type", enemyData.type)
AttributeUtils.SetAttribute(enemyObject, "selectedSpawn", selectedSpawn)
enemyObject.Parent = player.Enemies
enemyIndex += 1
local timeToReachEnd = (
workspace.ShootingRanges[range].Spawns[1].Position - workspace.ShootingRanges[range].Ends[1].Position
).Magnitude / enemyData.speed
trove:Add(task.delay(timeToReachEnd + 0.1, function()
if AttributeUtils.GetAttribute(enemyObject, "health", 0) > 0 then
AttributeUtils.IncrementAttribute(player, "ActiveRangeHealth", -healthDecrement)
if AttributeUtils.GetAttribute(player, "ActiveRangeHealth", 0) <= 0 then
ShootingRange.EndShootingRange(player)
end
end
end))
task.wait(randomObject:NextNumber(1, 2))
end
end)
end
function ShootingRange.EndShootingRange(player)
ShootingRange.Systems.Network.GetEvent("ShootingRangeEnded"):FireClient(player)
local rangeInfo = RangeInfo[player:GetAttribute("ActiveRange")]
AttributeUtils.SetAttribute(player, "ActiveRange", AttributeUtils.Nil)
AttributeUtils.SetAttribute(player, "ActiveRangeHealth", AttributeUtils.Nil)
AttributeUtils.SetAttribute(player, "ActiveRangeSpecialEnergy", AttributeUtils.Nil)
AttributeUtils.SetAttribute(player, "ActiveLaneRangeIndex", AttributeUtils.Nil)
AttributeUtils.SetAttribute(player, "RangeEnemiesDefeated", AttributeUtils.Nil)
rangeTroves[player.UserId]:Destroy()
local selectedGrade = NumberUtils.GetWeightedRandomItem(rangeInfo.rewards.tokenGradeTable)
local selectedRarity = NumberUtils.GetWeightedRandomItem(rangeInfo.rewards.tokenRarityTable)
local simulationTokens = AttributeUtils.GetAttribute(player, "simulationTokens")
if simulationTokens then
simulationTokens[selectedRarity .. selectedGrade] += 1
AttributeUtils.SetAttribute(player, "simulationTokens", simulationTokens)
ShootingRange.Systems.Network.GetEvent("SendPlayerReward"):FireClient(player, {
grade = selectedGrade,
rarity = selectedRarity,
})
end
end
function ShootingRange.EnemyHit(player, enemyId, damage, energy)
local enemyObject = player.Enemies[enemyId]
local equippedItems = AttributeUtils.GetAttribute(player, "equippedItems", {})
local itemInfo = ItemInfo[equippedItems.playerBowSlot]
local enemyInfo =
EnemyInfo[AttributeUtils.GetAttribute(player, "ActiveRange")][AttributeUtils.GetAttribute(enemyObject, "type")]
local withinRange = false
if
damage >= itemInfo.damageRange[1]
and damage <= itemInfo.damageRange[2]
and energy >= enemyInfo.energyRange[1]
and energy <= enemyInfo.energyRange[2]
then
withinRange = true
end
if not withinRange then
print("hit not within range")
return
end
local realDamage = StatCalculationUtils.GetTotalDamage(player, damage)
AttributeUtils.IncrementAttribute(enemyObject, "health", -realDamage)
local realEnergy = StatCalculationUtils.GetTotalEnergy(player, energy)
local currentSpecialEnergy = AttributeUtils.GetAttribute(player, "ActiveRangeSpecialEnergy", 0)
AttributeUtils.SetAttribute(
player,
"ActiveRangeSpecialEnergy",
math.clamp(currentSpecialEnergy + realEnergy, 0, 300)
)
if AttributeUtils.GetAttribute(enemyObject, "health") <= 0 then
AttributeUtils.IncrementAttribute(player, "RangeEnemiesDefeated", 1)
if
AttributeUtils.GetAttribute(player, "RangeEnemiesDefeated", 0)
== RangeInfo[AttributeUtils.GetAttribute(player, "ActiveRange")].enemyKillRequirement
then
ShootingRange.EndShootingRange(player)
end
end
end
function ShootingRange.SpecialUsed(player)
AttributeUtils.IncrementAttribute(player, "ActiveRangeSpecialEnergy", -100)
end
function ShootingRange.Start()
ShootingRange.Systems.Network.GetEvent("StartShootingRange").OnServerEvent:Connect(ShootingRange.StartShootingRange)
ShootingRange.Systems.Network.GetEvent("EnemyHit").OnServerEvent:Connect(ShootingRange.EnemyHit)
ShootingRange.Systems.Network.GetEvent("SpecialUsed").OnServerEvent:Connect(ShootingRange.SpecialUsed)
Players.PlayerAdded:Connect(ShootingRange.SetupPlayer)
end
return ShootingRange