movement update and fixes

This commit is contained in:
2021-12-27 17:18:47 +01:00
parent 5f6e23b921
commit 56ffd396bc
5 changed files with 89 additions and 82 deletions

View File

@@ -10,6 +10,4 @@ Lua game inspired by an old arcade game(Qix/Gals Panic) using LOVE2D for Linux.
- Add more pictures (manga?) - Add more pictures (manga?)
- Fix Mesh
- Fix balls sometimes attaching to the lines - Fix balls sometimes attaching to the lines

View File

@@ -39,8 +39,13 @@ function Player:init(def, level )
end end
function Player:isMoveAllowed(point) function Player:isMoveAllowed(point)
print("Player:isMoveAllowed " .. tostring(point[1]) .. "," .. tostring(point[2]))
point = {point[1] + self.width/2, point[2] + self.width/2}
print("Player:isMoveAllowed " .. tostring(point[1]) .. "," .. tostring(point[2]))
for i,segment in pairs (self.trailSegments) do for i,segment in pairs (self.trailSegments) do
if segment:pointInSegment(point) then if segment:pointInSegment(point) then
print("Player:isMoveAllowed = false")
print_r(segment)
return false return false
end end
end end

View File

@@ -1,10 +1,9 @@
-- require('mobdebug').start() -- require('mobdebug').start()
Segment = Class {}
Segment = Class{}
local symbols = {['up'] = 207, ['down'] = 209, ['right'] = 199, ['left'] = 182} local symbols = {['up'] = 207, ['down'] = 209, ['right'] = 199, ['left'] = 182}
function Segment:init(firstPoint, secondPoint,lineWidth, face, color) function Segment:init(firstPoint, secondPoint, lineWidth, face, color)
self.firstPointX = firstPoint[1] self.firstPointX = firstPoint[1]
self.firstPointY = firstPoint[2] self.firstPointY = firstPoint[2]
self.secondPointX = secondPoint[1] self.secondPointX = secondPoint[1]
@@ -14,15 +13,16 @@ function Segment:init(firstPoint, secondPoint,lineWidth, face, color)
self.horizontal = self.firstPointY == self.secondPointY self.horizontal = self.firstPointY == self.secondPointY
self.face = face or '' self.face = face or ''
self.direction = self:calculateDirection() self.direction = self:calculateDirection()
self.color = color or {r=255,g=255,b=255} self.color = color or {r = 255, g = 255, b = 255}
end end
function Segment:render() function Segment:render()
love.graphics.setColor(self.color.r,self.color.g, self.color.b,255) love.graphics.setColor(self.color.r, self.color.g, self.color.b, 255)
love.graphics.setLineWidth(self.lineWidth) love.graphics.setLineWidth(self.lineWidth)
love.graphics.line(self.firstPointX,self.firstPointY,self.secondPointX, self.secondPointY) love.graphics.line(self.firstPointX, self.firstPointY, self.secondPointX,
self.secondPointY)
love.graphics.setColor(255,255, 255, 255) love.graphics.setColor(255, 255, 255, 255)
-- debug -- debug
-- love.graphics.setFont(gFonts['small']) -- love.graphics.setFont(gFonts['small'])
@@ -31,14 +31,18 @@ function Segment:render()
end end
function Segment:segmentToPoints() function Segment:segmentToPoints()
return self.firstPointX, self.firstPointY, self.secondPointX, self.secondPointY return self.firstPointX, self.firstPointY, self.secondPointX,
self.secondPointY
end end
function Segment:length() function Segment:length()
return math.sqrt( (self.secondPointX - self.firstPointX) * (self.secondPointX - self.firstPointX) + (self.secondPointY - self.firstPointY) * (self.secondPointY - self.firstPointY) ) return math.sqrt((self.secondPointX - self.firstPointX) *
(self.secondPointX - self.firstPointX) +
(self.secondPointY - self.firstPointY) *
(self.secondPointY - self.firstPointY))
end end
function Segment:sideOfSegment(x,y) function Segment:sideOfSegment(x, y)
if y < self.firstPointY and y < self.secondPointY then -- above if y < self.firstPointY and y < self.secondPointY then -- above
return 'up' return 'up'
end end
@@ -55,9 +59,7 @@ function Segment:sideOfSegment(x,y)
end end
function Segment:debug(msg) function Segment:debug(msg)
if not msg then if not msg then msg = '' end
msg = ''
end
-- print("Segment:debug ".. msg .." ...............") -- print("Segment:debug ".. msg .." ...............")
-- print("FirstPoint: "..tostring(self.firstPointX)..','..tostring(self.firstPointY)) -- print("FirstPoint: "..tostring(self.firstPointX)..','..tostring(self.firstPointY))
-- print("SecondPoint: "..tostring(self.secondPointX)..','..tostring(self.secondPointY)) -- print("SecondPoint: "..tostring(self.secondPointX)..','..tostring(self.secondPointY))
@@ -72,22 +74,22 @@ function Segment:copy()
copy.secondPointY = self.secondPointY copy.secondPointY = self.secondPointY
copy.lineWidth = self.lineWidth copy.lineWidth = self.lineWidth
copy.face = self.face copy.face = self.face
return Segment({copy.firstPointX,copy.firstPointY},{copy.secondPointX,copy.secondPointY},copy.lineWidth, copy.face) return Segment({copy.firstPointX, copy.firstPointY},
{copy.secondPointX, copy.secondPointY}, copy.lineWidth,
copy.face)
end end
function Segment:endEqualsStartOf(s) function Segment:endEqualsStartOf(s)
-- segments are linked at the end of this one -- segments are linked at the end of this one
if self.secondPointX == s.firstPointX and if self.secondPointX == s.firstPointX and self.secondPointY == s.firstPointY then
self.secondPointY == s.firstPointY then
return true return true
end end
return false return false
end end
function Segment:endEqualsEndOf(s) function Segment:endEqualsEndOf(s)
-- segments are linked at the end of this one -- segments are linked at the end of this one
if self.secondPointX == s.secondPointX and self.secondPointY == s.secondPointY then if self.secondPointX == s.secondPointX and self.secondPointY ==
return true s.secondPointY then return true end
end
return false return false
end end
@@ -129,27 +131,17 @@ function Segment:splitInTwoWithPoint(point) -- number
local s1, s2 = {} local s1, s2 = {}
if point > 0 then if point > 0 then
if self.vertical then if self.vertical then
s1 = Segment( s1 = Segment({self.firstPointX, self.firstPointY},
{self.firstPointX, self.firstPointY}, {self.secondPointX, point}, self.lineWidth, self.face)
{self.secondPointX, point}, s2 = Segment({self.firstPointX, point},
self.lineWidth, self.face {self.secondPointX, self.secondPointY}, self.lineWidth,
) self.face)
s2 = Segment(
{self.firstPointX, point},
{self.secondPointX,self.secondPointY},
self.lineWidth, self.face
)
else else
s1 = Segment( s1 = Segment({self.firstPointX, self.firstPointY},
{self.firstPointX, self.firstPointY}, {point, self.secondPointY}, self.lineWidth, self.face)
{point, self.secondPointY}, s2 = Segment({point, self.firstPointY},
self.lineWidth, self.face {self.secondPointX, self.secondPointY}, self.lineWidth,
) self.face)
s2 = Segment(
{point, self.firstPointY},
{self.secondPointX,self.secondPointY},
self.lineWidth, self.face
)
end end
else else
return nil, nil return nil, nil
@@ -181,16 +173,14 @@ function Segment:joinPerpendicular(s)
-- changes the self, not the parameter -- changes the self, not the parameter
if s.vertical then if s.vertical then
local sx = s.firstPointX local sx = s.firstPointX
if math.abs(self.firstPointX - sx) < if math.abs(self.firstPointX - sx) < math.abs(self.secondPointX - sx) then
math.abs(self.secondPointX - sx) then
self.firstPointX = sx self.firstPointX = sx
else else
self.secondPointX = sx self.secondPointX = sx
end end
else else
local sy = s.firstPointY local sy = s.firstPointY
if math.abs(self.firstPointY - sy) < if math.abs(self.firstPointY - sy) < math.abs(self.secondPointY - sy) then
math.abs(self.secondPointY - sy) then
self.firstPointY = sy self.firstPointY = sy
else else
self.secondPointY = sy self.secondPointY = sy
@@ -259,14 +249,14 @@ function Segment:compare(s)
return self.firstPointY - s.firstPointY return self.firstPointY - s.firstPointY
end end
else else
return (self.firstPointX + self.firstPointY + return (self.firstPointX + self.firstPointY + self.secondPointX +
self.secondPointX + self.secondPointY) - ( self.secondPointY) -
s.firstPointX + s.firstPointY + s.secondPointX (s.firstPointX + s.firstPointY + s.secondPointX +
+s.secondPointY) s.secondPointY)
end end
end end
function Segment:splitInThreeWithSegments(s1,s2) function Segment:splitInThreeWithSegments(s1, s2)
local p1, p2, p3, t = {} local p1, p2, p3, t = {}
if self.direction == 'down' or self.direction == 'right' then if self.direction == 'down' or self.direction == 'right' then
if s1:compare(s2) < 0 then if s1:compare(s2) < 0 then
@@ -286,23 +276,32 @@ function Segment:splitInThreeWithSegments(s1,s2)
end end
end end
return p1,p2,p3 return p1, p2, p3
end end
function Segment:insert(list) function Segment:insert(list) end
end
function Segment:pointInSegment(point) function Segment:pointInSegment(point)
-- remove "+ 8" and replace by sprite.width / 2
if self.direction == 'up' or self.direction == 'down' then if self.direction == 'up' or self.direction == 'down' then
if point[1] <= self.firstPointX + 1 and if point[1] <= self.firstPointX + 1 and point[1] >= self.firstPointX - 1 then
point[1] >= self.firstPointX - 1 then if self.direction == 'down' then
return true if point[2] >= self.firstPointY and point[2] <=
self.secondPointY then return true end
else
if point[2] >= self.secondPointY and point[2] <=
self.firstPointY then return true end
end
end end
else else
if point[2] <= self.firstPointY + 1 and if point[2] <= self.firstPointY + 1 and point[2] >= self.firstPointY - 1 then
point[2] >= self.firstPointY - 1 then if self.direction == 'right' then
return true if point[1] >= self.firstPointX and point[1] <=
self.secondPointX then return true end
else
if point[1] >= self.secondPointX and point[1] <=
self.firstPointX then return true end
end
end end
end end

View File

@@ -25,9 +25,9 @@ function Entity:changeState(name)
-- printChangeState to "..name) -- printChangeState to "..name)
end end
-- function Entity:isMoveAllowed(point) function Entity:isMoveAllowed(point)
-- return point.x, point.y return true
-- end end
function Entity:changeAnimation(name) function Entity:changeAnimation(name)
self.currentAnimation = self.animations[name] self.currentAnimation = self.animations[name]

View File

@@ -9,9 +9,11 @@ function EntityWalkState:init(entity)
end end
function EntityWalkState:update(dt) function EntityWalkState:update(dt)
-- print("EntityWalkState - Current Position:" .. tostring(self.entity.x) .. "," .. tostring(self.entity.y))
if self.entity.direction == 'up' then if self.entity.direction == 'up' then
if self.entity:isMoveAllowed({self.entity.x, self.entity.y - self.entity.walkingSpeed * dt}) then local move = {math.floor(self.entity.x + 0.5), math.floor(self.entity.y - self.entity.walkingSpeed * dt + 0.5)}
self.entity.y = self.entity.y - self.entity.walkingSpeed * dt if self.entity:isMoveAllowed(move) then
self.entity.y = move[2]
if self.entity.y + TILE_SIZE / 2 < LEVEL_RENDER_OFFSET_TOP - 2 then if self.entity.y + TILE_SIZE / 2 < LEVEL_RENDER_OFFSET_TOP - 2 then
self.entity.y = self.entity.y + self.entity.walkingSpeed * dt self.entity.y = self.entity.y + self.entity.walkingSpeed * dt
Event.dispatch('back-to-wall') Event.dispatch('back-to-wall')
@@ -19,8 +21,9 @@ function EntityWalkState:update(dt)
end end
end end
elseif self.entity.direction == 'down' then elseif self.entity.direction == 'down' then
if self.entity:isMoveAllowed({self.entity.x, self.entity.y + self.entity.walkingSpeed * dt}) then local move = {math.floor(self.entity.x + 0.5), math.floor(self.entity.y + self.entity.walkingSpeed * dt + 0.5)}
self.entity.y = self.entity.y + self.entity.walkingSpeed * dt if self.entity:isMoveAllowed(move) then
self.entity.y = move[2]
if self.entity.y + TILE_SIZE / 2 > VIRTUAL_HEIGHT - LEVEL_RENDER_OFFSET + 2 then if self.entity.y + TILE_SIZE / 2 > VIRTUAL_HEIGHT - LEVEL_RENDER_OFFSET + 2 then
self.entity.y = self.entity.y - self.entity.walkingSpeed * dt self.entity.y = self.entity.y - self.entity.walkingSpeed * dt
Event.dispatch('back-to-wall') Event.dispatch('back-to-wall')
@@ -28,8 +31,9 @@ function EntityWalkState:update(dt)
end end
end end
elseif self.entity.direction == 'right' then elseif self.entity.direction == 'right' then
if self.entity:isMoveAllowed({self.entity.x + self.entity.walkingSpeed * dt, self.entity.y}) then local move = {math.floor(self.entity.x + self.entity.walkingSpeed * dt + 0.5), math.floor(self.entity.y + 0.5)}
self.entity.x = self.entity.x + self.entity.walkingSpeed * dt if self.entity:isMoveAllowed(move) then
self.entity.x = move[1]
if self.entity.x + TILE_SIZE / 2 > VIRTUAL_WIDTH - TILE_SIZE / 2 + 2 or self.entity.previousdirection == 'left' then if self.entity.x + TILE_SIZE / 2 > VIRTUAL_WIDTH - TILE_SIZE / 2 + 2 or self.entity.previousdirection == 'left' then
self.entity.x = self.entity.x - self.entity.walkingSpeed * dt self.entity.x = self.entity.x - self.entity.walkingSpeed * dt
Event.dispatch('back-to-wall') Event.dispatch('back-to-wall')
@@ -37,8 +41,9 @@ function EntityWalkState:update(dt)
end end
end end
elseif self.entity.direction == 'left' then elseif self.entity.direction == 'left' then
if self.entity:isMoveAllowed({self.entity.x - self.entity.walkingSpeed * dt, self.entity.y}) then local move = {math.floor(self.entity.x - self.entity.walkingSpeed * dt + 0.5),math.floor(self.entity.y + 0.5)}
self.entity.x = self.entity.x - self.entity.walkingSpeed * dt if self.entity:isMoveAllowed(move) then
self.entity.x = move[1]
if self.entity.x < 0 or self.entity.previousdirection == 'right' then if self.entity.x < 0 or self.entity.previousdirection == 'right' then
self.entity.x = self.entity.x + self.entity.walkingSpeed * dt self.entity.x = self.entity.x + self.entity.walkingSpeed * dt
Event.dispatch('back-to-wall') Event.dispatch('back-to-wall')