Jump to content

Programming a molotov


Einlander
 Share

Recommended Posts

I am trying to implement a molotov cocktail ala Left 4 Dead in lua for Leadwerks. I think I have most of it figured out, but i'm having some issues with the collisions. I would like to be able to throw one at my feet and not have the particles interact with the player.

 

Pardon my spaghetti code:

 

function Script:Start()

self.enabled = true

self.exploded=false

self.particlelist={}

self.particalspawntime=.03

self.collisonpoint = Vec3()

self.particlecount=15

self.particlecounter=0

self.particlecounter2=0

self.timer=0

self.settle = true

self.settletimer = 0

self.settletime = .05

self.burn=false

self.burntime=10

self.burntimer=0

self.burnout=false

self.burnouttime=.1

self.burnouttimer=0

--self.burnaoutparticlecounter=self.particlecount

end

 

 

 

function Script:UpdateWorld()

 

end

 

 

 

function Script:UpdatePhysics()

if (self.exploded==true) and (self.enabled==true) and (self.particlecounter < self.particlecount) then

local timer2 = Time:Millisecs()

if ((timer2 -self.timer)/1000)>self.particalspawntime then

--Debug:Error("Times Up")

-- create box

local model = Model:Box()

-- add to particle list

table.insert(self.particlelist,model)

--model:SetPosition(self.entity:GetPosition())

model:SetPosition(self.collisonpoint.x,self.collisonpoint.y,self.collisonpoint.z)

model:SetColor(0.0,0.0,1.0)

model:SetScale(.5,.1,.5)

model:SetFriction(0,.3)

-- create collision

local shape = Shape:ConvexHull(model:GetSurface(0))

--local shape = Shape:PolyMesh(model:GetSurface(0))

model:SetShape(shape)

model:SetCollisionType(Collision.Prop)

 

 

--set mass

 

model:SetMass(5)

model:AddForce(math.random(300,500),0,math.random(300,500),false)

 

--model:PhysicsSetPosition(self.collisonpoint.x,self.collisonpoint.y+1,self.collisonpoint.z,.2)

self.particlecounter = self.particlecounter + 1

if self.particlecounter == self.particlecount then

self.settle = true

self.settletimer = Time:Millisecs()

self.particlecounter2=#self.particlelist

--self.enabled = false

end

 

self.timer = Time:Millisecs()

end

 

end

if (self.enabled == true) and (self.burnout == true) then

--Debug:Error("burnout begin")

end

if (self.enabled == true) and (self.exploded==true) and (self.settle == true) and (self.particlecounter2 > 0) then

local timer2 = Time:Millisecs()

if ((timer2 -self.settletimer)/1000)>self.settletime then

--Debug:Error(((timer2-self.settletimer)/1000))

local mdl= self.particlelist[self.particlecounter2]

--mdl:SetCollisionType(Collision.Prop)

--mdl:SetRotation(0,mdl:GetRotation().y,0)

--mdl:AddForce(math.random(1,1),math.random(1,1),math.random(1,1),false)

mdl:PhysicsSetPosition(mdl:GetPosition().x,mdl:GetPosition().y+.5,mdl:GetPosition().z,.1)

mdl:SetCollisionType(Collision.Debris)

 

 

--mdl:Release()

--table.remove(self.particlelist,self.particlecounter2)

self.settletimer = Time:Millisecs()

self.particlecounter2 = self.particlecounter2-1

end

end

 

if (self.enabled == true) and (self.exploded==true) and (self.burn == true) and (#self.particlelist > 0) then

local timer2 = Time:Millisecs()

if ((timer2 -self.burntimer)/1000)>self.burntime then

--local mdl= self.particlelist[#self.particlelist]

--table.remove(self.particlelist,#self.particlelist)

--mdl:Release()

--self.burntimer = Time:Millisecs()

self.burnout=true

self.burn=false

self.burnouttimer = Time:Millisecs()

end

end

 

if (self.enabled == true) and (self.burnout == true) and (self.exploded==true) and (self.burn == false) and (#self.particlelist > 0) then

local timer2 = Time:Millisecs()

if ((timer2 -self.burnouttimer)/1000)>self.burnouttime then

local mdl= self.particlelist[#self.particlelist]

table.remove(self.particlelist,#self.particlelist)

mdl:Release()

self.burnouttimer = Time:Millisecs()

end

end

 

if (self.enabled == true) and (self.burnout == true) and (self.exploded==true) and (self.burn == false) and (#self.particlelist <= 0) then

self.enabled=false

collectgarbage()

self.entity:Release()

end

 

if (self.enabled == true) and (self.exploded==true) and (self.settle == true) and (self.particlecounter2 <1) and (self.burn==false)then

self.settle =false

self.burn=true

self.burntimer = Time:Millisecs()

end

end

 

 

 

function Script:Collision(entity, position, normal, speed)

 

if (self.exploded==false) and (self.enabled == true) and (speed > 8) then

--Debug:Error("break "..tostring(speed))

self.collisonpoint = self.entity:GetPosition()

self.entity:SetCollisionType(Collision.Debris)

self:Detonate()

end

end

 

function Script:Detonate() --in

if self.exploded==false and (self.enabled == true) then

self.exploded=true

self.timer = Time:Millisecs()

self.entity:Hide()

end

end

 

 

 

 

--This function will be called when the entity is deleted.

function Script:Delete()

collectgarbage()

end

 

 

 

--This function will be called when the last instance of this script is deleted.

function Script:Cleanup()

collectgarbage()

end

 

This was designed to be used with the fpsplayer.

Instructions:

  • Attach the script to an object with a collision type of prop.
  • Set the mass to something between 1 and 5 so you can pick it up.
  • The object needs to hit an object at a speed greater than 8

The object on impact will on impact dissapear and blocks will explode out of it.

It will "burn" for 10 seconds, then it will clean up, removing the particles oldest to newest.

When there are no more molotovs in the scene it will clean up after itself.

It also attempts to flow down slopes for a little distance to simulate liquids.

 

THE REQUEST:

Got to bold it for the TL;DR crowd.

 

Is there a way to make it interact with the world but not interact with the character controller?

 

ToDo: Replace blocks with Emitters to similate fire, create trigger areas to hurt player when inside.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...