Module: DXOpal::Sprite::CollisionCheck::ClassMethods
- Included in:
- DXOpal::Sprite
- Defined in:
- lib/dxopal/sprite/collision_check.rb
Instance Method Summary collapse
-
#check(offences, defences, shot = :shot, hit = :hit) ⇒ Object
Run collision checking for set of sprites - offences: Sprite or [Sprite] - defences: Sprite or [Sprite] - shot: method name - hit: method name.
Instance Method Details
#check(offences, defences, shot = :shot, hit = :hit) ⇒ Object
Run collision checking for set of sprites
-
offences: Sprite or [Sprite]
-
defences: Sprite or [Sprite]
-
shot: method name
-
hit: method name
This method has two modes.
-
If `offences` and `defences` are the same, collision check will be performed on each pair from the array. Method `hit` is called on the each sprite, with the other sprite as an argument.
-
Otherwise, collision check will be performed on each pair of offence sprite and defence sprite. In this case, method `shot` is called on the offence sprite and method `hit` is called on the defence sprite, with the other sprite as an argument.
TODO: return true if any collition is detected TODO: skip collision checking if shot/hit returned `:discard`
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/dxopal/sprite/collision_check.rb', line 26 def check(offences, defences, shot=:shot, hit=:hit) offences = Array(offences) defences = Array(defences) i = j = 0 # trick to use i, j in the `#{}` if offences.equal?(defences) # any-vs-any mode sprites = offences.select{|x| x.is_a?(Sprite)} n = sprites.length %x{ for (var i=0; i<n; i++) { for (var j=i+1; j<n; j++) { if (#{sprites[i] === sprites[j]}) { #{sprites[i].__send__(hit)}; #{sprites[j].__send__(hit)}; } } } } else # offence-vs-defence mode %x{ for (var i=0; i<offences.length; i++) { for (var j=0; j<defences.length; j++) { if (#{offences[i] === defences[j]}) { #{offences[i].__send__(shot, defences[j])}; #{defences[j].__send__(hit, offences[i])}; } } } } end end |