Giter Club home page Giter Club logo

evlightning's Introduction

EvLightning

EvLightning is a Roblox Lua library that aims to generate realistic-looking lightning bolts. This could be used to easily add lightning strike effects to your game.

Lightning demo

Usage

local LightningBolt = require(path.to.EvLightning)

local myBolt = LightningBolt.new(Vector3.new(0, 400, 0), Vector3.new(0, 0, 0), {
  color = BrickColor.new("Really red");
  -- More options are available, see below...
})
myBolt:Draw(workspace)

A lightning bolt is split into lines, and where the lines meet is considered to be a "bend". At each bend, there is a chance to create a fork, which will create another bolt with its own bends, which in turn have chances to create their own forks, etc. You can control how many bends are in the bolt and the chance of creating a fork at each bend.

API

LightningBolt LightningBolt.new(Vector3 from, Vector3 to[, dictionary options])

Returns an instance of LightningBolt. Automatically generates in the constructor, but further methods must be called on the returned instance to render the bolt.

Options

There are a number of options that you can pass in a third argument to LightningBolt.new in the form of a table dictionary. All options are optional, and have default values.

Option name Description Default value Type
seed A numerical seed which could be used to generate the same lightning bolt across a network. Random Number
bends The number of bends the main bolt should have. 6 Integer
fork_bends The number of bends to put into forks off of the main bolt. 2 Integer
fork_chance The chance to create a new fork off of each bend. (0-100) 50 Number
transparency The transparency of the main bolt. Transparency is reduced at every fork. 0.4 Number
thickness The thickness of the main bolt. Thickness is reduced at every fork. 1 Number
max_depth The maximum depth that forks can reach off the main bolt, which is depth 0. 3 Integer
color The color of the bolt White BrickColor or Color3
material The material of the bolt Enum.Material.Neon Enum.Material
decay The number of seconds for the bolt to exist after being drawn. Infinite Number

Note that the fork_chance option is not an equal chance for every bend in the bolt. The chance is distributed in a gradient down the length of the bolt, meaning that if the fork_chance is 50, then at the very top it will be 0, at the middle it will be 25, and at the very bottom it will be 50. This is done so that less forks are generated towards the top, which makes the bolt look more realistic. If you need to create upwards-forking lightning, then simply reverse the to and from arguments.

void bolt:Draw([Instance parent = Workspace])

Draws the lightning bolt in the world with the given options. An optional argument, parent, can be passed, which will be the parent of the generated lightning bolt parts. If this argument is omitted, there will be a new model created in Workspace containing the bolt parts.

array bolt:GetLines()

Returns a table of tables, containing the line information. The member tables have the keys origin, goal, and depth. This is only necessary if you want to draw the lightning bolt manually (i.e. not calling Draw).

{
	{
		origin 	= Vector3 origin,
		goal 	= Vector3 endpoint,
		depth	= Number depth
	},
	{
		origin 	= Vector3 origin,
		goal 	= Vector3 endpoint,
		depth	= Number depth
	},
	-- ...
}

void bolt:Destroy()

If the lightning bolt has been drawn, this method will destroy the model that was created.

dictionary bolt:GetOptions()

Returns a table of the options that were passed into the constructor. Default values are not applied here.

bool bolt:IsDestroyed()

Returns true if the lightning bolt has either been destroyed or has decayed.

bool bolt:IsDrawn()

Returns true of the Draw method has been called on this particular instance.

evlightning's People

Contributors

evaera avatar immortalslayer avatar thundermaker300 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

evlightning's Issues

Modification Requests

  • Please do not use game.Workspace, as Workspace can be renamed. Instead use game:GetService("Workspace") or workspace
  • Instead of instantiating a part with inline default values, please create a global partTemplate and Clone() it and change its properties as appropriate. Also, please do not mix string enums with enums (style issue). Also, please set the parent last, as it is much faster to do so. This should also be encouraged by the use of Clone():
local partTemplate = Instance.new("Part")
partTemplate.Anchored = true
partTemplate.CanCollide = false
partTemplate.BrickColor = BrickColor.new("White")
partTemplate.Material = Enum.Material.Neon
partTemplate.TopSurface = Enum.SurfaceType.Smooth
partTemplate.FormFactor = Enum.FormFactor.Custom
partTemplate.BottomSurface = Enum.SurfaceType.Smooth

-- within function LightningBolt:Draw(parent)
local part = partTemplate:Clone()

if self.options.color then
	part.BrickColor = self.options.color
end

if self.options.material then
	part.Material = self.options.material
end

part.Size = Vector3.new(1-line.depth*2*0.1, 1-line.depth*2*0.1, (line.origin-line.goal).magnitude+0.5)
part.CFrame = CFrame.new((line.goal+line.origin)/2, line.goal)
part.Transparency = line.transparency
part.Parent = model
  • Please do not iterate through arrays using pairs. This will make the ghost of simple for loops come for you.
for i = 1, #t do
	print(t[i])
end
  • When constructing a new array, especially when it's a copy, repeatedly doing #t for every write operation is going to make my head spin. For a non-copy, a count variable is preferable. A copy can easily be written as so:
local runLines = {}
for i = 1, #self.lines do
	runLines[i] = self.lines[i]
end
  • If you want a method function to be strictly internal, you might consider to do the following:
function LightningBolt:generateBolt() -- What you have
local function generateBolt(self) -- A way to truly hide API you don't want exposed
  • Please be consistent on your spacing around binary operators. "3 + 5" or "3+5", but pick one and stick with it.
  • This information is already contained within the comments of the module. Why would it ever be necessary to have this?
LightningBolt._version = 1
  • It is STRONGLY preferable to not recursively generate objects within objects (#L134). If possible, please avoid unnecessary abstraction for internal syntactic sugar. Only do it if you absolutely have to.
  • Color is preferable to BrickColor (API change request)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.