Class: WolfRpg::Map

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

Defined Under Namespace

Classes: Event

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Map

Returns a new instance of Map.



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/wolfrpg/map.rb', line 11

def initialize(filename)
  @filename = File.basename(filename, '.*')
  FileCoder.open(filename, :read) do |coder|
    coder.verify(MAGIC_NUMBER)

    @tileset_id = coder.read_int

    # Read basic data
    @width = coder.read_int
    @height = coder.read_int
    coder.skip(4) # skip event count
    @events = []

    # Read tiles
    #TODO: interpret this data later
    @tiles = coder.read(@width * @height * 3 * 4)

    # Read events
    while (indicator = coder.read_byte) == 0x6F
      @events << Event.new(coder)
    end
    if indicator != 0x66
      raise "unexpected event indicator: #{indicator.to_s(16)}"
    end
    unless coder.eof?
      raise "file not fully parsed"
    end
  end
end

Instance Attribute Details

#eventsObject (readonly)

Returns the value of attribute events.



6
7
8
# File 'lib/wolfrpg/map.rb', line 6

def events
  @events
end

#filenameObject (readonly)

DEBUG



9
10
11
# File 'lib/wolfrpg/map.rb', line 9

def filename
  @filename
end

#heightObject (readonly)

Returns the value of attribute height.



5
6
7
# File 'lib/wolfrpg/map.rb', line 5

def height
  @height
end

#tileset_idObject (readonly)

Returns the value of attribute tileset_id.



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

def tileset_id
  @tileset_id
end

#widthObject (readonly)

Returns the value of attribute width.



4
5
6
# File 'lib/wolfrpg/map.rb', line 4

def width
  @width
end

Instance Method Details

#dump(filename) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/wolfrpg/map.rb', line 41

def dump(filename)
  FileCoder.open(filename, :write) do |coder|
    coder.write(MAGIC_NUMBER)
    coder.write_int(@tileset_id)
    coder.write_int(@width)
    coder.write_int(@height)
    coder.write_int(@events.size)
    coder.write(@tiles)
    @events.each do |event|
      next unless event
      coder.write_byte(0x6F)
      event.dump(coder)
    end
    coder.write_byte(0x66)
  end
end

#grep(needle) ⇒ Object

DEBUG method that searches for a string somewhere in the map



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/wolfrpg/map.rb', line 59

def grep(needle)
  @events.each do |event|
    event.pages.each do |page|
      page.commands.each_with_index do |command, line|
        command.string_args.each do |arg|
          if m = arg.match(needle)
            print "#{@filename}/#{event.id}/#{page.id+1}/#{line+1}: #{command.cid}\n\t#{command.args}\n\t#{command.string_args}\n"
            break
          end
        end
      end
    end
  end
end

#grep_cid(cid) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/wolfrpg/map.rb', line 74

def grep_cid(cid)
  @events.each do |event|
    event.pages.each do |page|
      page.commands.each_with_index do |command, line|
        if command.cid == cid
          print "#{@filename}/#{event.id}/#{page.id+1}/#{line+1}: #{command.cid}\n\t#{command.args}\n\t#{command.string_args}\n"
        end
      end
    end
  end
end