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



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
# 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)

  size = 16

  @levels  = @save_file.levels
  @levels.each {|level| level.each {|sprite| sprite.x = sprite.x * size; sprite.y = sprite.y * size}}

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

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

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

  @collision_detection = AuthorEngine::CollisionDetection.new(@sprites, @levels, @save_file.sprites)
  @game.authorengine_collision_detection = @collision_detection

  @game.init

  @show_touch_controls = false
  @touch_joystick = TouchJoystick.new(x: 100, radius: 50, side: :left)
  @touch_buttons = []
  @touch_buttons.push(
    TouchButton.new(
      label: "X", color: @game.red, x: 50, width: 50, height: 50, side: :right, for_key: "x"
      ),
    TouchButton.new(
      label: "Y", color: @game.yellow, x: 125, width: 50, height: 50, side: :right, for_key: "y"
    )
  )

  @fullscreen_button = TouchButton.new(label: "Fullscreen", color: @game.black, x: 50, y: 10, width: 100, height: 50, side: :right)
  touch_handler_setup

  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



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 156

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



62
63
64
65
66
# File 'lib/author_engine/game/opal/game_runner.rb', line 62

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

#draw_touch_controlsObject



121
122
123
124
125
# File 'lib/author_engine/game/opal/game_runner.rb', line 121

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

#fullscreen_changedObject



200
201
202
# File 'lib/author_engine/game/opal/game_runner.rb', line 200

def fullscreen_changed
  resize_canvas
end

#load_spritesObject



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/author_engine/game/opal/game_runner.rb', line 177

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



132
133
# File 'lib/author_engine/game/opal/game_runner.rb', line 132

def reposition_touch_controls
end

#resize_canvasObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/author_engine/game/opal/game_runner.rb', line 135

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

  if width < height
    @game.authorengine_scale = `window.innerWidth / 128.0`
  else
    @game.authorengine_scale = `window.innerHeight / 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
  return nil
end

#run_gameObject



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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/author_engine/game/opal/game_runner.rb', line 73

def run_game
  `window.requestAnimationFrame(function() {#{run_game}})` # placed here to ensure next frame is called even if draw or update throw an error
  `#{@game.authorengine_canvas_context}.clearRect(0,0, window.innerWidth, window.innerHeight)`
  `#{@game.authorengine_canvas_context}.fillStyle = "#222"`
  `#{@game.authorengine_canvas_context}.fillRect(0,0, window.innerWidth, window.innerHeight)`

  @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


  if @sprites.size == (@spritesheet_width/@sprite_size)*(@spritesheet_height/@sprite_size)
    width = 128 * @game.authorengine_scale

    # `#{@game.authorengine_canvas_context}.setTransform(1, 0, 0, 1, 0, 0)`
    `#{@game.authorengine_canvas_context}.save()`
    `#{@game.authorengine_canvas_context}.translate(window.innerWidth/2 - #{width/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()`
    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
    @game.draw_background
    @game.text("Loading sprite #{@sprites.size}/#{(@spritesheet_width/@sprite_size)*(@spritesheet_height/@sprite_size)}...", 0, @game.height/2, 8)
  end

  return nil
end

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



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/author_engine/game/opal/game_runner.rb', line 204

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



68
69
70
71
# File 'lib/author_engine/game/opal/game_runner.rb', line 68

def update
  @game.update
  return nil
end

#update_touch_controlsObject



127
128
129
130
# File 'lib/author_engine/game/opal/game_runner.rb', line 127

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