Module: AGL::Movement

Included in:
GameObject
Defined in:
lib/minigl/movement.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bottomObject (readonly)

Returns the value of attribute bottom.



71
72
73
# File 'lib/minigl/movement.rb', line 71

def bottom
  @bottom
end

#hObject (readonly)

Returns the value of attribute h.



71
72
73
# File 'lib/minigl/movement.rb', line 71

def h
  @h
end

#leftObject (readonly)

Returns the value of attribute left.



71
72
73
# File 'lib/minigl/movement.rb', line 71

def left
  @left
end

#passableObject (readonly)

Returns the value of attribute passable.



71
72
73
# File 'lib/minigl/movement.rb', line 71

def passable
  @passable
end

#rightObject (readonly)

Returns the value of attribute right.



71
72
73
# File 'lib/minigl/movement.rb', line 71

def right
  @right
end

#speedObject (readonly)

Returns the value of attribute speed.



71
72
73
# File 'lib/minigl/movement.rb', line 71

def speed
  @speed
end

#stored_forcesObject

Returns the value of attribute stored_forces.



72
73
74
# File 'lib/minigl/movement.rb', line 72

def stored_forces
  @stored_forces
end

#topObject (readonly)

Returns the value of attribute top.



71
72
73
# File 'lib/minigl/movement.rb', line 71

def top
  @top
end

#wObject (readonly)

Returns the value of attribute w.



71
72
73
# File 'lib/minigl/movement.rb', line 71

def w
  @w
end

#xObject

Returns the value of attribute x.



72
73
74
# File 'lib/minigl/movement.rb', line 72

def x
  @x
end

#yObject

Returns the value of attribute y.



72
73
74
# File 'lib/minigl/movement.rb', line 72

def y
  @y
end

Instance Method Details

#boundsObject



74
75
76
# File 'lib/minigl/movement.rb', line 74

def bounds
  Rectangle.new @x, @y, @w, @h
end

#check_contact(obst, ramps) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/minigl/movement.rb', line 176

def check_contact obst, ramps
  @top = @bottom = @left = @right = nil
  obst.each do |o|
    x2 = @x + @w; y2 = @y + @h; x2o = o.x + o.w; y2o = o.y + o.h
    @right = o if !o.passable && x2.round(6) == o.x.round(6) && y2 > o.y && @y < y2o
    @left = o if !o.passable && @x.round(6) == x2o.round(6) && y2 > o.y && @y < y2o
    @bottom = o if y2.round(6) == o.y.round(6) && x2 > o.x && @x < x2o
    @top = o if !o.passable && @y.round(6) == y2o.round(6) && x2 > o.x && @x < x2o
  end    
  if @bottom.nil?
    ramps.each do |r|
      if r.contact? self
        @bottom = r
        break
      end
    end
  end
end

#cycle(points, cur_point, speed, obstacles = nil) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/minigl/movement.rb', line 277

def cycle points, cur_point, speed, obstacles = nil
  if obstacles
    move_carrying points[cur_point], speed, obstacles
  else
    move_free points[cur_point], speed
  end
  if @speed.x == 0 and @speed.y == 0
    if cur_point == points.length - 1; cur_point = 0
    else; cur_point += 1; end
  end
  cur_point
end

#find_down_limit(coll_list) ⇒ Object



208
209
210
211
212
213
214
# File 'lib/minigl/movement.rb', line 208

def find_down_limit coll_list
  limit = @y + @h + @speed.y
  coll_list.each do |c|
    limit = c.y if c.y < limit && c.y >= @y + @h
  end
  limit
end

#find_left_limit(coll_list) ⇒ Object



201
202
203
204
205
206
207
# File 'lib/minigl/movement.rb', line 201

def find_left_limit coll_list
  limit = @x + @speed.x
  coll_list.each do |c|
    limit = c.x + c.w if !c.passable && c.x + c.w > limit
  end
  limit
end

#find_right_limit(coll_list) ⇒ Object



194
195
196
197
198
199
200
# File 'lib/minigl/movement.rb', line 194

def find_right_limit coll_list
  limit = @x + @w + @speed.x
  coll_list.each do |c|
    limit = c.x if !c.passable && c.x < limit
  end
  limit
end

#find_up_limit(coll_list) ⇒ Object



215
216
217
218
219
220
221
# File 'lib/minigl/movement.rb', line 215

def find_up_limit coll_list
  limit = @y + @speed.y
  coll_list.each do |c|
    limit = c.y + c.h if !c.passable && c.y + c.h > limit
  end
  limit
end

#move(forces, obst, ramps) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/minigl/movement.rb', line 78

def move forces, obst, ramps
  forces.x += Game.gravity.x; forces.y += Game.gravity.y
  forces.x += @stored_forces.x; forces.y += @stored_forces.y
  @stored_forces.x = @stored_forces.y = 0

  # check_contact obst, ramps
  forces.x = 0 if (forces.x < 0 and @left) or (forces.x > 0 and @right)
  forces.y = 0 if (forces.y < 0 and @top) or (forces.y > 0 and @bottom)

  @speed.x += forces.x; @speed.y += forces.y
  @speed.x = 0 if @speed.x.abs < @min_speed.x
  @speed.y = 0 if @speed.y.abs < @min_speed.y
  @speed.x = (@speed.x <=> 0) * @max_speed.x if @speed.x.abs > @max_speed.x
  @speed.y = (@speed.y <=> 0) * @max_speed.y if @speed.y.abs > @max_speed.y
  
  ramps.each do |r|
    r.can_collide? self
  end
  
  x = @speed.x < 0 ? @x + @speed.x : @x
  y = @speed.y < 0 ? @y + @speed.y : @y
  w = @w + (@speed.x < 0 ? -@speed.x : @speed.x)
  h = @h + (@speed.y < 0 ? -@speed.y : @speed.y)
  move_bounds = Rectangle.new x, y, w, h
  coll_list = []
  obst.each do |o|
    coll_list << o if move_bounds.intersects o.bounds
  end

  if coll_list.length > 0
    up = @speed.y < 0; rt = @speed.x > 0; dn = @speed.y > 0; lf = @speed.x < 0
    if @speed.x == 0 || @speed.y == 0
      # Ortogonal
      if rt; x_lim = find_right_limit coll_list
      elsif lf; x_lim = find_left_limit coll_list
      elsif dn; y_lim = find_down_limit coll_list
      elsif up; y_lim = find_up_limit coll_list
      end
      if rt && @x + @w + @speed.x > x_lim; @x = x_lim - @w; @speed.x = 0
      elsif lf && @x + @speed.x < x_lim; @x = x_lim; @speed.x = 0
      elsif dn && @y + @h + @speed.y > y_lim; @y = y_lim - @h; @speed.y = 0
      elsif up && @y + @speed.y < y_lim; @y = y_lim; @speed.y = 0
      end
    else
      # Diagonal
      x_aim = @x + @speed.x + (rt ? @w : 0); x_lim_def = x_aim
      y_aim = @y + @speed.y + (dn ? @h : 0); y_lim_def = y_aim
      coll_list.each do |c|
        if c.passable; x_lim = x_aim
        elsif rt; x_lim = c.x
        else; x_lim = c.x + c.w
        end
        if dn; y_lim = c.y
        elsif c.passable; y_lim = y_aim
        else; y_lim = c.y + c.h
        end
      
        if c.passable
          y_lim_def = y_lim if dn && @y + @h <= y_lim && y_lim < y_lim_def
        elsif (rt && @x + @w > x_lim) || (lf && @x < x_lim)
          # Can't limit by x, will limit by y
          y_lim_def = y_lim if (dn && y_lim < y_lim_def) || (up && y_lim > y_lim_def)
        elsif (dn && @y + @h > y_lim) || (up && @y < y_lim)
          # Can't limit by y, will limit by x 
          x_lim_def = x_lim if (rt && x_lim < x_lim_def) || (lf && x_lim > x_lim_def)
        else
          xTime = 1.0 * (x_lim - @x - (@speed.x < 0 ? 0 : @w)) / @speed.x
          yTime = 1.0 * (y_lim - @y - (@speed.y < 0 ? 0 : @h)) / @speed.y
          if xTime > yTime
            # Will limit by x
            x_lim_def = x_lim if (rt && x_lim < x_lim_def) || (lf && x_lim > x_lim_def)
          elsif (dn && y_lim < y_lim_def) || (up && y_lim > y_lim_def)
            y_lim_def = y_lim
          end
        end
      end
      if x_lim_def != x_aim
        @speed.x = 0
        if lf; @x = x_lim_def
        else; @x = x_lim_def - @w
        end
      end
      if y_lim_def != y_aim
        @speed.y = 0
        if up; @y = y_lim_def
        else; @y = y_lim_def - @h
        end
      end
    end
  end
  @x += @speed.x
  @y += @speed.y
  
  ramps.each do |r|
    r.check_intersection self
  end
  check_contact obst, ramps
end

#move_carrying(aim, speed, obstacles) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/minigl/movement.rb', line 223

def move_carrying aim, speed, obstacles
  x_d = aim.x - @x; y_d = aim.y - @y
  distance = Math.sqrt(x_d**2 + y_d**2)
  @speed.x = 1.0 * x_d * speed / distance
  @speed.y = 1.0 * y_d * speed / distance
  
  x_aim = @x + @speed.x; y_aim = @y + @speed.y
  passengers = []
  obstacles.each do |o|
    if @x + @w > o.x && o.x + o.w > @x
      foot = o.y + o.h
      if foot.round(6) == @y.round(6) || @speed.y < 0 && foot < @y && foot > y_aim
        passengers << o
      end
    end
  end

  if @speed.x > 0 && x_aim >= aim.x || @speed.x < 0 && x_aim <= aim.x
    passengers.each do |p| p.x += aim.x - @x end
    @x = aim.x; @speed.x = 0
  else
    passengers.each do |p| p.x += @speed.x end
    @x = x_aim
  end
  if @speed.y > 0 && y_aim >= aim.y || @speed.y < 0 && y_aim <= aim.y
    @y = aim.y; @speed.y = 0
  else
    @y = y_aim
  end

  passengers.each do |p| p.y = @y - p.h end
end

#move_free(aim, speed) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/minigl/movement.rb', line 256

def move_free aim, speed
  x_d = aim.x - @x; y_d = aim.y - @y
  distance = Math.sqrt(x_d**2 + y_d**2)
  @speed.x = 1.0 * x_d * speed / distance
  @speed.y = 1.0 * y_d * speed / distance

  if (@speed.x < 0 and @x + @speed.x <= aim.x) or (@speed.x >= 0 and @x + @speed.x >= aim.x)
    @x = aim.x
    @speed.x = 0
  else
    @x += @speed.x
  end

  if (@speed.y < 0 and @y + @speed.y <= aim.y) or (@speed.y >= 0 and @y + @speed.y >= aim.y)
    @y = aim.y
    @speed.y = 0
  else
    @y += @speed.y
  end
end