Updated! Need help with collision and constant enemies still.

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
nickoricko
Prole
Posts: 2
Joined: Thu Jul 13, 2017 8:24 am

Updated! Need help with collision and constant enemies still.

Post by nickoricko »

URGENT! ASSIGNMENT DUE IN THREE DAYS

Hello everyone, please don't roast me I'm quite aware I am not the best coder out there but I'm trying. I want to make my enemies spawn constantly, roughly about every 5 seconds. I'm not too sure how to make the lua timer to work with my code so any solution will do. I have a form of collision but it doesn't work anywhere near to where it's supposed to. When my player shoots bullets, it can kill the enemy from basically wherever the bullet goes. I was wondering if there's anyway to fix the collision or if I need to write a whole new one. If anyone could possibly help me out or write the collision for me it would be greatly appreciated.

Enemy code

Code: Select all

enemies_controller = {}
enemies_controller.enemies = {}
enemies_controllerSpawn = {}
enemies_controller.image = love.graphics.newImage('zookeeper.png')

function enemies_load()
  enemies_controller:Enemy(love.math.random(0, love.graphics.getWidth()), 660)
  enemies_controller:Enemy(love.math.random(0, love.graphics.getWidth()), -50)
  enemies_controller:Enemy(-38, love.math.random(0, love.graphics.getHeight()))
  enemies_controller:Enemy(1200, love.math.random(0, love.graphics.getHeight()))
  timer = 0
end


function enemies_controller:fire()
  if self.cooldown <= 0 then
  self.cooldown = 30
  bullet = {}
  bullet.x = self.x 
  bullet.y = self.y + 35
  table.insert(self.bullets, bullet)
end
end



function enemies_controller:Enemy(x, y)
  enemy = {}
  enemy.x = x
  enemy.y = y
  enemy.width = 10
  enemy.height = 10
  enemy.bullets = {}
  enemy.cooldown = 30
  enemy.speed = 20
  table.insert(self.enemies, enemy)
end

function enemies_controller:Spawn()
   for i=1,5 do
      enemy[#enemy + 1] = {
         x = enemy.x,
         y = enemy.y,
         w = enemy.width,
         h = enemy.height
      }
   end




function enemy_update(dt)
 timer = timer + dt
 if timer >= 5 then 
     table.insert(enemies_controller)
     timer = timer - 5
   end
 end
 end

function enemy_draw()
   for _,e in pairs(enemies_controller.enemies) do
    love.graphics.draw(enemies_controller.image, e.x, e.y, 10, 10)
  end
end
The game code

Code: Select all

function checkCollisions(enemies, bullets)
  for i, e in ipairs(enemies) do
    for _, o in ipairs(bullets) do
      if o.y <= e.y + e.height and o.x >= e.x and o.x <= e.x + e.width then
        table.remove(enemies, 1)
        table.remove(bullets,1)
        winner = true
      end
    end
  end
end


function game_load()
  winner = false
  background_image = love.graphics.newImage('background.png')
  bullet_image = love.graphics.newImage('banana.png')
  player = {}
  player.x = 580
  player.y = 200
  player.heat = 0
	player.heatp = 0.1
  bullets = {}
  player.ground = love.graphics.getHeight() - 80
  player.y_velocity = 0
  player.gravity = -1000  

  player.speed = 7
  player.image = love.graphics.newImage('rickstandard.png')
  enemies_controller:Enemy(love.math.random(0, love.graphics.getWidth()), 660)
  enemies_controller:Enemy(love.math.random(0, love.graphics.getWidth()), -50)
  enemies_controller:Enemy(-38, love.math.random(0, love.graphics.getHeight()))
  enemies_controller:Enemy(1200, love.math.random(0, love.graphics.getHeight()))
  timer = 0
end







function game_update(dt)
  if love.keyboard.isDown("d") then
    player.x = player.x + player.speed 
     player.image = love.graphics.newImage('rickright.png'),
     player.x == -1
  end
  if love.keyboard.isDown("a") then
    player.x = player.x - player.speed
     player.image = love.graphics.newImage('rickleft.png'),
     player.x == 1
  end
  if love.keyboard.isDown("s") then
    player.y = player.y + player.speed
     player.image = love.graphics.newImage('rickdown.png'),
     player.y == -1
    end
  if love.keyboard.isDown('w') then
    player.y = player.y - player.speed
     player.image = love.graphics.newImage('rickup.png'),
     player.y == 1
  end
  
  
  player.heat = math.max(0, player.heat - dt)
	if love.keyboard.isDown('space') and player.heat <= 0 then
		local direction = math.atan2(love.mouse.getY() - player.y, love.mouse.getX() - player.x)
		table.insert(bullets, {
			x = player.x + 60,
			y = player.y + 60,
			dir = direction,
			speed = 600,
		})
		player.heat = player.heatp
	end
  
  
 
  
	local i, o
	for i, o in ipairs(bullets) do
		o.x = o.x + math.cos(o.dir) * o.speed * dt
		o.y = o.y + math.sin(o.dir) * o.speed * dt
		if o.x < 0 or o.x > love.graphics.getWidth() then
      table.remove(bullets, i)
   elseif o.direction == -1 and o.x < o.xMax then
     table.remove(bullets, i)
	elseif o.direction == 1 and o.x > o.xMax then
		table.remove(bullets, i)
  else
    o.previousX = o.x
  
	end
  end



  

  if player.y_velocity ~= 0 then
    player.y = player.y + player.y_velocity * dt
    player.y_velocity = player.y_velocity - player.gravity * dt
  end

  if player.y > player.ground then
    player.y_velocity = 0
    player.y = player.ground
  end


timer = timer + dt
 if timer >= 5 then 
     enemies_controllerSpawn,
     timer = timer - 5
   end
   

checkCollisions(enemies_controller.enemies, bullets)
Last edited by nickoricko on Sat Jul 15, 2017 9:14 am, edited 1 time in total.
nickoricko
Prole
Posts: 2
Joined: Thu Jul 13, 2017 8:24 am

Re: Urgently need help with making enemies spawn constantly!

Post by nickoricko »

UPDATE

I have changed part of my code so now one enemy moves to the centre of the screen and stops but it doesn't work for all enemies. Can someone please help me as to how to make all the enemies move toward the middle, thanks.

Code: Select all

enemies_controller = {}
enemies_controller.enemies = {}
enemies_controller.image = love.graphics.newImage('zookeeper.png')

function enemies_load()
  enemies_controller:Enemy1(0, 0)
  enemies_controller:Enemy2(0, 0)
  enemies_controller:Enemy3(0, 0)
  enemies_controller:Enemy4(0, 0)
end

function enemies_controller:fire()
  if self.cooldown <= 0 then
  self.cooldown = 30
  bullet = {}
  bullet.x = self.x 
  bullet.y = self.y + 35
  table.insert(self.bullets, bullet)
end
end

function enemies_controller:Enemy1(x, y)
  enemy = {}
  love.math.newRandomGenerator(os.time())
  enemy.x = love.math.random(0, love.graphics.getWidth())
  enemy.y = 660
  enemy.width = 10
  enemy.height = 10
  enemy.bullets = {}
  enemy.cooldown = 30
  enemy.speed = 20
  table.insert(self.enemies, enemy)
end

function enemies_controller:Enemy2(x, y)
  enemy = {}
  love.math.random(os.time())
  enemy.x = love.math.random(0, love.graphics.getWidth())
  enemy.y = -50
  enemy.width = 10
  enemy.height = 10
  enemy.bullets = {}
  enemy.cooldown = 30
  enemy.speed = 20
  table.insert(self.enemies, enemy)
end

function enemies_controller:Enemy3(x, y)
  enemy = {}
  love.math.random(os.time())
  enemy.x = -38
  enemy.y = love.math.random(0, love.graphics.getHeight())
  enemy.width = 10
  enemy.height = 10
  enemy.bullets = {}
  enemy.cooldown = 30
  enemy.speed = 20
  table.insert(self.enemies, enemy)
end

function enemies_controller:Enemy4(x, y)
  enemy = {}
  love.math.random(os.time())
  enemy.x = 1200
  enemy.y = love.math.random(0, love.graphics.getHeight())
  enemy.width = 10
  enemy.height = 10
  enemy.bullets = {}
  enemy.cooldown = 30
  enemy.speed = 20
  table.insert(self.enemies, enemy)
end



[b]function enemy_update(dt)
if enemy.x < 550 then
  enemy.x = enemy.x + (enemy.speed * 2.5 * dt)
end
if enemy.x > 550 then
  enemy.x = enemy.x - (enemy.speed * 2.5 * dt)
end
if enemy.y < 270 then
  enemy.y = enemy.y + (enemy.speed * 2.5 * dt)
  end
if enemy.y > 270 then
  enemy.y = enemy.y - (enemy.speed * 2.5 * dt)
end
end[/b]
  


function enemy_draw()
   for _,e in pairs(enemies_controller.enemies) do
    love.graphics.draw(enemies_controller.image, e.x, e.y)
  end
end
User avatar
erasio
Party member
Posts: 118
Joined: Wed Mar 15, 2017 8:52 am
Location: Germany

Re: Urgently need help with making enemies spawn constantly!

Post by erasio »

It's time for game developers best friend!

Linear algebra! (aka vector math)

Alright. First. What vectors do we have?

Enemy position (enemy x and y), and the middle of the screen (window x and y). You can get the window size via love.window.getMode() and the center of your screen by dividing the result by 2.

Now we need the direction. To get a vector from the enemy to the destination we simply do destination - current location.

Aka directionX = centerX - enemyX, directionY = centerY - enemyY.

Now for the path we want to move. To easily modify this by the speed we want to have a vector pointing in the same direction but with a length of one unit.

For this we need the length of this vector.

This can be done like this:

Length = Math.sqrt((directionX * directionX) + (directionY * directionY))

Almost there. Now we need to divide directionX and directionY by length, multiply the result with enemy speed and dt, add the result of that to our current location (enemyX and enemyY).

And we're done! That's our new position!
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 151 guests