Module: CyberarmEngine::Common

Included in:
BackgroundImage, BackgroundNineSlice, Console, Element, Element::Container, GameObject, GameState, GuiState, Window
Defined in:
lib/cyberarm_engine/common.rb

Instance Method Summary collapse

Instance Method Details

#alt_down?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/cyberarm_engine/common.rb', line 124

def alt_down?
  Gosu.button_down?(Gosu::KB_LEFT_ALT) || Gosu.button_down?(Gosu::KB_RIGHT_ALT)
end

#control_down?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/cyberarm_engine/common.rb', line 116

def control_down?
  Gosu.button_down?(Gosu::KB_LEFT_CONTROL) || Gosu.button_down?(Gosu::KB_RIGHT_CONTROL)
end

#current_stateObject



7
8
9
# File 'lib/cyberarm_engine/common.rb', line 7

def current_state
  window.current_state
end

#darken(color, amount = 25) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/cyberarm_engine/common.rb', line 62

def darken(color, amount = 25)
  if color.respond_to?(:alpha)
    Gosu::Color.rgba(color.red - amount, color.green - amount, color.blue - amount, color.alpha)
  else
    Gosu::Color.rgb(color.red - amount, color.green - amount, color.blue - amount)
  end
end

#draw_rect(x, y, width, height, color, z = 0, mode = :default) ⇒ Object



46
47
48
# File 'lib/cyberarm_engine/common.rb', line 46

def draw_rect(x, y, width, height, color, z = 0, mode = :default)
  Gosu.draw_rect(x, y, width, height, color, z, mode)
end

#fill(color, z = 0) ⇒ Object



50
51
52
# File 'lib/cyberarm_engine/common.rb', line 50

def fill(color, z = 0)
  draw_rect(0, 0, window.width, window.height, color, z)
end

#find_element_by_tag(container, tag, list = []) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cyberarm_engine/common.rb', line 34

def find_element_by_tag(container, tag, list = [])
  return unless container

  container.children.each do |child|
    list << child if child.style.tag == tag

    find_element_by_tag(child, tag, list) if child.is_a?(CyberarmEngine::Element::Container)
  end

  list.first
end

#get_asset(path, hash, klass, retro = false, tileable = false) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/cyberarm_engine/common.rb', line 76

def get_asset(path, hash, klass, retro = false, tileable = false)
  asset = nil
  hash.detect do |_asset, instance|
    if _asset == path
      asset = instance
      true
    end
  end

  unless asset
    instance = nil
    instance = if klass == Gosu::Image
                 klass.new(path, retro: retro, tileable: tileable)
               else
                 klass.new(path)
               end

    hash[path] = instance
    asset = instance
  end

  asset
end

#get_image(path, retro: false, tileable: false) ⇒ Object



100
101
102
# File 'lib/cyberarm_engine/common.rb', line 100

def get_image(path, retro: false, tileable: false)
  get_asset(path, Window::IMAGES, Gosu::Image, retro, tileable)
end

#get_sample(path) ⇒ Object



104
105
106
# File 'lib/cyberarm_engine/common.rb', line 104

def get_sample(path)
  get_asset(path, Window::SAMPLES, Gosu::Sample)
end

#get_song(path) ⇒ Object



108
109
110
# File 'lib/cyberarm_engine/common.rb', line 108

def get_song(path)
  get_asset(path, Window::SONGS, Gosu::Song)
end

#lighten(color, amount = 25) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/cyberarm_engine/common.rb', line 54

def lighten(color, amount = 25)
  if color.respond_to?(:alpha)
    Gosu::Color.rgba(color.red + amount, color.green + amount, color.blue + amount, color.alpha)
  else
    Gosu::Color.rgb(color.red + amount, color.green + amount, color.blue + amount)
  end
end

#opacity(color, ratio = 1.0) ⇒ Object



70
71
72
73
74
# File 'lib/cyberarm_engine/common.rb', line 70

def opacity(color, ratio = 1.0)
  alpha = 255 * ratio

  Gosu::Color.rgba(color.red, color.green, color.blue, alpha)
end

#pop_stateObject



18
19
20
# File 'lib/cyberarm_engine/common.rb', line 18

def pop_state
  window.pop_state
end

#previous_state(state = nil) ⇒ Object



11
12
13
14
15
16
# File 'lib/cyberarm_engine/common.rb', line 11

def previous_state(state = nil)
  raise "Only available for CyberarmEngine::GameState and subclasses" unless is_a?(CyberarmEngine::GameState) || state.is_a?(CyberarmEngine::GameState)

  i = window.states.index(state || self)
  window.states[i - 1] unless (i - 1).negative?
end

#push_state(klass, options = {}) ⇒ Object



3
4
5
# File 'lib/cyberarm_engine/common.rb', line 3

def push_state(klass, options = {})
  window.push_state(klass, options)
end

#shift_down?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/cyberarm_engine/common.rb', line 120

def shift_down?
  Gosu.button_down?(Gosu::KB_LEFT_SHIFT) || Gosu.button_down?(Gosu::KB_RIGHT_SHIFT)
end

#shift_stateObject



22
23
24
# File 'lib/cyberarm_engine/common.rb', line 22

def shift_state
  window.shift_state
end

#show_cursorObject



26
27
28
# File 'lib/cyberarm_engine/common.rb', line 26

def show_cursor
  window.show_cursor
end

#show_cursor=(boolean) ⇒ Object



30
31
32
# File 'lib/cyberarm_engine/common.rb', line 30

def show_cursor=(boolean)
  window.show_cursor = boolean
end

#windowObject



112
113
114
# File 'lib/cyberarm_engine/common.rb', line 112

def window
  CyberarmEngine::Window.instance
end