aboutsummaryrefslogtreecommitdiff
path: root/entities/enemy.lua
blob: f8f068172096af03714defd4526ae80814ec6068 (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
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,
    dead=false
}

function Enemy:draw()
    love.graphics.setColor({1,0,0})
    love.graphics.rectangle('fill', self.x, self.y, 8, 8)
    if self.collider then
        
    end
end

function Enemy:setCollider()
    self.collider = self.world:newRectangleCollider(self.x, self.y, 8, 8)
    self.collider:setFixedRotation(true)
    self.collider:setType('static')
end

function Enemy:update(dt)
    if self.collider then
        local playerColliders = self.world:queryCircleArea(self.x, self.y, 100,{'player'})
        local enemyColliders = self.world:queryCircleArea(self.x+4, self.y+4, 8,{'enemy'})
        if #playerColliders then
            self.chasing = true
            for i,collider in pairs(playerColliders) do
                local playerObject = collider:getObject()
                
                directionX = playerObject.x - self.x
                directionY = playerObject.y - self.y
                distance = math.sqrt(directionX * directionX + directionY * directionY)
                local angle = math.atan2(directionY, directionX)
                if distance > 20  then
                    -- self.x = self.x + directionX / distance * self.speed * dt
                    -- self.y = self.y + directionY / distance * self.speed * dt
                    self.x = self.x + self.speed * math.cos(angle) * dt
                    self.y = self.y + self.speed * math.sin(angle) * dt
                end
            end
            
            if self.collider then
                self.collider:setX(self.x+4)
                self.collider:setY(self.y+4)    
            end
            
        else
            self.chasing = false
        end

        if self.collider:enter('player') then
            -- self.dead = true
            self.collider:destroy()
        end
    end
end