Преглед изворни кода

Spears work; Make image shorter too;

bjorn пре 10 година
родитељ
комит
b30128074a
7 измењених фајлова са 56 додато и 8 уклоњено
  1. 5 5
      caveman.lua
  2. 1 1
      game.lua
  3. 2 0
      map.lua
  4. BIN
      media/graphics/dinoland/spear.png
  5. 5 2
      person.lua
  6. 1 0
      require.lua
  7. 42 0
      spear.lua

+ 5 - 5
caveman.lua

@@ -1,7 +1,7 @@
 Caveman = extend(Person)
 
-Caveman.rangeRange = {100, 150}
-Caveman.reloadRange = {3, 4}
+Caveman.rangeRange = {200, 350}
+Caveman.reloadRange = {1, 1.5}
 
 ----------------
 -- Core
@@ -31,9 +31,7 @@ end
 -- Helpers
 ----------------
 function Caveman:reloadSpear()
-  self.reloadTimer = timer.rot(self.reloadTimer, function()
-    self.hasSpear = true
-  end)
+  self.reloadTimer = timer.rot(self.reloadTimer)
 end
 
 function Caveman:inRange()
@@ -62,10 +60,12 @@ Caveman.attack.walkRate = {.4, .6}
 function Caveman.attack:update()
   self.direction = self:directionTo(ctx.pigeon)
   self.image = data.media.graphics.dinoland[self.gender].normal
+  self:reloadSpear()
 
   if self:inRange() then
     if self.walkTimer == 0 and self.reloadTimer == 0 then
       self.reloadTimer = lume.random(unpack(self.reloadRange))
+      ctx.projectiles:add(Spear, {x = self.body:getX(), y = self.body:getY() - self.h, direction = self.direction})
     end
   else
     if self.walkTimer == 0 then

+ 1 - 1
game.lua

@@ -22,7 +22,7 @@ function Game:load()
   self.goal = Goal()
 
   for i = 1, 50 do
-    self.enemies:add(Caveman, {x = 300 + love.math.random() * 1500, y = 300})
+    self.enemies:add(Caveman, {x = 300 + love.math.random() * 1500, y = self.map.height - self.map.ground.height})
   end
 
   --self.buildings:add(Building, {x = 300, width = 200, height = 80})

+ 2 - 0
map.lua

@@ -9,6 +9,8 @@ function Map:init()
   self.ground.fixture = love.physics.newFixture(self.ground.body, self.ground.shape)
   self.ground.fixture:setCategory(ctx.categories.ground)
 
+  self.ground.body:setUserData(self)
+
   ctx.view.xmax = self.width
   ctx.view.ymax = self.height
   ctx.event:emit('view.register', {object = self})

BIN
media/graphics/dinoland/spear.png


+ 5 - 2
person.lua

@@ -27,6 +27,11 @@ function Person:activate()
   Enemy.activate(self)
 end
 
+function Person:deactivate()
+  self.body:destroy()
+  ctx.event:emit('view.unregister', {object = self})
+end
+
 function Person:update()
   self.phlerp:update()
 
@@ -86,7 +91,5 @@ function Person.dead:update()
   local x, y = self.body:getLinearVelocity()
   if (math.abs(x) < 1 and math.abs(y) < 1) or (math.abs(x) > 5000 and math.abs(y) > 5000) then
     ctx.enemies:remove(self)
-    self.body:destroy()
-    ctx.event:emit('view.unregister', {object = self})
   end
 end

+ 1 - 0
require.lua

@@ -24,6 +24,7 @@ require 'map'
 require 'dinoland'
 require 'goal'
 require 'projectile'
+require 'spear'
 
 require 'hud'
 

+ 42 - 0
spear.lua

@@ -1,5 +1,47 @@
 Spear = extend(Projectile)
 
+Spear.scale = .3
+
 function Spear:activate()
+  self.body = love.physics.newBody(ctx.world, self.x, self.y, 'dynamic')
+  self.shape = love.physics.newCircleShape(10)
+  self.fixture = love.physics.newFixture(self.body, self.shape)
+  self.fixture:setCategory(6)
+
+  self.body:setUserData(self)
+
+  self.body:applyLinearImpulse((100 + love.math.random() * 100) * self.direction, -100 - love.math.random() * 100)
+
+  self.prevx = self.x
+  self.prevy = self.y
+
+  ctx.event:emit('view.register', {object = self})
+end
+
+function Spear:deactivate()
+  self.body:destroy()
+  ctx.event:emit('view.unregister', {object = self})
+end
+
+function Spear:update()
+  self.prevx = self.body:getX()
+  self.prevy = self.body:getY()
+end
+
+function Spear:draw()
+  local g = love.graphics
+  local image = data.media.graphics.dinoland.spear
+  local x = math.lerp(self.prevx, self.body:getX(), ls.accum / ls.tickrate)
+  local y = math.lerp(self.prevy, self.body:getY(), ls.accum / ls.tickrate)
+  local angle = math.direction(self.prevx, self.prevy, self.body:getPosition())
+
+  g.setColor(255, 255, 255)
+  g.draw(image, x, y, angle, self.scale / 2, self.scale, image:getWidth(), image:getHeight() / 2)
+end
 
+function Spear:collideWith(other)
+  if isa(other, Map) then
+    print('asdf')
+    ctx.projectiles:remove(self)
+  end
 end