Class: Conjuration::GameObject

Inherits:
Object
  • Object
show all
Includes:
Attributes
Defined in:
lib/conjuration/game_object.rb

Direct Known Subclasses

Button, Text

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Attributes

included, #inspect

Constructor Details

#initialize(x:, y:, width:, height:) ⇒ GameObject

Returns a new instance of GameObject.



18
19
20
21
22
23
24
25
26
27
# File 'lib/conjuration/game_object.rb', line 18

def initialize(x:, y:, width:, height:)
  super

  self.x = x
  self.y = y
  self.width = width
  self.height = height
  self.hovering = false
  self.clicking = Hash.new(false)
end

Class Method Details

.create(*args, scene:, **kwargs, &block) ⇒ Object



29
30
31
32
33
# File 'lib/conjuration/game_object.rb', line 29

def self.create(*args, scene:, **kwargs, &block)
  game_object = new(*args, **kwargs, &block)
  scene.add_object(game_object)
  game_object
end

Instance Method Details

#draw(context) ⇒ Object

Abstract Methods



55
# File 'lib/conjuration/game_object.rb', line 55

def draw(context); end

#in_bounds?(context) ⇒ Boolean

Helpers

Returns:

  • (Boolean)


44
45
46
47
48
49
# File 'lib/conjuration/game_object.rb', line 44

def in_bounds?(context)
  context.mouse_x >= x &&
    context.mouse_x <= x + width &&
    context.mouse_y >= y &&
    context.mouse_y <= y + height
end

#mouse_down(scene, key) ⇒ Object

Clicking



80
81
82
83
84
85
86
# File 'lib/conjuration/game_object.rb', line 80

def mouse_down(scene, key)
  on_mouse_down(scene, key)

  return unless !clicking[key] && scene.window.input_manager.button_down?(key) && in_bounds?(scene.window)

  clicking[key] = true
end

#mouse_entered(context) ⇒ Object



70
71
72
73
74
# File 'lib/conjuration/game_object.rb', line 70

def mouse_entered(context)
  return unless !hovering && in_bounds?(context)

  self.hovering = true
end

#mouse_exited(context) ⇒ Object

Hovering



64
65
66
67
68
# File 'lib/conjuration/game_object.rb', line 64

def mouse_exited(context)
  return unless hovering && !in_bounds?(context)

  self.hovering = false
end

#mouse_position(context) ⇒ Object



35
36
37
38
# File 'lib/conjuration/game_object.rb', line 35

def mouse_position(context)
  mouse_exited(context)
  mouse_entered(context)
end

#mouse_up(scene, key) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/conjuration/game_object.rb', line 88

def mouse_up(scene, key)
  on_mouse_up(scene, key)

  on_click(scene, key) if clicking[key] && !scene.window.input_manager.button_down?(key) && in_bounds?(scene.window)

  clicking[key] = false
end

#on_click(context, key) ⇒ Object



56
# File 'lib/conjuration/game_object.rb', line 56

def on_click(context, key); end

#on_mouse_down(context, key) ⇒ Object



57
# File 'lib/conjuration/game_object.rb', line 57

def on_mouse_down(context, key); end

#on_mouse_up(context, key) ⇒ Object



58
# File 'lib/conjuration/game_object.rb', line 58

def on_mouse_up(context, key); end