Random Enemies Shooting
Posted: Wed Jan 23, 2019 2:29 pm
Hi everyone! I have started learning lua a couple of days ago after finding out about love2d. It is making learning the language fun and I am both doing basic exercises and modifying code from tutorials to learn the gist of it.
I am making a silly shooter beat'em up game like spiderman & venom from the snes but without scrolling.
I can make the player's character shoot no problem, but apparently I am missing something on how to make more than one single enemy shoot.
What happens is that out of 3 enemies, only 1 shoots constantly and only after killing it the second one starts.
What I would like to have is a random enemy shooting a bullet for each cycle.
Here is the code from the enemies module
as you can see I have tried using the random function to get a random index of the possible shooting enemy, but only the same one shoots
This, instead, is the code from the draw section
I have tried by using a global bullet table, to keep track of all the bullets, and also by just iterating through the enemies that have shot.
I hope my problem was clear and that I provided all the necessary premises to get some help.
I am making a silly shooter beat'em up game like spiderman & venom from the snes but without scrolling.
I can make the player's character shoot no problem, but apparently I am missing something on how to make more than one single enemy shoot.
What happens is that out of 3 enemies, only 1 shoots constantly and only after killing it the second one starts.
What I would like to have is a random enemy shooting a bullet for each cycle.
Here is the code from the enemies module
Code: Select all
enemies_controller = {}
enemies_controller.__index = enemies_controller
enemies_controller.enemies = {}
enemySprite = love.graphics.newImage('walkingenemy.png')
function enemies_controller:spawn(x,y)
enemy = {}
enemy.x = x
enemy.y = y
enemy.width = 50
enemy.height = 100
enemy.shots = {}
enemy.cooldown = 40
enemy.fire = function()
if enemy.cooldown <= 0 then
enemy.cooldown = 40
shot = {}
bullet = {}
shot.x = enemy.x + 55
shot.y = enemy.y + 50
shot.width = 10
shot.height = 10
table.insert(enemy.shots, shot)
table.insert(bullets, shot)
end
end
table.insert(self.enemies, enemy)
end
function enemies_controller:randomization(dt)
rando = love.math.random(#enemies_controller.enemies)
for _,l in pairs(enemies_controller.enemies) do
print(_, l.x, l.y)
-- print(rando)
-- for _,v in pairs(enemies_controller.enemies) do
-- rando = math.random(#enemies_controller.enemies)
-- print(enemies_controller.enemies[rando], rando)
-- print(enemies_controller.enemies[rando].x,enemies_controller.enemies[rando].y)
enemies_controller.enemies[3].fire()
print("rando: ",rando, enemies_controller.enemies[rando])
end
end
This, instead, is the code from the draw section
Code: Select all
-- print("now")
for _,v in pairs(bullets) do
love.graphics.setColor(205,205,0) --Red
love.graphics.rectangle("fill", v.x, v.y, v.width, v.height)
love.graphics.setColor(255,255,255) --Red
-- end
end
for i,v in ipairs(enemies_controller.enemies) do
for k,p in pairs(v.shots) do
print("shots", k, p)
love.graphics.setColor(205,205,0) --Red
love.graphics.rectangle("fill", p.x, p.y, p.width, p.height)
love.graphics.setColor(255,255,255) --Red
end
end
I hope my problem was clear and that I provided all the necessary premises to get some help.