Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- init=false
- if(not init)then
- init=true
- oldtype=type
- function type(var)
- if(getmetatable(var) and getmetatable(var).__type)then
- if(oldtype(getmetatable(var).__type)=="function")then
- return getmetatable(var).__type(var)
- elseif(oldtype(getmetatable(var).__type)=="string")then
- return getmetatable(var).__type
- end
- else
- return oldtype(var)
- end
- end
- end
- local Vector2D={}
- Vector2D.__index=function(a,b,c)
- if(b=="x")then return a[1] end
- if(b=="y")then return a[2] end
- end
- Vector2D.__call=function(tab,x,y,...)--Argvar here
- if(#{...}>0)then error("Too many arguments!") end
- local t={x,y}
- return setmetatable(t,tab)
- end
- Vector2D.__tostring=function(tab)
- return tab.x.." "..tab.y
- end
- Vector2D.__type=function() return "Vector2D" end
- Vector2D.__add=function(a,b) return Vector2D(a.x+b.x,a.y+b.y) end
- Vector2D.__mul=function(a,b)
- if(type(b)=="Vector2D")then
- return Vector2D(a.x*b.x,a.y*b.y)
- end
- if(type(b)=="number")then
- return Vector2D(a.x*b,a.y*b)
- end
- end
- setmetatable(Vector2D,Vector2D)
- local a=Vector2D(12,12)
- local particle2dtable={}
- local Particle2D={}
- Particle2D.__call=function(tab,...)
- local t={}
- t._id=#particle2dtable+1
- t._created=CurTime()
- t.x=256
- t.y=256
- t.startsize=50
- t.endsize=100
- t.startalpha=255
- t.endalpha=0
- t.velocity=Vector2D(0,0)
- t.gravity=Vector2D(0,0)
- t.r=255
- t.g=255
- t.b=255
- t.w=64
- t.h=64
- t.rollspeed=-100
- t.roll=0
- t.lifetime=5
- t.radius=10
- t.alpha=255
- t.mat="gui/silkicons/box"
- t.resistance=0
- t.collides=true
- table.Inherit(t,Particle2D)
- setmetatable(t,tab)
- particle2dtable[t._id]=t
- return t
- end
- function Particle2D:SetPos(x,y)
- if(type(x)=="Vector2D")then
- self.x=x.x
- self.y=x.y
- else
- self.x=x
- self.y=y
- end
- end
- function Particle2D:SetStartSize(radius)
- self.startsize=radius
- end
- function Particle2D:SetEndSize(radius)
- self.endsize=radius
- end
- function Particle2D:SetStartAlpha(trans)
- self.startalpha=trans
- end
- function Particle2D:SetEndAlpha(trans)
- self.endalpha=trans
- end
- function Particle2D:SetVelocity(v2d)
- self.velocity=v2d
- end
- function Particle2D:GetVelocity()
- return self.velocity
- end
- function Particle2D:SetGravity(v2d)
- self.gravity=v2d
- end
- function Particle2D:SetSize(w,h)
- if(not h)then h=w end
- self.w=w
- self.h=h
- end
- function Particle2D:SetRollSpeed(speed)
- self.rollspeed=speed
- end
- function Particle2D:SetRoll(roll)
- self.roll=roll
- end
- function Particle2D:SetColor(r,g,b)
- if(type(r)=="table" and r.r and r.g and r.b)then--It's a color table
- self.r=r.r
- self.g=r.g
- self.b=r.b
- else
- self.r=r
- self.g=g
- self.b=b
- end
- end
- function Particle2D:SetLifeTime(time)
- self.lifetime=time
- end
- function Particle2D:SetMaterial(texture)
- self.mat=texture
- end
- function Particle2D:SetResistance(res)
- self.resistance=res
- end
- function Particle2D:EnableCollision(bool)
- self.collides=true
- end
- function Particle2D:Terminate()
- particle2dtable[self._id]=nil
- end
- function Particle2D:Draw()
- if(not self._init)then
- self._init=true
- self._lerptime=CurTime()+self.lifetime
- mat=surface.GetTextureID(self.mat)--Boosts fps
- end
- local lerp=(self._lerptime-CurTime())/self.lifetime
- self.alpha=Lerp(1-lerp,self.startalpha,self.endalpha)
- self:SetSize(Lerp(1-lerp,self.startsize,self.endsize))
- local integral=CurTime()-self._created
- self.roll=(self.roll+FrameTime()*self.rollspeed)
- self.velocity.x=self.velocity.x
- local vx=self.velocity.x--Do something with resistance here
- local vy=self.velocity.y
- local gx=self.gravity.x
- local gy=self.gravity.y
- local x=(vx*integral)+(gx*integral*integral)
- local y=(vy*integral)+(gy*integral*integral)
- local vec=Vector2D(x,y)
- local posx=self.x+(vx*integral)+(gx*integral*integral)
- local posy=self.y+(vy*integral)+(gy*integral*integral)
- if(self.collides)then
- if(posx>ScrW())then
- --Bounce goes here
- end
- if(posy>ScrH())then
- end
- posx=math.Clamp(posx,0,ScrW())
- posy=math.Clamp(posy,0,ScrH())
- end
- surface.SetTexture(mat)--Move to IMaterial
- surface.SetDrawColor(self.r,self.g,self.b,self.alpha)
- surface.DrawTexturedRectRotated(posx,posy,self.w,self.h,self.roll)
- if(CurTime()>self._lerptime)then
- self:Terminate()
- end
- end
- setmetatable(Particle2D,Particle2D)
- hook.Add("HUDPaint","2DParticleDraw",function()
- for k,v in pairs(particle2dtable) do
- v:Draw()
- end
- end)
- for i=1,260 do
- local a=Particle2D()
- a:SetRollSpeed(math.Rand(-800,800))
- a:SetPos(ScrW()/2,ScrH()/2)
- a:SetVelocity(Vector2D(math.Rand(-300,300),math.Rand(-100,-400)))
- a:SetGravity(Vector2D(0,128))
- a:SetStartSize(18)
- a:SetEndSize(42)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement