Advertisement
Void_scripter0

rifle

Feb 3rd, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.13 KB | None | 0 0
  1. if game:GetService("RunService"):IsClient() then error("Script must be server-side in order to work; use h/ and not hl/") end
  2. local Player,game,owner = owner,game
  3. local RealPlayer = Player
  4. do
  5. print("FE Compatibility code by Mokiros | Showcase by Bacon Hair!")
  6. local rp = RealPlayer
  7. script.Parent = rp.Character
  8.  
  9. --RemoteEvent for communicating
  10. local Event = Instance.new("RemoteEvent")
  11. Event.Name = "UserInput_Event"
  12.  
  13. --Fake event to make stuff like Mouse.KeyDown work
  14. local function fakeEvent()
  15. local t = {_fakeEvent=true,Functions={},Connect=function(self,f)table.insert(self.Functions,f) end}
  16. t.connect = t.Connect
  17. return t
  18. end
  19.  
  20. --Creating fake input objects with fake variables
  21. local m = {Target=nil,Hit=CFrame.new(),KeyUp=fakeEvent(),KeyDown=fakeEvent(),Button1Up=fakeEvent(),Button1Down=fakeEvent()}
  22. local UIS = {InputBegan=fakeEvent(),InputEnded=fakeEvent()}
  23. local CAS = {Actions={},BindAction=function(self,name,fun,touch,...)
  24. CAS.Actions[name] = fun and {Name=name,Function=fun,Keys={...}} or nil
  25. end}
  26. --Merged 2 functions into one by checking amount of arguments
  27. CAS.UnbindAction = CAS.BindAction
  28.  
  29. --This function will trigger the events that have been :Connect()'ed
  30. local function te(self,ev,...)
  31. local t = m[ev]
  32. if t and t._fakeEvent then
  33. for _,f in pairs(t.Functions) do
  34. f(...)
  35. end
  36. end
  37. end
  38. m.TrigEvent = te
  39. UIS.TrigEvent = te
  40.  
  41. Event.OnServerEvent:Connect(function(plr,io)
  42. if plr~=rp then return end
  43. m.Target = io.Target
  44. m.Hit = io.Hit
  45. if not io.isMouse then
  46. local b = io.UserInputState == Enum.UserInputState.Begin
  47. if io.UserInputType == Enum.UserInputType.MouseButton1 then
  48. return m:TrigEvent(b and "Button1Down" or "Button1Up")
  49. end
  50. for _,t in pairs(CAS.Actions) do
  51. for _,k in pairs(t.Keys) do
  52. if k==io.KeyCode then
  53. t.Function(t.Name,io.UserInputState,io)
  54. end
  55. end
  56. end
  57. m:TrigEvent(b and "KeyDown" or "KeyUp",io.KeyCode.Name:lower())
  58. UIS:TrigEvent(b and "InputBegan" or "InputEnded",io,false)
  59. end
  60. end)
  61. Event.Parent = NLS([==[
  62. local Player = game:GetService("Players").LocalPlayer
  63. local Event = script:WaitForChild("UserInput_Event")
  64.  
  65. local Mouse = Player:GetMouse()
  66. local UIS = game:GetService("UserInputService")
  67. local input = function(io,a)
  68. if a then return end
  69. --Since InputObject is a client-side instance, we create and pass table instead
  70. Event:FireServer({KeyCode=io.KeyCode,UserInputType=io.UserInputType,UserInputState=io.UserInputState,Hit=Mouse.Hit,Target=Mouse.Target})
  71. end
  72. UIS.InputBegan:Connect(input)
  73. UIS.InputEnded:Connect(input)
  74.  
  75. local h,t
  76. --Give the server mouse data 30 times every second, but only if the values changed
  77. --If player is not moving their mouse, client won't fire events
  78. while wait(1/30) do
  79. if h~=Mouse.Hit or t~=Mouse.Target then
  80. h,t=Mouse.Hit,Mouse.Target
  81. Event:FireServer({isMouse=true,Target=t,Hit=h})
  82. end
  83. end]==],Player.Character)
  84.  
  85. ----Sandboxed game object that allows the usage of client-side methods and services
  86. --Real game object
  87. local _rg = game
  88.  
  89. --Metatable for fake service
  90. local fsmt = {
  91. __index = function(self,k)
  92. local s = rawget(self,"_RealService")
  93. if s then return s[k] end
  94. end,
  95. __newindex = function(self,k,v)
  96. local s = rawget(self,"_RealService")
  97. if s then s[k]=v end
  98. end,
  99. __call = function(self,...)
  100. local s = rawget(self,"_RealService")
  101. if s then return s(...) end
  102. end
  103. }
  104. local function FakeService(t,RealService)
  105. t._RealService = typeof(RealService)=="string" and _rg:GetService(RealService) or RealService
  106. return setmetatable(t,fsmt)
  107. end
  108.  
  109. --Fake game object
  110. local g = {
  111. GetService = function(self,s)
  112. return self[s]
  113. end,
  114. Players = FakeService({
  115. LocalPlayer = FakeService({GetMouse=function(self)return m end},Player)
  116. },"Players"),
  117. UserInputService = FakeService(UIS,"UserInputService"),
  118. ContextActionService = FakeService(CAS,"ContextActionService"),
  119. }
  120. rawset(g.Players,"localPlayer",g.Players.LocalPlayer)
  121. g.service = g.GetService
  122.  
  123. g.RunService = FakeService({
  124. RenderStepped = _rg:GetService("RunService").Heartbeat,
  125. BindToRenderStep = function(self,name,_,fun)
  126.  
  127. end,
  128. UnbindFromRenderStep = function(self,name)
  129. self._btrs[name]:Disconnect()
  130. end,
  131. },"RunService")
  132.  
  133. setmetatable(g,{
  134. __index=function(self,s)
  135. return _rg:GetService(s) or typeof(_rg[s])=="function"
  136. and function(_,...)return _rg[s](_rg,...)end or _rg[s]
  137. end,
  138. __newindex = fsmt.__newindex,
  139. __call = fsmt.__call
  140. })
  141. --Changing owner to fake player object to support owner:GetMouse()
  142. game,owner = g,g.Players.LocalPlayer
  143. end
  144. class Rifle {
  145. constructor() {
  146. }
  147.  
  148. shoot() {};
  149. getAccuracy() {};
  150. getLoudness() {};
  151. getWeight() {};
  152. getRange() {};
  153. }
  154.  
  155. class SimpleRifle extends Rifle {
  156. constructor() {
  157. super();
  158. }
  159.  
  160. shoot() {
  161. console.log('pew pew pew');
  162. }
  163.  
  164. getAccuracy() {
  165. return 10;
  166. }
  167.  
  168. getLoudness() {
  169. return 160; // dB
  170. }
  171.  
  172. getWeight() {
  173. return 3000; // g
  174. }
  175.  
  176. getRange() {
  177. return 300; // m
  178. }
  179. }
  180.  
  181. class RifleDecorator extends Rifle {
  182. constructor(rifle) {
  183. super();
  184.  
  185. this.decoratedRifle = rifle;
  186. }
  187.  
  188. shoot () {
  189. return this.decoratedRifle.shoot();
  190. };
  191.  
  192. getAccuracy () {
  193. return this.decoratedRifle.getAccuracy();
  194. };
  195.  
  196. getLoudness() {
  197. return this.decoratedRifle.getLoudness();
  198. };
  199.  
  200. getWeight() {
  201. return this.decoratedRifle.getWeight();
  202. };
  203.  
  204. getRange() {
  205. return this.decoratedRifle.getRange();
  206. };
  207. }
  208.  
  209. class RifleWithTelescope extends RifleDecorator {
  210. constructor(rifle) {
  211. super(rifle);
  212. }
  213.  
  214. getAccuracy() {
  215. return super.getAccuracy() + 10;
  216. }
  217.  
  218. getRange() {
  219. return super.getRange() + 400;
  220. }
  221.  
  222. getWeight() {
  223. return super.getWeight() + 400;
  224. }
  225. }
  226.  
  227. class RifleWithGrenadeLauncher extends RifleDecorator {
  228. constructor(rifle) {
  229. super(rifle);
  230. }
  231.  
  232. getAccuracy() {
  233. return super.getAccuracy() - 2;
  234. }
  235.  
  236. getWeight() {
  237. return super.getWeight() + 1300;
  238. }
  239.  
  240. getLoudness() {
  241. return super.getLoudness() + 50;
  242. }
  243. }
  244.  
  245. let newRifle = new SimpleRifle();
  246.  
  247. console.log(newRifle);
  248.  
  249. newRifle = new RifleWithTelescope(newRifle);
  250.  
  251. console.log(newRifle);
  252.  
  253. newRifle = new RifleWithGrenadeLauncher(newRifle);
  254.  
  255. console.log(newRifle);
  256.  
  257. module.exports = {
  258. Rifle,
  259. SimpleRifle,
  260. RifleDecorator,
  261. RifleWithTelescope,
  262. RifleWithGrenadeLauncher
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement