class Area

Trida Area reprezentuje mapu, kde se odehrava hra

Attributes

crofts[R]

Pole policek pro vykrelseni mapy

gh_pos[R]

vychozi pozice duchu na mape

pc_pos[R]

vychozi pozice pacmana na mape

Public Class Methods

new() click to toggle source

Konstruktor vytvoti novou mapu

# File lib/area.rb, line 8
def initialize
  @crofts = []
  @map = 'mapa.txt'
  load_map
end

Public Instance Methods

bonus(l_c, t_c) click to toggle source

Overuje, jestli souradnice koliduji s bonusem na mape

  • Args :

    • l_c -> poloha na ose X

    • t_c -> poloha na ose Y

  • Returns :

    • 0 -> souradnice nekoliduji

    • 1 -> souradnice koliduji s malym bonusem

    • 10 -> souradnice koliduji s velkym bonusem

# File lib/area.rb, line 85
def bonus(l_c, t_c)
  @crofts.each do |line_croft|
    line_croft.each do |croft|
      if !croft.visited && colision?(l_c, t_c, croft, 2)
        is_bonus_val = bonus?(croft)
        return is_bonus_val if is_bonus_val > 0
      end
    end
  end
  0
end
colidate(l_c, t_c) click to toggle source

Overuje, jestli souradnice koliduji s polickem na mape

  • Args :

    • l_c -> poloha na ose X

    • t_c -> poloha na ose Y

  • Returns :

    • true -> souradnice nekoliduji

    • false -> souradnice koliduji

# File lib/area.rb, line 59
def colidate(l_c, t_c)
  @crofts.each do |line_croft|
    line_croft.each do |croft|
      return false if croft.type == '#' && colision?(l_c, t_c, croft, 1)
    end
  end
  true
end

Private Instance Methods

bonus?(croft) click to toggle source
# File lib/area.rb, line 97
def bonus?(croft)
  if croft.type == '*'
    croft.set_visited
    return 1
  elsif croft.type == '@'
    croft.set_visited
    return 10
  end
  0
end
colision?(l_c, t_c, croft, param) click to toggle source
# File lib/area.rb, line 68
def colision?(l_c, t_c, croft, param)
  c = CROFTWIDTH
  c_l_c = l_c - (croft.left + c / 2)
  c_t_c = t_c - (croft.top + c / 2)
  return true if c_l_c.abs < c / param && c_t_c.abs < c / param
  false
end
line_parse(line) click to toggle source
# File lib/area.rb, line 22
def line_parse(line)
  c = CROFTWIDTH
  t_l_c = []
  line.split('').each do |char|
    left = t_l_c.length * c
    top = @crofts.length * c + Y_START
    pac_or_gh(char, left, top) if char == 'P' || char == 'M'
    t_l_c.push(Croft.new(left, top, char))
  end
  t_l_c
end
load_map() click to toggle source
# File lib/area.rb, line 14
def load_map
  File.open(@map, 'r') do |f|
    f.each_line do |line|
      @crofts.push(line_parse(line))
    end
  end
end
pac_or_gh(char, left, top) click to toggle source
# File lib/area.rb, line 34
def pac_or_gh(char, left, top)
  if char == 'P'
    @pc_pos = { x: left, y: top }
  elsif char == 'M'
    @gh_pos = { x: left, y: top }
  end
end