summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorrealtradam <[email protected]>2021-12-18 20:38:08 -0500
committerrealtradam <[email protected]>2021-12-18 20:38:08 -0500
commit77e739ff9ebd4ab9fc7a2ed50472eaaa296a0509 (patch)
tree7d6f7fb3fb1f842688953a111752ed84d293e949
parent509f53c9bca30de88af6f12c371ca637a9a4f9a3 (diff)
downloadSteelWings-77e739ff9ebd4ab9fc7a2ed50472eaaa296a0509.tar.gz
SteelWings-77e739ff9ebd4ab9fc7a2ed50472eaaa296a0509.zip
Implemented camera and updated debug to work with camera
-rw-r--r--app/components/boid.rb9
-rw-r--r--app/components/camera.rb5
-rw-r--r--app/main.rb2
-rw-r--r--app/systems/camera.rb29
-rw-r--r--app/systems/debug/debug_render_vector_arrow.rb75
-rw-r--r--app/systems/update_boid_sprite.rb10
-rw-r--r--app/tick.rb40
7 files changed, 143 insertions, 27 deletions
diff --git a/app/components/boid.rb b/app/components/boid.rb
index 060f9c3..3358559 100644
--- a/app/components/boid.rb
+++ b/app/components/boid.rb
@@ -1,5 +1,6 @@
FF::Cmp.new('Boid',
- x: 0, y: 0,
- vx: 0, vy: 0,
- cx: 0, cy: 0,
- angle: 0)
+ x: 0, y: 0, # position
+ vx: 0, vy: 0, # velocity
+ cx: 0, cy: 0, # change in velocity
+ w: 0, h: 0, # width and height
+ angle: 0) # angle of velocity
diff --git a/app/components/camera.rb b/app/components/camera.rb
new file mode 100644
index 0000000..dc61bae
--- /dev/null
+++ b/app/components/camera.rb
@@ -0,0 +1,5 @@
+FF::Cmp.new('SingletonCamera',
+ :x, :y,
+ :zoom, :angle)
+
+FF::Cmp::SingletonCamera.new(x: 0.0, y: 0.0, zoom: 1.0, angle: 0.0)
diff --git a/app/main.rb b/app/main.rb
index 6b3abe6..00984e6 100644
--- a/app/main.rb
+++ b/app/main.rb
@@ -9,6 +9,7 @@ require 'app/components/rules/cohesion.rb'
require 'app/components/rules/alignment.rb'
require 'app/components/rules/separation.rb'
require 'app/components/debug/debug_vector_arrow.rb'
+require 'app/components/camera.rb'
require 'app/systems/render.rb'
require 'app/systems/update_boid_sprite.rb'
@@ -19,5 +20,6 @@ require 'app/systems/rules/alignment.rb'
require 'app/systems/rules/separation.rb'
require 'app/systems/rules/reset_change_vector.rb'
require 'app/systems/debug/debug_render_vector_arrow.rb'
+require 'app/systems/camera.rb'
require 'app/tick.rb'
diff --git a/app/systems/camera.rb b/app/systems/camera.rb
new file mode 100644
index 0000000..db6e3fc
--- /dev/null
+++ b/app/systems/camera.rb
@@ -0,0 +1,29 @@
+FF::Scn::Render.add(
+ # Update the position of the sprite according
+ # to camera values before the sprite is rendered
+ FF::Sys.new('Camera', priority: 97) do
+ camera = FF::Cmp::SingletonCamera[0]
+ camera.entities.each do |ent|
+ sprite = ent.components[FF::Cmp::Sprite][0].props
+
+ angle = camera.angle * (Math::PI / 180)
+ half_width = $gtk.args.grid.w * 0.5
+ half_height = $gtk.args.grid.h * 0.5
+ offset_x = sprite[:x] + (sprite[:w] / 2)
+ offset_y = sprite[:y] + (sprite[:h] / 2)
+ temp_x = (((offset_x - camera.x) * Math.cos(angle)) - ((offset_y - camera.y) * Math.sin(angle))) \
+ * camera.zoom + half_width - (sprite[:w] * camera.zoom / 2)
+ temp_y = (((offset_x - camera.x) * Math.sin(angle)) + ((offset_y - camera.y) * Math.cos(angle))) \
+ * camera.zoom + half_height - (sprite[:h] * camera.zoom / 2)
+ temp_rotate = sprite[:angle] + camera.angle
+ temp_width = sprite[:w] * camera.zoom
+ temp_height = sprite[:h] * camera.zoom
+
+ sprite[:x] = temp_x
+ sprite[:y] = temp_y
+ sprite[:w] = temp_width
+ sprite[:h] = temp_height
+ sprite[:angle] = temp_rotate
+ end
+ end
+)
diff --git a/app/systems/debug/debug_render_vector_arrow.rb b/app/systems/debug/debug_render_vector_arrow.rb
index ebd7080..ae53d1c 100644
--- a/app/systems/debug/debug_render_vector_arrow.rb
+++ b/app/systems/debug/debug_render_vector_arrow.rb
@@ -1,35 +1,74 @@
FF::Sys.new('DebugRenderVectorArrow', priority: 100) do
FF::Cmp::DebugVectorArrow[0].entities.each do |entity|
boid = entity.components[FF::Cmp::Boid][0]
+ sprite = entity.components[FF::Cmp::Sprite][0]
length = FF::Cmp::DebugVectorArrow[0].length
#puts "vx: #{boid.vx}"
#puts "cx: #{boid.cx}"
- vxtip = boid.x + (boid.vx * length)
- vytip = boid.y + (boid.vy * length)
+ boid_x = 0
+ boid_y = 0
+ boid_cx = 0
+ boid_cy = 0
+ boid_vx = 0
+ boid_vy = 0
+ vxtip = 0
+ vytip = 0
+ small_square = 15
+ big_square = 25
+ if !entity.components[FF::Cmp::SingletonCamera].nil? && !entity.components[FF::Cmp::SingletonCamera][0].nil?
+ camera = FF::Cmp::SingletonCamera[0]
+ vxtip = boid_x = sprite.props[:x]
+ vytip = boid_y = sprite.props[:y]
+ boid_vx = boid.vx * camera.zoom
+ boid_vy = boid.vy * camera.zoom
+ vxtip += ((Math.cos(camera.angle * (Math::PI / 180.0)) * boid_vx) * length) - ((Math.sin(camera.angle * (Math::PI / 180.0)) * boid_vy) * length)
+ vytip += ((Math.sin(camera.angle * (Math::PI / 180.0)) * boid_vx) * length) + ((Math.cos(camera.angle * (Math::PI / 180.0)) * boid_vy) * length)
+ boid_cx = (((Math.cos(camera.angle * (Math::PI / 180.0)) * boid.cx)) - ((Math.sin(camera.angle * (Math::PI / 180.0)) * boid.cy))) * camera.zoom
+ boid_cy = (((Math.sin(camera.angle * (Math::PI / 180.0)) * boid.cx)) + ((Math.cos(camera.angle * (Math::PI / 180.0)) * boid.cy))) * camera.zoom
+ half_height = sprite.props[:h] * 0.5
+ half_width = sprite.props[:w] * 0.5
+ vxtip += half_width
+ vytip += half_height
+ #boid_cx += half_width
+ #boid_cy += half_height
+ boid_x += half_width
+ boid_y += half_height
+ boid_vx += half_width
+ boid_vy += half_height
+ small_square *= camera.zoom
+ big_square *= camera.zoom
+ else
+ vxtip = boid_x = boid.x
+ vytip = boid_y = boid.y
+ boid_cx = boid.cx
+ boid_cy = boid.cy
+ vxtip += (boid.vx * length)
+ vytip += (boid.vy * length)
+ end
# Velocity
- $gtk.args.outputs.lines << [boid.x, boid.y, vxtip, vytip, 0, 255, 255, 255]
- $gtk.args.outputs.lines << [boid.x+1, boid.y, vxtip+1, vytip, 0, 255, 255, 255]
- $gtk.args.outputs.lines << [boid.x-1, boid.y, vxtip-1, vytip, 0, 255, 255, 255]
- $gtk.args.outputs.lines << [boid.x, boid.y+1, vxtip, vytip+1, 0, 255, 255, 255]
- $gtk.args.outputs.lines << [boid.x, boid.y-1, vxtip, vytip-1, 0, 255, 255, 255]
+ $gtk.args.outputs.lines << [boid_x, boid_y, vxtip, vytip, 0, 255, 255, 255]
+ $gtk.args.outputs.lines << [boid_x+1, boid_y, vxtip+1, vytip, 0, 255, 255, 255]
+ $gtk.args.outputs.lines << [boid_x-1, boid_y, vxtip-1, vytip, 0, 255, 255, 255]
+ $gtk.args.outputs.lines << [boid_x, boid_y+1, vxtip, vytip+1, 0, 255, 255, 255]
+ $gtk.args.outputs.lines << [boid_x, boid_y-1, vxtip, vytip-1, 0, 255, 255, 255]
#$gtk.args.outputs.lines << [boid.x, boid.y, boid.x + (boid.cx * length * 10), boid.y + (boid.cy * length), 0, 255, 0, 255]
# Change in Velocity
- $gtk.args.outputs.lines << [vxtip, vytip, vxtip + (boid.cx * length * 10), vytip + (boid.cy * length * 10), 255, 0, 255, 255]
- $gtk.args.outputs.lines << [vxtip+1, vytip, vxtip+1 + (boid.cx * length * 10), vytip + (boid.cy * length * 10), 255, 0, 255, 255]
- $gtk.args.outputs.lines << [vxtip-1, vytip, vxtip-1 + (boid.cx * length * 10), vytip + (boid.cy * length * 10), 255, 0, 255, 255]
- $gtk.args.outputs.lines << [vxtip, vytip+1, vxtip + (boid.cx * length * 10), vytip+1 + (boid.cy * length * 10), 255, 0, 255, 255]
- $gtk.args.outputs.lines << [vxtip, vytip-1, vxtip + (boid.cx * length * 10), vytip-1 + (boid.cy * length * 10), 255, 0, 255, 255]
+ $gtk.args.outputs.lines << [vxtip, vytip, vxtip + (boid_cx * length * 10), vytip + (boid_cy * length * 10), 255, 0, 255, 255]
+ $gtk.args.outputs.lines << [vxtip+1, vytip, vxtip+1 + (boid_cx * length * 10), vytip + (boid_cy * length * 10), 255, 0, 255, 255]
+ $gtk.args.outputs.lines << [vxtip-1, vytip, vxtip-1 + (boid_cx * length * 10), vytip + (boid_cy * length * 10), 255, 0, 255, 255]
+ $gtk.args.outputs.lines << [vxtip, vytip+1, vxtip + (boid_cx * length * 10), vytip+1 + (boid_cy * length * 10), 255, 0, 255, 255]
+ $gtk.args.outputs.lines << [vxtip, vytip-1, vxtip + (boid_cx * length * 10), vytip-1 + (boid_cy * length * 10), 255, 0, 255, 255]
# Square Velocity
- $gtk.args.outputs.borders << [vxtip - 24, vytip - 24, 48, 48, 0, 255, 255]
- $gtk.args.outputs.borders << [vxtip - 25, vytip - 26, 52, 52, 0, 255, 255]
- $gtk.args.outputs.borders << [vxtip - 25, vytip - 25, 50, 50, 0, 255, 255]
+ $gtk.args.outputs.borders << [vxtip - (big_square - 1), vytip - (big_square - 1), (big_square - 1) * 2, (big_square - 1) * 2, 0, 255, 255]
+ $gtk.args.outputs.borders << [vxtip - big_square, vytip - big_square, (big_square) * 2, (big_square) * 2, 0, 255, 255]
+ $gtk.args.outputs.borders << [vxtip - (big_square + 1), vytip - (big_square + 1), (big_square + 1) * 2, (big_square + 1) * 2, 0, 255, 255]
# Square Change in Velocity
- $gtk.args.outputs.borders << [vxtip - 14 + (boid.cx * length * 10), vytip - 14 + (boid.cy * length * 10), 28, 28, 255, 0, 255, 255]
- $gtk.args.outputs.borders << [vxtip - 15 + (boid.cx * length * 10), vytip - 15 + (boid.cy * length * 10), 30, 30, 255, 0, 255, 255]
- $gtk.args.outputs.borders << [vxtip - 16 + (boid.cx * length * 10), vytip - 16 + (boid.cy * length * 10), 32, 32, 255, 0, 255, 255]
+ $gtk.args.outputs.borders << [vxtip - (small_square - 1) + (boid_cx * length * 10), vytip - (small_square - 1) + (boid_cy * length * 10), (small_square - 1) * 2, (small_square - 1) * 2, 255, 0, 255, 255]
+ $gtk.args.outputs.borders << [vxtip - small_square + (boid_cx * length * 10), vytip - small_square + (boid_cy * length * 10), (small_square) * 2, (small_square) * 2, 255, 0, 255, 255]
+ $gtk.args.outputs.borders << [vxtip - (small_square + 1) + (boid_cx * length * 10), vytip - (small_square + 1) + (boid_cy * length * 10), (small_square + 1) * 2, (small_square + 1) * 2, 255, 0, 255, 255]
end
end
diff --git a/app/systems/update_boid_sprite.rb b/app/systems/update_boid_sprite.rb
index b89579d..4f7dc76 100644
--- a/app/systems/update_boid_sprite.rb
+++ b/app/systems/update_boid_sprite.rb
@@ -1,13 +1,17 @@
FF::Scn::Render.add(
- FF::Sys.new('UpdateBoidSprite', priority: 98) do
+ # Update the position of the boid sprite before
+ # the camera and before its rendered
+ FF::Sys.new('UpdateBoidSprite', priority: 95) do
FF::Cmp::Boid.each do |boid|
sprite = boid.entities[0].components[FF::Cmp::Sprite][0]
- sprite.props[:x] = boid.x - sprite.props[:w] / 2
- sprite.props[:y] = boid.y - sprite.props[:h] / 2
+ sprite.props[:x] = boid.x - boid.h / 2
+ sprite.props[:y] = boid.y - boid.w / 2
diff_angle = (((Math.atan2(boid.vy, boid.vx) * 180.0) / Math::PI).round(3) - 90) - sprite.props[:angle]
diff_angle = (diff_angle + 180) % 360 - 180
sprite.props[:angle] += diff_angle
+ sprite.props[:w] = boid.w
+ sprite.props[:h] = boid.h
end
end
)
diff --git a/app/tick.rb b/app/tick.rb
index 7539405..de40a9a 100644
--- a/app/tick.rb
+++ b/app/tick.rb
@@ -11,13 +11,14 @@ position_range = (100..700).to_a
sprite = FF::Cmp::Sprite.new
sprite.props[:path] = 'sprites/kenny/Ships/ship_0011.png'
FF::Ent.new(
- FF::Cmp::Boid.new(x: position_range.sample, y: position_range.sample, vx: 25, vy: 25),
+ FF::Cmp::Boid.new(x: position_range.sample, y: position_range.sample, vx: 25, vy: 25, w: sprite.props[:w], h: sprite.props[:h]),
sprite,
FF::Cmp::BoidBounds.new(strength: 1),
FF::Cmp::BoidsAlignment.new(strength: 1),
FF::Cmp::BoidsSeparation.new(distance: 150, strength: 0.01),
FF::Cmp::BoidsCohesion.new(strength: 100),
- #debug_arrow,
+ FF::Cmp::SingletonCamera[0],
+ debug_arrow,
)
end
#FF::Ent.new(
@@ -32,9 +33,44 @@ end
FF::Scn::Debug.add(FF::Sys::DebugRenderVectorArrow)
@pause = true
FF::Stg.remove FF::Scn::BoidRules
+@camera = FF::Cmp::SingletonCamera[0]
def tick args
args.outputs.background_color = [0,0,0]
FelFlame::Stage.call
+
+ # Moving Camera
+ if args.inputs.keyboard.keys[:down_or_held].include?(:d)
+ @camera.x += (Math.cos([email protected] * (Math::PI / 180.0)) * 5)
+ @camera.y += (Math.sin([email protected] * (Math::PI / 180.0)) * 5)
+ end
+ if args.inputs.keyboard.keys[:down_or_held].include?(:a)
+ @camera.x += (Math.cos([email protected] * (Math::PI / 180.0)) * -5)
+ @camera.y += (Math.sin([email protected] * (Math::PI / 180.0)) * -5)
+ end
+ if args.inputs.keyboard.keys[:down_or_held].include?(:w)
+ #@camera.y += 5
+ @camera.x -= (Math.sin([email protected] * (Math::PI / 180.0)) * 5)
+ @camera.y += (Math.cos([email protected] * (Math::PI / 180.0)) * 5)
+ end
+ if args.inputs.keyboard.keys[:down_or_held].include?(:s)
+ #@camera.y -= 5
+ @camera.x -= (Math.sin([email protected] * (Math::PI / 180.0)) * -5)
+ @camera.y += (Math.cos([email protected] * (Math::PI / 180.0)) * -5)
+ end
+ if args.inputs.keyboard.keys[:down_or_held].include?(:q)
+ @camera.angle += 3
+ end
+ if args.inputs.keyboard.keys[:down_or_held].include?(:e)
+ @camera.angle -= 3
+ end
+ if args.inputs.keyboard.keys[:down_or_held].include?(:z)
+ @camera.zoom *= 1.05
+ end
+ if args.inputs.keyboard.keys[:down_or_held].include?(:x)
+ @camera.zoom *= 0.95
+ end
+
+ # Pausing
if args.inputs.keyboard.keys[:down].include?(:right)
FF::Scn::BoidRules.call
end