Cleaned collisions.lua, objobjects.lua. Fixed level optimization typo. Added fairy FCS (altitude hold).

This commit is contained in:
lustlion 2022-02-08 08:22:27 +01:00
parent 010c19b10f
commit 1f7d967f77
7 changed files with 134 additions and 85 deletions

View File

@ -2,13 +2,17 @@ return {
name = "test", name = "test",
tileset = tileset.library, tileset = tileset.library,
tiles = { tiles = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, { 1, 14, 14, 14, 14, 14, 14, 1, 14, 14, 14, 14, 14, 14, 14, 1},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, { 1, 4, 0, 0, 0, 0, 2, 14, 4, 0, 0, 0, 0, 0, 2, 1},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, { 1, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1},
{ 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37}, { 1, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1},
{ 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37}, { 1, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1},
{ 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37}, { 1, 4, 0, 0, 0, 0, 2, 13, 4, 0, 0, 0, 0, 0, 2, 1},
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} { 1, 13, 13, 13, 13, 13, 13, 1, 4, 0, 0, 0, 0, 0, 2, 1},
{ 1, 1, 1, 1, 1, 1, 1, 1, 4, 0, 0, 0, 0, 0, 2, 1},
{ 1, 1, 1, 1, 1, 1, 1, 1, 13, 13, 13, 13, 13, 13, 13, 1},
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
}, },
objects = {} objects = {}
} }

View File

@ -2,31 +2,28 @@ Collision = {}
--[[ --[[
Collision Collision
[bool] isDisabled [bool flag] isDisabled
> if true used for collision > if true used for collision
[bool] isActive [bool flag] isColliding
> if true, this collision is active (on collision, duh)
[bool] isColliding
> if true, this collision is colliding > if true, this collision is colliding
[2d pos] from - x, y [vec2 position] from - x, y
> top right corner of collision box > top right corner of collision box
[2d pos] to - x, y [vec2 position] to - x, y
> bottom left corner of collision box > bottom left corner of collision box
[int] width [int property] width
> width of collision box > width of collision box
[int] height [int property] height
> height of collision box > height of collision box
--]] --]]
-- can also be called with only ox and oy, where they become the width and height instead -- can also be called with only ox and oy, where they become the width and height instead
function Collision:New(ox,oy,tx,ty) function Collision:New(ox,oy,tx,ty)
local o = {isColliding = false, isDisabled = false, isActive = false} local o = {isColliding = false, isDisabled = false}
if tx ~= nil and ty ~= nil then if tx ~= nil and ty ~= nil then
o.from = {x = ox, y = oy} o.from = {x = ox, y = oy}
@ -65,8 +62,6 @@ end
function Collision:Draw(color) function Collision:Draw(color)
if self.isColliding == true then if self.isColliding == true then
love.graphics.setColor(0,1,0,0.5) love.graphics.setColor(0,1,0,0.5)
elseif self.isActive == 1 then
love.graphics.setColor(0,1,1,0.5)
elseif color == 1 then elseif color == 1 then
love.graphics.setColor(1,0,0,0.5) love.graphics.setColor(1,0,0,0.5)
elseif color == 2 then elseif color == 2 then

View File

@ -5,9 +5,11 @@ Fairy = Entity:New(x,y)
-- behaviour -- behaviour
o.pos = {x = x, y = y} o.pos = {x = x, y = y}
o.speed = 1 o.speed = 1.4
o.range = 20 o.range = 20
o.vision_range = 120
o.target = {x = x, y = y} o.target = {x = x, y = y}
o.hover_distance = 60
-- animations -- animations
o.body = Animation:New(animation.fairy.flying) o.body = Animation:New(animation.fairy.flying)
@ -32,9 +34,30 @@ Fairy = Entity:New(x,y)
function Fairy:Smart() function Fairy:Smart()
if self:CheckVisionLine(main_Player) then if self:CheckVisionLine(main_Player,self.vision_range) then
self.target.x = main_Player.pos.x + main_Player.target_offset.x self.target.x = main_Player.pos.x + main_Player.target_offset.x
self.target.y = main_Player.pos.y + main_Player.target_offset.y self.target.y = main_Player.pos.y + main_Player.target_offset.y
local below = 1
while not isThereObjectAt(
self.target.x,
self.target.y + below * game.scale,
LoadedObjects.Collisions
) do
below = below + 1
if below >= self.hover_distance then break end
end
local top = 1
while not isThereObjectAt(
self.target.x,
self.target.y - top * game.scale,
LoadedObjects.Collisions
) do
top = top + 1
if top >= self.hover_distance then break end
end
self.target.y = self.target.y - top + below
end end
local distance_x = self.target.x - self.pos.x local distance_x = self.target.x - self.pos.x
@ -55,16 +78,12 @@ function Fairy:Smart()
local particle_data = { local particle_data = {
animation = animation.particle.simple, animation = animation.particle.simple,
sprite_tint = HEX2RGB("#fe00d1"), sprite_tint = HEX2RGB("#fed100"),
direction = angle-math.rad(180+math.random(60)-30), direction = angle-math.rad(180+math.random(60)-30),
speed = 0.8*(distance/50), speed = 0.8*(distance/50),
speed_increase = -0.01, speed_increase = -0.01,
} }
Particle:New( Particle:New(self.pos.x,self.pos.y,particle_data)
self.pos.x,
self.pos.y,
particle_data
)
end end
end end
@ -75,17 +94,20 @@ function Fairy:HandleAnimation()
end end
function Fairy:DoPhysics() function Fairy:DoPhysics()
local random_x = math.random(-2, 2)/10 local random_x = math.random(-4, 4)/10
local random_y = math.random(-2, 2)/10 local random_y = math.random(-4, 4)/10
self.vel.x = self.vel.x + random_x self.vel.x = self.vel.x + random_x
self.vel.y = self.vel.y + random_y self.vel.y = self.vel.y + random_y
-- move
self:CollisionMove() self:CollisionMove()
self.vel.x = 0
self.vel.y = 0
self:LightAdjust() self:LightAdjust()
end end
function Fairy:Debug() function Fairy:Debug()
Entity.Debug(self) Entity.Debug(self)
self:CheckVisionLineDebug(main_Player) self:CheckVisionLineDebug(main_Player,self.vision_range)
end end

View File

@ -73,7 +73,7 @@ function Entity:Kill()
self = nil self = nil
end end
function Entity:CheckVisionLine(entity) function Entity:CheckVisionLine(entity,range)
local target_x = entity.pos.x + entity.target_offset.x local target_x = entity.pos.x + entity.target_offset.x
local target_y = entity.pos.y + entity.target_offset.y local target_y = entity.pos.y + entity.target_offset.y
@ -83,15 +83,18 @@ function Entity:CheckVisionLine(entity)
local angle = GetAngleFromVector(distance_x,distance_y) local angle = GetAngleFromVector(distance_x,distance_y)
local distance = math.sqrt(distance_x ^ 2 + distance_y ^ 2) local distance = math.sqrt(distance_x ^ 2 + distance_y ^ 2)
local is_colliding = false local is_colliding = true
for i=1, distance, game.scale do if distance < range then
if isThereObjectAt( is_colliding = false
self.pos.x+math.cos(angle)*i, for i=1, distance, game.scale do
self.pos.y+math.sin(angle)*i, if isThereObjectAt(
LoadedObjects.Collisions self.pos.x+math.cos(angle)*i,
) then self.pos.y+math.sin(angle)*i,
is_colliding = true LoadedObjects.Collisions
) then
is_colliding = true
end
end end
end end
@ -131,15 +134,19 @@ end
-- returns true if theres a collision at that point. also marks collisioned tile as collision true -- returns true if theres a collision at that point. also marks collisioned tile as collision true
function Entity:isCollidingAt(x,y,object) function Entity:isCollidingAt(x,y,object)
local result = false for _, collision in pairs(object) do
for _, col in pairs(object) do if collision.disable then
result = x + self.boxCollision.from.x < col.to.x -- Dont calculate if disabled
and col.from.x < x + self.boxCollision.to.x elseif x + self.boxCollision.from.x < collision.to.x
and y + self.boxCollision.from.y < col.to.y and x + self.boxCollision.to.x > collision.from.x
and col.from.y < y + self.boxCollision.to.y and y + self.boxCollision.from.y < collision.to.y
if result == true then break end and y + self.boxCollision.to.y > collision.from.y
then
collision.isColliding = true
return true
end
end end
return result return false
end end
function Entity:isCollidingWith(entity) function Entity:isCollidingWith(entity)
@ -163,7 +170,7 @@ function Entity:isCollidingAtAll(x,y)
return result return result
end end
function Entity:CheckVisionLineDebug(entity) function Entity:CheckVisionLineDebug(entity,range)
local c1, c2, c3, a = love.graphics.getColor() local c1, c2, c3, a = love.graphics.getColor()
local target_x = entity.pos.x + entity.target_offset.x local target_x = entity.pos.x + entity.target_offset.x
@ -175,22 +182,24 @@ function Entity:CheckVisionLineDebug(entity)
local angle = GetAngleFromVector(distance_x,distance_y) local angle = GetAngleFromVector(distance_x,distance_y)
local distance = math.sqrt(distance_x ^ 2 + distance_y ^ 2) local distance = math.sqrt(distance_x ^ 2 + distance_y ^ 2)
for i=1, distance, game.scale do if distance < range then
if isThereObjectAt( for i=1, distance, game.scale do
self.pos.x+math.cos(angle)*i, if isThereObjectAt(
self.pos.y+math.sin(angle)*i, self.pos.x+math.cos(angle)*i,
LoadedObjects.Collisions self.pos.y+math.sin(angle)*i,
) then LoadedObjects.Collisions
love.graphics.setColor(1,0,0) ) then
else love.graphics.setColor(1,0,0)
love.graphics.setColor(0,1,0) else
love.graphics.setColor(0,1,0)
end
love.graphics.line(
self.pos.x+math.cos(angle)*i-1 - Camera.pos.x,
self.pos.y+math.sin(angle)*i-1 - Camera.pos.y,
self.pos.x+math.cos(angle)*i - Camera.pos.x,
self.pos.y+math.sin(angle)*i - Camera.pos.y
)
end end
love.graphics.line(
self.pos.x+math.cos(angle)*i-1 - Camera.pos.x,
self.pos.y+math.sin(angle)*i-1 - Camera.pos.y,
self.pos.x+math.cos(angle)*i - Camera.pos.x,
self.pos.y+math.sin(angle)*i - Camera.pos.y
)
end end
love.graphics.setColor(c1,c2,c3,a) love.graphics.setColor(c1,c2,c3,a)
@ -209,6 +218,14 @@ function Entity:Debug()
-Camera.pos.x + self.pos.x + self.boxCollision.to.x -(-Camera.pos.x + self.pos.x + self.boxCollision.from.x), -Camera.pos.x + self.pos.x + self.boxCollision.to.x -(-Camera.pos.x + self.pos.x + self.boxCollision.from.x),
-Camera.pos.y + self.pos.y + self.boxCollision.to.y -(-Camera.pos.y + self.pos.y + self.boxCollision.from.y) -Camera.pos.y + self.pos.y + self.boxCollision.to.y -(-Camera.pos.y + self.pos.y + self.boxCollision.from.y)
) )
if self.target ~= nil then
love.graphics.line(
-Camera.pos.x + self.pos.x,
-Camera.pos.y + self.pos.y,
-Camera.pos.x + self.target.x,
-Camera.pos.y + self.target.y
)
end
end end
require "data/scripts/entities/kupo" require "data/scripts/entities/kupo"
require "data/scripts/entities/arrow" require "data/scripts/entities/arrow"

View File

@ -1,4 +1,5 @@
function GameStep() function GameStep()
SetCollisionFlags()
if menu_type == "no" then if menu_type == "no" then
for _, particle in pairs(LoadedParticles) do for _, particle in pairs(LoadedParticles) do
particle:Smart() particle:Smart()

View File

@ -259,7 +259,6 @@ function TileOptimizeObjects()
for j = 1, #LevelTiles[i] do for j = 1, #LevelTiles[i] do
if LevelTiles[i][j].id ~= 0 then if LevelTiles[i][j].id ~= 0 then
local type = TileData[LevelTiles[i][j].id].type local type = TileData[LevelTiles[i][j].id].type
local light = TileData[LevelTiles[i][j].id].light
if type == "whole" and not isTileOptimized[i][j] then if type == "whole" and not isTileOptimized[i][j] then
@ -296,14 +295,12 @@ function TileOptimizeObjects()
then then
local type_check = TileData[LevelTiles[i+m][j+l].id].type local type_check = TileData[LevelTiles[i+m][j+l].id].type
if type_check == "whole" if type_check == "whole"
and not isTileOptimized[i+m][j+n] and not isTileOptimized[i+m][j+l]
then then
checkline = true checkline = true
else else
break break
end end
else
break
end end
end end
if checkline then if checkline then
@ -317,7 +314,7 @@ function TileOptimizeObjects()
end end
end end
logPrint("Group size: "..m.."x"..n) logPrint("- Group size: "..m.."x"..n)
unoptimized = unoptimized + m * n unoptimized = unoptimized + m * n
local base_x = tileProperties.scale * j * tileProperties.width + tileProperties.scale * (levelProperties.offset.x - tileProperties.height) local base_x = tileProperties.scale * j * tileProperties.width + tileProperties.scale * (levelProperties.offset.x - tileProperties.height)
local base_y = tileProperties.scale * i * tileProperties.height + tileProperties.scale * (levelProperties.offset.y - tileProperties.height) local base_y = tileProperties.scale * i * tileProperties.height + tileProperties.scale * (levelProperties.offset.y - tileProperties.height)

View File

@ -22,16 +22,16 @@ function LoadedObjects.DrawCollisions()
end end
end end
-- returns true if theres a collision at that point. also marks collisioned tile as collision true -- returns true if theres a collision at that point
function isThereObjectAt(x,y,objectType) function isThereObjectAt(x,y,objectType)
for _, collision in pairs(objectType) do for _, collision in pairs(objectType) do
if collision.disable then if collision.disable then
-- Dont calculate if dissabled -- Dont calculate if dissabled
elseif x >= collision.from.x elseif x >= collision.from.x
and x <= collision.to.x and x <= collision.to.x
and y >= collision.from.y and y >= collision.from.y
and y <= collision.to.y then and y <= collision.to.y then
collision.collision = true collision.isColliding = true
return true return true
end end
end end
@ -39,24 +39,37 @@ function isThereObjectAt(x,y,objectType)
end end
function isThereAnyCollisionAt(x,y) function isThereAnyCollisionAt(x,y)
return isThereObjectAt(x,y,LoadedObjects.Collisions) or isThereObjectAt(x,y,LoadedObjects.Ladders) or isThereObjectAt(x,y,LoadedObjects.Platforms) local Check = {
end LoadedObjects.Collisions,
-- flags LoadedObjects.Ladders,
function SetCollisionFlags(player) LoadedObjects.Platforms
for _, collision in pairs(LoadedObjects.Collisions) do }
collision.collision = false for _, type in pairs(Check) do
local result = isThereObjectAt(x,y,type)
if result then
return result
end
end end
return false
end
-- flags
function SetCollisionFlags()
local Check = {
LoadedObjects.Collisions,
LoadedObjects.Ladders,
LoadedObjects.Platforms
}
for _, type in pairs(Check) do
for _, object in pairs(type) do
object.isColliding = false
end
end
for _, platform in pairs(LoadedObjects.Platforms) do for _, platform in pairs(LoadedObjects.Platforms) do
platform.collision = false if main_Player.pos.y < platform.from.y then
if player.pos.y < platform.from.y then
platform.disable = false platform.disable = false
else else
platform.disable = true platform.disable = true
end end
end end
for _, ladder in pairs(LoadedObjects.Ladders) do
ladder.collision = false
end
end end