Class: Silo

Inherits:
Actor
  • Object
show all
Defined in:
lib/silo.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Silo

Returns a new instance of Silo.



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
# File 'lib/silo.rb', line 5

def initialize(opts={})
  super
  set_image 'silo-b.png', opts
  @factor = @width / @image.width
  @y = $lotu.height - @height/2
  @z = ZOrder::Silo

  @platform = []
  # How many missiles in stock?
  @stock = opts[:stock]
  # How many missiles on platform?
  @platform_capacity = Float(opts[:platform_capacity])
  # Load one missile every n seconds
  @load_time = opts[:load_time]

  # Missiles configuration
  @missile_conf = {
    :force => opts[:missile_force] || 1000,
    :speed => opts[:missile_speed] || 1000,
    :mass => opts[:missile_mass] || 10
  }

  # If negative, it adds some initial delay
  @elapsed_time = -2

  # Platform configuration in pixels
  @left_padding = 13 * @factor
  @right_padding = 24 * @factor
  @bottom_padding = 18 * @factor

  @missile_image_width = 26
  @missile_animation_width = @missile_image_width * 20 / 15

  @slot_width = platform_width/@platform_capacity
end

Instance Attribute Details

#stockObject

Returns the value of attribute stock.



3
4
5
# File 'lib/silo.rb', line 3

def stock
  @stock
end

Instance Method Details

#collision_radiusObject



41
42
43
# File 'lib/silo.rb', line 41

def collision_radius
  50
end

#initial_place(missile) ⇒ Object



77
78
79
80
81
82
# File 'lib/silo.rb', line 77

def initial_place(missile)
  res = @x - @width/2 + @left_padding + missile.width/2 + (@platform_capacity - 1) * @slot_width
  res += (@slot_width - missile.width)
  res -= @elapsed_time / @load_time * @slot_width
  res
end

#launch_missile(x, y) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/silo.rb', line 100

def launch_missile(x, y)
  if m = @platform.shift
    m.save_init_pos
    m.stop_interpolations
    target = Vector2d.new(x,y)
    m.align_to target
    m.target = target
    m.play_animation('missile.png', :width => @missile_animation_width*@factor)
  end
end

#load_platformObject



55
56
57
58
59
60
61
62
63
64
# File 'lib/silo.rb', line 55

def load_platform
  if @platform.length < @platform_capacity && @stock > 0
    @elapsed_time += dt
    while @platform.length < @platform_capacity && @elapsed_time >= @load_time && @stock > 0
      @elapsed_time -= @load_time
      @platform.push(new_missile)
      @stock -= 1
    end
  end
end

#new_missileObject



66
67
68
69
70
71
72
73
74
75
# File 'lib/silo.rb', line 66

def new_missile
  missile = PlayerMissile.new(:mass => @missile_conf[:mass],
                        :max_force => @missile_conf[:force],
                        :max_speed => @missile_conf[:speed],
                        :width => @missile_image_width*@factor)
  missile.x = initial_place(missile)
  missile.y = @y + @height/2 - missile.height/2 - @bottom_padding
  missile.interpolate(missile, :y, :init => missile.y+20, :end => missile.y, :duration => 0.5)
  missile
end

#place_missilesObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/silo.rb', line 84

def place_missiles
  @platform.each_with_index do |missile, i|
    place_to_be = @x - @width/2 + @left_padding + missile.width/2 + i * @slot_width
    place_to_be += i/(@platform_capacity-1) * (@slot_width - missile.width)
    if missile.x > initial_place(missile)
      missile.x = initial_place(missile)
    end
    if missile.x > place_to_be
      missile.x -= @slot_width * dt / @load_time
    end
    if missile.x <= place_to_be
      missile.x = place_to_be
    end
  end
end

#platform_widthObject



45
46
47
# File 'lib/silo.rb', line 45

def platform_width
  @width - @left_padding - @right_padding
end

#to_sObject



111
112
113
114
115
116
117
# File 'lib/silo.rb', line 111

def to_s
  ["@platform.length: #{@platform.length}",
   "Missiles in stock: #{@stock}",
   "Missiles on platform: #{@platform.length}",
   "Elapsed loading time: #{format '%.2f', @elapsed_time}",
   "Loading next missile in: #{format('%.2f', @load_time - @elapsed_time)}"]
end

#updateObject



49
50
51
52
53
# File 'lib/silo.rb', line 49

def update
  super
  load_platform
  place_missiles
end