From 6322712259d46fc7af82ae6271852a236f66ba6e Mon Sep 17 00:00:00 2001 From: Indrajith K L Date: Wed, 2 Mar 2022 03:36:05 +0530 Subject: * Test Enemy Added * Basic Hacky Enemy AI --- entities/enemy.lua | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++ entities/eniity.lua | 14 +++++++++++ entities/player.lua | 2 +- 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 entities/enemy.lua create mode 100644 entities/eniity.lua (limited to 'entities') diff --git a/entities/enemy.lua b/entities/enemy.lua new file mode 100644 index 0000000..7e18e95 --- /dev/null +++ b/entities/enemy.lua @@ -0,0 +1,71 @@ +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 +} + +function Enemy:draw() + love.graphics.setColor({1,0,0}) + love.graphics.rectangle('fill', self.x, self.y, 16, 16) + if self.collider then + + end +end + +function Enemy:setCollider() + self.collider = self.world:newRectangleCollider(self.x, self.y, 16, 16) + self.collider:setFixedRotation(true) +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 + self.chasing = true + for i,collider in pairs(colliders) 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 + end + end + self.collider:setX(self.x+8) + self.collider:setY(self.y+8) + else + self.chasing = false + end + end +end \ No newline at end of file diff --git a/entities/eniity.lua b/entities/eniity.lua new file mode 100644 index 0000000..1224649 --- /dev/null +++ b/entities/eniity.lua @@ -0,0 +1,14 @@ +Class = require("libs.hump.class") + +Entity = Class { + init = function(self) + end +} + +Entity:draw() +end + +Entity:update(dt) +end + +return Entity \ No newline at end of file diff --git a/entities/player.lua b/entities/player.lua index 0afdfd0..8017681 100644 --- a/entities/player.lua +++ b/entities/player.lua @@ -1,7 +1,7 @@ local Class = require("libs.hump.class"); Player = Class{ - init = function(self) + init = function(self, x, y) end } -- cgit v1.2.3