Class: CellWindow

Inherits:
Gosu::Window
  • Object
show all
Defined in:
lib/cellular_automata/gui_window.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ CellWindow

Returns a new instance of CellWindow.



4
5
6
7
8
9
10
# File 'lib/cellular_automata/gui_window.rb', line 4

def initialize(opts={})
  @scale = opts[:scale]
  @cell_width = scale**opts[:cell_scale]
  super opts[:width]*scale, opts[:height]*scale, opts[:fullscreen]
  @board = CellularAutomata::Board.new(width: opts[:width]/scale, height: opts[:height]/scale, rule: opts[:rule])
  self.caption = "Cellular Automata"
end

Instance Attribute Details

#boardObject (readonly)

Returns the value of attribute board.



2
3
4
# File 'lib/cellular_automata/gui_window.rb', line 2

def board
  @board
end

#cell_widthObject (readonly)

Returns the value of attribute cell_width.



2
3
4
# File 'lib/cellular_automata/gui_window.rb', line 2

def cell_width
  @cell_width
end

#pausedObject (readonly) Also known as: paused?

Returns the value of attribute paused.



2
3
4
# File 'lib/cellular_automata/gui_window.rb', line 2

def paused
  @paused
end

#scaleObject (readonly)

Returns the value of attribute scale.



2
3
4
# File 'lib/cellular_automata/gui_window.rb', line 2

def scale
  @scale
end

Instance Method Details

#button_down(id) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/cellular_automata/gui_window.rb', line 32

def button_down(id)
  if id == Gosu::KbSpace
    toggle_pause
  else
    exit
  end
end

#drawObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cellular_automata/gui_window.rb', line 16

def draw
  board.each_cell do |x, y|
    prev_1 = board.history[0][y][x] == 1
    prev_2 = board.history[1][y][x] == 1
    color = Gosu::Color::BLACK
    if board.state[y][x] == 1
      color = Gosu::Color.argb(0xff_FFFF00)
    elsif prev_1
      color = Gosu::Color.argb(0xff_AAAA00)
    elsif prev_2
      color = Gosu::Color.argb(0xff_444400)
    end
    Gosu.draw_rect(x * scale**2, y * scale**2, cell_width, cell_width, color, 0, :default)
  end
end

#toggle_pauseObject



40
41
42
# File 'lib/cellular_automata/gui_window.rb', line 40

def toggle_pause
  @paused = !@paused
end

#updateObject



12
13
14
# File 'lib/cellular_automata/gui_window.rb', line 12

def update
  board.tick! unless paused?
end