Worms no longer get stuck
This commit is contained in:
@@ -109,6 +109,9 @@ function Game:enter()
|
||||
|
||||
self.enemies = {
|
||||
Siren.new(self),
|
||||
Worm.new(self),
|
||||
Worm.new(self),
|
||||
Worm.new(self),
|
||||
Worm.new(self)
|
||||
}
|
||||
|
||||
|
||||
@@ -27,18 +27,12 @@ function Worm.new(game)
|
||||
|
||||
self.isEnemy = true
|
||||
self.tail = {}
|
||||
local startingPos = self:_findSpawnPoint()
|
||||
table.insert(self.tail, { startingPos.x, startingPos.y })
|
||||
self.game.world:add(self.tail[1], startingPos.x, startingPos.y, self.size.w, self.size.h)
|
||||
|
||||
self.nextDirections = self:_findOpenDirections()
|
||||
if #self.nextDirections > 0 then
|
||||
self.forwardDirection = lume.randomchoice(self.nextDirections)
|
||||
end
|
||||
|
||||
self.timer = Timer.new()
|
||||
self.timerHandles = {}
|
||||
|
||||
self:_spawn()
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
@@ -82,6 +76,8 @@ function Worm:draw()
|
||||
-- love.graphics.circle('fill', cx, cy, 4)
|
||||
end
|
||||
|
||||
--
|
||||
|
||||
function Worm:getCenter()
|
||||
local headX, headY = unpack(self.tail[1])
|
||||
return headX + (self.size.w / 2), headY + (self.size.h / 2)
|
||||
@@ -89,6 +85,8 @@ end
|
||||
|
||||
function Worm:getSize() return self.size.w, self.size.h end
|
||||
|
||||
--
|
||||
|
||||
function Worm:_startMoving()
|
||||
self.timerHandles.move = self.timer:every(self.stats.moveDelay, function() self:_move() end)
|
||||
end
|
||||
@@ -97,6 +95,41 @@ function Worm:_stopMoving()
|
||||
self.timer:cancel(self.timerHandles.move)
|
||||
end
|
||||
|
||||
--
|
||||
|
||||
function Worm:_spawn()
|
||||
assert(#self.tail == 0, 'Cannot spawn worm when tail length is non-zero. Got ' .. #self.tail)
|
||||
|
||||
local startingPos = self:_findSpawnPoint()
|
||||
table.insert(self.tail, { startingPos.x, startingPos.y })
|
||||
self.game.world:add(self.tail[1], startingPos.x, startingPos.y, self.size.w, self.size.h)
|
||||
|
||||
self.nextDirections = self:_findOpenDirections()
|
||||
if #self.nextDirections > 0 then
|
||||
self.forwardDirection = lume.randomchoice(self.nextDirections)
|
||||
end
|
||||
end
|
||||
|
||||
function Worm:_burrow()
|
||||
assert(#self.tail > 0, 'Cannot burrow worm when tail length is zero.')
|
||||
|
||||
self.isBurrowing = true
|
||||
|
||||
-- Remove tail
|
||||
if #self.tail > 0 then
|
||||
self.game.world:remove(self.tail[#self.tail])
|
||||
table.remove(self.tail)
|
||||
end
|
||||
|
||||
-- Done burrowing
|
||||
if #self.tail == 0 then
|
||||
self.isBurrowing = false
|
||||
self:_spawn()
|
||||
end
|
||||
end
|
||||
|
||||
--
|
||||
|
||||
function Worm:_findSpawnPoint()
|
||||
local pos = random.chooseUntil(self.game.env.nodes, function(element)
|
||||
local distanceToPlayer = vector.dist(element.x, element.y, self.game.player:getPos())
|
||||
@@ -130,7 +163,7 @@ end
|
||||
function Worm:_move()
|
||||
self.nextDirections = self:_findOpenDirections()
|
||||
|
||||
if #self.nextDirections > 0 then
|
||||
if #self.nextDirections > 0 and not self.isBurrowing then
|
||||
self.forwardDirection = lume.randomchoice(self.nextDirections)
|
||||
local dx, dy = unpack(self.forwardDirection)
|
||||
|
||||
@@ -144,7 +177,15 @@ function Worm:_move()
|
||||
|
||||
table.insert(self.tail, 1, { newHeadX, newHeadY })
|
||||
self.game.world:add(self.tail[1], newHeadX, newHeadY, self.size.w, self.size.h)
|
||||
else
|
||||
self:_burrow()
|
||||
end
|
||||
end
|
||||
|
||||
--
|
||||
|
||||
function Worm:_checkIsStuck()
|
||||
|
||||
end
|
||||
|
||||
return Worm
|
||||
|
||||
2
todo.md
2
todo.md
@@ -17,7 +17,6 @@
|
||||
|
||||
# in progress
|
||||
|
||||
- [ ] Let the worm get out of dead ends
|
||||
- [ ] Create scarecrow enemy
|
||||
- [ ] Make an entity that can wander the maze
|
||||
- [ ] Stop when the player is near
|
||||
@@ -43,3 +42,4 @@
|
||||
- [x] Make an entity that can wander the maze
|
||||
- [x] Add trailing body segments
|
||||
- [x] Add wall collision to the body
|
||||
- [x] Let the worm get out of dead ends
|
||||
|
||||
Reference in New Issue
Block a user