Class: Tmap::Map

Inherits:
Object
  • Object
show all
Defined in:
lib/tmap/map.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, speed, start_x, start_y) ⇒ Map

Returns a new instance of Map.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/tmap/map.rb', line 9

def initialize(path, speed, start_x, start_y)
  map = Psych.load_file(path)
  parse_file(map)
  @tileset = Gosu::Image.load_tiles($window, @tileset_file, @tile_size, @tile_size, false)
  @speed = speed
  @view_size_h = $window.height / @tile_size
  @view_size_w = $window.width / @tile_size
  @sliced_view_size_h = (@view_size_h / 2).floor
  @sliced_view_size_w = (@view_size_w / 2).floor
  @x = start_x
  @y = start_y
end

Instance Attribute Details

#speedObject

Returns the value of attribute speed.



7
8
9
# File 'lib/tmap/map.rb', line 7

def speed
  @speed
end

#xObject

Returns the value of attribute x.



7
8
9
# File 'lib/tmap/map.rb', line 7

def x
  @x
end

#yObject

Returns the value of attribute y.



7
8
9
# File 'lib/tmap/map.rb', line 7

def y
  @y
end

Instance Method Details

#drawObject



43
44
45
46
47
48
49
50
# File 'lib/tmap/map.rb', line 43

def draw
  map = viewable_map
  map.each_with_index do |line, j|
    line.each_with_index do |tile, i|
      @tileset[@tiles[tile].picture_id].draw(i * @tile_size, j * @tile_size, Zorder::MAP)
    end
  end
end

#player_xObject



22
23
24
# File 'lib/tmap/map.rb', line 22

def player_x
  @sliced_view_size_x * @tile_size
end

#player_yObject



26
27
28
# File 'lib/tmap/map.rb', line 26

def player_y
  @sliced_view_size_y * @tile_size
end

#update(direction) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tmap/map.rb', line 30

def update(direction)
  case direction
  when :left
    @x -= @speed unless blocked_or_out_of_bounds?(@x-@speed, @map[@y][@x - @speed], :width)
  when :right
    @x += @speed unless blocked_or_out_of_bounds?(@x+@speed, @map[@y][@x + @speed], :width)
  when :up
    @y -= @speed unless blocked_or_out_of_bounds?(@y-@speed, @map[@y - @speed][@x], :height)
  when :down
    @y += @speed unless blocked_or_out_of_bounds?(@y+@speed, @map[@y + @speed][@x], :height)
  end
end