2
0
bjorn 10 жил өмнө
parent
commit
b6bc857290
2 өөрчлөгдсөн 36 нэмэгдсэн , 7 устгасан
  1. 15 0
      deps/util.lua
  2. 21 7
      pigeon.lua

+ 15 - 0
deps/util.lua

@@ -1,2 +1,17 @@
 f = {}
 f.exe = function(x, ...) if type(x) == 'function' then return x(...) end return x end
+
+function math.hlola(x1, y1, x2, y2, x3, y3, x4, y4) -- Hot line on line action (boolean).
+  local function s(x1, y1, x2, y2, x3, y3)
+    return (y3 - y1) * (x2 - x1) > (y2 - y1) * (x3 - x1)
+  end
+  return s(x1, y1, x3, y3, x4, y4) ~= s(x2, y2, x3, y3, x4, y4) and s(x1, y1, x2, y2, x3, y3) ~= s(x1, y1, x2, y2, x4, y4)
+end
+
+function math.hlora(x1, y1, x2, y2, rx, ry, rw, rh) -- Hot line on rectangle action (boolean).
+  local rxw, ryh = rx + rw, ry + rh
+  return math.hlola(x1, y1, x2, y2, rx, ry, rxw, ry)
+      or math.hlola(x1, y1, x2, y2, rx, ry, rx, ryh)
+      or math.hlola(x1, y1, x2, y2, rxw, ry, rxw, ryh)
+      or math.hlola(x1, y1, x2, y2, rx, ryh, rxw, ryh)
+end

+ 21 - 7
pigeon.lua

@@ -6,7 +6,7 @@ function Pigeon:init()
   self.w = 20
   self.h = 40
   self.speed = 100
-  self.direction = 0
+  self.direction = {x = 1, y = 0}
   self.laser = false
 
   self.lives = 3
@@ -17,19 +17,30 @@ end
 function Pigeon:update()
   if love.keyboard.isDown('left') then
     self.x = self.x - self.speed * ls.tickrate
-    self.direction = -1
+    self.direction.x = -1
   elseif love.keyboard.isDown('right') then
     self.x = self.x + self.speed * ls.tickrate
-    self.direction = 1
+    self.direction.x = 1
+  end
+
+  if love.keyboard.isDown('up') then
+    self.direction.y = -1
+  elseif love.keyboard.isDown('down') then
+    self.direction.y = 1
+  else
+    self.direction.y = 0
   end
 
   self.laser = love.keyboard.isDown(' ')
 
   if self.laser then
     local kills = 0
+
+    local x1, y1 = self.x, self.y
+    local x2, y2 = self.x + self.direction.x * 1000, self.y + self.direction.y * 1000
+
     lume.each(ctx.people, function(person, i)
-      local x1, x2 = self.x, self.x + 1000 * self.direction
-      if person.x > math.min(x1, x2) and person.x < math.max(x1, x2) then
+      if math.hlora(x1, y1, x2, y2, person.x - person.w / 2, person.y - person.h / 2, self.w, self.h) then
         kills = kills + 1
         table.remove(ctx.people, i)
       end
@@ -61,7 +72,10 @@ function Pigeon:draw()
   g.rectangle('line', self.x - self.w / 2, self.y - self.h / 2, self.w, self.h)
 
   if self.laser then
-    love.graphics.setColor(255, 0, 0)
-    love.graphics.line(self.x, self.y, self.x + 1000 * self.direction, self.y)
+    local x1, y1 = self.x, self.y
+    local x2, y2 = self.x + self.direction.x * 1000, self.y + self.direction.y * 1000
+
+    g.setColor(255, 0, 0)
+    g.line(x1, y1, x2, y2)
   end
 end