Random Enemies Shooting

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
semordnilap
Prole
Posts: 3
Joined: Wed Jan 23, 2019 2:12 pm

Random Enemies Shooting

Post by semordnilap »

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

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
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 :ehem:

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 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.
monkyyy
Citizen
Posts: 52
Joined: Fri Mar 16, 2012 5:29 pm

Re: Random Enemies Shooting

Post by monkyyy »

I'm confused by many of your decisions

>enemy.shots = {}

Why is the "shots" managed per enemy? That should probably be a single global table

If bullets disappear based on an enemy dying, its this that's going to be causing it

>enemies_controller.enemies[3].fire()

If you want this random, it shouldn't be set to a constant

what is that for loop suppose to do?

-----

Remove the loop in "randomization" and try "enemies_controller.enemies[rando].fire()"

Your double depth key lookup and your naming conventions confuse me, but thats my best guess


-----

>love.graphics.setColor(255,255,255) --Red

Are you sure about that?

This is clearly super white, or 255 more then regular white

try (1,0,0)
semordnilap
Prole
Posts: 3
Joined: Wed Jan 23, 2019 2:12 pm

Re: Random Enemies Shooting

Post by semordnilap »

Hi! Thank you for taking the time to reply.
monkyyy wrote: Thu Jan 24, 2019 4:31 am I'm confused by many of your decisions

>enemy.shots = {}

Why is the "shots" managed per enemy? That should probably be a single global table

If bullets disappear based on an enemy dying, its this that's going to be causing it

>enemies_controller.enemies[3].fire()

If you want this random, it shouldn't be set to a constant

what is that for loop suppose to do?
I had both a table just for enemies' bullets and a global one for testing. Now I have only the global one.
Sorry about the fixed index, I was testing and trying to figure out why even by using a fixed index it would not work.
monkyyy wrote: Thu Jan 24, 2019 4:31 am
Remove the loop in "randomization" and try "enemies_controller.enemies[rando].fire()"

Your double depth key lookup and your naming conventions confuse me, but thats my best guess
Removed, thanks
Sorry for the naming conventions :/
monkyyy wrote: Thu Jan 24, 2019 4:31 am
>love.graphics.setColor(255,255,255) --Red

Are you sure about that?

This is clearly super white, or 255 more then regular white

try (1,0,0)
I was pasting the same line changing the arguments, but completely forgot to change the comments. Thank you for having me notice.

I was able to solve the situation in the end. The problem was the fact that the fire() function was under the enemies_controller:spawn(). I've made it an independent function now and all the enemies fire :awesome:

Here is how I changed the code, I included a random firing and a firing triggered by the player passing in front of the enemy.

Code: Select all

enemies_controller = {}
enemies_controller.__index = enemies_controller
enemies_controller.enemies = {}

enemySprite = love.graphics.newImage('walkingenemy.png')

function enemies_controller:spawn()
   enemy = {}
   enemy.x = math.random(1100, 1150)
   enemy.y = math.random(40, 550)
   enemy.width = 120
   enemy.height = 135
   enemy.cooldown = 60
--   enemy.kinds = {"Hunter", "Mage", "Knight"}
--   enemy.Sprites = {["Mage"] = love.graphics.newImage('walkingenemy.png')}
   table.insert(enemies_controller.enemies, enemy)
end

function enemy_fire(x,y, enem)
   if enem.cooldown <= 0 then
      enem.cooldown = 60
      shot = {}
      shot.x = x + 55
      shot.y = y + 50
      shot.width = 10
      shot.height = 10
      table.insert(bullets, shot)
   end
end

function enemies_controller:randomization(dt)
   if #enemies_controller.enemies > 0 then
      rando = love.math.random(#enemies_controller.enemies)
      if love.math.random(0,20) == 1 then
	 enemy_fire(enemies_controller.enemies[rando].x,enemies_controller.enemies[rando].y,enemies_controller.enemies[rando])
      end
   end
end

function enemies_controller:shoot(dt, _player)
   for _,v in pairs(enemies_controller.enemies) do
      if math.random(0,15) == 1 then
	 if v.y + (v.height / 2) >= _player.y and v.y + (v.height / 2) <= _player.y + _player.height then
	    enemy_fire(v.x,v.y,v)
	 end
      end
   end
end

Hope this is better.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 81 guests