aboutsummaryrefslogtreecommitdiff
path: root/entities/enemy.lua
diff options
context:
space:
mode:
Diffstat (limited to 'entities/enemy.lua')
-rw-r--r--entities/enemy.lua33
1 files changed, 27 insertions, 6 deletions
diff --git a/entities/enemy.lua b/entities/enemy.lua
index f8f0681..b440b41 100644
--- a/entities/enemy.lua
+++ b/entities/enemy.lua
@@ -1,10 +1,20 @@
Class = require("libs.hump.class")
require("libs.tserial")
+anim8 = require("libs.anim8.anim8")
Enemy = Class{
init = function(self, x, y, gameWorld)
self.x = x
self.y = y
self.world = gameWorld
+ self.grid=anim8.newGrid(32,32,self.spriteSheet:getWidth(), self.spriteSheet:getHeight())
+
+ self.animations = {}
+ self.animations.down = anim8.newAnimation(self.grid('1-3', 1),0.1)
+ self.animations.left = anim8.newAnimation(self.grid('1-3', 2),0.1)
+ self.animations.right = anim8.newAnimation(self.grid('1-3', 3),0.1)
+ self.animations.up = anim8.newAnimation(self.grid('1-3', 4),0.1)
+ self.dir = self.animations.down
+ print(self.dir)
end,
x=0,
y=0,
@@ -12,12 +22,16 @@ Enemy = Class{
collider=nil,
world=nil,
chasing=false,
- dead=false
+ dead=false,
+ dir="down",
+ spriteSheet= love.graphics.newImage("assets/images/enemy.png"),
+ animations = {},
+ grid=nil
}
function Enemy:draw()
love.graphics.setColor({1,0,0})
- love.graphics.rectangle('fill', self.x, self.y, 8, 8)
+ self.dir:draw(self.spriteSheet, self.x, self.y, nil, 0.5, 0.5)
if self.collider then
end
@@ -27,19 +41,26 @@ function Enemy:setCollider()
self.collider = self.world:newRectangleCollider(self.x, self.y, 8, 8)
self.collider:setFixedRotation(true)
self.collider:setType('static')
+ self.collider:setCollisionClass('enemy')
end
function Enemy:update(dt)
+ if self.chasing == false then
+ self.dir:gotoFrame(2)
+ end
+ self.dir: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
+ if #playerColliders > 0 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
+ local angle = math.atan2(directionX,directionY)
+ print((angle*180)/3.14)
distance = math.sqrt(directionX * directionX + directionY * directionY)
local angle = math.atan2(directionY, directionX)
if distance > 20 then
@@ -51,8 +72,8 @@ function Enemy:update(dt)
end
if self.collider then
- self.collider:setX(self.x+4)
- self.collider:setY(self.y+4)
+ self.collider:setX(self.x+8)
+ self.collider:setY(self.y+8)
end
else
@@ -64,4 +85,4 @@ function Enemy:update(dt)
self.collider:destroy()
end
end
-end \ No newline at end of file
+end