diff options
| author | arngo <[email protected]> | 2021-12-17 21:19:15 -0500 |
|---|---|---|
| committer | arngo <[email protected]> | 2021-12-17 21:19:15 -0500 |
| commit | b8b008c726810dc5e3f18b5637a5e5c882da7dd4 (patch) | |
| tree | a64630f800ca3cdd304cf664e27b03970c71aace | |
| parent | c85f6533efb50981ef8cf59e840a1f8b58b4deef (diff) | |
| download | SteelWings-b8b008c726810dc5e3f18b5637a5e5c882da7dd4.tar.gz SteelWings-b8b008c726810dc5e3f18b5637a5e5c882da7dd4.zip | |
add cohesion and debug stuff
| -rw-r--r-- | app/components/debug/debug_vector_arrow.rb | 2 | ||||
| -rw-r--r-- | app/components/rules/cohesion.rb | 2 | ||||
| -rw-r--r-- | app/main.rb | 6 | ||||
| -rw-r--r-- | app/scenes/scenes.rb | 3 | ||||
| -rw-r--r-- | app/systems/debug/debug_render_vector_arrow.rb | 16 | ||||
| -rw-r--r-- | app/systems/rules/bounds.rb | 2 | ||||
| -rw-r--r-- | app/systems/rules/cohesion.rb | 27 | ||||
| -rw-r--r-- | app/systems/rules/reset_change_vector.rb | 8 | ||||
| -rw-r--r-- | app/systems/update_boid_position.rb | 11 | ||||
| -rw-r--r-- | app/systems/update_boid_sprite.rb | 7 | ||||
| -rw-r--r-- | app/tick.rb | 33 |
11 files changed, 107 insertions, 10 deletions
diff --git a/app/components/debug/debug_vector_arrow.rb b/app/components/debug/debug_vector_arrow.rb new file mode 100644 index 0000000..e65161d --- /dev/null +++ b/app/components/debug/debug_vector_arrow.rb @@ -0,0 +1,2 @@ +FF::Cmp.new('DebugVectorArrow', + length: 1) diff --git a/app/components/rules/cohesion.rb b/app/components/rules/cohesion.rb new file mode 100644 index 0000000..6acd5ea --- /dev/null +++ b/app/components/rules/cohesion.rb @@ -0,0 +1,2 @@ +FF::Cmp.new('BoidsCohesion', + strength: 100) diff --git a/app/main.rb b/app/main.rb index cf31f77..1ab9e32 100644 --- a/app/main.rb +++ b/app/main.rb @@ -5,9 +5,15 @@ require 'app/scenes/scenes.rb' require 'app/components/sprite.rb' require 'app/components/boid.rb' require 'app/components/rules/bounds.rb' +require 'app/components/rules/cohesion.rb' +require 'app/components/debug/debug_vector_arrow.rb' require 'app/systems/render.rb' require 'app/systems/update_boid_sprite.rb' +require 'app/systems/update_boid_position.rb' require 'app/systems/rules/bounds.rb' +require 'app/systems/rules/cohesion.rb' +require 'app/systems/rules/reset_change_vector.rb' +require 'app/systems/debug/debug_render_vector_arrow.rb' require 'app/tick.rb' diff --git a/app/scenes/scenes.rb b/app/scenes/scenes.rb index ceabc95..a414795 100644 --- a/app/scenes/scenes.rb +++ b/app/scenes/scenes.rb @@ -1,4 +1,5 @@ FF::Stg.add( FF::Scn.new('Render'), - FF::Scn.new('BoidRules') + FF::Scn.new('BoidRules'), + FF::Scn.new('Debug') ) diff --git a/app/systems/debug/debug_render_vector_arrow.rb b/app/systems/debug/debug_render_vector_arrow.rb new file mode 100644 index 0000000..19d3235 --- /dev/null +++ b/app/systems/debug/debug_render_vector_arrow.rb @@ -0,0 +1,16 @@ +FF::Sys.new('DebugRenderVectorArrow', priority: 100) do + FF::Cmp::DebugVectorArrow[0].entities.each do |entity| + boid = entity.components[FF::Cmp::Boid][0] + ox = boid.x + entity.components[FF::Cmp::Sprite][0].props.w / 2 + oy = boid.y + entity.components[FF::Cmp::Sprite][0].props.h / 2 + length = FF::Cmp::DebugVectorArrow[0].length + #puts "vx: #{boid.vx}" + #puts "cx: #{boid.cx}" + vxtip = ox + (boid.vx * length) + vytip = oy + (boid.vy * length) + $gtk.args.outputs.lines << [ox, oy, vxtip, vytip, 255, 255, 0, 255] + #$gtk.args.outputs.lines << [ox, oy, ox + (boid.cx * length * 10), oy + (boid.cy * length), 0, 255, 0, 255] + $gtk.args.outputs.lines << [vxtip, vytip, vxtip + (boid.cx * length * 10), vytip + (boid.cy * length * 10), 0, 0, 255, 255] + + end +end diff --git a/app/systems/rules/bounds.rb b/app/systems/rules/bounds.rb index 602f19c..0f1fc74 100644 --- a/app/systems/rules/bounds.rb +++ b/app/systems/rules/bounds.rb @@ -1,5 +1,5 @@ FF::Scn::BoidRules.add( - FF::Sys.new('BoidBounds') do + FF::Sys.new('BoidBounds', priority: 50) do FF::Cmp::BoidBounds.each do |boid_bounds| boid = boid_bounds.entities[0].components[FF::Cmp::Boid][0] diff --git a/app/systems/rules/cohesion.rb b/app/systems/rules/cohesion.rb new file mode 100644 index 0000000..7e33961 --- /dev/null +++ b/app/systems/rules/cohesion.rb @@ -0,0 +1,27 @@ +FF::Scn::BoidRules.add( + FF::Sys.new('BoidsCohesion', priority: 50) do + center_mass = [0.0, 0.0] + boids_count = FF::Cmp::BoidsCohesion.data.count + + FF::Cmp::BoidsCohesion.each do |cohesion| + boid = cohesion.entities[0].components[FF::Cmp::Boid][0] + center_mass[0] += boid.x + center_mass[1] += boid.y + #puts boid.x + #puts boid.y + end + + #puts center_mass + FF::Cmp::BoidsCohesion.each do |cohesion| + boid_update = cohesion.entities[0].components[FF::Cmp::Boid][0] + 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.strength.to_i + boid_update.cy += (move_boid[1] - boid_update.y) / cohesion.strength.to_i + end + end +) diff --git a/app/systems/rules/reset_change_vector.rb b/app/systems/rules/reset_change_vector.rb new file mode 100644 index 0000000..28656e0 --- /dev/null +++ b/app/systems/rules/reset_change_vector.rb @@ -0,0 +1,8 @@ +FF::Scn::BoidRules.add( + FF::Sys.new('ResetChangeVector', priority: 45) do + FF::Cmp::Boid.each do |boid| + boid.cx = 0 + boid.cy = 0 + end + end +) diff --git a/app/systems/update_boid_position.rb b/app/systems/update_boid_position.rb new file mode 100644 index 0000000..50ab9a7 --- /dev/null +++ b/app/systems/update_boid_position.rb @@ -0,0 +1,11 @@ +FF::Scn::BoidRules.add( + FF::Sys.new('UpdateBoidPosition', priority: 98) do + FF::Cmp::Boid.each do |boid| + boid.vx += boid.cx + boid.vy += boid.cy + boid.x += boid.vx + boid.y += boid.vy + # TODO: based on direction of the vector, needs to update the rotation of sprite too + end + end +) diff --git a/app/systems/update_boid_sprite.rb b/app/systems/update_boid_sprite.rb index 57d5847..8afaf56 100644 --- a/app/systems/update_boid_sprite.rb +++ b/app/systems/update_boid_sprite.rb @@ -1,15 +1,8 @@ FF::Scn::Render.add( FF::Sys.new('UpdateBoidSprite', priority: 98) do FF::Cmp::Boid.each do |boid| - boid.vx += boid.cx - boid.vy += boid.cy - boid.x += boid.vx - boid.y += boid.vy boid.entities[0].components[FF::Cmp::Sprite][0].props[:x] = boid.x boid.entities[0].components[FF::Cmp::Sprite][0].props[:y] = boid.y - # based on direction of the vector, needs to update the rotation of sprite too - boid.cx = 0 - boid.cy = 0 end end ) diff --git a/app/tick.rb b/app/tick.rb index b8910f4..7c44f47 100644 --- a/app/tick.rb +++ b/app/tick.rb @@ -1,4 +1,35 @@ -FF::Ent.new(FF::Cmp::Sprite.new, FF::Cmp::Boid.new(vx: 10, vy: 10), FF::Cmp::BoidBounds.new) +FF::Ent.new(FF::Cmp::Sprite.new, + FF::Cmp::Boid.new(x: 10, y: 10, vx: 0, vy: 0), + FF::Cmp::BoidBounds.new, + FF::Cmp::BoidsCohesion.new) +FF::Ent.new(FF::Cmp::Sprite.new, + FF::Cmp::Boid.new(x: 50, y: 50), + FF::Cmp::BoidBounds.new, + FF::Cmp::BoidsCohesion.new) +FF::Ent.new(FF::Cmp::Sprite.new, + FF::Cmp::Boid.new(x: 70, y: 20), + FF::Cmp::BoidBounds.new, + FF::Cmp::BoidsCohesion.new) +FF::Ent.new(FF::Cmp::DebugVectorArrow.new(length: 5), + FF::Cmp::Sprite.new, + FF::Cmp::Boid.new(x: 150, y: 250), + FF::Cmp::BoidBounds.new, + FF::Cmp::BoidsCohesion.new) +FF::Scn::Debug.add(FF::Sys::DebugRenderVectorArrow) +@pause = false def tick args + args.outputs.background_color = [0,0,0] FelFlame::Stage.call + if args.inputs.keyboard.keys[:down].include?(:right) + FF::Scn::BoidRules.call + end + if args.inputs.keyboard.keys[:down].include?(:space) + if @pause + FF::Stg.remove FF::Scn::BoidRules + @pause = false + else + FF::Stg.add FF::Scn::BoidRules + @pause = true + end + end end |
