Class: Core::Tools::WorldmapEditor

Inherits:
Window
  • Object
show all
Includes:
REXML
Defined in:
lib/tools/worldmap_editor.rb

Instance Method Summary collapse

Constructor Details

#initializeWorldmapEditor

Returns a new instance of WorldmapEditor.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/tools/worldmap_editor.rb', line 25

def initialize
  super(1024, 768, false)
  self.caption = "Essytas Worldmap Editor"
  Core.window = self
  @font = Core.font(Core::DEFAULT_FONT, 20)
  @xoff = @yoff = 0
  @unpress = []
  @gui = {
    :list => Core::GUI::Container.new(0, 0, 192, 768, 24)
  }
  @maps = []
  start = Time.now.sec
  Dir.new("maps/").each { |file|
    next if File.directory?(file) or File.extname(file) != ".tmx"
    map = Core::Parse.map(file.sub(".tmx", ""), true)
    if !map.properties[:name] or map.properties[:name].empty?
      map.properties[:name] = file.sub(".tmx", "")
    end
    map = AbstractMap.new(map.properties[:name], file.sub(".tmx", ""))
    @maps.push(map)
    @gui[:list].add(Core::GUI::Button.new(0, 0, 168, 24, map.name, lambda { clicked(map) }, true, :left))
  }
  puts("INFO: Loaded #{@maps.size} maps in #{Time.now.sec - start} seconds")
  @world = load_world
end

Instance Method Details

#check_attribute(doc, attribute) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/tools/worldmap_editor.rb', line 117

def check_attribute(doc, attribute)
  if !doc.root.elements['/map/properties/property[@name="'+attribute+'"]']
    props = doc.root.elements['/map/properties[1]']
    if !props
      props = Element.new('properties')
      doc.root.elements['/map'] << props
      props = doc.root.elements['/map/properties[1]']
    end
    prop = Element.new("property")
    prop.add_attribute("name", "#{attribute}")
    prop.add_attribute("value", "")
    props << prop
    doc.root.elements.add(props)
  else
    puts("found attribute #{attribute}")
  end
end

#clicked(map) ⇒ Object



183
184
185
# File 'lib/tools/worldmap_editor.rb', line 183

def clicked(map)
  @current = map
end

#drawObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/tools/worldmap_editor.rb', line 153

def draw
  @gui.each_value { |el|
    el.draw
  }
  y = 0
  @world.size.times {
    x = 0
    @world.size.times {
      if @world[y][x]
        px = 192 + @xoff + (x * 32)
        py = @yoff + (y * 24)
        c = Gosu::Color::GRAY
        draw_quad(px, py, c, px+32, py, c, px+32, py+24, c, px, py+24, c) 
      end
      x += 1
    }
    y += 1
  }
  x = (mouse_x.to_i - 192) / 32
  y = mouse_y.to_i / 24
  if @world[y] and @world[y][x]
    c = Gosu::Color::WHITE
    @font.draw(@world[y][x].name, mouse_x + 12, mouse_y + 12, 200, 1, 1, Gosu::Color::BLACK)
    w = @font.text_width(@world[y][x].name) + 8
    x = mouse_x + 8
    y = mouse_y + 8
    draw_quad(x, y, c, x+w, y, c, x+w, y+24, c, x, y+24, c, 100)
  end
end

#load_worldObject



64
65
66
67
68
69
70
71
72
73
# File 'lib/tools/worldmap_editor.rb', line 64

def load_world
  begin
    file = File.open("tools/world.map", "r")
    world = Marshal.load(file)
    file.close
    return world
  rescue
    return Array.new(@maps.size) { Array.new(@maps.size) }
  end
end

#needs_cursor?Boolean

Returns:

  • (Boolean)


207
208
209
# File 'lib/tools/worldmap_editor.rb', line 207

def needs_cursor?
  return true
end

#place_map(mx, my) ⇒ Object



135
136
137
138
139
140
141
142
# File 'lib/tools/worldmap_editor.rb', line 135

def place_map(mx, my)
  x = mx.to_i/32
  y = my.to_i/24
  if y >= @world.size or x >= @world.first.size
    return
  end
  @world[y][x] = @current
end

#pressed?(key) ⇒ Boolean

Returns:

  • (Boolean)


187
188
189
190
191
192
193
194
195
196
197
# File 'lib/tools/worldmap_editor.rb', line 187

def pressed?(key)
  p = button_down?(key)
  if p
    if @unpress.include?(key)
      p = false
    else
      @unpress.push(key)
    end
  end
  return p
end

#property_element(doc, attribute) ⇒ Object



113
114
115
# File 'lib/tools/worldmap_editor.rb', line 113

def property_element(doc, attribute)
  return doc.root.elements['/map/properties/property[@name="'+attribute+'"]']
end

#remove_map(mx, my) ⇒ Object



144
145
146
147
148
149
150
151
# File 'lib/tools/worldmap_editor.rb', line 144

def remove_map(mx, my)
  x = mx.to_i/32
  y = my.to_i/24
  if y >= @world.size or x >= @world.first.size
    return
  end
  @world[y][x] = nil
end

#save_worldObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/tools/worldmap_editor.rb', line 75

def save_world
  Marshal.dump(@world, File.open("tools/world.map", "w"))
  y = 0
  @world.size.times {
    x = 0
    @world[y].size.times {
      if @world[y][x]
        @world[y][x].left = @world[y][x - 1] if x > 0
        @world[y][x].right = @world[y][x + 1] if x < @world[y].size
        @world[y][x].upper = @world[y - 1][x] if y > 0
        @world[y][x].lower = @world[y + 1][x] if y < @world.size
        doc = Document.new(File.open("maps/#{@world[y][x].file}.tmx"))
        doc.context[:attribute_quote] = :quote
        
        if @world[y][x].upper
          check_attribute(doc, "upper")
          property_element(doc, "upper").add_attribute("value", @world[y][x].upper.file)
        end
        if @world[y][x].lower
          check_attribute(doc, "lower")
          property_element(doc, "lower").add_attribute("value", @world[y][x].lower.file)
        end
        if @world[y][x].left
          check_attribute(doc, "left")
          property_element(doc, "left").add_attribute("value", @world[y][x].left.file)
        end
        if @world[y][x].right
          check_attribute(doc, "right")
          property_element(doc, "right").add_attribute("value", @world[y][x].right.file)
        end
        doc.write(File.open("maps/#{@world[y][x].file}.tmx", "w"))
      end
      x += 1
    }
    y += 1
  }
end

#unpressObject



199
200
201
202
203
204
205
# File 'lib/tools/worldmap_editor.rb', line 199

def unpress
  @unpress.each { |key|
    if !button_down?(key)
      @unpress.delete(key)
    end
  }
end

#updateObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/tools/worldmap_editor.rb', line 51

def update
  @gui.each_value { |el|
    el.update
  }
  if @current and pressed?(Gosu::MsLeft) and mouse_x > 192
    place_map(mouse_x - 192, mouse_y)
  end
  if pressed?(Gosu::MsRight) and mouse_x > 192
    remove_map(mouse_x - 192, mouse_y)
  end
  unpress
end