Class: Window

Inherits:
Gosu::Window
  • Object
show all
Defined in:
lib/ruby-ai/window.rb

Constant Summary collapse

COLORS =
{
  :player => [1.0, 0.5764706134796143, 0.0, 1.0], #orange, paintbrush 'tangerine'
  :goal => [0.0, 0.9764706492424011, 0.0, 1.0], #green, paintbrush 'spring'
  :wall => [0.5803921818733215, 0.32156863808631897, 0.0, 1.0], #brown, paintbrush 'mocha'
  :empty => [0.0, 0.0, 0.0, 1.0], #black, paintbrush 'licorice'
}
DIMS =
{:x => 64, :y => 64}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(level) ⇒ Window

Returns a new instance of Window.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ruby-ai/window.rb', line 25

def initialize(level)

  super(DIMS[:x] * 10, DIMS[:y] * 10, false)

  self.caption = 'Ruby AI'
  @drawn = []
  @ui = UI.new(self)
  @drawn << @ui
  @tiles = []
  @level = level

  load_level

  @ai = AI.new(@player)
  unless @ai.respond_to?('level', "#{@level}")
    puts "Your AI class must define a method 'level(num)'"
    @player.stop = true
  end
  @ai_tick = 0.5
  @last_ai_update = Time.now
end

Instance Attribute Details

#tilesObject (readonly)

Returns the value of attribute tiles.



14
15
16
# File 'lib/ruby-ai/window.rb', line 14

def tiles
  @tiles
end

Instance Method Details

#add_tile(pixel, x, y) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/ruby-ai/window.rb', line 104

def add_tile(pixel, x, y)
  if pixel_equal?(pixel, COLORS[:wall])
    wall = Wall.new(self, {:x => x, :y => y})
    @tiles << wall
    @drawn << wall
  elsif pixel_equal?(pixel, COLORS[:player])
    @player = Player.new(self, {:x => x, :y => y})
    @drawn << @player

    empty = Empty.new(self, {:x => x, :y => y})
    @tiles << empty
  elsif pixel_equal?(pixel, COLORS[:goal])
    @goal = Goal.new(self, {:x => x, :y => y})
    @tiles << @goal
    @drawn << @goal
  elsif pixel_equal?(pixel, COLORS[:empty])
    empty = Empty.new(self, {:x => x, :y => y})
    @tiles << empty
  end
end

#button_down(id) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/ruby-ai/window.rb', line 175

def button_down(id)
  if id == Gosu::KbEscape
    close
  elsif id == Gosu::KbReturn
    if @player.stop || @won
      exit
    end
    if @player.pause
      @player.pause = false
    else
      @player.pause = true
    end
  elsif id == Gosu::KbDown
    @ai_tick = @ai_tick * 2 unless @ai_tick > 1
  elsif id == Gosu::KbUp
    @ai_tick = @ai_tick / 2 unless @ai_tick < 0.01
  end
end

#centering_modifiers(image_dims) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/ruby-ai/window.rb', line 87

def centering_modifiers(image_dims)
  mods = {:x => 0, :y => 0}
  [:x, :y].each do |dim|
    if image_dims[dim] < DIMS[dim]
      diff = DIMS[dim]- image_dims[dim]
      mods[dim] = diff / 2
    end
  end
  mods
end

#drawObject



171
172
173
# File 'lib/ruby-ai/window.rb', line 171

def draw
  @drawn.each {|o| o.send(:draw)}
end

#load_levelObject



47
48
49
50
51
52
53
54
55
56
57
58
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
85
# File 'lib/ruby-ai/window.rb', line 47

def load_level
  level_image = Gosu::Image.new(self, APP_DIR + "levels/level#{@level}.png", false)
  puts "Loading #{level_image.width * level_image.height} tiles."
  image_dims = {:x => level_image.width, :y => level_image.height}

  reverse = {:x => [true, false].sample, :y => [true, false].sample}
  # reverse = {:x => false, :y => false}
  rotate_by = [0, 90, 180, 270].sample
  # rotate_by = 270

  count = 0
  coords = {}
  mods = centering_modifiers(image_dims)

  image_dims[:x].times do |x|
    image_dims[:y].times do |y|
      count += 1
      if count > 99
        print '.'
        count = 0
      end

      coords[:x] = x
      coords[:y] = y

      [:x, :y].each do |dim|
        coords[dim] = image_dims[dim] - 1 - coords[dim] if reverse[dim]
      end

      (rotate_by / 90).times do
        rotate90!(image_dims, coords)
      end

      pixel = level_image.get_pixel(x, y)
      add_tile(pixel, (coords[:x] + mods[:x]) * 10, (coords[:y] + mods[:y]) * 10)
    end
  end
  puts 'Done.'
end

#pixel_equal?(pixel, values) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
128
129
130
# File 'lib/ruby-ai/window.rb', line 125

def pixel_equal?(pixel, values)
  4.times do |i|
    return false unless rough_equal?(pixel[i], values[i])
  end
  true
end

#rotate90!(dims, coords) ⇒ Object



98
99
100
101
102
# File 'lib/ruby-ai/window.rb', line 98

def rotate90!(dims, coords)
  old = coords[:y]
  coords[:y] = coords[:x]
  coords[:x] = dims[:y] - 1 - old
end

#rough_equal?(float1, float2) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
135
136
# File 'lib/ruby-ai/window.rb', line 132

def rough_equal?(float1, float2)
  one = (float1 * 100).to_i
  two = (float2 * 100).to_i
  one == two
end

#updateObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/ruby-ai/window.rb', line 138

def update
  if won?
    @ui.text = 'Success! Press <enter> to exit.'
    return
  end

  if @player.stop
    @ui.text = 'Game over. Press <enter> to exit.'
    return
  end

  if @player.pause
    @ui.text = 'Paused. Press <enter> to resume.'
    return
  end

  @ui.text = ''

  if Time.now - @last_ai_update > @ai_tick
    @last_ai_update = Time.now
    @player.send(:moved=, false)
    @ai.level(@level.to_i)
  end
end

#won?Boolean

Returns:

  • (Boolean)


163
164
165
166
167
168
169
# File 'lib/ruby-ai/window.rb', line 163

def won?
  return true if @won
  if @player.x == @goal.x && @player.y == @goal.y
    puts 'Success!'
    @won = true
  end
end