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