Class: AuthorEngine::PlayViewer

Inherits:
View
  • Object
show all
Defined in:
lib/author_engine/views/play_viewer.rb

Constant Summary

Constants included from AuthorEngine::Part::Colors

AuthorEngine::Part::Colors::COLORS

Instance Attribute Summary

Attributes inherited from View

#background, #height, #width, #x, #y

Instance Method Summary collapse

Methods inherited from View

#blur, #button_down, #initialize, instance, instance=, #mouse_inside_view?

Methods included from AuthorEngine::Part::Colors

#black, #blue, #brown, #dark_blue, #dark_gray, #dark_green, #dark_purple, #green, #indigo, #light_gray, #orange, #peach, #pink, #red, #rgb, #white, #xml_color, #yellow

Methods included from Support

#code_editor, #mouse_over?, #sprite_editor, #window

Constructor Details

This class inherits a constructor from AuthorEngine::View

Instance Method Details

#button_up(id) ⇒ Object



153
154
155
156
157
158
159
160
161
162
# File 'lib/author_engine/views/play_viewer.rb', line 153

def button_up(id)
  if id == Gosu::MsLeft && window.mouse_x.between?((window.width / 2) - (@loading_icon.width/2) * window.square_scale, (window.width / 2) + (@loading_icon.width/2) * window.square_scale)
    if window.mouse_y.between?((window.height / 2) - (@loading_icon.height/2) * window.square_scale, (window.height / 2) + (@loading_icon.height/2) * window.square_scale)
      play
    end
  end

  stop if id == Gosu::KbEscape
  play if id == Gosu::KbP && window.control_button_down?
end

#catch_error(&block) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/author_engine/views/play_viewer.rb', line 37

def catch_error(&block)
  begin
    block.call if block
  rescue => error
    error_response(error)
  rescue SyntaxError => error
    error_response(error)
  end
end

#drawObject



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

def draw
  if @running
    Gosu.draw_rect(0, 0, window.width, window.height, @background)
    Gosu.flush
    catch_error do
      draw_game
    end
  else
    super
    if @last_error
      Gosu.scale(2,2, (window.width / 2) - (@error_icon.width / 2), (window.height / 2) - (@error_icon.height / 2)) do
        @error_icon.draw_rot(window.width / 2, window.height / 2, 10, 0, 0.5, 0.5, 1 * window.square_scale, 1 * window.square_scale, @error_icon_colors[@error_icon_color])
      end
      @error_message.draw_markup
    else
      @loading_icon.draw_rot(window.width / 2, window.height / 2, 10, @loading_icon_angle, 0.5, 0.5, 1 * window.square_scale, 1 * window.square_scale)
      @text.draw
    end
  end
end

#draw_gameObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/author_engine/views/play_viewer.rb', line 126

def draw_game
  x_offset = 0
  ratio    = Window::VIEW_WIDTH / window.width
  if window.scale_x != window.scale_y
    x_offset = window.width / 2 - (window.width * ratio * window.square_scale) / 2
  end

  Gosu.clip_to(x_offset, 0, (window.base_size * window.square_scale), (window.base_size * window.square_scale)) do
    Gosu.translate(x_offset, 0) do
      Gosu.scale(window.square_scale, window.square_scale) do
        @game.draw_background
        @game.draw
      end
    end
  end
end

#error_response(error) ⇒ Object



47
48
49
50
51
52
# File 'lib/author_engine/views/play_viewer.rb', line 47

def error_response(error)
  stop

  @last_error = error
  format_error(text: @error_message, error: error)
end

#focusObject



77
78
79
80
# File 'lib/author_engine/views/play_viewer.rb', line 77

def focus
  window.caption = "Play"
  play
end

#format_error(text:, error:) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/author_engine/views/play_viewer.rb', line 54

def format_error(text:, error:)
  puts "#{error.class}: #{error.message}"
  puts error.backtrace.join("\n")

  max_width = window.width - (@x_padding*2)
  char_width= text.font.text_width("0")
  chars_line= (max_width.to_f / char_width.to_f).ceil

  backtrace = "<c=#{xml_color(orange)}>#{error.class}</c>\nBacktrace:\n"
  error.backtrace.each {|trace| next unless trace.include?("(eval)"); backtrace+="  #{trace}\n"}
  trace_buffer = "#{error.message}"
  buffer = ""

  trace_buffer.lines do |line|
    line.chomp.chars.each_slice(chars_line).each do |slice|
      string = slice.join
      buffer += "  #{string}\n"
    end
  end

  text.message = "#{backtrace}#{buffer}"
end

#playObject



82
83
84
85
86
# File 'lib/author_engine/views/play_viewer.rb', line 82

def play
  catch_error do
    run(code: code_editor.code)
  end
end

#run(code: nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/author_engine/views/play_viewer.rb', line 26

def run(code: nil)
  @last_error = nil
  @game = Game.new(code: code)

  @running = true
  window.show_cursor = false
  window.container.lock

  @game.init
end

#setupObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/author_engine/views/play_viewer.rb', line 3

def setup
  @running = false

  @error_icon = AuthorEngine::Image.new("assets/ui/error_icon.png", retro: true)
  @error_icon_color = 0
  @error_icon_colors = [yellow, orange]

  @loading_icon = AuthorEngine::Image.new("assets/ui/loading_icon.png", retro: true)
  @loading_icon_angle = 0
  @loading_icon_rot_step = 45

  @icon_rot_period = 500
  @last_icon_rot = Gosu.milliseconds

  @error_message = Text.new(message: "", x: @x_padding, y: window.container.header_height, z: Float::INFINITY)

  @text = Text.new(message: "Press [Control+P] to Play")
  @text.x = window.width/2 - @text.width/2
  @text.y = window.height/2 + @text.height + @loading_icon.height * window.square_scale

  @last_error = nil
end

#stopObject



147
148
149
150
151
# File 'lib/author_engine/views/play_viewer.rb', line 147

def stop
  @running = false
  window.show_cursor = true
  window.container.unlock
end

#updateObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/author_engine/views/play_viewer.rb', line 109

def update
  # super
  if @running
    catch_error do
      @game.authorengine_collision_detection.clear
      update_game
    end
  else
    if (Gosu.milliseconds - @last_icon_rot) > @icon_rot_period
      @loading_icon_angle = (@loading_icon_angle + @loading_icon_rot_step) % 360
      @error_icon_color += 1
      @error_icon_color = 0 if @error_icon_color == @error_icon_colors.size
      @last_icon_rot = Gosu.milliseconds
    end
  end
end

#update_gameObject



143
144
145
# File 'lib/author_engine/views/play_viewer.rb', line 143

def update_game
  @game.update if @game
end