Class: AuthorEngine::LevelPicker

Inherits:
Object
  • Object
show all
Includes:
Support
Defined in:
lib/author_engine/level_picker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Support

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

Constructor Details

#initialize(x: nil, y: nil, z: 15) ⇒ LevelPicker

Returns a new instance of LevelPicker.



7
8
9
10
11
12
13
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
# File 'lib/author_engine/level_picker.rb', line 7

def initialize(x: nil, y: nil, z: 15)
  @x, @y, @z = x, y, z

  @offset = 1 * window.square_scale
  @width = window.width - @offset * 4
  @height = (window.sprite_size * window.square_scale) + @offset * 2

  @x = window.width / 2 - self.width / 2

  @current_level = AuthorEngine::Text.new(y: @y + @offset*2, z: @z + 1)

  @active_level = 0
  @active_layer = 0

  @max_layers = 16

  @buttons = []

  @buttons << Button.new(label: "-", color: Gosu::Color::GRAY,  x: (window.width / 2) - @offset*8, y: @y + @offset*2, z: 18, tooltip: "↓ Decrease Layer") do
    @active_layer-=1 unless @active_layer < 1
  end
  @buttons << Button.new(label: "+", color: Gosu::Color::GRAY, x: (@buttons.last.x + @buttons.last.width) + @offset*2, y: @y + @offset*2, z: 18, tooltip: "↑ Increase Layer") do
    @active_layer+=1 if @active_layer < @max_layers
  end

  @buttons << Button.new(label: "<", x: (@x + @width) - @width/3, y: @y + @offset*2, z: 18, tooltip: "← Previous Level", color: Gosu::Color::GRAY) do
    @active_level-=1
    @active_level = LevelEditor.instance.levels.size - 1 if @active_level < 0
  end

  @buttons << Button.new(label: "-", x: @buttons.last.x + @buttons.last.width + @offset, y: @y + @offset*2, z: 18, tooltip: "Destroy Level (Non-recoverable)", color: Gosu::Color::RED) do
    if LevelEditor.instance.levels[@active_level].is_a?(Array)
      LevelEditor.instance.levels.delete_at(@active_level)
      @active_level-=1
    end

    if LevelEditor.instance.levels.size == 0
      LevelEditor.instance.levels.push([])
      @active_level = LevelEditor.instance.levels.size - 1
    end
  end

  @buttons << Button.new(label: "+", x: @buttons.last.x + @buttons.last.width + @offset, y: @y + @offset*2, z: 18, tooltip: "Add Level", color: Gosu::Color::GREEN) do
    LevelEditor.instance.levels.push([])
    @active_level = LevelEditor.instance.levels.size - 1
  end


  @buttons << Button.new(label: ">", x: @buttons.last.x + @buttons.last.width + @offset, y: @y + @offset*2, z: 18, tooltip: "→ Next Level", color: Gosu::Color::GRAY) do
    @active_level+=1
    @active_level = 0 unless @active_level < LevelEditor.instance.levels.size
  end
end

Instance Attribute Details

#active_layerObject (readonly)

Returns the value of attribute active_layer.



6
7
8
# File 'lib/author_engine/level_picker.rb', line 6

def active_layer
  @active_layer
end

#active_levelObject (readonly)

Returns the value of attribute active_level.



6
7
8
# File 'lib/author_engine/level_picker.rb', line 6

def active_level
  @active_level
end

#heightObject (readonly)

Returns the value of attribute height.



5
6
7
# File 'lib/author_engine/level_picker.rb', line 5

def height
  @height
end

#offsetObject (readonly)

Returns the value of attribute offset.



5
6
7
# File 'lib/author_engine/level_picker.rb', line 5

def offset
  @offset
end

#widthObject (readonly)

Returns the value of attribute width.



5
6
7
# File 'lib/author_engine/level_picker.rb', line 5

def width
  @width
end

#xObject (readonly)

Returns the value of attribute x.



5
6
7
# File 'lib/author_engine/level_picker.rb', line 5

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



5
6
7
# File 'lib/author_engine/level_picker.rb', line 5

def y
  @y
end

#zObject (readonly)

Returns the value of attribute z.



5
6
7
# File 'lib/author_engine/level_picker.rb', line 5

def z
  @z
end

Instance Method Details

#button_up(id) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/author_engine/level_picker.rb', line 72

def button_up(id)
  @buttons.each {|b| b.button_up(id) }

  case id
  when Gosu::KbUp
    @active_layer+=1 if @active_layer < @max_layers

  when Gosu::KbDown
    @active_layer-=1 unless @active_layer < 1

  when Gosu::KbLeft
    @active_level -= 1
    @active_level = LevelEditor.instance.levels.size - 1 if @active_level < 0

  when Gosu::KbRight
    @active_level += 1
    @active_level = 0 unless @active_level < LevelEditor.instance.levels.size
  end
end

#drawObject



61
62
63
64
65
66
67
68
69
70
# File 'lib/author_engine/level_picker.rb', line 61

def draw
  Gosu.draw_rect(@x, @y, @width, @height, Gosu::Color.rgba(255,255,255,220), @z)
  Gosu.draw_rect(@x+4, @y+4, @width-8, @height-8, Gosu::Color.rgba(10, 10, 10,200), @z)

  @buttons.each(&:draw)

  @current_level.message = "Level: #{@active_level}\nLayer: #{@active_layer}/#{@max_layers}"
  @current_level.x = @x + @offset*2
  @current_level.draw
end