aboutsummaryrefslogtreecommitdiff
path: root/entities/enemy.lua
blob: 7e18e9509fe92e8fd0a306d3631ee0fc2c21e97d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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