summaryrefslogtreecommitdiff
path: root/enemy.lua
diff options
context:
space:
mode:
authorcursed22bc <admin@pixeldawn.org>2026-03-09 23:48:43 +0200
committercursed22bc <admin@pixeldawn.org>2026-03-09 23:48:43 +0200
commit6f160bac033726c9bddecee42f24616ee537c4be (patch)
treecff0cbdb52847add84c2a09e2985b259e1cbed90 /enemy.lua
parentfa90ee5c494ef7db5c779fe7eec33d6312237f9b (diff)
hacking layering
Diffstat (limited to 'enemy.lua')
-rw-r--r--enemy.lua76
1 files changed, 76 insertions, 0 deletions
diff --git a/enemy.lua b/enemy.lua
new file mode 100644
index 0000000..9732e98
--- /dev/null
+++ b/enemy.lua
@@ -0,0 +1,76 @@
+local Enemy = {}
+local Entity = require("entity")
+Enemy.__index = Enemy
+setmetatable(Enemy, { __index = Entity })
+
+function Enemy.new(spawnX, spawnY, width, height, physicsWidth, physicsHeight)
+ local self = setmetatable(Entity:new(spawnX or 0, spawnY or 0, width or 16, height or 16, physicsWidth or width or 8, physicsHeight or height or 8), Enemy)
+ self.direction = math.random(1, 2) == 1 and 1 or -1
+ self.speed = 20
+ self.turnDelay = 0.35
+ self.turning = false
+ self.turnTimer = 0
+ return self
+end
+
+function Enemy:setWorldContext(world)
+ self.world = world
+ self:enablePhysics(world.physicsWorld, "dynamic")
+end
+
+function Enemy:noPathAhead()
+ if not self.world then return false end
+
+ local frontX
+ if self.direction == 1 then
+ frontX = self.x + self.physicsWidth * 1.5 + 1
+ else
+ frontX = self.x + self.physicsWidth * 0.5 - 1
+ end
+
+ local midY = self.y + self.physicsHeight * 0.5
+ local belowY = self.y + self.physicsHeight + 1
+
+ local wallAhead = self.world:isTileSolidAtPixel(frontX, midY)
+ local floorAhead = self.world:isTileSolidAtPixel(frontX, belowY)
+
+ return wallAhead or not floorAhead
+end
+
+function Enemy:patrolPlatform(dt)
+ self:syncFromPhysicsBody()
+
+ if self.turning then
+ self.turnTimer = self.turnTimer - dt
+ if self.turnTimer <= 0 then
+ self.direction = self.direction * -1
+ self.turning = false
+ end
+ if self.body then
+ local _, vy = self.body:getLinearVelocity()
+ self.body:setLinearVelocity(0, vy)
+ end
+ return
+ end
+
+ if self:noPathAhead() then
+ self.turning = true
+ self.turnTimer = self.turnDelay
+ end
+
+ if self.body then
+ local _, vy = self.body:getLinearVelocity()
+ self.body:setLinearVelocity(self.speed * self.direction, vy)
+ end
+end
+
+function Enemy:update(dt)
+ self:patrolPlatform(dt)
+end
+
+function Enemy:draw()
+ --bug: texture is drawn shifted by half width to the left
+ love.graphics.draw(self.texture, self.x + self.physicsWidth / 2, self.y)
+end
+
+return Enemy \ No newline at end of file