movement update and fixes
This commit is contained in:
@@ -10,6 +10,4 @@ Lua game inspired by an old arcade game(Qix/Gals Panic) using LOVE2D for Linux.
|
||||
|
||||
- Add more pictures (manga?)
|
||||
|
||||
- Fix Mesh
|
||||
|
||||
- Fix balls sometimes attaching to the lines
|
||||
|
||||
@@ -39,8 +39,13 @@ function Player:init(def, level )
|
||||
end
|
||||
|
||||
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
|
||||
if segment:pointInSegment(point) then
|
||||
print("Player:isMoveAllowed = false")
|
||||
print_r(segment)
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
-- require('mobdebug').start()
|
||||
|
||||
Segment = Class {}
|
||||
|
||||
local symbols = {['up'] = 207, ['down'] = 209, ['right'] = 199, ['left'] = 182}
|
||||
@@ -20,7 +19,8 @@ end
|
||||
function Segment:render()
|
||||
love.graphics.setColor(self.color.r, self.color.g, self.color.b, 255)
|
||||
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)
|
||||
|
||||
@@ -31,11 +31,15 @@ function Segment:render()
|
||||
end
|
||||
|
||||
function Segment:segmentToPoints()
|
||||
return self.firstPointX, self.firstPointY, self.secondPointX, self.secondPointY
|
||||
return self.firstPointX, self.firstPointY, self.secondPointX,
|
||||
self.secondPointY
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
function Segment:sideOfSegment(x, y)
|
||||
@@ -55,9 +59,7 @@ function Segment:sideOfSegment(x,y)
|
||||
end
|
||||
|
||||
function Segment:debug(msg)
|
||||
if not msg then
|
||||
msg = ''
|
||||
end
|
||||
if not msg then msg = '' end
|
||||
-- print("Segment:debug ".. msg .." ...............")
|
||||
-- print("FirstPoint: "..tostring(self.firstPointX)..','..tostring(self.firstPointY))
|
||||
-- print("SecondPoint: "..tostring(self.secondPointX)..','..tostring(self.secondPointY))
|
||||
@@ -72,22 +74,22 @@ function Segment:copy()
|
||||
copy.secondPointY = self.secondPointY
|
||||
copy.lineWidth = self.lineWidth
|
||||
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
|
||||
|
||||
function Segment:endEqualsStartOf(s)
|
||||
-- segments are linked at the end of this one
|
||||
if self.secondPointX == s.firstPointX and
|
||||
self.secondPointY == s.firstPointY then
|
||||
if self.secondPointX == s.firstPointX and self.secondPointY == s.firstPointY then
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
function Segment:endEqualsEndOf(s)
|
||||
-- segments are linked at the end of this one
|
||||
if self.secondPointX == s.secondPointX and self.secondPointY == s.secondPointY then
|
||||
return true
|
||||
end
|
||||
if self.secondPointX == s.secondPointX and self.secondPointY ==
|
||||
s.secondPointY then return true end
|
||||
return false
|
||||
end
|
||||
|
||||
@@ -129,27 +131,17 @@ function Segment:splitInTwoWithPoint(point) -- number
|
||||
local s1, s2 = {}
|
||||
if point > 0 then
|
||||
if self.vertical then
|
||||
s1 = Segment(
|
||||
{self.firstPointX, self.firstPointY},
|
||||
{self.secondPointX, point},
|
||||
self.lineWidth, self.face
|
||||
)
|
||||
s2 = Segment(
|
||||
{self.firstPointX, point},
|
||||
{self.secondPointX,self.secondPointY},
|
||||
self.lineWidth, self.face
|
||||
)
|
||||
s1 = Segment({self.firstPointX, self.firstPointY},
|
||||
{self.secondPointX, point}, self.lineWidth, self.face)
|
||||
s2 = Segment({self.firstPointX, point},
|
||||
{self.secondPointX, self.secondPointY}, self.lineWidth,
|
||||
self.face)
|
||||
else
|
||||
s1 = Segment(
|
||||
{self.firstPointX, self.firstPointY},
|
||||
{point, self.secondPointY},
|
||||
self.lineWidth, self.face
|
||||
)
|
||||
s2 = Segment(
|
||||
{point, self.firstPointY},
|
||||
{self.secondPointX,self.secondPointY},
|
||||
self.lineWidth, self.face
|
||||
)
|
||||
s1 = Segment({self.firstPointX, self.firstPointY},
|
||||
{point, self.secondPointY}, self.lineWidth, self.face)
|
||||
s2 = Segment({point, self.firstPointY},
|
||||
{self.secondPointX, self.secondPointY}, self.lineWidth,
|
||||
self.face)
|
||||
end
|
||||
else
|
||||
return nil, nil
|
||||
@@ -181,16 +173,14 @@ function Segment:joinPerpendicular(s)
|
||||
-- changes the self, not the parameter
|
||||
if s.vertical then
|
||||
local sx = s.firstPointX
|
||||
if math.abs(self.firstPointX - sx) <
|
||||
math.abs(self.secondPointX - sx) then
|
||||
if math.abs(self.firstPointX - sx) < math.abs(self.secondPointX - sx) then
|
||||
self.firstPointX = sx
|
||||
else
|
||||
self.secondPointX = sx
|
||||
end
|
||||
else
|
||||
local sy = s.firstPointY
|
||||
if math.abs(self.firstPointY - sy) <
|
||||
math.abs(self.secondPointY - sy) then
|
||||
if math.abs(self.firstPointY - sy) < math.abs(self.secondPointY - sy) then
|
||||
self.firstPointY = sy
|
||||
else
|
||||
self.secondPointY = sy
|
||||
@@ -259,10 +249,10 @@ function Segment:compare(s)
|
||||
return self.firstPointY - s.firstPointY
|
||||
end
|
||||
else
|
||||
return (self.firstPointX + self.firstPointY +
|
||||
self.secondPointX + self.secondPointY) - (
|
||||
s.firstPointX + s.firstPointY + s.secondPointX
|
||||
+s.secondPointY)
|
||||
return (self.firstPointX + self.firstPointY + self.secondPointX +
|
||||
self.secondPointY) -
|
||||
(s.firstPointX + s.firstPointY + s.secondPointX +
|
||||
s.secondPointY)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -289,20 +279,29 @@ function Segment:splitInThreeWithSegments(s1,s2)
|
||||
return p1, p2, p3
|
||||
end
|
||||
|
||||
function Segment:insert(list)
|
||||
|
||||
end
|
||||
function Segment:insert(list) end
|
||||
|
||||
function Segment:pointInSegment(point)
|
||||
-- remove "+ 8" and replace by sprite.width / 2
|
||||
if self.direction == 'up' or self.direction == 'down' then
|
||||
if point[1] <= self.firstPointX + 1 and
|
||||
point[1] >= self.firstPointX - 1 then
|
||||
return true
|
||||
if point[1] <= self.firstPointX + 1 and point[1] >= self.firstPointX - 1 then
|
||||
if self.direction == 'down' then
|
||||
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
|
||||
else
|
||||
if point[2] <= self.firstPointY + 1 and
|
||||
point[2] >= self.firstPointY - 1 then
|
||||
return true
|
||||
if point[2] <= self.firstPointY + 1 and point[2] >= self.firstPointY - 1 then
|
||||
if self.direction == 'right' then
|
||||
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
|
||||
|
||||
|
||||
@@ -25,9 +25,9 @@ function Entity:changeState(name)
|
||||
-- printChangeState to "..name)
|
||||
end
|
||||
|
||||
-- function Entity:isMoveAllowed(point)
|
||||
-- return point.x, point.y
|
||||
-- end
|
||||
function Entity:isMoveAllowed(point)
|
||||
return true
|
||||
end
|
||||
|
||||
function Entity:changeAnimation(name)
|
||||
self.currentAnimation = self.animations[name]
|
||||
|
||||
@@ -9,9 +9,11 @@ function EntityWalkState:init(entity)
|
||||
end
|
||||
|
||||
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:isMoveAllowed({self.entity.x, self.entity.y - self.entity.walkingSpeed * dt}) then
|
||||
self.entity.y = self.entity.y - self.entity.walkingSpeed * dt
|
||||
local move = {math.floor(self.entity.x + 0.5), math.floor(self.entity.y - self.entity.walkingSpeed * dt + 0.5)}
|
||||
if self.entity:isMoveAllowed(move) then
|
||||
self.entity.y = move[2]
|
||||
if self.entity.y + TILE_SIZE / 2 < LEVEL_RENDER_OFFSET_TOP - 2 then
|
||||
self.entity.y = self.entity.y + self.entity.walkingSpeed * dt
|
||||
Event.dispatch('back-to-wall')
|
||||
@@ -19,8 +21,9 @@ function EntityWalkState:update(dt)
|
||||
end
|
||||
end
|
||||
elseif self.entity.direction == 'down' then
|
||||
if self.entity:isMoveAllowed({self.entity.x, self.entity.y + self.entity.walkingSpeed * dt}) then
|
||||
self.entity.y = self.entity.y + self.entity.walkingSpeed * dt
|
||||
local move = {math.floor(self.entity.x + 0.5), math.floor(self.entity.y + self.entity.walkingSpeed * dt + 0.5)}
|
||||
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
|
||||
self.entity.y = self.entity.y - self.entity.walkingSpeed * dt
|
||||
Event.dispatch('back-to-wall')
|
||||
@@ -28,8 +31,9 @@ function EntityWalkState:update(dt)
|
||||
end
|
||||
end
|
||||
elseif self.entity.direction == 'right' then
|
||||
if self.entity:isMoveAllowed({self.entity.x + self.entity.walkingSpeed * dt, self.entity.y}) then
|
||||
self.entity.x = self.entity.x + self.entity.walkingSpeed * dt
|
||||
local move = {math.floor(self.entity.x + self.entity.walkingSpeed * dt + 0.5), math.floor(self.entity.y + 0.5)}
|
||||
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
|
||||
self.entity.x = self.entity.x - self.entity.walkingSpeed * dt
|
||||
Event.dispatch('back-to-wall')
|
||||
@@ -37,8 +41,9 @@ function EntityWalkState:update(dt)
|
||||
end
|
||||
end
|
||||
elseif self.entity.direction == 'left' then
|
||||
if self.entity:isMoveAllowed({self.entity.x - self.entity.walkingSpeed * dt, self.entity.y}) then
|
||||
self.entity.x = self.entity.x - self.entity.walkingSpeed * dt
|
||||
local move = {math.floor(self.entity.x - self.entity.walkingSpeed * dt + 0.5),math.floor(self.entity.y + 0.5)}
|
||||
if self.entity:isMoveAllowed(move) then
|
||||
self.entity.x = move[1]
|
||||
if self.entity.x < 0 or self.entity.previousdirection == 'right' then
|
||||
self.entity.x = self.entity.x + self.entity.walkingSpeed * dt
|
||||
Event.dispatch('back-to-wall')
|
||||
|
||||
Reference in New Issue
Block a user