# denom / denom / entitylib / entitylib.lua

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97``` ```entitylib = {} function entitylib.load() entitylib.data = {} entitylib.img = love.graphics.newImage("entitylib/enemy.png") entitylib.new_zombie_dt = 0 end function entitylib.random_zombie() entitylib.new(math.random(16,128-16)*32,math.random(16,128-16)*32) end function entitylib.dist(x1,y1,x2,y2) return math.sqrt( (x1-x2)^2 + (y1-y2)^2 ) end function entitylib.draw() for _,v in ipairs(entitylib.data) do love.graphics.draw(entitylib.img,v.x+maplib.tx,v.y+maplib.ty,0,1,1,16,16) -- love.graphics.rectangle("line",v.x+maplib.tx-16,v.y+maplib.ty-16,32,32) end end function entitylib.checkpath(range,x1,y1,x2,y2) local res = range/16 local xseg,yseg = (x2-x1)/res,(y2-y1)/res local xt,yt = x1+xseg,y1+yseg for i = 1,res do tilex,tiley = maplib.attile(xt-maplib.tx,yt-maplib.ty) if maplib.cmap[tilex] and maplib.cmap[tilex][tiley] then return false end xt = xt + xseg yt = yt + yseg end return true end function entitylib.update(dt) entitylib.new_zombie_dt = entitylib.new_zombie_dt + dt if entitylib.new_zombie_dt > 0.2 then entitylib.new_zombie_dt = 0 entitylib.random_zombie() end for i,v in ipairs(entitylib.data) do local route = entitylib.routetoplayer(v) if route == "direct" then v:run(dt,playerlib.x-maplib.tx,playerlib.y-maplib.ty) elseif route == "arc1" then v:run(dt,playerlib.x-maplib.tx,v.y) elseif route == "arc2" then v:run(dt,v.x,playerlib.y-maplib.ty) end end end function entitylib.routetoplayer(v) if entitylib.dist(playerlib.x-maplib.tx,playerlib.y-maplib.ty,v.x,v.y) < v.range then local x1,y1,x2,y2 = playerlib.x,playerlib.y,v.x+maplib.tx,v.y+maplib.ty if entitylib.checkpath(v.range,playerlib.x,playerlib.y,v.x+maplib.tx,v.y+maplib.ty) then -- Direct return "direct" elseif entitylib.checkpath(v.range,x1,y2,x2,y2) and entitylib.checkpath(v.range,x1,y1,x1,y2) then -- First Arc return "arc1" elseif entitylib.checkpath(v.range,x2,y1,x2,y2) and entitylib.checkpath(v.range,x1,y1,x2,y1) then -- Second Arc return "arc2" end end end function entitylib.new(x,y) local ent = {} ent.x = x ent.y = y ent.hp = math.random(15,25) ent.range = math.random(400,1200) ent.speed = math.random(140,180) ent.skip = false ent.run = function(self,dt,tx,ty) local direction = math.atan2(tx-self.x,self.y-ty)+math.pi/2 newx = self.x-(math.cos(direction)*dt*self.speed) newy = self.y-(math.sin(direction)*dt*self.speed) for i,v in ipairs(entitylib.data) do if entitylib.dist(self.x,self.y,v.x,v.y) < 32 then self.skip = true v.skip = false end end if not self.skip then self.x = newx self.y = newy else self.skip = false end end table.insert(entitylib.data,ent) end ```