Class: AuthorEngine::SaveFile

Inherits:
Object
  • Object
show all
Defined in:
lib/author_engine/save_file.rb

Defined Under Namespace

Classes: SpriteSheetData

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ SaveFile

Returns a new instance of SaveFile.



20
21
22
23
24
25
26
27
28
# File 'lib/author_engine/save_file.rb', line 20

def initialize(file)
  @file = file
  @buffer = ""
  @mode = :compact # or :inflated

  @code, @sprites, @levels = nil, nil, nil

  load unless RUBY_ENGINE == "opal"
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



19
20
21
# File 'lib/author_engine/save_file.rb', line 19

def code
  @code
end

#fileObject (readonly)

Returns the value of attribute file.



18
19
20
# File 'lib/author_engine/save_file.rb', line 18

def file
  @file
end

#levelsObject (readonly)

Returns the value of attribute levels.



19
20
21
# File 'lib/author_engine/save_file.rb', line 19

def levels
  @levels
end

#modeObject (readonly)

Returns the value of attribute mode.



18
19
20
# File 'lib/author_engine/save_file.rb', line 18

def mode
  @mode
end

#spritesObject (readonly)

Returns the value of attribute sprites.



19
20
21
# File 'lib/author_engine/save_file.rb', line 19

def sprites
  @sprites
end

Class Method Details

.create(file) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/author_engine/save_file.rb', line 5

def self.create(file)
  if File.exists?(file)
    return false
  else
    File.open(file, "w") {|f| f.write ""}
    if File.exists?(file)
      return true
    else
      return false
    end
  end
end

Instance Method Details

#identify_mode(string) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/author_engine/save_file.rb', line 132

def identify_mode(string)
  if RUBY_ENGINE != "opal" && string.lines.size > 0 && string.lines.first.include?("# inflated")
    @mode = :inflated
  else
    @mode = :compact
  end
end

#inflate!Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/author_engine/save_file.rb', line 44

def inflate!
  load
  @mode = :inflated
  save_code(@code)
  save_spritesheet(Gosu::Image.new(@sprites, retro: true))

  file = File.read(@file)
  unless file.lines.first.include?("# inflated")
    File.open(@file, "w") do |f|
      f.write "# inflated\n"
      f.write file
    end
  end

end

#inflated?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/author_engine/save_file.rb', line 60

def inflated?
  @mode == :inflated
end

#load(from_file = true, data = nil) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/author_engine/save_file.rb', line 117

def load(from_file = true, data = nil)
  string = ""
  if from_file
    File.open(@file, "r") {|f| string = f.read}
  else
    string = data
  end

  identify_mode(string)

  load_code(string)
  load_spritesheet(string)
  load_levels(string)
end

#load_code(string) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/author_engine/save_file.rb', line 140

def load_code(string)
  buffer = ""
  in_code= false
  string.each_line do |line|
    if line.start_with?("___CODE___")
      if line.strip.include?("?") && inflated?
        # load from file
        puts "Loading code from: #{line.strip.split("?").last}"
        buffer = File.read(line.strip.split("?").last)
        break
      end
      in_code = true
      next
    end
    if line.start_with?("___") && in_code
      break
    end

    buffer+="#{line}" if in_code
  end

  @code = buffer
end

#load_levels(string) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/author_engine/save_file.rb', line 214

def load_levels(string)
  levels   = []
  in_level = false

  string.each_line do |line|
    if line.start_with?("___LEVELS___")
      # if line.strip.start_with?("__LEVELS___?")
      #   # load from file
      #   puts "Loading level data from: #{line.strip.split("?").last}"

      #   break
      # end
      in_level = true
      next
    end
    if line.start_with?("___") && in_level
      break
    end

    if in_level
      level = []
      # 0 - Sprite, 1 - X, 2 - Y, 3 - Z
      line.strip.split(",").each_slice(4) do |sprite|
        level << Sprite.new(Integer(sprite[0]), Integer(sprite[1]), Integer(sprite[2]), Integer(sprite[3]))
      end
      levels << level
    end
  end

  @levels = levels
end

#load_spritesheet(string) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/author_engine/save_file.rb', line 164

def load_spritesheet(string)
  buffer = ""
  width  = 0
  height = 0
  in_sprites = false

  string.each_line do |line|
    if line.strip.start_with?("___SPRITES___")
      if line.strip.include?("?") && inflated?
        # load from file
        puts "Loading spritesheet from: #{line.strip.split("?").last}"
        image  = Gosu::Image.new(line.strip.split("?").last, retro: true)
        buffer = image.to_blob
        width, height = image.width, image.height
        break
      end
      in_sprites = true
      next
    end
    if line.start_with?("___") && in_sprites
      break
    end

    next unless in_sprites

    if line.include?("x")
      a = line.strip.split("x")
      width = a.first.to_i
      height = a.last.to_i
      next
    end


    buffer += line.strip
  end

  stream = nil
  if RUBY_ENGINE != "opal"
    if inflated?
      stream = buffer
    else
      stream = buffer.scan(/../).map { |x| x.hex }.pack('c*')
    end
  else
    stream = buffer.scan(/../).map { |x| Integer(x.hex) }
  end

  @sprites = SpriteSheetData.new(width, height, stream)
end

#project_nameObject



64
65
66
# File 'lib/author_engine/save_file.rb', line 64

def project_name
  File.basename(@file, ".authorengine")
end

#project_pathObject



68
69
70
# File 'lib/author_engine/save_file.rb', line 68

def project_path
  File.expand_path(@file).sub(File.basename(@file), "")
end

#saveObject



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

def save
  @buffer = "# inflated\n" if inflated?
  save_code
  save_spritesheet
  save_levels

  File.open(@file, "w") do |f|
    f.write @buffer
  end

  @buffer = ""
  puts "Saved #{file}"
end

#save_code(code = CodeEditor.instance.code) ⇒ Object

code is a String



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/author_engine/save_file.rb', line 73

def save_code(code = CodeEditor.instance.code)
  if inflated?
    @buffer+= "___CODE___?#{project_path}#{project_name}.rb\n"
    File.write("#{project_path}#{project_name}.rb", code)
    puts "Saved code to #{project_path}#{project_name}.rb"
  else
    @buffer+= "___CODE___\n"
  end
  @buffer+= code
  # @buffer+="\n" # CodeEditor always has this newline
end

#save_levelsObject



109
110
111
112
113
114
115
# File 'lib/author_engine/save_file.rb', line 109

def save_levels
  @buffer+= "___LEVELS___\n"
  LevelEditor.instance.levels.each do |level|
    @buffer+= "#{level.map {|s| "#{s.sprite},#{s.x},#{s.y},#{s.z}"}.join(",")}\n"
  end
  @buffer.strip # Level is the last element saved
end

#save_spritesheet(sheet = SpriteEditor.instance.spritesheet) ⇒ Object

sheet is a Gosu::Image



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/author_engine/save_file.rb', line 86

def save_spritesheet(sheet = SpriteEditor.instance.spritesheet)

  if inflated?
    @buffer+= "___SPRITES___?#{project_path}#{project_name}.png\n"
    sheet.save("#{project_path}#{project_name}.png")
    puts "Saved spritesheet to #{project_path}#{project_name}.png"
  else
    @buffer+= "___SPRITES___\n"
  end

  @buffer+="#{sheet.width}x#{sheet.height}"
  @buffer+="\n"

  pack = sheet.to_blob.unpack('H*').first
  # @buffer+= pack
  pack.chars.each_slice(1024) do |slice|
    @buffer+=slice.join
    @buffer+="\n"
  end

  @buffer+="\n"
end