summaryrefslogtreecommitdiffhomepage
path: root/run.rb
blob: fafaca305bc9de8357b27f634832a11730478b47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
require 'ruby2d'
require 'ruby2d/camera'
require 'felflame'
module Ruby2D
  class Window
    def mouse_pry
      @mouse_buttons_down
    end
  end
end
# configuration for the simulation
# change values here to see a change in the simulation
FF::Cmp.new('SingletonConfig',
            # Show vectors
            debug: false,

            # If the camera should follow the "center mass"
            follow: false,

            # The velocity limit of a boid
            # Set to negative to disable
            limit: 6.0,

            # How strong the bounds should be
            # Higher is stronger
            bounds_strength: 10.0,
            # What the bounds are
            xmax: 450.0, xmin: -580.0,
            ymax: 250.0, ymin: -340.0,

            # How much the boids try to pull together
            # Smaller is stronger
            cohesion: 10000.0,

            # How much the boids push away from eachother
            # Smaller is stronger
            seperation: 115.0, 
            # What the range of seperating should be
            seperation_distance: 70.0,

            # Alignment rule disabled
            # How strong their vector alignment should be
            # Smaller is stronger
            alignment: 1000.0,

            # How much they try to follow their target
            # smaller is stronger
            target_strength: 40.0,
            # Radius that fish should avoid the piranha
            target_fear: 150.0,

            # These are later set by the mouse position
            target_x: 0, target_y: 0)
$config = FF::Cmp::SingletonConfig.new

# Used by the Camera Library
set(title: "Ruboids", width: 1164, height: 764, resizable: true)


Dir[File.join(__dir__, 'lib/**', '*.rb')].sort.each { |file| require file }

class GameWindow < Ruby2D::Window
  def initialize
    super
    # Comment out to remove a rule
    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::Bounds

    FF::Stg.add FF::Scn::BoidCalculations

    # background
    Camera::Image.new('assets/aquarium.png', x: -get(:width)+57, y: -get(:height)+97, z: -98)

    # stores array of valid positions on which to spawn
    # (not actually valid but close enough)
    randspot = [(($config.xmin.to_i)..($config.xmax.to_i)).to_a, (($config.ymin.to_i)..($config.ymax.to_i)).to_a]

    @mouse_right_held = false
    @x_mouse_previous = get(:mouse_x)
    @y_mouse_previous = get(:mouse_y)

    70.times do
      Fish.create(randspot[0].sample.to_f, randspot[1].sample.to_f)
    end

    unless $config.debug
      FF::Cmp::BoidVisuals.each do |boid|
        boid.vect.remove
      end
    end
  end

  def update
    if mouse_down(:right)
      @mouse_right_held = true
    elsif mouse_up(:right)
      @mouse_right_held = false
    end

    if @mouse_right_held && mouse_move
      Camera.x += Camera.coordinate_to_worldspace(@x_mouse_previous, @y_mouse_previous)[0] - Camera.coordinate_to_worldspace(get(:mouse_x), get(:mouse_y))[0]
      Camera.y += Camera.coordinate_to_worldspace(@x_mouse_previous, @y_mouse_previous)[1] - Camera.coordinate_to_worldspace(get(:mouse_x), get(:mouse_y))[1]
      #Camera.y -= (@mouse_move_delta_y * 5) / Camera.zoom #Camera.coordinate_to_worldspace(@mouse_move_delta_y,0)
    end

    if mouse_scroll
      if @mouse_scroll_delta_y < 0
      Camera.zoom *= 1.1
      elsif @mouse_scroll_delta_y > 0 
      Camera.zoom /= 1.1
      end
    end

    @x_mouse_previous = get(:mouse_x)
    @y_mouse_previous = get(:mouse_y)

    $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 mouse_down(:left)
      a = Piranha.create($config.target_x - 75, $config.target_y - 75)
      a.components[FF::Cmp::BoidVisuals][0].vect.remove unless $config.debug
    end
    FF::Stage.call 
    Camera.y += 5 if key_held('s')
    Camera.y -= 5 if key_held('w')
    Camera.x += 5 if key_held('d')
    Camera.x -= 5 if key_held('a')
    Camera.zoom *= 1.1 if key_held('z')
    Camera.zoom /= 1.1 if key_held('x')
  end

  def render
    #puts get(:fps)
  end
end


gamewindow = GameWindow.new
gamewindow.set(title: get(:title), width: get(:width), height: get(:height), resizable: get(:resizable))
gamewindow.show