Class: AsciiParadise::RotatingDNA

Inherits:
Animation show all
Defined in:
lib/ascii_paradise/animations/rotating_dna.rb

Constant Summary collapse

THRESHOLD_N_RUNS_ALLOWED =
#

THRESHOLD_N_RUNS_ALLOWED

#
250
UPDATE_SPEED =
#

UPDATE_SPEED

How fast the DNA will rotate, at each interval.

#
0.05
DNA_X_Y_MOVEMENT_SPEED =
#

DNA_X_Y_MOVEMENT_SPEED

The higher this number is, the faster the DNA will “rotate”. 2 is my default.

#
2

Constants inherited from Animation

Animation::RUN_N_TIMES

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Animation

#ascii_files?, #dataset?, #delay?, #display, #display_the_frame, #do_clear?, #do_use_colour_spray, #do_use_disco_inferno, #do_use_half_colours, #do_use_rainbow_colours, #guess_filename, is_animated?, #is_animated?, #load_animated_ascii_files_from_this_directory, #load_ascii_files, #load_ascii_files_and_determine_the_dataset, #load_dataset, #mediumpurple, #parse_dataset_from_this_control_file, #return_filename, #return_the_name_to_the_proper_animation_directory_of_this_animated_component, run, #run_n_times?, #run_with_this_dataset, #run_with_this_dataset_while_waiting_for_keypress_events, #set_dataset, #set_delay, #set_run_n_times, #set_use_ascii_files_from_this_directory, #show_frame_at_this_position, #show_to_the_user_how_to_operate_the_keypress_interface, #sleep_with_the_default_delay, #use_disco_inferno?

Methods inherited from Base

animation_dir?, #animation_directory?, #clear_screen, #colour_parse_this_string, #debug?, #do_not_run_already, #do_not_use_clear, #do_use_random_colour, #do_wait_for_keypress_event, e, #e, #enable_debug, #is_animated?, #menu, #project_base_dir?, #register_sigint, #remove_trailing_ansci_escape_code, #report_how_many_animated_components_exist, #return_basename_of_this_file_without_the_extension, #return_random_colour, #rev, #royalblue, run, #set_use_this_colour, #sfancy, #sfile, #show_available_components, #show_help, #simp, #slategrey, #sort_files, #static_dir?, #steelblue, #swarn, #use_colours?

Constructor Details

#initialize(use_this_runmode = :dna, run_already = true) ⇒ RotatingDNA

#

initialize

#


36
37
38
39
40
41
42
43
44
45
# File 'lib/ascii_paradise/animations/rotating_dna.rb', line 36

def initialize(
    use_this_runmode = :dna,
    run_already      = true
  )
  super()
  reset
  register_sigint
  set_runmode(use_this_runmode)
  run if run_already
end

Class Method Details

.[](runmode = :dna) ⇒ Object

#

AsciiParadise::RotatingDNA[]

The runmodes can be:

AsciiParadise::RotatingDNA[:dna]
AsciiParadise::RotatingDNA[:star]
#


171
172
173
# File 'lib/ascii_paradise/animations/rotating_dna.rb', line 171

def self.[](runmode = :dna)
  new(runmode)
end

Instance Method Details

#draw_dna_frame(time_scale, horiz_scale, vert_scale, curve_length, pair_space, pair_offset = pair_space/2, char = '*') ⇒ Object

#

draw_dna_frame

Draw the DNA frame. This is the main powerhorse of this class.

#


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ascii_paradise/animations/rotating_dna.rb', line 91

def draw_dna_frame(
    time_scale,   horiz_scale, vert_scale, 
    curve_length, pair_space, 
    pair_offset = pair_space/2, char = '*'
  )
  frame = Array.new(vert_scale) { Array.new(curve_length, ' ') }
  time_now  = Time.now.to_f # Get the start-time.
  sin_start = time_scale * time_now
  half_vs   = vert_scale/2
  (0...curve_length).each { |i|
    v = half_vs * Math.sin(sin_start+horiz_scale*i)
    p1 = half_vs + v
    p2 = half_vs - v
    frame[p1][i] = char
    frame[p2][i] = char
    if (i + pair_offset) % pair_space < 1
      p1,p2 = [p1, p2].sort
      (p1.ceil...p2.ceil).each { |y| frame[y][i] = char }
    end
  }
  clear
  e frame.map {|line| line.join }
end

#draw_star_frame(clear, time_scale, horiz_scale, vert_scale, curve_length, pair_space, pair_offset = pair_space / 2, char = '*') ⇒ Object

#

draw_star_frame

#


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
# File 'lib/ascii_paradise/animations/rotating_dna.rb', line 59

def draw_star_frame(
    clear,
    time_scale,
    horiz_scale,
    vert_scale,
    curve_length,
    pair_space,
    pair_offset = pair_space / 2,
    char = '*'
  )
  frame = Array.new(vert_scale){Array.new(curve_length, ' ')}
  t = Time.now.to_f
  sin_start = time_scale*t
  half_vs = vert_scale / 2
  (0...curve_length).each{|i| 
    if (i+pair_offset) % pair_space < 1
      v = half_vs * Math.sin(sin_start+horiz_scale*i)
      p1 = (half_vs + v)
      p2 = (half_vs - v)
      frame[p1][i] = char
      frame[p2][i] = char
    end
  }
  print_clear
  e frame.map {|line| line.join }
end

#increment_counterObject

#

increment_counter

#


118
119
120
# File 'lib/ascii_paradise/animations/rotating_dna.rb', line 118

def increment_counter
  @n_runs += 1
end
#

print_clear

#


125
126
127
# File 'lib/ascii_paradise/animations/rotating_dna.rb', line 125

def print_clear
  print `clear`
end

#resetObject

#

reset

#


50
51
52
53
54
# File 'lib/ascii_paradise/animations/rotating_dna.rb', line 50

def reset
  super()
  set_runmode # Default mode is :dna. The other mode is :star
  @n_runs = 0
end

#runObject

#

run

#


132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/ascii_paradise/animations/rotating_dna.rb', line 132

def run
  loop {
    case runmode?
    when :dna, :default
      draw_dna_frame(DNA_X_Y_MOVEMENT_SPEED, 0.1, 11, 60, 8, 4)
    else
      draw_star_frame(`clear`,2,0.1,11,60,4,2)
    end
    sleep UPDATE_SPEED
    increment_counter
    exit if @n_runs > THRESHOLD_N_RUNS_ALLOWED
  }
end

#runmode?Boolean Also known as: draw_what?

#

runmode?

#

Returns:

  • (Boolean)


158
159
160
# File 'lib/ascii_paradise/animations/rotating_dna.rb', line 158

def runmode?
  @runmode
end

#set_runmode(use_this_runmode = :dna) ⇒ Object

#

set_runmode

#


149
150
151
# File 'lib/ascii_paradise/animations/rotating_dna.rb', line 149

def set_runmode(use_this_runmode = :dna)
  @runmode = use_this_runmode
end