fixed arrow physics

This commit is contained in:
lustlion 2022-02-22 01:14:38 +01:00
parent f0bc02c418
commit a4490bc827
10 changed files with 123 additions and 115 deletions

49
code/canvas.lua Normal file
View File

@ -0,0 +1,49 @@
Canvas = {class = "Canvas"}
function Canvas:New(name)
local o = {}
o.name = name
o.width = game.width/game.scale
o.height = game.height/game.scale
o.canvas = love.graphics.newCanvas(o.width,o.height)
setmetatable(o, self)
self.__index = self
Canvas[name] = o
end
function Canvas:Recreate()
self.canvas:release()
self = Canvas:New(self.name)
end
function Canvas:Reset()
love.graphics.setCanvas(Canvas[self.name].canvas)
love.graphics.setBlendMode("replace")
love.graphics.setColor(0,0,0,0)
love.graphics.rectangle(
"fill",
0,
0,
self.width,
self.height
)
love.graphics.setCanvas()
end
function Canvas:DrawingStart()
self:Reset()
love.graphics.setCanvas(Canvas[self.name].canvas)
end
function Canvas:DrawingEnd()
love.graphics.setCanvas()
love.graphics.setBlendMode("alpha")
love.graphics.setColor(1,1,1,1)
end
function Canvas:Draw()
love.graphics.draw(self.canvas)
end
require "code/canvasses/darkness"

View File

@ -0,0 +1,15 @@
Canvas:New("Darkness")
function Canvas.Darkness:Reset()
love.graphics.setCanvas(Canvas.Darkness.canvas)
love.graphics.setBlendMode("replace")
love.graphics.setColor(0,0,0,0.95)
love.graphics.rectangle(
"fill",
0,
0,
self.width,
self.height
)
love.graphics.setCanvas()
end

View File

@ -1,44 +0,0 @@
Darkness = {}
Canvas = {}
function Darkness.Create()
Darkness.width = game.width/game.scale
Darkness.height = game.height/game.scale
Canvas.Darkness = love.graphics.newCanvas(
Darkness.width,
Darkness.height
)
end
function Darkness.Recreate()
Canvas.Darkness:release()
Darkness.Create()
end
function Darkness.Reset()
love.graphics.setCanvas(Canvas.Darkness)
love.graphics.setBlendMode("replace")
love.graphics.setColor(0,0,0,0.95)
love.graphics.rectangle(
"fill",
0,
0,
Darkness.width,
Darkness.height
)
love.graphics.setCanvas()
end
function Darkness.DrawStart()
Darkness.Reset()
love.graphics.setCanvas(Canvas.Darkness)
end
function Darkness.DrawEnd()
love.graphics.setCanvas()
love.graphics.setBlendMode("alpha")
love.graphics.setColor(1,1,1,1)
love.graphics.draw(Canvas.Darkness)
end
Darkness.Create()

View File

@ -6,7 +6,7 @@ Arrow = Entity:New(x,y)
o.type = "arrow" o.type = "arrow"
o.pos = {x = x, y = y} o.pos = {x = x, y = y}
o.speed = speed or 0 o.speed = speed or 10
o.sprite_rotation = rotation or 0 o.sprite_rotation = rotation or 0
o.vel = { o.vel = {
x = o.speed * math.cos(o.sprite_rotation), x = o.speed * math.cos(o.sprite_rotation),
@ -19,6 +19,11 @@ Arrow = Entity:New(x,y)
-- animations -- animations
o.body = Animation:New(animation.kupo.arrow) o.body = Animation:New(animation.kupo.arrow)
o.boxCollision = {
from = {x = -0.5, y = -0.5}, --gameworld pixels
to = {x = 0.5, y = 0.5} -- gameworld pixels
}
table.insert(LoadedObjects.Entities,o) table.insert(LoadedObjects.Entities,o)
o.id = #LoadedObjects.Entities o.id = #LoadedObjects.Entities
@ -27,52 +32,25 @@ Arrow = Entity:New(x,y)
return o return o
end end
function Arrow:HandleAnimation() function Arrow:DrawBackground()
self:Draw(self.body) self:Draw(self.body)
end end
function Arrow:DoPhysics() function Arrow:DoPhysics()
if not self.stuck then if not self:isCollidingAt(self.pos.x + self.vel.x, self.pos.y, LoadedObjects.Collisions) then
-- horizontal collisions
if not isThereAnyCollisionAt(
self.pos.x + self.vel.x,
self.pos.y
) then
self.pos.x = self.pos.x + self.vel.x self.pos.x = self.pos.x + self.vel.x
else else
while not isThereObjectAt(
self.pos.x + math.sign(self.vel.x),
self.pos.y,
LoadedObjects.Collisions
) do
self.pos.x = self.pos.x + math.sign(self.vel.x)
end
self.stuck = true self.stuck = true
end end
-- vertical collision if not self:isCollidingAt(self.pos.x, self.pos.y + self.vel.y, LoadedObjects.Collisions) then
if not isThereAnyCollisionAt(
self.pos.x,
self.pos.y + self.vel.y
) then
self.pos.y = self.pos.y + self.vel.y self.pos.y = self.pos.y + self.vel.y
else else
while not isThereObjectAt(
self.pos.x,
self.pos.y + math.sign(self.vel.y),
LoadedObjects.Collisions
) do
self.pos.y = self.pos.y + math.sign(self.vel.y)
end
self.stuck = true self.stuck = true
end end
-- stuck into collisions
if self.stuck then if self.stuck then
--lets allow the arrow to tip a bit into the thing self.pos.x = self.pos.x + self.vel.x * (2/3)
self.pos.x = self.pos.x + self.vel.x / 5 self.pos.y = self.pos.y + self.vel.y * (2/3)
self.pos.y = self.pos.y + self.vel.y / 5
self.vel.x = 0 self.vel.x = 0
self.vel.y = 0 self.vel.y = 0
self.illuminated = false
end
end end
end end

View File

@ -89,7 +89,7 @@ function Kupo:Smart()
if self.bow_aim_frame > self.bow_aim_frames then if self.bow_aim_frame > self.bow_aim_frames then
self.bow_aim_frame = self.bow_aim_frame - self.bow_aim_frames self.bow_aim_frame = self.bow_aim_frame - self.bow_aim_frames
self.bow_frame = self.bow_frame + 1 self.bow_frame = self.bow_frame + 1
Arrow:New(self.pos.x,self.pos.y,self.bow_rotation,15) Arrow:New(self.pos.x,self.pos.y,self.bow_rotation,10)
end end
else else
self.bow_frame = self.bow_frame + 1 self.bow_frame = self.bow_frame + 1
@ -157,12 +157,5 @@ function Kupo:HandleAnimation()
end end
function Kupo:DoPhysics() function Kupo:DoPhysics()
self:CollisionMove()
-- horizontal collisions
if not isThereAnyCollisionAt(self.pos.x + self.vel.x, self.pos.y) then
self.pos.x = self.pos.x + self.vel.x
end
if not isThereAnyCollisionAt(self.pos.x, self.pos.y + self.vel.y) then
self.pos.y = self.pos.y + self.vel.y
end
end end

View File

@ -35,11 +35,6 @@
y = nil y = nil
} }
o.boxCollision = {
from = {x = -8, y = -16}, --gameworld pixels
to = {x = 8, y = 0} -- gameworld pixels
}
o.lightRange = 40 -- screen pixels o.lightRange = 40 -- screen pixels
-- status -- status

View File

@ -59,16 +59,37 @@ function Entity:Move()
end end
function Entity:CollisionMove() function Entity:CollisionMove()
local r = false
if not self:isCollidingAt(self.pos.x + self.vel.x, self.pos.y, LoadedObjects.Collisions) then if not self:isCollidingAt(self.pos.x + self.vel.x, self.pos.y, LoadedObjects.Collisions) then
self.pos.x = self.pos.x + self.vel.x self.pos.x = self.pos.x + self.vel.x
else else
self.vel.x = 0 self.vel.x = 0
r = true
end end
if not self:isCollidingAt(self.pos.x, self.pos.y + self.vel.y, LoadedObjects.Collisions) then if not self:isCollidingAt(self.pos.x, self.pos.y + self.vel.y, LoadedObjects.Collisions) then
self.pos.y = self.pos.y + self.vel.y self.pos.y = self.pos.y + self.vel.y
else else
self.vel.y = 0 self.vel.y = 0
r = true
end end
return r
end
function Entity:MoveX()
local r = false
if not self:isCollidingAt(self.pos.x + self.vel.x, self.pos.y, LoadedObjects.Collisions) then
self.pos.x = self.pos.x + self.vel.x
else
self.vel.x = 0
r = true
end
if not self:isCollidingAt(self.pos.x, self.pos.y + self.vel.y, LoadedObjects.Collisions) then
self.pos.y = self.pos.y + self.vel.y
else
self.vel.y = 0
r = true
end
return r
end end
function Entity:LightAdjust(x,y) function Entity:LightAdjust(x,y)
@ -181,13 +202,13 @@ end
function Entity:isCollidingAtAll(x,y) function Entity:isCollidingAtAll(x,y)
local result = false local result = false
if not result then if not result then
result = self:isCollidingAt(x,y,objects.collisions) result = self:isCollidingAt(x,y,LoadedObjects.Platforms)
end end
if not result then if not result then
result = self:isCollidingAt(x,y,objects.ladders) result = self:isCollidingAt(x,y,LoadedObjects.Platforms)
end end
if not result then if not result then
result = self:isCollidingAt(x,y,objects.platforms) result = self:isCollidingAt(x,y,LoadedObjects.Platforms)
end end
return result return result
end end
@ -250,8 +271,10 @@ function Entity:Debug()
end end
end end
function Entity:DrawBackground() function Entity:HandleAnimation()
end
function Entity:DrawBackground()
end end
require "code/entities/kupo" require "code/entities/kupo"

View File

@ -2,7 +2,7 @@ function GameworldDrawPrepare()
if game_resize then if game_resize then
Camera.height = game.height Camera.height = game.height
Camera.width = game.width Camera.width = game.width
Darkness.Recreate() Canvas.Darkness.Recreate()
end end
pcr, pcg, pcb, pca = love.graphics.getColor() pcr, pcg, pcb, pca = love.graphics.getColor()
love.graphics.scale(game.scale,game.scale) love.graphics.scale(game.scale,game.scale)
@ -74,8 +74,8 @@ function GameworldDrawForeground()
end end
function GameworldDrawDarkness() function GameworldDrawDarkness()
Darkness.Reset() Canvas.Darkness:Reset()
Darkness.DrawStart() Canvas.Darkness:DrawingStart()
love.graphics.setBlendMode("replace") love.graphics.setBlendMode("replace")
love.graphics.setColor(0,0,0,0) love.graphics.setColor(0,0,0,0)
for _, light in pairs(LoadedObjects.Lights) do for _, light in pairs(LoadedObjects.Lights) do
@ -93,7 +93,8 @@ function GameworldDrawDarkness()
) )
end end
end end
Darkness.DrawEnd() Canvas.Darkness:DrawingEnd()
Canvas.Darkness:Draw()
end end
function GameworldDrawLights() function GameworldDrawLights()

View File

@ -20,11 +20,11 @@ require "code/audio"
-- objects -- objects
require "code/entity" require "code/entity"
require "code/canvas"
require "code/collision" require "code/collision"
require "code/lights" require "code/lights"
-- functions -- functions
require "code/darkness"
require "code/debug" require "code/debug"
require "code/demo" require "code/demo"
require "code/keybind" require "code/keybind"

View File

@ -29,8 +29,6 @@ function love.load()
logPrint(loveInitLog) logPrint(loveInitLog)
loveInitLog = nil loveInitLog = nil
Canvas = {}
Darkness.Create()
Camera.width = game.width Camera.width = game.width
Camera.height = game.height Camera.height = game.height
@ -52,8 +50,8 @@ function love.load()
main_Player = Player:New(75,50) main_Player = Player:New(75,50)
--Kupo:New(100,150) Kupo:New(100,150)
--Kupo:New(300,150) Kupo:New(300,150)
HookAnchor:New(200,89) HookAnchor:New(200,89)
HookAnchor:New(400,89) HookAnchor:New(400,89)
Fairy:New(200,88) Fairy:New(200,88)