/ld27

To get this branch, use:
bzr branch http://9ix.org/bzr/ld27

« back to all changes in this revision

Viewing changes to sprite.lua

  • Committer: Josh C
  • Date: 2013-08-24 03:54:55 UTC
  • Revision ID: josh@9ix.org-20130824035455-upi2mnssckafqxgs
map loading, super basic movement

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
 
   self:doPhysics('x', elapsed)
32
 
   self:doPhysics('y', elapsed)
33
 
   self:doPhysics('rotation', elapsed)
34
 
 
35
 
   if self.onUpdate then self:onUpdate(elapsed) end
36
 
end
37
 
 
38
 
function Sprite:doPhysics(dir, elapsed)
39
 
   local vel = self.velocity
40
 
   local acc = self.acceleration
41
 
   local drag = self.drag
42
 
   local minVel = self.minVelocity
43
 
   local maxVel = self.maxVelocity
44
 
 
45
 
   -- check existence of properties
46
 
 
47
 
   if STRICT then
48
 
      assert(vel, 'active sprite has no velocity property')
49
 
      assert(acc, 'active sprite has no acceleration property')
50
 
      assert(drag, 'active sprite has no drag property')
51
 
      assert(minVel, 'active sprite has no minVelocity property')
52
 
      assert(maxVel, 'active sprite has no maxVelocity property')
53
 
      assert(dir=='x' or dir=='y' or dir=='rotation', 'direction should be x, y, or rotation')
54
 
   end
55
 
 
56
 
   vel.x = vel.x or 0
57
 
   vel.y = vel.y or 0
58
 
   vel.rotation = vel.rotation or 0
59
 
 
60
 
   -- physics
61
 
 
62
 
   if acc[dir] and acc[dir] ~= 0 then
63
 
      vel[dir] = vel[dir] + acc[dir] * elapsed
64
 
   else
65
 
      if drag[dir] then
66
 
         if vel[dir] > 0 then
67
 
            vel[dir] = vel[dir] - drag[dir] * elapsed
68
 
            if vel[dir] < 0 then vel[dir] = 0 end
69
 
         elseif vel[dir] < 0 then
70
 
            vel[dir] = vel[dir] + drag[dir] * elapsed
71
 
            if vel[dir] > 0 then vel[dir] = 0 end
72
 
         end
73
 
      end
74
 
   end
75
 
 
76
 
   if minVel[dir] and vel[dir] < minVel[dir] then vel[dir] = minVel[dir] end
77
 
   if maxVel[dir] and vel[dir] > maxVel[dir] then vel[dir] = maxVel[dir] end
78
 
 
79
 
   -- ugly hack for falling through floor on really slow frames
80
 
   if math.abs(vel[dir] * elapsed) > 16 then
81
 
      print('skip')
82
 
      return
83
 
   end
84
 
 
85
 
   if vel[dir] ~= 0 then self[dir] = self[dir] + vel[dir] * elapsed end
86
 
 
87
 
   if the.activeMaze and (dir == 'x' or dir == 'y') then -- doesn't make sense for rotation
88
 
      if self[dir] < 0 then self[dir] = 0 end
89
 
      local edge = the.activeMaze[util.dim(dir)] - the.player[util.dim(dir)]
90
 
      -- TODO: take map position into account
91
 
      if self[dir] > edge then self[dir] = edge end
92
 
   end
93
 
end