Skip to content

Commit

Permalink
Add particle effects
Browse files Browse the repository at this point in the history
  • Loading branch information
synthic committed Dec 5, 2019
1 parent a460f29 commit add9224
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 27 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Moonman
# ![Logo](https://github.com/synthic/moonman/raw/master/assets/logo.png)

A game about an astronaut made with [LÖVE](https://love2d.org).<br><br>
A game about an astronaut made with [LÖVE](https://love2d.org).

## Credits

Expand Down
83 changes: 58 additions & 25 deletions main.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
-- Version number
version = 'V1.0'

-- Timer variable
dtotal = 0
function love.load()
-- Version number
version = 'V1.0'

-- Enemies table
enemies = {}
-- Timer variable
dtotal = 0

function love.load()
-- Set screen attributes
screen = {
width = 320,
Expand Down Expand Up @@ -37,9 +34,6 @@ function love.load()
-- Set game state
gamestate = 'title'

-- Initialize score
score = 0

-- Set window attributes
love.window.setTitle('Moonman')
love.window.setMode(screen.width * 2, screen.height * 2)
Expand Down Expand Up @@ -141,6 +135,11 @@ function love.update(dt)
bullet.x = 1000
score = score + 100
playSound(enemyDestroySfx)

local explosion = getExplosion(getBlast(50))
explosion:setPosition(enemy.x + enemy.width/2, enemy.y + enemy.height/2)
explosion:emit(5)
table.insert(explosions, explosion)
end

-- Collision checking (player)
Expand All @@ -150,6 +149,15 @@ function love.update(dt)
else table.remove(enemies, i) end
end

-- Update explosions
for i=#explosions, 1, -1 do
local explosion = explosions[i]
explosion:update(dt)
if explosion:getCount() == 0 then
table.remove(explosions, i)
end
end

-- Timer (1s)
dtotal = dtotal + dt
if dtotal >= 1 then
Expand Down Expand Up @@ -216,9 +224,15 @@ function love.draw(dt)
love.graphics.draw(animEnemy.spriteSheet, animEnemy.quads[spriteNum], enemy.x, enemy.y)
end

-- Draw projectiles
-- Draw projectile
love.graphics.draw(bullet.img, bullet.x, bullet.y)

-- Draw explosions
for i=#explosions, 1, -1 do
local explosion = explosions[i]
love.graphics.draw(explosion, 0, 0)
end

-- Set drawing target to window
love.graphics.setCanvas()

Expand All @@ -238,17 +252,6 @@ function love.draw(dt)
end
end

function newEnemy(x,y)
local enemy = {}
enemy.x = x
enemy.y = y
enemy.width = 24
enemy.height = 24
enemy.speed = love.math.random(100, 300)
enemy.removed = false
table.insert(enemies, enemy)
end

function startGame()
-- Reset positions
player.x = 40
Expand All @@ -258,8 +261,9 @@ function startGame()
-- Reset score
score = 0

-- Reset enemies
-- Reset entities
enemies = {}
explosions = {}

-- Start music
musicTrack:play()
Expand All @@ -284,19 +288,48 @@ end
function shootGun()
-- Shoot if no projectile on screen
if bullet.x > screen.width then
bullet.x = player.x + player.width
bullet.x = player.x + player.width - 2
bullet.y = player.y + ((player.height / 2) - (bullet.height / 2))
playSound(shootSfx)
end
end

function newEnemy(x,y)
local enemy = {}
enemy.x = x
enemy.y = y
enemy.width = 24
enemy.height = 24
enemy.speed = love.math.random(100, 300)
enemy.removed = false
table.insert(enemies, enemy)
end

function playSound(sound)
sound:stop()
pitchMod = 0.8 + love.math.random(0, 10) / 25
sound:setPitch(pitchMod)
sound:play()
end

function getBlast(size)
local blast = love.graphics.newCanvas(size, size)
love.graphics.setCanvas(blast)
love.graphics.setColor(255, 255, 255, 255)
love.graphics.circle('fill', size/2, size/2, size/2)
love.graphics.setCanvas()
return blast
end

function getExplosion(image)
pSystem = love.graphics.newParticleSystem(image, 30)
pSystem:setParticleLifetime(0.5, 0.5)
pSystem:setLinearAcceleration(-100, -100, 100, 100)
pSystem:setColors(255, 255, 0, 255, 255, 153, 51, 255, 64, 64, 64, 0)
pSystem:setSizes(0.5, 0.5)
return pSystem
end

--[[
Create animation from sprite sheet
https://love2d.org/wiki/Tutorial:Animation
Expand Down

0 comments on commit add9224

Please sign in to comment.