/ld26-basecode

To get this branch, use:
bzr branch http://9ix.org/bzr/ld26-basecode

« back to all changes in this revision

Viewing changes to sprite.lua

  • Committer: Josh C
  • Date: 2013-04-23 15:34:14 UTC
  • Revision ID: josh@9ix.org-20130423153414-bj2hg9rncp1jc6xb
s/zoeplat/basecode/

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
function Sprite:displaceDir(other, dir)
3
 
   if not self.solid or self == other or not other.solid then return end
4
 
   if STRICT then assert(other:instanceOf(Sprite), 'asked to displace a non-sprite') end
5
 
 
6
 
   if other.sprites then
7
 
      -- handle groups
8
 
 
9
 
      for _, spr in pairs(other.sprites) do
10
 
         self:displace(spr, dir)
11
 
      end
12
 
   else
13
 
      -- handle sprites
14
 
      local dim = util.dim(dir)
15
 
 
16
 
      local negMove = (other[dir] - self[dir]) + other[dim]
17
 
      local posMove = (self[dir] + self[dim]) - other[dir]
18
 
 
19
 
      -- TODO: re-add hinting?
20
 
      if negMove < posMove then
21
 
         chg = - negMove
22
 
      else
23
 
         chg = posMove
24
 
      end
25
 
   end
26
 
 
27
 
   other[dir] = other[dir] + chg
28
 
end
29
 
 
30
 
function Sprite:update(elapsed)
31
 
   if self.onUpdate then self:onUpdate(elapsed) end
32
 
end
33
 
 
34
 
function Sprite:doPhysics(dir, elapsed)
35
 
   local vel = self.velocity
36
 
   local acc = self.acceleration
37
 
   local drag = self.drag
38
 
   local minVel = self.minVelocity
39
 
   local maxVel = self.maxVelocity
40
 
 
41
 
   -- check existence of properties
42
 
 
43
 
   if STRICT then
44
 
      assert(vel, 'active sprite has no velocity property')
45
 
      assert(acc, 'active sprite has no acceleration property')
46
 
      assert(drag, 'active sprite has no drag property')
47
 
      assert(minVel, 'active sprite has no minVelocity property')
48
 
      assert(maxVel, 'active sprite has no maxVelocity property')
49
 
      assert(dir=='x' or dir=='y' or dir=='rotation', 'direction should be x, y, or rotation')
50
 
   end
51
 
 
52
 
   vel.x = vel.x or 0
53
 
   vel.y = vel.y or 0
54
 
   vel.rotation = vel.rotation or 0
55
 
 
56
 
   -- physics
57
 
 
58
 
   if acc[dir] and acc[dir] ~= 0 then
59
 
      vel[dir] = vel[dir] + acc[dir] * elapsed
60
 
   else
61
 
      if drag[dir] then
62
 
         if vel[dir] > 0 then
63
 
            vel[dir] = vel[dir] - drag[dir] * elapsed
64
 
            if vel[dir] < 0 then vel[dir] = 0 end
65
 
         elseif vel[dir] < 0 then
66
 
            vel[dir] = vel[dir] + drag[dir] * elapsed
67
 
            if vel[dir] > 0 then vel[dir] = 0 end
68
 
         end
69
 
      end
70
 
   end
71
 
 
72
 
   if minVel[dir] and vel[dir] < minVel[dir] then vel[dir] = minVel[dir] end
73
 
   if maxVel[dir] and vel[dir] > maxVel[dir] then vel[dir] = maxVel[dir] end
74
 
 
75
 
   -- ugly hack for falling through floor on really slow frames
76
 
   if math.abs(vel[dir] * elapsed) > 32 then
77
 
      print('skip')
78
 
      return
79
 
   end
80
 
 
81
 
   if vel[dir] ~= 0 then self[dir] = self[dir] + vel[dir] * elapsed end
82
 
 
83
 
   if self[dir] < 0 then self[dir] = 0 end
84
 
   local edge = the.view.map[util.dim(dir)] - the.player[util.dim(dir)]
85
 
   -- TODO: take map position into account
86
 
   if self[dir] > edge then self[dir] = edge end
87
 
end