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 ++++++++++++++++++++++++--------------------------- entities/pickable.lua | 47 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 30 deletions(-) create mode 100644 entities/pickable.lua (limited to 'entities') 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 diff --git a/entities/pickable.lua b/entities/pickable.lua new file mode 100644 index 0000000..00dc174 --- /dev/null +++ b/entities/pickable.lua @@ -0,0 +1,47 @@ +Class = require("libs.hump.class") + +Pickable = Class{ + init = function(self, x, y, w, h, name, gameWorld) + self.x = x + self.y = y + self.width = w + self.height = h + self.image = name + self.world = gameWorld + self.image = love.graphics.newImage("assets/images/"..name..".png") + self.sfx = love.audio.newSource("assets/sfx/"..name..".wav", "static") + end, + x=0, + y=0, + width=0, + height=0, + image=nil, + world=nil, + collider=nil, + acquired=false, + sfx=nil +} + +function Pickable:setCollider(colliderData) + self.collider = self.world:newRectangleCollider(self.x, self.y, self.width, self.height) + self.collider:setFixedRotation(true) + self.collider:setType('static') + self.collider:setCollisionClass('pickable') + self.collider:setObject(colliderData) +end + +function Pickable:draw() + love.graphics.draw(self.image,self.x, self.y) +end + +function Pickable:update(dt) + if self.collider then + if self.collider:enter('player') then + self.acquired = true + self.sfx:play() + self.collider:destroy() + end + end +end + +return Pickable \ No newline at end of file -- cgit v1.2.3