diff --git a/src/Particle.lua b/src/Particle.lua new file mode 100644 index 0000000..9442656 --- /dev/null +++ b/src/Particle.lua @@ -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 \ No newline at end of file diff --git a/src/tfm.lua b/src/tfm.lua index 5bf6145..a6e2083 100644 --- a/src/tfm.lua +++ b/src/tfm.lua @@ -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' @@ -176,7 +177,11 @@ local tfm = { chatMessages = {}, physicObjects = {}, explosions = {}, - joints = {} + joints = {}, + particles = { + all = {}, + head = nil + } } } } @@ -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) diff --git a/test.lua b/test.lua index fd294fc..ebf6407 100644 --- a/test.lua +++ b/test.lua @@ -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 @@ -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 @@ -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)