Page 1 of 1

Codding player death

Posted: Mon Dec 31, 2018 7:21 pm
by Pardaleco
Hello everyone! I'm having trouble finding a way to create an effective death for the player. When I collide with an enemy (Collision_with_enemy == true) I want everything to stop it's movement and display Game Over in the middle of the window.

The printing of game over I got working, the collisions are also getting correctly registered.

This next code is my love.update

Code: Select all

function love.update(dt)
  if Collision_with_enemy == false then
    if timer <= 0 then
      EnemyPreset_7()
    end
    
    UpdateLazy(dt)
    UpdatePressure(dt)
    UpdatePlayer(dt)
    enemytimer(dt)
    EnemyCollision()
    
    print(Collided_with_enemy)
 end
end
My intent is to run the game normally until there's a collision with an enemy where everything jus stops. This just makes so that nothing happens everything is just frozen.
Am I unable to create this types of conditions in love.update??
Why does everything freeze even though the variable Collision_with_enemy is indeed false?

Re: Codding player death

Posted: Mon Dec 31, 2018 8:36 pm
by elendiel7
I personally don't think this is enough code to diagnose your issue.

Re: Codding player death

Posted: Mon Dec 31, 2018 10:11 pm
by zorg
I concur, show more code. :3

Re: Codding player death

Posted: Tue Jan 01, 2019 5:38 pm
by TheHUG
is Collision_with_enemy perhaps nil at the start of the game? while nil may be falsey, I don't think nil == false
you could try
if not Collision_with_enemy then
instead of
if Collision_with_enemy == false then

Re: Codding player death

Posted: Tue Jan 01, 2019 6:06 pm
by elendiel7
That wouldn't be it. He stated it was verified false -> "Why does everything freeze even though the variable Collision_with_enemy is indeed false?" The print statement below would print nil if it were nil.

Re: Codding player death

Posted: Wed Jan 02, 2019 6:57 pm
by Pardaleco
Thanks everyone for the replies! I will post more code like asked


In this next piece of code everyting player related is coded.

Code: Select all

-- everything player related

player = {
position = vector2.new(100, 800-(304+65)),
velocity = vector2.new(0, 0),
width = 30,
height = 65,
maxspeed= vector2.new(400, 800),
frictioncoefficient = 400,
--maxspeedair = vector2.new(50,800),
mass = 1,
onGround = true
}

function UpdatePlayer(dt)
  
 -- print(player.onGround)
  local acceleration = vector2.new(0, 0)
  local gravity = vector2.new(0, 1000)

  acceleration = vector2.applyForce(gravity, player.mass, acceleration) -- applying gravity to the player


  local friction = vector2.mult(player.velocity, -1)
  friction = vector2.normalize(friction)
  friction = vector2.mult(friction, player.frictioncoefficient)
  acceleration = vector2.applyForce(friction, player.mass, acceleration) -- applying friction to the player
  local movedirection = vector2.new(0, -1)
  
--Movement imput start

  if love.keyboard.isDown("right") then

    local move = vector2.new(1600, 0)
    acceleration = vector2.applyForce(move, player.mass, acceleration)
    movedirection.x = 1
    
  end

  if love.keyboard.isDown("left") then
    local move = vector2.new(-1600, 0)
    acceleration = vector2.applyForce(move, player.mass, acceleration)
    movedirection.x= -1
  end
  
  if (player.onGround) then 
    if love.keyboard.isDown("up") then
      
      local jump = vector2.new(0, -40000)
      acceleration = vector2.applyForce(jump, player.mass, acceleration)
      movedirection.y = 1
      player.onGround = false
     
    end
  end


-- MOvement imput end



  local futurevelocity = vector2.add(player.velocity,vector2.mult(acceleration, dt))
  futurevelocity = vector2.limit(futurevelocity, player.maxspeed.x)
  local futureposition = vector2.add(player.position,vector2.mult(futurevelocity, dt))
  acceleration = CheckCollision(world, futureposition, movedirection, acceleration)



  player.velocity = vector2.add(player.velocity, vector2.mult(acceleration, dt))
  
  if player.onGround == true then -- Air movement restrictions
    player.velocity = vector2.limit(player.velocity, player.maxspeed.x)
  else
    player.velocity = vector2.limit(player.velocity, vector2.magnitude(player.maxspeed))
    if player.velocity.x > 400 then
      player.velocity.x = 400
    elseif player.velocity.x < -400 then
      player.velocity.x = -400
    end
  end
  
  player.position = vector2.add(player.position, vector2.mult(player.velocity, dt)) -- Player movement


-- Player collision with edges of the window start

  if (player.position.x > love.graphics.getWidth() - player.width) then 
    
    player.position.x = love.graphics.getWidth() - player.width
    player.velocity.x = (player.velocity.x * -1) / 2

  elseif (player.position.x < 0) then
    player.position.x = 0
    player.velocity.x = (player.velocity.x * -1) / 2

  end

  if (player.position.y > love.graphics.getHeight() - player.height) then
    player.position.y = love.graphics.getHeight() - player.height
    player.velocity.y = 0
    player.onGround = true
  end
  
  -- Player collision with edges of the window end
   
end

function DrawPlayer()
  love.graphics.rectangle("fill", player.position.x, player.position.y, player.width, player.height)
end



-- Player collisions start

function CheckCollision(world, futureposition, movedirection, acceleration)
  for i = 1, table.getn(world), 1 do
    local collisiondir = GetBoxCollisionDirection(futureposition.x, futureposition.y, player.width, player.height, world[i].position.x, world[i].position.y, world[i].size.x, world[i].size.y)
    --print(collisiondir.x .. " " .. collisiondir.y)
    if (collisiondir.x ~= 0 or collisiondir.y ~= 0) then
      if collisiondir.y == movedirection.y then --down collision
        player.velocity.y = 0
        acceleration.y = 0
        player.onGround=true
      elseif collisiondir.y == 1 then --up collision
        player.velocity.y = 0
        acceleration.y = 0
      elseif movedirection.x ~= collisiondir.x then --side collision
        player.velocity.x = 0
        acceleration.x = 0
      end
    end
  end
  return acceleration
end 

function Death()
  if Collided_with_enemy then
    love.graphics.print("GAME OVER", 700, 400)
    
  end
end


This next code is the update (for now) for the 2 types of enemies my games has

Code: Select all

function UpdateLazy(dt)
  

  for i=1, table.getn(Lazynumber), 1 do
    if Lazynumber[i] ~= nil then
    
      Lazynumber[i].position = vector2.sub(Lazynumber[i].position, 
      vector2.mult(Lazyproperties.velocity, dt)) -- movement
        if Lazynumber[i].position.x + Lazyproperties.Width < 0 then
          table.remove(Lazynumber, 1) -- remove from table if out of screen
        end
    end
  end

end



function UpdatePressure(dt)
  
  Pressure_death = false  
    
    for i=1, table.getn(Pressurenumber), 1 do
      if Pressurenumber[i] ~= nil then
        Pressurenumber[i].position = vector2.sub(Pressurenumber[i].position, vector2.mult(Pressureproperties.velocity, dt))
        if (Pressurenumber[i].position.x + Pressureproperties.Width < 0) or (Pressure_death == true) then
          table.remove(Pressurenumber, 1) -- remove from table if out of screen or dead (NOT WORKING)
        end
      end
    end
    
    for i=1, table.getn(bullets), 1 do
      for j=1, table.getn(Pressurenumber), 1 do
        if bullets[i] ~= nil then
        
          if CheckBoxCollision(bullets[i].x, bullets[i].y, bulletproperties.size.x , bulletproperties.size.y , Pressurenumber[j].position.x, Pressurenumber[j].position.y, Pressureproperties.Width, Pressureproperties.Height) then
          
            Pressure_death = true
          end
        end
      end
    end
end
  
If ay more code is needed jut ask :)

Re: Codding player death

Posted: Wed Jan 02, 2019 10:40 pm
by zorg
Yes, post everything, a whole .love file, or lua files, direct link them in your post, or host them on pastebin or something; since those two snippets, from a glance, didn't contain the issue you stated;

i really don't want to play this variation of 20 questions (20 requests?) with anyone, and i'm sure no one else wants to either, so let us actually help you and since life is short, let's not waste any of our time please, including yours.

Re: Codding player death

Posted: Thu Jan 03, 2019 12:00 am
by Pardaleco
I'm sorry, i'll put a link to my dropbox then with all the files.

Here it is: https://www.dropbox.com/s/lgccz3c2rvpfg ... e.rar?dl=0

Re: Codding player death

Posted: Thu Jan 03, 2019 1:20 am
by pgimeno
I have noticed that in the first post, your snippet said
Pardaleco wrote: Mon Dec 31, 2018 7:21 pm

Code: Select all

function love.update(dt)
  if Collision_with_enemy == false then
    ... etc ...
    print(Collided_with_enemy)
 end
end
Note "Collision_with_enemy" vs. "Collided_with_enemy".

I tried adding the same 'if' to your code but using Collided_with_enemy and it worked.

Re: Codding player death

Posted: Thu Jan 03, 2019 3:18 am
by Pardaleco
Thank you for the reply! by reading your reply something in my head clicked and I was able to fix the issue in another way! I was being silly and not thinking correctly!