# $Id: starout.rb,v 1.2 2003/10/24 02:43:59 knok Exp $

require 'color'
require 'ming/ming'
include Ming

class LightLine
  attr_reader :obj
  def initialize(angle, length, color, movie, dim)
    @angle = angle
    @length = length
    @movie = movie
    @dimension = dim
    @shape = SWFShape.new
    @dx = Math.cos(deg2rad(angle)) * length
    @dy = Math.sin(deg2rad(angle)) * length
    @shape.set_line(1, color.red, color.blue, color.green)
    @shape.move_pen_to(0, 0)
    @shape.draw_line_to(@dx, @dy)
    @obj = @movie.add(@shape)
    @toffset = 0
    @tmax = 10
  end
  def offset(o)
    @toffset = o
  end
  def maxtime(t)
    @tmax = t
  end
  def deg2rad(d)
    return d * Math.const_get(:PI) / 180
  end
  def position(time)
    i = time - @toffset
    if (i < 0)
      i = @tmax
    else
      i = i % @tmax
    end
    @obj.move_to(@dimension[0] / 2 + @dx * i, @dimension[1] / 2 + @dy * i)
  end
end

class StarOut
  attr_reader :lines
  def initialize(movie, dim, bg)
    @movie = movie
    @dim = dim
    @bg = bg
    @curtime = 0
    @black = Color.new(0, 0, 0)
    @lines = []
  end
  def setstars(num, color)
    0.upto(num - 1) do |x|
      ary = genlp()
      l = LightLine.new(ary[0], ary[1], color, @movie, @dim)
      l.offset(ary[2])
      l.maxtime(ary[3])
      @lines.push(l)
    end
  end
  def iteration()
    @lines.each do |line|
      line.position(@curtime)
    end
    if (@curtime > 21)
      @black.to_white(16)
      @bg.add_color(@black.red, @black.green, @black.blue)
    end
    @curtime = @curtime + 1
    @black
  end
  def genlp()
    angle = rand(360)
    length = rand(30) + 5
    offset = rand(20)
    maxtime = rand(15) + 200/length + 5
    return [angle, length, offset, maxtime]
  end
end
