浏览代码

Spears; Decorations; Huts; etc.

bjorn 10 年之前
父节点
当前提交
779cd434cb
共有 8 个文件被更改,包括 50 次插入90 次删除
  1. 9 69
      building.lua
  2. 1 1
      caveman.lua
  3. 17 0
      dinoland.lua
  4. 4 8
      game.lua
  5. 5 4
      map.lua
  6. 8 0
      person.lua
  7. 5 7
      pigeon.lua
  8. 1 1
      spear.lua

+ 9 - 69
building.lua

@@ -1,87 +1,27 @@
 Building = class()
 
-Building.wallWidth = 16
+Building.size = 60
 
 function Building:activate()
   self.y = ctx.map.height - ctx.map.ground.height
+  self.image = data.media.graphics.dinoland['hut' .. love.math.random(1, 2)]
 
-  self.pieces = {}
-
-  -- Roof
-  local piece = {}
-  piece.body = love.physics.newBody(ctx.world, self.x, self.y - self.height - self.wallWidth / 2, 'dynamic')
-  piece.body:setUserData(self)
-  piece.shape = love.physics.newRectangleShape(self.width, self.wallWidth)
-  piece.fixture = love.physics.newFixture(piece.body, piece.shape)
-  piece.fixture:setCategory(ctx.categories.building)
-  piece.fixture:setMask(ctx.categories.debris)
-  piece.phlerp = PhysicsInterpolator(piece.body)
-  table.insert(self.pieces, piece)
-
-  -- Left wall
-  piece = {}
-  piece.body = love.physics.newBody(ctx.world, self.x - self.width / 2 + self.wallWidth / 2, self.y - self.height / 2, 'dynamic')
-  piece.body:setUserData(self)
-  piece.shape = love.physics.newRectangleShape(self.wallWidth, self.height)
-  piece.fixture = love.physics.newFixture(piece.body, piece.shape)
-  piece.fixture:setCategory(ctx.categories.building)
-  piece.fixture:setMask(ctx.categories.debris)
-  piece.phlerp = PhysicsInterpolator(piece.body)
-  table.insert(self.pieces, piece)
-
-  -- Right wall
-  piece = {}
-  piece.body = love.physics.newBody(ctx.world, self.x + self.width / 2 - self.wallWidth / 2, self.y - self.height / 2, 'dynamic')
-  piece.body:setUserData(self)
-  piece.shape = love.physics.newRectangleShape(self.wallWidth, self.height)
-  piece.fixture = love.physics.newFixture(piece.body, piece.shape)
-  piece.fixture:setCategory(ctx.categories.building)
-  piece.fixture:setMask(ctx.categories.debris)
-  piece.phlerp = PhysicsInterpolator(piece.body)
-  table.insert(self.pieces, piece)
-
-  self.destroyed = false
+  self.personTimer = 6 + love.math.random() * 3
 
   ctx.event:emit('view.register', {object = self})
 end
 
 function Building:update()
-  table.each(self.pieces, function(piece)
-    piece.phlerp:update()
+  self.personTimer = timer.rot(self.personTimer, function()
+    ctx.enemies:add(Caveman, {x = self.x, y = self.y - 20})
+    return 6 + love.math.random() * 3
   end)
-
-  if ctx.pigeon.body:getY() + ctx.pigeon.shapeSize / 2 > self.pieces[1].body:getY() - self.wallWidth / 2 then
-    --self.pieces[1].fixture:setCategory(ctx.categories.oneWayPlatform)
-  else
-    --self.pieces[1].fixture:setCategory(ctx.categories.building)
-  end
 end
 
 function Building:draw()
   local g = love.graphics
-
-  table.each(self.pieces, function(piece)
-    piece.phlerp:lerp()
-    local points = {piece.body:getWorldPoints(piece.shape:getPoints())}
-    g.setColor(255, 255, 255)
-    g.polygon('line', points)
-    piece.phlerp:delerp()
-  end)
+  local scale = self.size / self.image:getWidth()
+  g.setColor(255, 255, 255)
+  g.draw(self.image, self.x, self.y, 0, scale, scale, self.image:getWidth() / 2, self.image:getHeight())
 end
 
-function Building:collideWith(other)
-  if isa(other, Building) then return true
-  elseif isa(other, Pigeon) then self:destroy() end
-end
-
-function Building:destroy()
-  if self.destroyed then return end
-
-  self.destroyed = true
-
-  table.each(self.pieces, function(piece)
-    --piece.body:setType('dynamic')
-    piece.body:applyAngularImpulse(lume.random(1000, 2000) * (love.math.random() > .5 and 1 or -1))
-    piece.fixture:setCategory(ctx.categories.debris)
-  end)
-end

+ 1 - 1
caveman.lua

@@ -65,7 +65,7 @@ function Caveman.attack:update()
   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})
+      ctx.projectiles:add(Spear, {x = self.body:getX(), y = self.body:getY() - self.h, direction = self.direction})
     end
   else
     if self.walkTimer == 0 then

+ 17 - 0
dinoland.lua

@@ -14,6 +14,8 @@ function Dinoland:init()
   obstacle.shape = love.physics.newRectangleShape(obstacle.width, obstacle.height)
   obstacle.fixture = love.physics.newFixture(obstacle.body, obstacle.shape)
   obstacle.fixture:setCategory(ctx.categories.oneWayPlatform)
+  obstacle.body:setUserData(obstacle)
+  obstacle.tag = 'platform'
 
   table.insert(self.obstacles, obstacle)
 
@@ -23,6 +25,8 @@ function Dinoland:init()
   obstacle.shape = love.physics.newRectangleShape(obstacle.width, obstacle.height)
   obstacle.fixture = love.physics.newFixture(obstacle.body, obstacle.shape)
   obstacle.fixture:setCategory(ctx.categories.oneWayPlatform)
+  obstacle.body:setUserData(obstacle)
+  obstacle.tag = 'platform'
 
   table.insert(self.obstacles, obstacle)
 
@@ -40,3 +44,16 @@ function Dinoland:init()
     table.insert(self.decorations, {image = data.media.graphics.dinoland['shrub' .. love.math.random(1, 4)], x = obstacle.body:getX() - obstacle.width / 2 + love.math.random() * obstacle.width, y = obstacle.body:getY() - obstacle.height / 2, height = 50 + love.math.random() * 60, direction = love.math.random() > .5 and -1 or 1})
   end
 end
+
+function Dinoland:spawnHuts()
+  for i = 1, 4 do
+    local obstacle
+    if love.math.random() < .75 then
+      obstacle = self.ground
+    else
+      obstacle = self.obstacles[love.math.random(1, #self.obstacles)]
+    end
+
+    ctx.buildings:add(Building, {x = obstacle.body:getX() - obstacle.width / 2 + 40 + love.math.random() * (obstacle.width - 80), y = obstacle.body:getX() - obstacle.height / 2})
+  end
+end

+ 4 - 8
game.lua

@@ -21,18 +21,14 @@ function Game:load()
   self.hud = Hud()
   self.goal = Goal()
 
-  for i = 1, 50 do
-    self.enemies:add(Caveman, {x = 300 + love.math.random() * 1500, y = self.map.height - self.map.ground.height - 400})
-  end
-
-  --self.buildings:add(Building, {x = 300, width = 200, height = 80})
+  self.map:spawnHuts()
 
   self.world:setContactFilter(function(fixtureA, fixtureB)
     local a, b = fixtureA:getBody():getUserData(), fixtureB:getBody():getUserData()
     if not a or not b then return true end
-    f.exe(a.collideWith, a, b, fixtureA, fixtureB)
-    f.exe(b.collideWith, b, a, fixtureB, fixtureA)
-    return true
+    local resultA = f.exe(a.collideWith, a, b, fixtureA, fixtureB)
+    local resultB = f.exe(b.collideWith, b, a, fixtureB, fixtureA)
+    return resultA or resultB
   end)
 end
 

+ 5 - 4
map.lua

@@ -65,17 +65,18 @@ function Map:draw()
   g.setColor(136, 87, 44)
   physics.draw('fill', self.ground)
 
+  love.math.setRandomSeed(1)
   drawGrass(self.ground)
 
-  local seed = love.math.getRandomSeed()
-  table.each(self.obstacles, function(obstacle)
+  for i = 1, #self.obstacles do
+    local obstacle = self.obstacles[i]
     love.math.setRandomSeed(obstacle.body:getX() + obstacle.width)
     g.setColor(136, 87, 44)
     physics.draw('fill', obstacle)
 
     drawGrass(obstacle)
-  end)
-  love.math.setRandomSeed(seed)
+  end
+  love.math.setRandomSeed(love.timer.getTime())
 
   table.each(self.decorations, function(d)
     local scale = d.height / d.image:getHeight()

+ 8 - 0
person.lua

@@ -53,6 +53,14 @@ function Person:draw()
   self.phlerp:delerp()
 end
 
+function Person:collideWith(other)
+  if other.tag == 'platform' and self.body:getY() > other.body:getY() then
+    return false
+  end
+
+  return true
+end
+
 ----------------
 -- Helpers
 ----------------

+ 5 - 7
pigeon.lua

@@ -6,7 +6,7 @@ Pigeon = class()
 Pigeon.walkForce = 600
 Pigeon.maxSpeed = 350
 Pigeon.jumpForce = 3000
-Pigeon.flySpeed = 50
+Pigeon.flySpeed = 100
 Pigeon.rocketForce = 500
 Pigeon.maxFlySpeed = 300
 Pigeon.maxFuel = 25
@@ -160,11 +160,9 @@ function Pigeon:collideWith(other, myFixture)
     elseif self.state == self.air and select(2, self.body:getLinearVelocity()) > 0 and (myFixture == self.feet.left.fixture or myFixture == self.feet.right.fixture) then
       other:changeState('dead')
     end
-  elseif isa(other, Building) then
-    if self.state == self.peck and (myFixture == self.beak.top.fixture or myFixture == self.beak.bottom.fixture) then
-      --other:destroy()
-    end
   end
+
+  return true
 end
 
 ----------------
@@ -495,9 +493,9 @@ function Pigeon.laser:update()
     end)
 
     if love.keyboard.isDown('up', 'right') then
-      self.laser.direction = self.laser.direction - self.laserTurnSpeed * ls.tickrate * math.sign(math.pi / 2 - self.laser.direction) * .1
+      self.laser.direction = self.laser.direction - self.laserTurnSpeed * ls.tickrate * math.sign(math.pi / 2 - self.laser.direction) * .2
     elseif love.keyboard.isDown('down', 'left') then
-      self.laser.direction = self.laser.direction + self.laserTurnSpeed * ls.tickrate * math.sign(math.pi / 2 - self.laser.direction) * .1
+      self.laser.direction = self.laser.direction + self.laserTurnSpeed * ls.tickrate * math.sign(math.pi / 2 - self.laser.direction) * .2
     end
   end
 end

+ 1 - 1
spear.lua

@@ -40,7 +40,7 @@ function Spear:draw()
 end
 
 function Spear:collideWith(other)
-  if isa(other, Map) then
+  if isa(other, Map) or other.tag == 'platform' then
     ctx.projectiles:remove(self)
   end
 end