summaryrefslogtreecommitdiffhomepage
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/00-define.rb4
-rw-r--r--lib/02-apply_boid_calculations.rb13
-rw-r--r--lib/mobs/fish.rb20
-rw-r--r--lib/mobs/piranha.rb20
-rw-r--r--lib/rules/alignment.rb4
-rw-r--r--lib/rules/cohesion.rb12
-rw-r--r--lib/rules/limit.rb8
-rw-r--r--lib/rules/seperation.rb25
-rw-r--r--lib/rules/target.rb28
9 files changed, 117 insertions, 17 deletions
diff --git a/lib/00-define.rb b/lib/00-define.rb
index 0075b7f..930f56f 100644
--- a/lib/00-define.rb
+++ b/lib/00-define.rb
@@ -12,5 +12,9 @@ FelFlame::Components.new('Boids',
vx: 0, vy: 0,
# calculated velocity change for next frame
cx: 0, cy: 0)
+FelFlame::Components.new('Fish')
+FF::Cmp::Fish.new
+FelFlame::Components.new('Piranha')
+FF::Cmp::Piranha.new
FelFlame::Components.new('BoidVisuals',
:obj, :vect)
diff --git a/lib/02-apply_boid_calculations.rb b/lib/02-apply_boid_calculations.rb
index 8b84ab5..ec3f2de 100644
--- a/lib/02-apply_boid_calculations.rb
+++ b/lib/02-apply_boid_calculations.rb
@@ -32,6 +32,19 @@ FF::Scn::BoidCalculations.add(FF::Sys.new('ApplyBoidCalculations', priority: 75)
group_velocity[1] += boid.vy
end
+ 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|
+ boid = ent.components[FF::Cmp::Boids].first
+
+ center_mass[0] += boid.x
+ center_mass[1] += boid.y
+ group_velocity[0] += boid.vx
+ group_velocity[1] += boid.vy
+ end
+
@center_vel.x1 = @center.x = (center_mass[0] / boids_count)
@center_vel.y1 = @center.y = (center_mass[1] / boids_count)
diff --git a/lib/mobs/fish.rb b/lib/mobs/fish.rb
new file mode 100644
index 0000000..a9999ae
--- /dev/null
+++ b/lib/mobs/fish.rb
@@ -0,0 +1,20 @@
+class Fish
+ def self.create(x, y)
+ puts 'creating...'
+ FF::Ent.new(
+ FF::Cmp::Fish[0],
+ FF::Cmp::Boids.new(x: x, y: y),
+ FF::Cmp::BoidVisuals.new(
+ obj: Camera::Image.new(
+ 'assets/Guppy Large Normal.png',
+ width: 25, height: 26
+ ),
+ vect: Camera::Line.new(
+ width: 7,
+ color: [1.0,0,0,0.5],
+ z: -1
+ )
+ )
+ )
+ end
+end
diff --git a/lib/mobs/piranha.rb b/lib/mobs/piranha.rb
new file mode 100644
index 0000000..c7ce8b1
--- /dev/null
+++ b/lib/mobs/piranha.rb
@@ -0,0 +1,20 @@
+class Piranha
+ def self.create(x, y)
+ puts 'creating...'
+ FF::Ent.new(
+ FF::Cmp::Piranha[0],
+ FF::Cmp::Boids.new(x: x, y: y),
+ FF::Cmp::BoidVisuals.new(
+ obj: Camera::Image.new(
+ 'assets/Predator 1.png',
+ width: 45, height: 46
+ ),
+ vect: Camera::Line.new(
+ width: 7,
+ color: [1.0,0,0,0.5],
+ z: -1
+ )
+ )
+ )
+ end
+end
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