Class: AuthorEngine::OpalExporter

Inherits:
Object
  • Object
show all
Defined in:
lib/author_engine/game/opal/exporter.rb

Instance Method Summary collapse

Constructor Details

#initialize(project_file:) ⇒ OpalExporter

Returns a new instance of OpalExporter.



7
8
9
10
11
# File 'lib/author_engine/game/opal/exporter.rb', line 7

def initialize(project_file:)
  @project_file = project_file

  save(export)
end

Instance Method Details

#build_authorengine?Boolean

Rebuild author_engine runtime if it doesn’t exist or if its out of date

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/author_engine/game/opal/exporter.rb', line 34

def build_authorengine?
  authorengine_runtime = "#{export_directory}/js/author_engine.js"

  if File.exists?(authorengine_runtime)
    file = File.open(authorengine_runtime)
    version = file.first.gsub("/", "").strip
    file.close

    AuthorEngine::VERSION != version
  else
    true
  end
end

#build_opal?Boolean

Rebuild opal runtime if it doesn’t exist or if its out of date

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/author_engine/game/opal/exporter.rb', line 19

def build_opal?
  opal_runtime = "#{export_directory}/js/runtime.js"

  if File.exists?(opal_runtime)
    file = File.open(opal_runtime)
    version = file.first.gsub("/", "").strip
    file.close

    Opal::VERSION != version
  else
    true
  end
end

#exportObject



187
188
189
# File 'lib/author_engine/game/opal/exporter.rb', line 187

def export
  template
end

#export_directoryObject



191
192
193
194
195
196
197
# File 'lib/author_engine/game/opal/exporter.rb', line 191

def export_directory
  filename  = File.basename(@project_file)
  directory = File.expand_path(@project_file.sub(filename, ''))
  name      = filename.sub(".authorengine", "")

  return "#{directory}/#{name}"
end

#game_runtimeObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/author_engine/game/opal/exporter.rb', line 88

def game_runtime
  program = %{
require "author_engine/opal"

`var callback = function(){
  \#{AuthorEngine::GameRunner.new(`projectString`).show}
};

if (
document.readyState === "complete" ||
(document.readyState !== "loading" && !document.documentElement.doScroll)
) {
  callback();
} else {
  document.addEventListener("DOMContentLoaded", callback);
}`
  }

  puts "Transpiling to JavaScript using Opal..."

  opal_builder = nil
  if build_opal?
    puts "  Building Opal runtime..."

    opal_builder = Opal::Builder.new
    opal_builder.build("opal")
    opal_builder.build("opal-parser")
  else
    puts "  Skipping Opal runtime. Already exists and up to date (v#{Opal::VERSION})..."
  end

  author_engine_builder = nil
  if build_authorengine?
    puts "  Building AuthorEngine runtime..."

    author_engine_builder = Opal::Builder.new
    base_path = File.expand_path("../../../..", __FILE__)
    author_engine_builder.append_paths("#{base_path}")

    author_engine_builder.build_require("author_engine/opal")
  else
    puts "  Skipping AuthorEngine runtime. Already exists and up to date (v#{AuthorEngine::VERSION})..."
  end

  opal_builder_js  = nil
  if opal_builder
    opal_runtime_js = opal_builder.build_str("", "(inline)").to_s
  end

  author_engine_js = nil
  if author_engine_builder
    author_engine_js = author_engine_builder.build_str(program, "(inline)").to_s
  end

  return {opal_runtime: opal_runtime_js, author_engine_runtime: author_engine_js}
end

#projectObject



80
81
82
83
84
85
86
# File 'lib/author_engine/game/opal/exporter.rb', line 80

def project
  file = File.read(@project_file)

  %{
var projectString = `#{file}`;
  }
end

#project_nameObject



13
14
15
16
# File 'lib/author_engine/game/opal/exporter.rb', line 13

def project_name
  name = File.basename(@project_file, ".authorengine")
  return name.split("_").map {|n| n.capitalize}.join(" ")
end

#save(string) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
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
245
246
247
# File 'lib/author_engine/game/opal/exporter.rb', line 199

def save(string)
  filename  = File.basename(@project_file)
  directory = File.expand_path(@project_file.sub(filename, ''))
  name      = filename.sub(".authorengine", "")
  export_path = "#{directory}/#{name}"

  unless File.exists?(export_path)
    Dir.mkdir(export_path)
  end
  unless File.exists?("#{export_path}/fonts")
    Dir.mkdir("#{export_path}/fonts")
  end
  unless File.exists?("#{export_path}/js")
    Dir.mkdir("#{export_path}/js")
  end

  puts "Saving to \"#{export_path}\""
  File.open("#{export_path}/#{name}.html", "w") do |file|
    file.write(string)
  end

  hash = game_runtime
  if hash[:opal_runtime]
    File.open("#{export_path}/js/runtime.js", "w") do |file|
      file.write("// #{Opal::VERSION}\n")
      file.write(hash[:opal_runtime])
    end
  end

  puts "  Building game..."
  File.open("#{export_path}/game.js", "w") do |file|
    file.write(project)
  end

  if hash[:author_engine_runtime]
    File.open("#{export_path}/js/author_engine.js", "w") do |file|
      file.write("// #{AuthorEngine::VERSION}\n")
      file.write(hash[:author_engine_runtime])
    end
  end

  fonts_path = "#{File.expand_path("../../../../../", __FILE__)}/assets/fonts"
  font_files = Dir.glob("#{fonts_path}/*")
  font_files.each do |file|
    FileUtils.cp(file, "#{export_path}/fonts/#{File.basename(file)}")
  end

  puts "Saved."
end

#stylesheetObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/author_engine/game/opal/exporter.rb', line 48

def stylesheet
  %{
@font-face { font-family: Connection; src: url('fonts/Connection.otf'); }
@font-face { font-family: ConnectionBold; src: url('fonts/ConnectionBold.otf'); }

body {
  margin: 0;
  padding: 0;
  background: #222;
}
#canvas {
  display: block;
  margin: 0 auto;
  cursor: none;
}
#loading {
  font-family: Connection, sans-serif;
  color: white;
  text-align: center;

  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  width: 50%;
  height: 30%;
  margin: auto;
}
  }
end

#templateObject



145
146
147
148
149
150
151
152
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
182
183
184
185
# File 'lib/author_engine/game/opal/exporter.rb', line 145

def template
  %{
<!doctype html5>
<html>
  <head>
<meta content="width=device-width, initial-scale=1" name="viewport" />
<meta charset="utf-8" />
<title>#{project_name} | AuthorEngine</title>
  </head>
  <style>
#{stylesheet}
  </style>
  <body>
<h1 id="loading">Loading...</h1>
<canvas id="canvas">
  <h1>Your Browser Does Not Support HTML5 Canvas!</h1>
</canvas>

<script src="game.js"></script>
<script>
  // Add a small delay before loading application in order to finish loading page and show "Loading..."
  window.setTimeout(function() {
    console.log("Loading Opal runtime...");

    var opal_runtime = document.createElement('script');
    opal_runtime.onload = function() {
      console.log("Loading AuthorEngine runtime...");

      var author_engine_runtime = document.createElement('script');
      author_engine_runtime.src = "js/author_engine.js";
      document.head.appendChild(author_engine_runtime);
    }
    opal_runtime.src = "js/runtime.js";

    document.head.appendChild(opal_runtime);
  }, 500);
</script>
  </body>
</html>
  }
end