summaryrefslogtreecommitdiffstats
path: root/qtruby/rubylib/examples/ruboids/ruboids/Cloud.rb
blob: 5d30222a63bc9df37f9eaafb2ff63fd26b52d45c (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
#
# Copyright (c) 2001 by Jim Menard <jimm@io.com>
#
# Released under the same license as Ruby. See
# http://www.ruby-lang.org/en/LICENSE.txt.
#

require 'Thing'
require 'CloudView'
require 'Params'

class Bubble

    attr_reader :loc, :radius, :color

    def initialize
	@radius = rand($PARAMS['cloud_max_bubble_radius']) + 1
	@loc = Point.new(0, rand(8) - 4, rand(8) - 4)
	c = 0.85 + rand() * 0.15
	@color = [c, c, c]
    end

end


class Cloud < Thing

    attr_reader :speed, :bubbles, :width

    def initialize
	minSpeed = $PARAMS['cloud_min_speed']
	minBubbles = $PARAMS['cloud_min_bubbles']
	@speed = rand($PARAMS['cloud_max_speed'] - minSpeed) + minSpeed
	numBubbles = rand($PARAMS['cloud_max_bubbles'] - minBubbles) +
	    minBubbles
	@bubbles = []
	prevBubble = nil
	(0 ... numBubbles).each { | i |
	    bubble = Bubble.new()
	    if !prevBubble.nil?
		bubble.loc.x = prevBubble.loc.x +
		    rand((prevBubble.radius + bubble.radius) * 0.66)
	    end

	    @bubbles[i] = prevBubble = bubble
	}

	@width = bubbles.last.loc.x +
	    @bubbles.first.radius + @bubbles.last.radius

	@view = CloudView.new(self)
    end

    def move
	@position.x += pixelsPerSecToPixelsPerMove(speed)
	halfWorldWidth = $PARAMS['world_width']
	if (@position.x >=  halfWorldWidth / 2)
	    @position.x = -(halfWorldWidth + @width)
	end
    end
end