diff options
| author | realtradam <[email protected]> | 2021-08-09 14:28:43 -0400 |
|---|---|---|
| committer | realtradam <[email protected]> | 2021-08-09 14:28:43 -0400 |
| commit | f80fd9ea0e340db6286feb014e437c9ca0c93dd0 (patch) | |
| tree | 46446d902eb7f523f1542105162e46ea00b032f8 | |
| parent | c1eb3f4c18404d4921adc3d38dfa7438a47f4aa0 (diff) | |
| download | ruboids-f80fd9ea0e340db6286feb014e437c9ca0c93dd0.tar.gz ruboids-f80fd9ea0e340db6286feb014e437c9ca0c93dd0.zip | |
.
| -rw-r--r-- | lib/00-define.rb | 5 | ||||
| -rw-r--r-- | lib/01-render.rb | 9 | ||||
| -rw-r--r-- | lib/02-apply_boid_calculations.rb | 12 | ||||
| -rw-r--r-- | lib/rules/limit.rb | 10 | ||||
| -rw-r--r-- | lib/rules/seperation.rb | 17 | ||||
| -rw-r--r-- | lib/rules/target.rb | 46 | ||||
| -rw-r--r-- | run.rb | 28 |
7 files changed, 93 insertions, 34 deletions
diff --git a/lib/00-define.rb b/lib/00-define.rb index 930f56f..1ebd8fd 100644 --- a/lib/00-define.rb +++ b/lib/00-define.rb @@ -11,10 +11,11 @@ FelFlame::Components.new('Boids', # current velocity vx: 0, vy: 0, # calculated velocity change for next frame - cx: 0, cy: 0) + cx: 0, cy: 0, + 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 a4542e9..5cc76e0 100644 --- a/lib/01-render.rb +++ b/lib/01-render.rb @@ -1,12 +1,17 @@ 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] - renderable.vect.x1 = renderable.obj.x = boid.x + renderable.obj.width + if boid.flipped + pad = -renderable.obj.width + else + pad = 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 - renderable.vect.x2 += (boid.vx * 10.0) + renderable.vect.x2 += (boid.vx * 10.0) renderable.vect.y2 += (boid.vy * 10.0) end Camera._redraw diff --git a/lib/02-apply_boid_calculations.rb b/lib/02-apply_boid_calculations.rb index ec3f2de..e6b070d 100644 --- a/lib/02-apply_boid_calculations.rb +++ b/lib/02-apply_boid_calculations.rb @@ -22,6 +22,18 @@ FF::Scn::BoidCalculations.add(FF::Sys.new('ApplyBoidCalculations', priority: 75) boid.x += boid.vx boid.y += boid.vy + if boid.vx < 0 && !boid.flipped + boid.flipped = true + spr = boid.entities.first.components[FF::Cmp::BoidVisuals].first.obj + spr.width = -(spr.width).abs + # flip + elsif boid.vx > 0 && boid.flipped + # unflip + boid.flipped = false + spr = boid.entities.first.components[FF::Cmp::BoidVisuals].first.obj + spr.width = (spr.width).abs + end + boid.cx = 0.0 boid.cy = 0.0 diff --git a/lib/rules/limit.rb b/lib/rules/limit.rb index dba9f96..b2a8f32 100644 --- a/lib/rules/limit.rb +++ b/lib/rules/limit.rb @@ -3,10 +3,16 @@ FF::Sys.new('Limit') do unless $config.limit < 0 FF::Cmp::Boids.each do |boid| + if boid.entities[0].components[FF::Cmp::Piranha].nil? + multi = 1.0 + else + multi = 0.3 + end + absolute_velocity = Math.sqrt((boid.vx**2) + (boid.vy**2)) if absolute_velocity > $config.limit - boid.vx = (boid.vx / absolute_velocity) * $config.limit - boid.vy = (boid.vy / absolute_velocity) * $config.limit + boid.vx = (boid.vx / absolute_velocity) * $config.limit * multi + boid.vy = (boid.vy / absolute_velocity) * $config.limit * multi end end end diff --git a/lib/rules/seperation.rb b/lib/rules/seperation.rb index 20fd2e6..65493e8 100644 --- a/lib/rules/seperation.rb +++ b/lib/rules/seperation.rb @@ -1,5 +1,6 @@ FF::Sys.new('Seperation', priority: 50) do - FF::Cmp::Boids.each do |boid_update| + FF::Cmp::Fish[0].entities.each do |ent| + boid_update = ent.components[FF::Cmp::Boids].first newvec = [0.0,0.0] FF::Cmp::Boids.each do |boid_check| next if boid_check == boid_update @@ -11,6 +12,20 @@ FF::Sys.new('Seperation', priority: 50) do boid_update.cx += newvec[0] / $config.seperation boid_update.cy += newvec[1] / $config.seperation end + FF::Cmp::Piranha[0].entities.each do |ent| + boid_update = ent.components[FF::Cmp::Boids].first + 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 + end + end + boid_update.cx += newvec[0] / ($config.seperation / 10) + boid_update.cy += newvec[1] / ($config.seperation / 10) + end end =begin diff --git a/lib/rules/target.rb b/lib/rules/target.rb index f74bf7d..9177845 100644 --- a/lib/rules/target.rb +++ b/lib/rules/target.rb @@ -1,25 +1,41 @@ 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 + 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 + end + boid.cx += ((closest[1].x - boid.x) / $config.target_strength) + boid.cy += ((closest[1].y - boid.y) / $config.target_strength) + end + FF::Cmp::Fish[0].entities.each do |ent| - farthest = [] - boid = ent.component[FF::Cmp::Boids].first + closest = [] + boid = ent.components[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 + piranha = _piranha.components[FF::Cmp::Boids].first + unless Math.sqrt(((boid.x - piranha.x)**2) + ((boid.y + piranha.y)**2)).abs > 250 + boid.cx -= ($config.target_strength / (piranha.x - boid.x)) + boid.cy -= ($config.target_strength / (piranha.y - boid.y)) + #boid.cy -= ((piranha.y - boid.y) / (1.0 / $config.target_strength)) end + #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 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::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 @@ -6,7 +6,7 @@ require 'felflame' # change values here to see a change in the simulation FF::Cmp.new('SingletonConfig', # Show vectors - debug: true, + debug: false, # If the camera should follow the "center mass" follow: false, @@ -19,7 +19,7 @@ FF::Cmp.new('SingletonConfig', # Higher is stronger bounds_strength: 1.0, # What the bounds are - xmax: 480.0, xmin: -580.0, + xmax: 450.0, xmin: -580.0, ymax: 250.0, ymin: -340.0, # How much the boids try to pull together @@ -28,17 +28,17 @@ FF::Cmp.new('SingletonConfig', # How much the boids push away from eachother # Smaller is stronger - seperation: 375.0, + seperation: 60.0, # What the range of seperating should be - seperation_distance: 150.0, + seperation_distance: 50.0, # How strong their vector alignment should be # Smaller is stronger alignment: 1000.0, - # How much they try to follow their target(your mouse cursor) - # Smaller is stronger - target_strength: 2500.0, + # How much they try to follow their target + # Larger is strongzer + target_strength: 500.0, # These are later set by the mouse position target_x: 0, target_y: 0) @@ -54,7 +54,7 @@ Dir[File.join(__dir__, 'lib/**', '*.rb')].sort.each { |file| require file } FF::Scn::BoidCalculations.add FF::Sys::Cohesion #FF::Scn::BoidCalculations.add FF::Sys::Alignment FF::Scn::BoidCalculations.add FF::Sys::Seperation -#FF::Scn::BoidCalculations.add FF::Sys::Target +FF::Scn::BoidCalculations.add FF::Sys::Target FF::Scn::BoidCalculations.add FF::Sys::Bounds FF::Stg.add FF::Scn::BoidCalculations @@ -64,12 +64,12 @@ class GameWindow < Ruby2D::Window super Camera::Image.new('assets/Background.png', x: -get(:width)+57, y: -get(:height)+97, z: -99) randspot = ((-get(:height) / 2)..(get(:height)/2)).to_a - 7.times do + 50.times do Fish.create(randspot.sample.to_f, randspot.sample.to_f) end - 2.times do - Piranha.create(randspot.sample.to_f, randspot.sample.to_f) - end + #2.times do + # Piranha.create(randspot.sample.to_f, randspot.sample.to_f) + #end unless $config.debug FF::Cmp::BoidVisuals.each do |boid| boid.vect.remove @@ -80,6 +80,9 @@ class GameWindow < Ruby2D::Window def update $config.target_x = Camera.coordinate_to_worldspace(get(:mouse_x), get(:mouse_y))[0] $config.target_y = Camera.coordinate_to_worldspace(get(:mouse_y), get(:mouse_y))[1] + if key_down('space') + Piranha.create($config.target_x, $config.target_y) + end FF::Stage.call Camera.y += 1 if key_held('s') Camera.y -= 1 if key_held('w') @@ -90,6 +93,7 @@ class GameWindow < Ruby2D::Window end def render + puts get(:fps) end end |
