Class: AuthorEngine::GameRunner

Inherits:
Object
  • Object
show all
Includes:
TouchHandler
Defined in:
lib/author_engine/game/opal/game_runner.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TouchHandler

#copy_touch, #handle_touch_cancel, #handle_touch_end, #handle_touch_move, #handle_touch_start, #set_touch, #touch_handler_setup

Constructor Details

#initialize(project_string) ⇒ GameRunner

Returns a new instance of GameRunner.



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
40
41
42
43
44
45
46
47
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
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
# File 'lib/author_engine/game/opal/game_runner.rb', line 14

def initialize(project_string)
  AuthorEngine::GameRunner.instance=(self)

  @save_file = AuthorEngine::SaveFile.new(nil)
  @save_file.load(false, project_string)

  @sprites = []
  @spritesheet = nil
  @spritesheet_width  = @save_file.sprites.columns
  @spritesheet_height = @save_file.sprites.rows
  @sprite_size = 16

  @game = Game.new(code: @save_file.code)
  resize_canvas

  @fps = 0
  @counted_frames = 0
  @frame_count_stated_at = 0

  @show_touch_controls = false

  @game_loaded = false

  @loader_tasks = [
    [
      "Loading levels",
      proc {
        @levels  = @save_file.levels
        @levels.each {|level| level.each {|sprite| sprite.x = sprite.x * @sprite_size; sprite.y = sprite.y * @sprite_size}}
      }
    ],

    [
      "Evaluating game",
      proc {
        @game.authorengine_eval_code
      },
    ],

    [
      "Loading sprites",
      proc {
        build_spritesheet_and_sprites_list
      },
    ],

    [
      "Setting up collision detection",
      proc {
        @collision_detection = AuthorEngine::CollisionDetection.new(@sprites, @levels, @save_file.sprites)
        @game.authorengine_collision_detection = @collision_detection
      },
    ],

    [
      "Initializing game",
      proc {
        @game.init
      },
    ],

    [
      "Setting up touch controls",
      proc {
        @touch_joystick = TouchJoystick.new(radius: 50)
        @touch_buttons = []
        @touch_buttons.push(
          TouchButton.new(
            label: "X", color: @game.red, width: 50, height: 50, for_key: "x"
            ),
          TouchButton.new(
            label: "Y", color: @game.yellow, width: 50, height: 50, for_key: "y"
          )
        )

        @fullscreen_button = TouchButton.new(label: "Fullscreen", color: @game.black, width: 100, height: 50)
        touch_handler_setup
        reposition_touch_controls
      },
    ],

    [
      "Loading done",
      proc {
        @game_loaded = true
      },
    ],
  ]

  return self
end

Instance Attribute Details

#fpsObject (readonly)

Returns the value of attribute fps.



12
13
14
# File 'lib/author_engine/game/opal/game_runner.rb', line 12

def fps
  @fps
end

#gameObject (readonly)

Returns the value of attribute game.



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

def game
  @game
end

#levelsObject (readonly)

Returns the value of attribute levels.



12
13
14
# File 'lib/author_engine/game/opal/game_runner.rb', line 12

def levels
  @levels
end

#save_fileObject (readonly)

Returns the value of attribute save_file.



12
13
14
# File 'lib/author_engine/game/opal/game_runner.rb', line 12

def save_file
  @save_file
end

#spritesObject (readonly)

Returns the value of attribute sprites.



12
13
14
# File 'lib/author_engine/game/opal/game_runner.rb', line 12

def sprites
  @sprites
end

#spritesheetObject (readonly)

Returns the value of attribute spritesheet.



12
13
14
# File 'lib/author_engine/game/opal/game_runner.rb', line 12

def spritesheet
  @spritesheet
end

Class Method Details

.instanceObject



3
4
5
# File 'lib/author_engine/game/opal/game_runner.rb', line 3

def self.instance
  @instance
end

.instance=(klass) ⇒ Object



6
7
8
# File 'lib/author_engine/game/opal/game_runner.rb', line 6

def self.instance=(klass)
  @instance = klass
end

Instance Method Details

#build_spritesheet_and_sprites_listObject



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/author_engine/game/opal/game_runner.rb', line 266

def build_spritesheet_and_sprites_list
  spritesheet_data = @save_file.sprites
  width = spritesheet_data.columns
  height= spritesheet_data.rows
  size  = 16

  temp_canvas = `document.createElement('canvas')`
  temp_canvas_context = `#{temp_canvas}.getContext('2d')`
  `#{temp_canvas}.width  = #{spritesheet_data.columns}`
  `#{temp_canvas}.height = #{spritesheet_data.rows}`

  buffer = `new Uint8ClampedArray(#{spritesheet_data.to_blob})`
  image_data = `new ImageData(#{buffer}, #{width})`
  `#{temp_canvas_context}.putImageData(#{image_data}, 0, 0)`

  @spritesheet = `new Image()`
  `#{@spritesheet}.onload = function() { #{load_sprites} }`
  `#{@spritesheet}.src = #{temp_canvas}.toDataURL()`

end

#drawObject



106
107
108
109
110
# File 'lib/author_engine/game/opal/game_runner.rb', line 106

def draw
  @game.draw_background
  @game.draw
  return nil
end

#draw_touch_controlsObject



177
178
179
180
181
# File 'lib/author_engine/game/opal/game_runner.rb', line 177

def draw_touch_controls
  @fullscreen_button.draw
  @touch_buttons.each(&:draw)
  @touch_joystick.draw
end

#fullscreen_changedObject



310
311
312
# File 'lib/author_engine/game/opal/game_runner.rb', line 310

def fullscreen_changed
  resize_canvas
end

#load_spritesObject



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/author_engine/game/opal/game_runner.rb', line 287

def load_sprites
  spritesheet_data = @save_file.sprites
  width = spritesheet_data.columns
  height= spritesheet_data.rows
  size  = 16

  temp_canvas = `document.createElement('canvas')`
  temp_canvas_context = `#{temp_canvas}.getContext('2d')`
  `#{temp_canvas}.width  = #{size}`
  `#{temp_canvas}.height = #{size}`

  (height/size).times do |y|
    (width/size).times do |x|
      `#{temp_canvas_context}.clearRect(0,0, #{size}, #{size})`
      `#{temp_canvas_context}.drawImage(#{@spritesheet}, #{x * size}, #{y * size}, #{size}, #{size}, 0, 0, #{size}, #{size})`

      `createImageBitmap(#{@spritesheet}, #{x * size}, #{y * size}, #{size}, #{size}).then(sprite => { #{@sprites.push(`sprite`)} })`
    end
  end

  return nil
end

#reposition_touch_controlsObject



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
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
# File 'lib/author_engine/game/opal/game_runner.rb', line 188

def reposition_touch_controls
  return nil unless @touch_joystick

  width  = `window.innerWidth`
  height = `window.innerHeight`
  game_width = 128 * @game.authorengine_scale
  game_height = 128 * @game.authorengine_scale

  # place controls under game
  if width < height
    area_width  = width
    area_height = height - game_height

    puts "space: width #{area_width} x height #{area_height}"

    @touch_joystick.x = @touch_joystick.radius + @touch_joystick.radius
    @touch_joystick.y = game_height + area_height / 2

    padding = 10
    last_x = 20
    @touch_buttons.reverse.each do |button|
      button.x = width - (last_x + button.width)
      button.y = (height - area_height) + area_height / 2 - button.height / 2

      last_x += button.width + padding
    end

    @fullscreen_button.x = width - (width / 2 + @fullscreen_button.width / 2)
    @fullscreen_button.y = height - @fullscreen_button.height

  # place controls beside game
  else
    area_width  = (`window.innerWidth` - game_width) / 2
    area_height = game_height

    puts "space: width #{area_width} x height #{area_height}"

    @touch_joystick.x = @touch_joystick.radius + @touch_joystick.radius
    @touch_joystick.y = game_height / 2

    padding = 10
    last_x = 50
    @touch_buttons.reverse.each do |button|
      button.x = width - (last_x + button.width)
      button.y = game_height / 2 - button.height / 2

      last_x += button.width + padding
    end

    @fullscreen_button.x = width - @fullscreen_button.width
    @fullscreen_button.y = 0
  end

  return nil
end

#resize_canvasObject



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/author_engine/game/opal/game_runner.rb', line 244

def resize_canvas
  width  = `window.innerWidth`
  height = `window.innerHeight`

  if width < height
    @game.authorengine_scale = `#{width} / 128.0`
  else
    @game.authorengine_scale = `#{height} / 128.0`
  end

  `#{@game.authorengine_canvas}.width  = #{width}`
  `#{@game.authorengine_canvas}.height = #{height}`
  `#{@game.authorengine_canvas}.style.width  = #{width}`
  `#{@game.authorengine_canvas}.style.height = #{height}`

  `#{@game.authorengine_canvas_context}.imageSmoothingEnabled = false`

  reposition_touch_controls
  `#{@game.authorengine_canvas_context}.clearRect(0, 0, window.innerWidth, window.innerHeight)`
  return nil
end

#run_gameObject



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
144
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
# File 'lib/author_engine/game/opal/game_runner.rb', line 117

def run_game
  `window.requestAnimationFrame(function() {#{run_game}})` # placed here to ensure next frame is called even if draw or update throw an error
  width  = `window.innerWidth`
  height = `window.innerHeight`
  game_width = 128 * @game.authorengine_scale
  game_height = 128 * @game.authorengine_scale

  area_width  = (`window.innerWidth` - game_width) / 2

  `#{@game.authorengine_canvas_context}.clearRect(#{area_width},0, #{game_width}, #{game_height})`

  @counted_frames+=1

  if @game.milliseconds - @frame_count_stated_at >= 1000.0
    @fps = @counted_frames
    @frame_count_stated_at = @game.milliseconds
    @counted_frames = 0
  end

  `#{@game.authorengine_canvas_context}.save()`
  `#{@game.authorengine_canvas_context}.translate(window.innerWidth/2 - #{game_height/2}, 0)`
  `#{@game.authorengine_canvas_context}.scale(#{@game.authorengine_scale}, #{@game.authorengine_scale})`
  `#{@game.authorengine_canvas_context}.save()`

  region = `new Path2D()`
  `#{region}.rect(0, 0, 128, 128)`
  `#{@game.authorengine_canvas_context}.clip(#{region})`
  `#{@game.authorengine_canvas_context}.save()`


  if @game_loaded or @loader_tasks.empty?
    draw
    `#{@game.authorengine_canvas_context}.restore()`
    `#{@game.authorengine_canvas_context}.restore()`
    `#{@game.authorengine_canvas_context}.restore()`

    update

    if @show_touch_controls
      draw_touch_controls
      update_touch_controls
    end

  else
    task = @loader_tasks.shift
    @game.rect(0, 0, @game.width, @game.height, @game.dark_purple)
    @game.text("AuthorEngine v#{AuthorEngine::VERSION}", 2, @game.height / 2 - 20, 10)
    @game.text("#{task[0]}...", 6, @game.height / 2 - 4, 8, 0, @game.light_gray)
    @game.text("Empowered by Opal v#{Opal::VERSION}, a Ruby interpeter.", 4, @game.height - 6, 4, 0, @game.indigo)

    `#{@game.authorengine_canvas_context}.restore()`
    `#{@game.authorengine_canvas_context}.restore()`
    `#{@game.authorengine_canvas_context}.restore()`

    task[1].call
  end

  return nil
end

#show(update_interval = (1000.0 / 60)) ⇒ Object



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/author_engine/game/opal/game_runner.rb', line 314

def show(update_interval = (1000.0 / 60))
  return unless RUBY_ENGINE == "opal"

  `window.addEventListener('resize', () => { #{resize_canvas} })`
  `document.addEventListener('keydown', (event) => { #{@show_touch_controls = false; AuthorEngine::Part::OpalInput::KEY_STATES[`event.key`] = true} })`
  `document.addEventListener('keyup',   (event) => { #{AuthorEngine::Part::OpalInput::KEY_STATES[`event.key`] = false} })`

  `#{@game.authorengine_canvas}.addEventListener('touchstart',  (event) => { #{@show_touch_controls = true; handle_touch_start(`event`)} })`
  `#{@game.authorengine_canvas}.addEventListener('touchmove',   (event) => { #{handle_touch_move(`event`)} })`
  `#{@game.authorengine_canvas}.addEventListener('touchcancel', (event) => { #{handle_touch_cancel(`event`)} })`
  `#{@game.authorengine_canvas}.addEventListener('touchend',    (event) => { #{handle_touch_end(`event`)} })`

  `#{@game.authorengine_canvas}.addEventListener('fullscreenchange',    () => { #{fullscreen_changed} })`

  `document.getElementById('loading').style.display = "none"`

  `window.requestAnimationFrame(function() {#{run_game}})`
  return nil
end

#updateObject



112
113
114
115
# File 'lib/author_engine/game/opal/game_runner.rb', line 112

def update
  @game.update
  return nil
end

#update_touch_controlsObject



183
184
185
186
# File 'lib/author_engine/game/opal/game_runner.rb', line 183

def update_touch_controls
  @touch_buttons.each { |button| button.trigger?(@current_touches) }
  @touch_joystick.update(@current_touches)
end