diff options
Diffstat (limited to 'lib/rules')
| -rw-r--r-- | lib/rules/alignment.rb | 4 | ||||
| -rw-r--r-- | lib/rules/cohesion.rb | 12 | ||||
| -rw-r--r-- | lib/rules/limit.rb | 8 | ||||
| -rw-r--r-- | lib/rules/seperation.rb | 25 | ||||
| -rw-r--r-- | lib/rules/target.rb | 28 |
5 files changed, 60 insertions, 17 deletions
diff --git a/lib/rules/alignment.rb b/lib/rules/alignment.rb index b30683c..4072227 100644 --- a/lib/rules/alignment.rb +++ b/lib/rules/alignment.rb @@ -14,7 +14,7 @@ FF::Sys.new('Alignment', priority: 50) do move_boid[0] /= boids_count - 1.0 move_boid[1] /= boids_count - 1.0 - boid_update.cx += (move_boid[0] - boid_update.vx) / $alignment - boid_update.cy += (move_boid[1] - boid_update.vy) / $alignment + boid_update.cx += (move_boid[0] - boid_update.vx) / $config.alignment + boid_update.cy += (move_boid[1] - boid_update.vy) / $config.alignment end end diff --git a/lib/rules/cohesion.rb b/lib/rules/cohesion.rb index efbd71d..5abefd7 100644 --- a/lib/rules/cohesion.rb +++ b/lib/rules/cohesion.rb @@ -1,21 +1,23 @@ FF::Sys.new('Cohesion', priority: 50) do center_mass = [0.0,0.0] - boids_count = FF::Cmp::Boids.each.to_a.count + boids_count = FF::Cmp::Fish[0].entities.count - FF::Cmp::Boids.each do |boid| + FF::Cmp::Fish[0].entities.each do |ent| + boid = ent.components[FF::Cmp::Boids].first center_mass[0] += boid.x center_mass[1] += boid.y end - FF::Cmp::Boids.each do |boid_update| + FF::Cmp::Fish[0].entities.each do |ent| + boid_update = ent.components[FF::Cmp::Boids].first move_boid = center_mass.dup move_boid[0] -= boid_update.x move_boid[1] -= boid_update.y move_boid[0] /= boids_count - 1.0 move_boid[1] /= boids_count - 1.0 - boid_update.cx += (move_boid[0] - boid_update.x) / $cohesion - boid_update.cy += (move_boid[1] - boid_update.y) / $cohesion + boid_update.cx += (move_boid[0] - boid_update.x) / $config.cohesion + boid_update.cy += (move_boid[1] - boid_update.y) / $config.cohesion end end diff --git a/lib/rules/limit.rb b/lib/rules/limit.rb index c454de4..dba9f96 100644 --- a/lib/rules/limit.rb +++ b/lib/rules/limit.rb @@ -1,12 +1,12 @@ # 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 $limit < 0 + unless $config.limit < 0 FF::Cmp::Boids.each do |boid| absolute_velocity = Math.sqrt((boid.vx**2) + (boid.vy**2)) - if absolute_velocity > $limit - boid.vx = (boid.vx / absolute_velocity) * $limit - boid.vy = (boid.vy / absolute_velocity) * $limit + if absolute_velocity > $config.limit + boid.vx = (boid.vx / absolute_velocity) * $config.limit + boid.vy = (boid.vy / absolute_velocity) * $config.limit end end end diff --git a/lib/rules/seperation.rb b/lib/rules/seperation.rb index 31039f3..20fd2e6 100644 --- a/lib/rules/seperation.rb +++ b/lib/rules/seperation.rb @@ -3,12 +3,31 @@ FF::Sys.new('Seperation', priority: 50) do 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 < $seperation_distance + 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] / $seperation - boid_update.cy += newvec[1] / $seperation + boid_update.cx += newvec[0] / $config.seperation + boid_update.cy += newvec[1] / $config.seperation 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 diff --git a/lib/rules/target.rb b/lib/rules/target.rb index ffee93a..f74bf7d 100644 --- a/lib/rules/target.rb +++ b/lib/rules/target.rb @@ -1,6 +1,28 @@ FF::Sys.new('Target', priority: 50) do - FF::Cmp::Boids.each do |boid| - boid.cx += ($x_target - boid.x) / $target_strength - boid.cy += ($y_target - boid.y) / $target_strength + + FF::Cmp::Fish[0].entities.each do |ent| + farthest = [] + boid = ent.component[FF::Cmp::Boids].first + FF::Cmp::Piranha[0].entities.each do |_piranha| + piranha = _piranha.component[FF::Cmp::Boids].first + if farthest[0].nil? || Math.sqrt(((boid.x - piranha.x)**2) + ((boid.y + piranha.y)**2)).abs < farthest[0] + farthest[0] = Math.sqrt(((boid.x - piranha.x)**2) + ((boid.y + piranha.y)**2)).abs + farthest[1] = piranha + end + end + boid.cx -= (2 * (piranha.x - boid.x) / $config.target_strength) + boid.cy -= (2 * (piranha.y - boid.y) / $config.target_strength) end + + # FF::Cmp::Piranha[0].entities.each do |ent| + # boid = ent.components[FF::Cmp::Boids].first + # boid.cx += ($config.target_x - boid.x) / $config.target_strength + # boid.cy += ($config.target_y - boid.y) / $config.target_strength + # #find closest fish + # end + + #FF::Cmp::Boids.each do |boid| + # boid.cx += ($config.target_x - boid.x) / $config.target_strength + # boid.cy += ($config.target_y - boid.y) / $config.target_strength + #end end |
