movement restrictions

This commit is contained in:
2021-12-27 11:46:57 +01:00
parent 526f0c369c
commit 5f6e23b921
5 changed files with 71 additions and 23 deletions

View File

@@ -38,6 +38,15 @@ function Player:init(def, level )
end)
end
function Player:isMoveAllowed(point)
for i,segment in pairs (self.trailSegments) do
if segment:pointInSegment(point) then
return false
end
end
return true
end
function Player:changeState(state)
Entity.changeState(self,state)
end
@@ -88,6 +97,7 @@ function Player:onEdge()
end
function Player:onWalking()
-- if position of player collides with its own trail, stop motion
if #self.trail > 1 then
table.remove(self.trail)
table.remove(self.trailSegments)
@@ -113,9 +123,6 @@ function Player:onChangeDirection()
table.remove(self.trailSegments)
end
local k = #self.trailSegments
local j = #self.trail + 1
table.insert(self.trail,
{['x'] = self.x + self.width/2,['y'] = self.y + self.height/2}
)

View File

@@ -292,3 +292,19 @@ end
function Segment:insert(list)
end
function Segment:pointInSegment(point)
if self.direction == 'up' or self.direction == 'down' then
if point[1] <= self.firstPointX + 1 and
point[1] >= self.firstPointX - 1 then
return true
end
else
if point[2] <= self.firstPointY + 1 and
point[2] >= self.firstPointY - 1 then
return true
end
end
return false
end

View File

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

View File

@@ -10,27 +10,34 @@ end
function EntityWalkState:update(dt)
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
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')
self.entity:changeState('edge')
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
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')
self.entity:changeState('edge')
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
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')
self.entity:changeState('edge')
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
if self.entity.x < 0 or self.entity.previousdirection == 'right' then
self.entity.x = self.entity.x + self.entity.walkingSpeed * dt
@@ -38,6 +45,7 @@ function EntityWalkState:update(dt)
self.entity:changeState('edge')
end
end
end
if love.keyboard.isDown('left') then
if self.entity.direction ~= 'left' then
self.entity.previousDirection = self.entity.direction

View File

@@ -24,6 +24,7 @@ function PlayState:update(dt)
self.level:update(dt)
self.player:update(dt)
self:updateBalls(dt, self.level)
self:checkEndLevel(self.level)
end
function PlayState:render()
@@ -81,6 +82,18 @@ function PlayState:updateBalls(dt, level)
end
end
function PlayState:checkEndLevel (level)
if #self.balls <= 0 then
-- game over - reset level
self:gameOver()
end
-- check perimeter of level, if low enough, create new level move on to the next stage
if level:getPerimeter() < 500 then
self:nextStage()
end
end
function PlayState:renderBalls(dt)
for k,ball in pairs(self.balls) do
ball:render()