diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/00-define.rb | 6 | ||||
| -rw-r--r-- | lib/01-render.rb | 14 | ||||
| -rw-r--r-- | lib/02-apply_boid_calculations.rb | 27 | ||||
| -rw-r--r-- | lib/mobs/fish.rb | 4 | ||||
| -rw-r--r-- | lib/mobs/piranha.rb | 4 | ||||
| -rw-r--r-- | lib/rules/alignment.rb | 4 | ||||
| -rw-r--r-- | lib/rules/bounds.rb | 2 | ||||
| -rw-r--r-- | lib/rules/cohesion.rb | 5 | ||||
| -rw-r--r-- | lib/rules/limit.rb | 14 | ||||
| -rw-r--r-- | lib/rules/seperation.rb | 50 | ||||
| -rw-r--r-- | lib/rules/target.rb | 23 |
11 files changed, 85 insertions, 68 deletions
diff --git a/lib/00-define.rb b/lib/00-define.rb index 1ebd8fd..4815367 100644 --- a/lib/00-define.rb +++ b/lib/00-define.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + FF::Stg.add FF::Scn.new('Default') FF::Scn.new('BoidCalculations') @@ -12,10 +14,10 @@ FelFlame::Components.new('Boids', vx: 0, vy: 0, # calculated velocity change for next frame cx: 0, cy: 0, - flipped: false) + flipped: false) FelFlame::Components.new('Fish') FF::Cmp::Fish.new FelFlame::Components.new('Piranha') FF::Cmp::Piranha.new FelFlame::Components.new('BoidVisuals', - :obj, :vect,) + :obj, :vect) diff --git a/lib/01-render.rb b/lib/01-render.rb index 5cc76e0..635b295 100644 --- a/lib/01-render.rb +++ b/lib/01-render.rb @@ -1,12 +1,14 @@ +# frozen_string_literal: true + FF::Scn::Default.add(FelFlame::Systems.new('Render', priority: 99) do FelFlame::Components::Boids.each do |boid| renderable = boid.entities[0].components[FF::Cmp::BoidVisuals][0] - if boid.flipped - pad = -renderable.obj.width - else - pad = 0 - end - renderable.vect.x1 = renderable.obj.x = boid.x + renderable.obj.width + (pad*3) + pad = if boid.flipped + -renderable.obj.width + else + 0 + end + renderable.vect.x1 = renderable.obj.x = boid.x + renderable.obj.width + (pad * 3) renderable.vect.y1 = renderable.obj.y = boid.y + renderable.obj.height renderable.vect.x2 = renderable.vect.x1 += renderable.obj.width / 2.0 renderable.vect.y2 = renderable.vect.y1 += renderable.obj.height / 2.0 diff --git a/lib/02-apply_boid_calculations.rb b/lib/02-apply_boid_calculations.rb index e6b070d..5b31b8c 100644 --- a/lib/02-apply_boid_calculations.rb +++ b/lib/02-apply_boid_calculations.rb @@ -1,18 +1,20 @@ +# frozen_string_literal: true + FF::Scn::BoidCalculations.add(FF::Sys.new('ApplyBoidCalculations', priority: 75) do - @center ||= Camera::Circle.new(color: [0.25,0.0,0.5,0.5], + @center ||= Camera::Circle.new(color: [0.25, 0.0, 0.5, 0.5], radius: 10, sectors: 10, - z: -49) - @center_vel ||= Camera::Line.new(color: [0.25,0.0,0.5,0.5], + z: -49) + @center_vel ||= Camera::Line.new(color: [0.25, 0.0, 0.5, 0.5], width: 12, - z: -50) + z: -50) unless $config.debug @center.remove @center_vel.remove end - group_velocity = [0.0,0.0] - center_mass = [0.0,0.0] + group_velocity = [0.0, 0.0] + center_mass = [0.0, 0.0] boids_count = FF::Cmp::Boids.each.to_a.count FF::Cmp::Boids.each do |boid| @@ -22,30 +24,29 @@ FF::Scn::BoidCalculations.add(FF::Sys.new('ApplyBoidCalculations', priority: 75) boid.x += boid.vx boid.y += boid.vy - if boid.vx < 0 && !boid.flipped + if boid.vx.negative? && !boid.flipped boid.flipped = true spr = boid.entities.first.components[FF::Cmp::BoidVisuals].first.obj - spr.width = -(spr.width).abs + spr.width = -spr.width.abs # flip - elsif boid.vx > 0 && boid.flipped + elsif boid.vx.positive? && boid.flipped # unflip boid.flipped = false spr = boid.entities.first.components[FF::Cmp::BoidVisuals].first.obj - spr.width = (spr.width).abs + spr.width = spr.width.abs end boid.cx = 0.0 boid.cy = 0.0 - center_mass[0] += boid.x center_mass[1] += boid.y group_velocity[0] += boid.vx group_velocity[1] += boid.vy end - group_velocity = [0.0,0.0] - center_mass = [0.0,0.0] + group_velocity = [0.0, 0.0] + center_mass = [0.0, 0.0] boids_count = FF::Cmp::Fish[0].entities.count FF::Cmp::Fish[0].entities.each do |ent| diff --git a/lib/mobs/fish.rb b/lib/mobs/fish.rb index a2ba7cd..c8f4136 100644 --- a/lib/mobs/fish.rb +++ b/lib/mobs/fish.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class Fish def self.create(x, y) FF::Ent.new( @@ -10,7 +12,7 @@ class Fish ), vect: Camera::Line.new( width: 7, - color: [1.0,0,0,0.5], + color: [1.0, 0, 0, 0.5], z: -1 ) ) diff --git a/lib/mobs/piranha.rb b/lib/mobs/piranha.rb index 8cbb2c6..c7c112d 100644 --- a/lib/mobs/piranha.rb +++ b/lib/mobs/piranha.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class Piranha def self.create(x, y) FF::Ent.new( @@ -10,7 +12,7 @@ class Piranha ), vect: Camera::Line.new( width: 7, - color: [1.0,0,0,0.5], + color: [1.0, 0, 0, 0.5], z: -1 ) ) diff --git a/lib/rules/alignment.rb b/lib/rules/alignment.rb index 4072227..0db72f5 100644 --- a/lib/rules/alignment.rb +++ b/lib/rules/alignment.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + FF::Sys.new('Alignment', priority: 50) do - group_velocity = [0.0,0.0] + group_velocity = [0.0, 0.0] boids_count = FF::Cmp::Boids.each.to_a.count FF::Cmp::Boids.each do |boid| diff --git a/lib/rules/bounds.rb b/lib/rules/bounds.rb index 5045d60..8524e02 100644 --- a/lib/rules/bounds.rb +++ b/lib/rules/bounds.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + FF::Sys.new('Bounds', priority: 50) do FF::Cmp::Boids.each do |boid| if boid.x > $config.xmax diff --git a/lib/rules/cohesion.rb b/lib/rules/cohesion.rb index 5abefd7..3905ba5 100644 --- a/lib/rules/cohesion.rb +++ b/lib/rules/cohesion.rb @@ -1,6 +1,7 @@ -FF::Sys.new('Cohesion', priority: 50) do +# frozen_string_literal: true - center_mass = [0.0,0.0] +FF::Sys.new('Cohesion', priority: 50) do + center_mass = [0.0, 0.0] boids_count = FF::Cmp::Fish[0].entities.count FF::Cmp::Fish[0].entities.each do |ent| diff --git a/lib/rules/limit.rb b/lib/rules/limit.rb index 5df8bc4..2921230 100644 --- a/lib/rules/limit.rb +++ b/lib/rules/limit.rb @@ -1,13 +1,15 @@ +# frozen_string_literal: true + # This special function is already called by apply_boid_calculations.rb # do not add or call this function elsewhere FF::Sys.new('Limit') do - unless $config.limit < 0 + unless $config.limit.negative? FF::Cmp::Boids.each do |boid| - if boid.entities[0].components[FF::Cmp::Piranha].nil? - multi = 1.0 - else - multi = 0.3 - end + multi = if boid.entities[0].components[FF::Cmp::Piranha].nil? + 1.0 + else + 0.3 + end absolute_velocity = Math.sqrt((boid.vx**2) + (boid.vy**2)) if absolute_velocity > ($config.limit * multi) diff --git a/lib/rules/seperation.rb b/lib/rules/seperation.rb index 65493e8..ad5f0fd 100644 --- a/lib/rules/seperation.rb +++ b/lib/rules/seperation.rb @@ -1,12 +1,15 @@ +# frozen_string_literal: true + FF::Sys.new('Seperation', priority: 50) do FF::Cmp::Fish[0].entities.each do |ent| boid_update = ent.components[FF::Cmp::Boids].first - newvec = [0.0,0.0] + newvec = [0.0, 0.0] FF::Cmp::Boids.each do |boid_check| next if boid_check == boid_update + if Math.sqrt(((-boid_check.x + boid_update.x)**2) + ((-boid_check.y + boid_update.y)**2)).abs < $config.seperation_distance - newvec[0] -= boid_check.x - boid_update.x - newvec[1] -= boid_check.y - boid_update.y + newvec[0] -= boid_check.x - boid_update.x + newvec[1] -= boid_check.y - boid_update.y end end boid_update.cx += newvec[0] / $config.seperation @@ -14,13 +17,14 @@ FF::Sys.new('Seperation', priority: 50) do end FF::Cmp::Piranha[0].entities.each do |ent| boid_update = ent.components[FF::Cmp::Boids].first - newvec = [0.0,0.0] + newvec = [0.0, 0.0] FF::Cmp::Piranha[0].entities.each do |ent2| boid_check = ent2.components[FF::Cmp::Boids].first next if boid_check == boid_update + if Math.sqrt(((-boid_check.x + boid_update.x)**2) + ((-boid_check.y + boid_update.y)**2)).abs < $config.seperation_distance - newvec[0] -= boid_check.x - boid_update.x - newvec[1] -= boid_check.y - boid_update.y + newvec[0] -= boid_check.x - boid_update.x + newvec[1] -= boid_check.y - boid_update.y end end boid_update.cx += newvec[0] / ($config.seperation / 10) @@ -28,21 +32,19 @@ FF::Sys.new('Seperation', priority: 50) do end end -=begin -FF::Sys.new('Seperation', priority: 50) do - FF::Cmp::Fish[0].entities.each do |ent_update| - boid_update = ent_update.components[FF::Cmp::Boids].first - newvec = [0.0,0.0] - FF::Cmp::Fish[0].entities.each do |ent_check| - boid_check = ent_update.components[FF::Cmp::Boids].first - next if boid_check == boid_update - if Math.sqrt(((-boid_check.x + boid_update.x)**2) + ((-boid_check.y + boid_update.y)**2)).abs < $config.seperation_distance - newvec[0] -= boid_check.x - boid_update.x - newvec[1] -= boid_check.y - boid_update.y - end - end - boid_update.cx += newvec[0] / $config.seperation - boid_update.cy += newvec[1] / $config.seperation - end -end -=end +# FF::Sys.new('Seperation', priority: 50) do +# FF::Cmp::Fish[0].entities.each do |ent_update| +# boid_update = ent_update.components[FF::Cmp::Boids].first +# newvec = [0.0,0.0] +# FF::Cmp::Fish[0].entities.each do |ent_check| +# boid_check = ent_update.components[FF::Cmp::Boids].first +# next if boid_check == boid_update +# if Math.sqrt(((-boid_check.x + boid_update.x)**2) + ((-boid_check.y + boid_update.y)**2)).abs < $config.seperation_distance +# newvec[0] -= boid_check.x - boid_update.x +# newvec[1] -= boid_check.y - boid_update.y +# end +# end +# boid_update.cx += newvec[0] / $config.seperation +# boid_update.cy += newvec[1] / $config.seperation +# end +# end diff --git a/lib/rules/target.rb b/lib/rules/target.rb index c0ced5c..8bd53a5 100644 --- a/lib/rules/target.rb +++ b/lib/rules/target.rb @@ -1,15 +1,14 @@ -FF::Sys.new('Target', priority: 50) do +# frozen_string_literal: true +FF::Sys.new('Target', priority: 50) do FF::Cmp::Piranha[0].entities.each do |ent| closest = [] boid = ent.components[FF::Cmp::Boids].first FF::Cmp::Fish[0].entities.each do |_fish| fish = _fish.components[FF::Cmp::Boids].first - unless (fish.x > $config.xmax) || (fish.x < $config.xmin) || (fish.y > $config.ymax) || (fish.y < $config.ymin) - if closest[0].nil? || Math.sqrt(((boid.x - fish.x)**2) + ((boid.y - fish.y)**2)).abs < closest[0] - closest[0] = Math.sqrt(((boid.x - fish.x)**2) + ((boid.y - fish.y)**2)).abs - closest[1] = fish - end + if !((fish.x > $config.xmax) || (fish.x < $config.xmin) || (fish.y > $config.ymax) || (fish.y < $config.ymin)) && (closest[0].nil? || Math.sqrt(((boid.x - fish.x)**2) + ((boid.y - fish.y)**2)).abs < closest[0]) + closest[0] = Math.sqrt(((boid.x - fish.x)**2) + ((boid.y - fish.y)**2)).abs + closest[1] = fish end end unless closest[0].nil? @@ -23,12 +22,12 @@ FF::Sys.new('Target', priority: 50) do boid = ent.components[FF::Cmp::Boids].first FF::Cmp::Piranha[0].entities.each do |_piranha| piranha = _piranha.components[FF::Cmp::Boids].first - unless Math.sqrt(((boid.x - piranha.x)**2) + ((boid.y - piranha.y)**2)).abs > $config.target_fear - #boid.cx -= ($config.target_strength / (piranha.x + boid.x)) - #boid.cy -= ($config.target_strength / (piranha.y + boid.y)) - boid.cx -= ((piranha.x - boid.x) / ($config.target_strength)) - boid.cy -= ((piranha.y - boid.y) / ($config.target_strength)) - end + next if Math.sqrt(((boid.x - piranha.x)**2) + ((boid.y - piranha.y)**2)).abs > $config.target_fear + + #boid.cx -= ($config.target_strength / (piranha.x + boid.x)) + #boid.cy -= ($config.target_strength / (piranha.y + boid.y)) + boid.cx -= ((piranha.x - boid.x) / $config.target_strength) + boid.cy -= ((piranha.y - boid.y) / $config.target_strength) #if closest[0].nil? || Math.sqrt(((boid.x - fish.x)**2) + ((boid.y + fish.y)**2)).abs < closest[0] # closest[0] = Math.sqrt(((boid.x - fish.x)**2) + ((boid.y + fish.y)**2)).abs # closest[1] = fish |
