diff options
author | Indrajith K L | 2022-03-05 05:44:05 +0530 |
---|---|---|
committer | Indrajith K L | 2022-03-05 05:44:05 +0530 |
commit | 37bf7eab28727f428cf456c61f698e0a3be55bb0 (patch) | |
tree | a7015077b87f334821887020d2f6f70f4a5f9f28 /entities/enemy.lua | |
parent | 7683830ed25213c4e444c5587559fe803b480ea7 (diff) | |
download | YEAD-37bf7eab28727f428cf456c61f698e0a3be55bb0.tar.gz YEAD-37bf7eab28727f428cf456c61f698e0a3be55bb0.tar.bz2 YEAD-37bf7eab28727f428cf456c61f698e0a3be55bb0.zip |
* Player/ Enemy Chase
* Pickable Entity
* Enemies
Diffstat (limited to 'entities/enemy.lua')
-rw-r--r-- | entities/enemy.lua | 56 |
1 files changed, 26 insertions, 30 deletions
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 |