diff options
Diffstat (limited to 'entities/enemy.lua')
-rw-r--r-- | entities/enemy.lua | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/entities/enemy.lua b/entities/enemy.lua new file mode 100644 index 0000000..7e18e95 --- /dev/null +++ b/entities/enemy.lua @@ -0,0 +1,71 @@ +Class = require("libs.hump.class") +require("libs.tserial") +Enemy = Class{ + init = function(self, x, y, gameWorld) + self.x = x + self.y = y + self.world = gameWorld + end, + x=0, + y=0, + speed=60, + collider=nil, + world=nil, + chasing=false +} + +function Enemy:draw() + love.graphics.setColor({1,0,0}) + love.graphics.rectangle('fill', self.x, self.y, 16, 16) + if self.collider then + + end +end + +function Enemy:setCollider() + self.collider = self.world:newRectangleCollider(self.x, self.y, 16, 16) + self.collider:setFixedRotation(true) +end + +function Enemy:update(dt) + if self.collider then + -- Do the Query + local colliders = self.world:queryCircleArea(self.x, self.y, 100,{'player'}) + local vx = 0 + local vy = 0 + if #colliders then + self.chasing = true + for i,collider in pairs(colliders) do + local playerObject = collider:getObject() + + if playerObject.dir=="up" then + vy = self.speed * -1 + end + + if playerObject.dir=="left" then + vx = self.speed * -1 + end + + if playerObject.dir=="down" then + vy = self.speed + end + + if playerObject.dir=="right" then + vx = self.speed + end + + directionX = playerObject.x - self.x + directionY = playerObject.y - self.y + distance = math.sqrt(directionX * directionX + directionY * directionY) + if distance >16 then + self.x = self.x + directionX / distance * self.speed * dt + self.y = self.y + directionY / distance * self.speed * dt + end + end + self.collider:setX(self.x+8) + self.collider:setY(self.y+8) + else + self.chasing = false + end + end +end
\ No newline at end of file |