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.pos = {x = x, y = y}
o.speed = speed or 0
o.speed = speed or 10
o.sprite_rotation = rotation or 0
o.vel = {
x = o.speed * math.cos(o.sprite_rotation),
@ -19,6 +19,11 @@ Arrow = Entity:New(x,y)
-- animations
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)
o.id = #LoadedObjects.Entities
@ -27,52 +32,25 @@ Arrow = Entity:New(x,y)
return o
end
function Arrow:HandleAnimation()
function Arrow:DrawBackground()
self:Draw(self.body)
end
function Arrow:DoPhysics()
if not self.stuck 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
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
end
-- vertical collision
if not isThereAnyCollisionAt(
self.pos.x,
self.pos.y + self.vel.y
) then
self.pos.y = self.pos.y + self.vel.y
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
end
-- stuck into collisions
if self.stuck then
--lets allow the arrow to tip a bit into the thing
self.pos.x = self.pos.x + self.vel.x / 5
self.pos.y = self.pos.y + self.vel.y / 5
self.vel.x = 0
self.vel.y = 0
self.illuminated = false
end
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.stuck = 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.stuck = true
end
if self.stuck then
self.pos.x = self.pos.x + self.vel.x * (2/3)
self.pos.y = self.pos.y + self.vel.y * (2/3)
self.vel.x = 0
self.vel.y = 0
end
end

View File

@ -89,7 +89,7 @@ function Kupo:Smart()
if self.bow_aim_frame > self.bow_aim_frames then
self.bow_aim_frame = self.bow_aim_frame - self.bow_aim_frames
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
else
self.bow_frame = self.bow_frame + 1
@ -157,12 +157,5 @@ function Kupo:HandleAnimation()
end
function Kupo:DoPhysics()
-- 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
self:CollisionMove()
end

View File

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

View File

@ -59,16 +59,37 @@ function Entity:Move()
end
function Entity:CollisionMove()
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
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
function Entity:LightAdjust(x,y)
@ -181,13 +202,13 @@ end
function Entity:isCollidingAtAll(x,y)
local result = false
if not result then
result = self:isCollidingAt(x,y,objects.collisions)
result = self:isCollidingAt(x,y,LoadedObjects.Platforms)
end
if not result then
result = self:isCollidingAt(x,y,objects.ladders)
result = self:isCollidingAt(x,y,LoadedObjects.Platforms)
end
if not result then
result = self:isCollidingAt(x,y,objects.platforms)
result = self:isCollidingAt(x,y,LoadedObjects.Platforms)
end
return result
end
@ -250,8 +271,10 @@ function Entity:Debug()
end
end
function Entity:DrawBackground()
function Entity:HandleAnimation()
end
function Entity:DrawBackground()
end
require "code/entities/kupo"

View File

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

View File

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

View File

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