Skip to content

Commit

Permalink
#4 fix
Browse files Browse the repository at this point in the history
Didn't thought it's actually possible to fix this without good revamping of class, BUT YEAH, BABY
  • Loading branch information
N1ckn1ght authored Dec 3, 2022
1 parent 8fe959a commit 5efed5d
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 55 deletions.
96 changes: 64 additions & 32 deletions collisionDetector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ end

function CollisionDetector:update()
for _, player in pairs(self.trackablePlayers) do
if (self:fieldsCollisionCheckOnPlayer(player) or self:bordersCollisionCheckOnPlayer(player)) then
-- distinct player boid to convex parts
local npps = {}
for i = 1, #player.hitboxes do
npps[i] = player:getNormals(i)
end

if (self:fieldsCollisionCheckOnPlayer(player, npps) or self:bordersCollisionCheckOnPlayer(player, npps)) then
-- gimmick condition
end
end
Expand All @@ -36,47 +42,73 @@ function CollisionDetector:trackBorder(border)
self.trackableBorders[#self.trackableBorders + 1] = border
end

function CollisionDetector:bordersCollisionCheckOnPlayer(player)
return false
end
function CollisionDetector:bordersCollisionCheckOnPlayer(player, npps)
-- for _, border in pairs(self.trackableBorders) do
-- local col = true
-- local border_proj = dotProduct(Vector:create(dot.x, dot.y), axis)
-- for k = 1, #player.hitboxes do
-- local npp = npps[k]

function CollisionDetector:fieldsCollisionCheckOnPlayer(player)
-- distinct player boid to convex parts
local npps = {}
for i = 1, #player.hitboxes do
npps[i] = player:getNormals(i)
end
-- for k1, np in pairs({npp, npf}) do
-- for k2, v in pairs(np) do
-- local p = player:getMinMaxProj(k, v)
-- local q = field:getMinMaxProj(index, j, v)

-- if ((p[2] < q[1]) or (q[2] < p[1])) then
-- col = false
-- break
-- end
-- end
-- if (not col) then
-- break
-- end
-- end

-- if (col) then
-- onCollision(player, {"border", border})
-- return true
-- end
-- end
-- end
end

function CollisionDetector:fieldsCollisionCheckOnPlayer(player, npps)
for _, field in pairs(self.trackableFields) do
-- cycling through all pipe pairs, starting from field.curr
for i = 1, field.count do
local index = (i + field.curr - 2) % field.count + 1
-- now cycling through all parts of given pipe pair (2 for lower, 2 for upper)
for j = 1, #field.pipe do
local npf = field:getNormals(index, j)
col = true
-- now cycling thourgh distinct convex part of player
for k = 1, #player.hitboxes do
local npp = npps[k]

for k1, np in pairs({npp, npf}) do
for k2, v in pairs(np) do
local p = player:getMinMaxProj(k, v)
local q = field:getMinMaxProj(index, j, v)

if ((p[2] < q[1]) or (q[2] < p[1])) then
col = false
-- fast checking
if (field.pipes[index][1] - math.max(field.pipeEndWidth, field.pipeWidth) / 2 > player.size * player.k + player.location.x) then
return false
end
if not (field.pipes[index][1] + math.max(field.pipeEndWidth, field.pipeWidth) / 2 < -player.size * player.k + player.location.x) then
-- now cycling through all parts of given pipe pair (2 for lower, 2 for upper)
for j = 1, #field.pipe do
local npf = field:getNormals(index, j)
col = true
-- now cycling thourgh distinct convex part of player
for k = 1, #player.hitboxes do
local npp = npps[k]

for k1, np in pairs({npp, npf}) do
for k2, v in pairs(np) do
local p = player:getMinMaxProj(k, v)
local q = field:getMinMaxProj(index, j, v)

if ((p[2] < q[1]) or (q[2] < p[1])) then
col = false
break
end
end
if (not col) then
break
end
end
if (not col) then
break
end
end

if (col) then
onCollision(player, field, index, j)
return true
if (col) then
onCollision(player, {"pipe", field, index, j})
return true
end
end
end
end
Expand Down
38 changes: 26 additions & 12 deletions field.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,37 @@ end

function Field:update(dt)
local dbf = dt * self.velocity
self.distance = self.distance + dbf -- general distance counter
for k, v in pairs(self.pipes) do
v[1] = v[1] - dbf -- always moving pipes backwards
if (v[1] < -math.max(self.pipeWidth, self.pipeEndWidth)) then
-- general distance counter (e.g. for counting score, it'd be good to divide distance by pipeDistance)
self.distance = self.distance + dbf

local curr = self.curr
local first = true

for i = 1, self.count do
local index = (i + curr - 2) + 1
if (index > self.count) then
index = index - self.count
end

-- always moving pipes backwards
self.pipes[index][1] = self.pipes[index][1] - dbf
if (self.pipes[index][1] < -math.max(self.pipeWidth, self.pipeEndWidth)) then
-- respawn pipe with random y (if it's not on screen anymore)
self.pipes[k] = self:randomPipe(self.pipes[self.last][1] + self.pipeDistance)
self.last = self.curr
self.pipes[index] = self:randomPipe(self.pipes[self.last][1] + self.pipeDistance)
if (first) then
self.pipes[index][1] = self.pipes[index][1] - dbf
first = false
end
self.curr = self.curr + 1
if (self.curr > self.count) then
self.curr = 1
elseif (self.curr == 2) then
-- hotfix for issue #3
-- if (self.last > self.curr) basically
self.pipes[k][1] = self.pipes[k][1] - dbf
self.last = self.last + 1
if (self.last > self.count) then
self.last = 1
end
end
end
if (self.curr > self.count) then
self.curr = self.curr - self.count
end

-- 1 - linear, 2 - logarithmic
if self.accelerationType == 1 then
Expand Down
44 changes: 33 additions & 11 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@ function love.load()
math.randomseed(os.time())
Width = love.graphics.getWidth()
Height = love.graphics.getHeight()
initSounds()

DifficultyPresets = {
--{G, VelocityOnClick and VelocityOnClick2, {Velocity, InitialDistance, pipeDistance, pipeGap, pipeWidth, pipeEndWidth,
--pipeEndHeight, acceleration, velocityLimit, accelerationType, gapSubtract, gapLimit, maxRandomGap (experimental)}}
{1520, 520, {250, Width / 2, 550, 180, 65, 72, 30, 0.003, 1000, 2}}, -- Easy
{1520, 520, {250, Width / 2, 500, 150, 60, 80, 30, 0.003, 1000, 2}}, -- Normal
{2000, 666, {250, Width / 2, 450, 140, 60, 80, 30, 0.006, 1000, 2}}, -- Hard
{1240, 430, {1000, Width / 2, 750, 100, 60, 66, 30, 0.003, 2000, 2}}, -- Fast
{1520, 520, {250, Width / 2, 550, 150, 60, 300, 30, 0.003, 2000, 2}}, -- Flat
{1100, 250, {450, Width , 60, 180, 60, 60, 30, 0.003, 1000, 2, 60}} -- Dense
-- {G, VelocityOnClick and VelocityOnClick2, {Velocity, InitialDistance, pipeDistance, pipeGap, pipeWidth, pipeEndWidth,
-- pipeEndHeight, acceleration, velocityLimit, accelerationType, gapSubtract, gapLimit, maxRandomGap (experimental)}}
{1520, 520, {250, Width / 2, 550, 180, 65, 72, 30, 0.003, 1000, 2}}, -- Easy
{1520, 520, {250, Width / 2, 500, 150, 60, 80, 30, 0.003, 1000, 2}}, -- Normal
{2000, 666, {250, Width / 2, 450, 140, 60, 80, 30, 0.006, 1000, 2}}, -- Hard
{1240, 400, {800, Width / 2, 950, 100, 60, 66, 30, 0.003, 2000, 2}}, -- Fast
{1520, 520, {250, Width / 2, 550, 150, 60, 300, 30, 0.003, 1500, 2}}, -- Flat
{1100, 250, {450, Width , 60, 180, 60, 60, 30, 0.003, 1500, 2, 60}}, -- Dense
{1100, 250, {450, Width , 1, 300, 1, 1, 0, 0.003, 1500, 2, 15}} -- Debug
}

InitialX = 150
Difficulty = DifficultyPresets[6]
Difficulty = DifficultyPresets[2]

Score = 0
ActiveGame = true
Expand All @@ -45,7 +47,14 @@ function love.update(dt)
Player:update(dt)
Pipes:update(dt)
Detector:update()
Score = math.floor((Pipes.distance - Difficulty[3][2] + InitialX) / Pipes.pipeDistance)
-- Score = math.floor((Pipes.distance - Difficulty[3][2] + InitialX) / Pipes.pipeDistance)
-- local newScore = math.floor((Pipes.distance - Difficulty[3][2] + InitialX) / Pipes.pipeDistance)
-- if (newScore > Score) then
-- Score = newScore
-- print(Score)
-- SoundPoint:stop()
-- SoundPoint:play()
-- end
end
end

Expand All @@ -59,15 +68,28 @@ end
function love.mousepressed(x, y, button)
if ActiveGame and button == 1 then
Player.velocity.y = -VelocityOnClick
SoundWing:stop()
SoundWing:play()
end
if ActiveGame and button == 2 then
Player.velocity.y = -VelocityOnClick2
SoundWing:stop()
SoundWing:play()
end
end

function onCollision(player, field, index, rect)
function onCollision(player, cause)
ActiveGame = false
player.color = {1, 0, 0, 1}
SoundHit:play()
end

function initSounds()
SoundDie = love.audio.newSource("sound/die.wav", "static")
SoundHit = love.audio.newSource("sound/hit.wav", "static")
SoundPoint = love.audio.newSource("sound/point.wav", "static")
SoundSwoosh = love.audio.newSource("sound/swoosh.wav", "static")
SoundWing = love.audio.newSource("sound/wing.wav", "static")
end

-- http://lua-users.org/wiki/CopyTable
Expand Down

0 comments on commit 5efed5d

Please sign in to comment.