Class: Game

Inherits:
Gosu::Window
  • Object
show all
Defined in:
lib/fantasy/loop.rb

Instance Method Summary collapse

Constructor Details

#initialize(screen_width, screen_height) ⇒ Game

Returns a new instance of Game.



4
5
6
7
8
9
10
11
# File 'lib/fantasy/loop.rb', line 4

def initialize(screen_width, screen_height)
  Camera.initialize
  Global.initialize(screen_width, screen_height)

  super(Global.screen_width, Global.screen_height)

  Global.presentation_proc.call
end

Instance Method Details

#button_down(button_id) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fantasy/loop.rb', line 14

def button_down(button_id)
  case button_id
  when Cursor.down then cursor_down_pressed
  when Cursor.up then cursor_up_pressed
  when Cursor.left then cursor_left_pressed
  when Cursor.right then cursor_right_pressed
  when Cursor.space_bar then space_bar_pressed

  when Mouse.left then mouse_button_left_pressed
  end

  Global.button_proc&.call(button_id)

  super
end

#check_clickObject



86
87
88
89
90
91
92
93
# File 'lib/fantasy/loop.rb', line 86

def check_click
  (
    Global.actors +
    Global.shapes
  ).sort_by(&:layer).each do |e|
    e.on_click_do if Utils.collision_at?(e, mouse_x, mouse_y)
  end
end

#cursor_down_pressedObject

rubocop:enable Metrics/CyclomaticComplexity



31
32
33
34
# File 'lib/fantasy/loop.rb', line 31

def cursor_down_pressed
  Global.cursor_down_proc&.call
  invoke_input_method_in_entities("on_cursor_down_do")
end

#cursor_left_pressedObject



41
42
43
44
# File 'lib/fantasy/loop.rb', line 41

def cursor_left_pressed
  Global.cursor_left_proc&.call
  invoke_input_method_in_entities("on_cursor_left_do")
end

#cursor_right_pressedObject



46
47
48
49
# File 'lib/fantasy/loop.rb', line 46

def cursor_right_pressed
  Global.cursor_right_proc&.call
  invoke_input_method_in_entities("on_cursor_right_do")
end

#cursor_up_pressedObject



36
37
38
39
# File 'lib/fantasy/loop.rb', line 36

def cursor_up_pressed
  Global.cursor_up_proc&.call
  invoke_input_method_in_entities("on_cursor_up_do")
end

#drawObject



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/fantasy/loop.rb', line 73

def draw
  Gosu.draw_rect(0, 0, Global.screen_width, Global.screen_height, Global.background)

  (
    Global.backgrounds +
    Global.tile_maps +
    Global.actors +
    Global.hud_texts +
    Global.hud_images +
    Global.shapes
  ).sort_by(&:layer).each(&:draw)
end

#mouse_button_left_pressedObject



56
57
58
59
60
# File 'lib/fantasy/loop.rb', line 56

def mouse_button_left_pressed
  Global.mouse_button_left_proc&.call

  check_click
end

#space_bar_pressedObject



51
52
53
54
# File 'lib/fantasy/loop.rb', line 51

def space_bar_pressed
  Global.space_bar_proc&.call
  invoke_input_method_in_entities("on_space_bar_do")
end

#updateObject



62
63
64
65
66
67
68
69
70
71
# File 'lib/fantasy/loop.rb', line 62

def update
  Global.update

  Global.actors.each(&:move)
  Global.hud_texts.each(&:move)
  Global.hud_images.each(&:move)
  Camera.main.move

  Global.loop_proc&.call
end