From 37bf7eab28727f428cf456c61f698e0a3be55bb0 Mon Sep 17 00:00:00 2001 From: Indrajith K L Date: Sat, 5 Mar 2022 05:44:05 +0530 Subject: * Player/ Enemy Chase * Pickable Entity * Enemies --- entities/enemy.lua | 56 +++++++++++++++++++++++++----------------------------- 1 file changed, 26 insertions(+), 30 deletions(-) (limited to 'entities/enemy.lua') diff --git a/entities/enemy.lua b/entities/enemy.lua index 7e18e95..f8f0681 100644 --- a/entities/enemy.lua +++ b/entities/enemy.lua @@ -11,61 +11,57 @@ Enemy = Class{ speed=60, collider=nil, world=nil, - chasing=false + chasing=false, + dead=false } function Enemy:draw() love.graphics.setColor({1,0,0}) - love.graphics.rectangle('fill', self.x, self.y, 16, 16) + 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, 16, 16) + 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 - -- Do the Query - local colliders = self.world:queryCircleArea(self.x, self.y, 100,{'player'}) - local vx = 0 - local vy = 0 - if #colliders 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(colliders) do + for i,collider in pairs(playerColliders) 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 + 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 - self.collider:setX(self.x+8) - self.collider:setY(self.y+8) + + 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 \ No newline at end of file -- cgit v1.2.3