Skip to content

Commit

Permalink
feat: Supported tfm.exec.addParticleMethod method
Browse files Browse the repository at this point in the history
- Supported tfm.exec.addParticle(particleType, xPosition, yPosition,
xSpeed, ySpeed, xAcceleration, yAcceleration, targetPlayer) method
- Created a new Particle class which could create particles easily
- Added a new field particles under tfm.get.data. The particles table is
implemented in the following structure

```lua
particles = {
  all = {}, -- holding global particles
  head = nil -- holds the last spawned global particle
  [playerName] = { -- holds particles related to a particular player
   all = {},
   head = nil

  }

- Added test cases for both Particle.lua and for the new mthod under
tfm.exec namespace

related: #20
  • Loading branch information
Seniru committed Feb 18, 2020
1 parent a819f2f commit 7ccb627
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 4 deletions.
19 changes: 19 additions & 0 deletions src/Particle.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
local typeAssert = require "extra.TypeError"

return function(particleType, xPosition, yPosition, xSpeed, ySpeed, xAcceleration, yAcceleration)

typeAssert('Particle', 'number', 1, particleType)
typeAssert('Particle', 'number', 2, xPosition)
typeAssert('Particle', 'number', 3, yPosition)

return {
particleType = particleType,
xPosition = xPosition,
yPosition = yPosition,
xSpeed = xSpeed or 0,
ySpeed = ySpeed or 0,
xAcceleration = xAcceleration or 0,
yAcceleration = yAcceleration or 0
}

end
35 changes: 33 additions & 2 deletions src/tfm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local Object = require 'src.Object'
local PhysicObject = require 'src.PhysicObject'
local Conjuration = require 'src.Conjuration'
local Joint = require 'src.Joint'
local Particle = require 'src.Particle'
local typeAssert = require 'extra.TypeError'

require 'src.events'
Expand Down Expand Up @@ -176,7 +177,11 @@ local tfm = {
chatMessages = {},
physicObjects = {},
explosions = {},
joints = {}
joints = {},
particles = {
all = {},
head = nil
}
}
}
}
Expand Down Expand Up @@ -326,7 +331,33 @@ function tfm.exec.disableWatchCommand(activate)
end

function tfm.exec.displayParticle(particleType, xPosition, yPosition, xSpeed, ySpeed, xAcceleration, yAcceleration, targetPlayer)
error('Not implemented')
local particle = Particle(particleType, xPosition, yPosition, xSpeed, ySpeed, xAcceleration, yAcceleration)
local type = nil

for name, id in next, tfm.enum.particle do
if id == particleType then
type = name
end
end

if not targetPlayer then
tfm.get.data.particles.all[#tfm.get.data.particles.all + 1] = particle
tfm.get.data.particles.head = particle
else
if not tfm.get.data.particles[targetPlayer] then
tfm.get.data.particles[targetPlayer] = {
all = {particle},
head = particle
}
else
tfm.get.data.particles[targetPlayer].all[#tfm.get.data.particles[targetPlayer].all + 1] = particle
tfm.get.data.particles[targetPlayer].head = particle
end
end

print(label('[Game: Particle]') .. '\ttarget: ' .. (targetPlayer or 'All') .. ' | particle: ' .. particleType .. (type and ' (' .. type .. ')' or '') .. ' ...\t' .. func('(tfm.exec.displayParticle)'))

return particle
end

function tfm.exec.explosion(xPosition, yPosition, power, radius, miceOnly)
Expand Down
48 changes: 46 additions & 2 deletions test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ local PhysicObject = require 'src.PhysicObject'
local Player = require 'src.Player'
local Conjuration = require 'src.Conjuration'
local Joint = require 'src.Joint'
local Particle = require 'src.Particle'

function test:before()
--insert players to the game
Expand Down Expand Up @@ -198,6 +199,29 @@ function test:Joint()

end

function test:Particle()

assertErrors(Particle) -- Should throw errors when called without args

local particle = Particle(1, 10, 20, 30, 30, 10, 20)
local particle2 = Particle(2, 30, 50)

assertType(particle, "table")
assertType(particle2, "table")
assertEqual(particle.particleType, 1)
assertEqual(particle2.xPosition, 30)
assertEqual(particle.yPosition, 20)
assertEqual(particle.xSpeed, 30)
assertEqual(particle.ySpeed, 30)
assertEqual(particle.xAcceleration, 10)
assertEqual(particle.yAcceleration, 20)
assertEqual(particle2.xSpeed, 0)
assertEqual(particle2.ySpeed, 0)
assertEqual(particle2.xAcceleration, 0)
assertEqual(particle2.yAcceleration, 0)

end

function test:tfm()

-- addConjuration
Expand Down Expand Up @@ -366,8 +390,28 @@ function test:tfm()
tfm.exec.disableWatchCommand(true)
assertEqual(tfm.get.room.allowedWatchCommand, false)

assertErrors(tfm.exec.displayParticle)

--displayParticle
assertErrors(tfm.exec.displayParticle) -- Should throw errors when called without args
local particle1 = tfm.exec.displayParticle(3, 10, 10, 300, 300, 1000, 1000, nil)
local particle2 = tfm.exec.displayParticle(4, 20, 20, 400, 400, 2000, 2000, 'souris1')
local particle3 = tfm.exec.displayParticle(5, 30, 30)
local particle4 = tfm.exec.displayParticle(30, 30, 30)
local particle5 = tfm.exec.displayParticle(10, 30, 30, nil, nil, nil, nil, 'souris2')
local particle6 = tfm.exec.displayParticle(30, 50, 50, nil, nil, nil, nil, 'souris2')

-- general tests
assertEqual(particle1.particleType, 3)
assertEqual(particle2.xSpeed, 400)
assertEqual(particle3.yAcceleration, 0)
-- testing if particles were displayed and stored correctly
assertTableEquals(tfm.get.data.particles.head, particle4)
assertTableEquals(tfm.get.data.particles.all[1], particle1)
assertTableEquals(tfm.get.data.particles['souris1'].all[1], particle2)
assertTableEquals(tfm.get.data.particles['souris1'].all[1], tfm.get.data.particles['souris1'].head)
assertTableEquals(tfm.get.data.particles['souris2'].all[1], particle5)
assertTableEquals(tfm.get.data.particles['souris2'].all[2], particle6)
assertTableEquals(tfm.get.data.particles['souris2'].all[2], tfm.get.data.particles['souris2'].head)
assertEqual(tfm.get.data.particles['souris2'].head.particleType, 30)
--explosion
assertErrors(tfm.exec.explosion) -- Should throw errors when called without args
local e1 = tfm.exec.explosion(10, 10, 30, 10)
Expand Down

0 comments on commit 7ccb627

Please sign in to comment.