bestguigui 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c098e1e773be91e7506d48bb7f1c2fef8025af453f60add33b975be1aa836aff
4
+ data.tar.gz: 981e6bf032e7389399657099a8deae6ea70e610de92ca803fe71d3e9aee65397
5
+ SHA512:
6
+ metadata.gz: 1f4cb28f78d7535d7c7bf740441c9233dd711f4f27ffdfb0803ab6538a15e1fcd662b29744b321479a845bc18842248cca41098939c64d8dabfaceba8bb0e19a
7
+ data.tar.gz: 8fb391d5e19062ecda593814427dac9ee454ab44f53c28e9ef9f493502d51f91023c74ab0940ee655b9dfd1b9d1270c61419215cfc9f2b0bfb66cf0d9c0a2c8c
@@ -0,0 +1,37 @@
1
+ module Bestguigui
2
+ # Sous-classe pour ton jeu, appelle switch_state pour changer d'écran.
3
+ class App < Gosu::Window
4
+ def initialize(width: 800, height: 600, title: "Game")
5
+ super(width, height)
6
+ self.caption = title
7
+ @last_update_time = Gosu.milliseconds
8
+ end
9
+
10
+ def switch_state(state)
11
+ @state&.exit
12
+ @state = state
13
+ @state.app = self
14
+ @state.enter
15
+ end
16
+
17
+ def update
18
+ now = Gosu.milliseconds
19
+ dt = (now - @last_update_time) / 1000.0
20
+ @last_update_time = now
21
+
22
+ @state&.update(dt)
23
+ end
24
+
25
+ def draw
26
+ @state&.draw
27
+ end
28
+
29
+ def button_down(id)
30
+ @state&.button_down(id)
31
+ end
32
+
33
+ def button_up(id)
34
+ @state&.button_up(id)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,55 @@
1
+ module Bestguigui
2
+ # Rectangle positionné (x, y, width, height) avec détection de collision
3
+ # AABB. Sous-classe pour tes joueurs/ennemis/objets, avec ton propre
4
+ # update/draw.
5
+ #
6
+ # class Player < Bestguigui::Entity
7
+ # def initialize
8
+ # super(x: 400, y: 280, width: 40, height: 40)
9
+ # end
10
+ #
11
+ # def update(dt)
12
+ # self.x += 200 * dt if Gosu.button_down?(Gosu::KB_RIGHT)
13
+ # end
14
+ #
15
+ # def draw
16
+ # Gosu.draw_rect(x, y, width, height, Gosu::Color::YELLOW, 1)
17
+ # end
18
+ # end
19
+ #
20
+ # player.collides?(coin) # => true/false
21
+ class Entity
22
+ attr_accessor :x, :y, :width, :height
23
+
24
+ def initialize(x: 0, y: 0, width: 0, height: 0)
25
+ @x = x
26
+ @y = y
27
+ @width = width
28
+ @height = height
29
+ end
30
+
31
+ def update(dt); end
32
+ def draw; end
33
+
34
+ def left
35
+ x
36
+ end
37
+
38
+ def right
39
+ x + width
40
+ end
41
+
42
+ def top
43
+ y
44
+ end
45
+
46
+ def bottom
47
+ y + height
48
+ end
49
+
50
+ # Chevauchement de rectangles (AABB), le classique en 2D pour du jam.
51
+ def collides?(other)
52
+ left < other.right && right > other.left && top < other.bottom && bottom > other.top
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,23 @@
1
+ module Bestguigui
2
+ # Associe des actions nommées à un ou plusieurs boutons (clavier, manette,
3
+ # souris — Gosu les identifie tous par le même type d'id, donc une manette
4
+ # se rajoute sans rien changer d'autre).
5
+ #
6
+ # @keys = Bestguigui::Input.new(
7
+ # up: [Gosu::KB_UP, Gosu::KB_W],
8
+ # down: [Gosu::KB_DOWN, Gosu::KB_S],
9
+ # left: [Gosu::KB_LEFT, Gosu::KB_A],
10
+ # right: [Gosu::KB_RIGHT, Gosu::KB_D]
11
+ # )
12
+ #
13
+ # @keys.down?(:up) # true si une des touches liées à :up est enfoncée
14
+ class Input
15
+ def initialize(bindings)
16
+ @bindings = bindings
17
+ end
18
+
19
+ def down?(action)
20
+ (@bindings[action] || []).any? { |key| Gosu.button_down?(key) }
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,124 @@
1
+ module Bestguigui
2
+ # Liste de choix avec un index sélectionné. Gère l'input (haut/bas/valider)
3
+ # et son propre rendu (texte + fond + bordure optionnels) — un state n'a en
4
+ # général rien d'autre à écrire que la construction du menu.
5
+ #
6
+ # @menu = Bestguigui::Menu.new(
7
+ # {
8
+ # "New Game" => lambda { app.switch_state(GameplayState.new) },
9
+ # "Load" => lambda { app.switch_state(LoadState.new) },
10
+ # "Exit" => lambda { app.close! }
11
+ # },
12
+ # x: 300, y: 220,
13
+ # font_size: 32,
14
+ # color: Gosu::Color::WHITE,
15
+ # selected_color: Gosu::Color::YELLOW,
16
+ # background: Gosu::Color.new(200, 20, 20, 20),
17
+ # border_color: Gosu::Color::WHITE,
18
+ # border_width: 2,
19
+ # padding: 16,
20
+ # line_height: 40
21
+ # )
22
+ #
23
+ # def button_down(id) = @menu.button_down(id)
24
+ # def draw = @menu.draw
25
+ #
26
+ # Rendu custom possible via each_item si le style intégré ne convient pas :
27
+ # @menu.each_item { |label, selected| ... }
28
+ class Menu
29
+ def initialize(items, x: 0, y: 0, font_size: 28, color: Gosu::Color::WHITE,
30
+ selected_color: Gosu::Color::YELLOW, background: nil,
31
+ border_color: nil, border_width: 2, padding: 12, line_height: 40)
32
+ @labels = items.keys
33
+ @actions = items.values
34
+ @index = 0
35
+
36
+ @x = x
37
+ @y = y
38
+ @font_size = font_size
39
+ @color = color
40
+ @selected_color = selected_color
41
+ @background = background
42
+ @border_color = border_color
43
+ @border_width = border_width
44
+ @padding = padding
45
+ @line_height = line_height
46
+ end
47
+
48
+ def up
49
+ @index = (@index - 1) % @labels.size
50
+ end
51
+
52
+ def down
53
+ @index = (@index + 1) % @labels.size
54
+ end
55
+
56
+ def confirm
57
+ @actions[@index].call
58
+ end
59
+
60
+ # Mapping clavier par défaut : flèches ou Z/S pour naviguer,
61
+ # Entrée/Espace pour valider.
62
+ def button_down(id)
63
+ case id
64
+ when Gosu::KB_UP, Gosu::KB_W then up
65
+ when Gosu::KB_DOWN, Gosu::KB_S then down
66
+ when Gosu::KB_RETURN, Gosu::KB_SPACE then confirm
67
+ end
68
+ end
69
+
70
+ def selected_label
71
+ @labels[@index]
72
+ end
73
+
74
+ # yield(label, selected?) pour chaque item, dans l'ordre.
75
+ def each_item
76
+ @labels.each_with_index { |label, i| yield label, i == @index }
77
+ end
78
+
79
+ def font
80
+ @font ||= Gosu::Font.new(@font_size)
81
+ end
82
+
83
+ def draw
84
+ draw_background
85
+ draw_border
86
+ draw_items
87
+ end
88
+
89
+ private
90
+
91
+ def box_width
92
+ @labels.map { |label| font.text_width("> #{label}") }.max + @padding * 2
93
+ end
94
+
95
+ def box_height
96
+ @padding * 2 + @labels.size * @line_height
97
+ end
98
+
99
+ def draw_background
100
+ return unless @background
101
+
102
+ Gosu.draw_rect(@x, @y, box_width, box_height, @background, 0)
103
+ end
104
+
105
+ def draw_border
106
+ return unless @border_color
107
+
108
+ w, h, t = box_width, box_height, @border_width
109
+ Gosu.draw_rect(@x, @y, w, t, @border_color, 1) # haut
110
+ Gosu.draw_rect(@x, @y + h - t, w, t, @border_color, 1) # bas
111
+ Gosu.draw_rect(@x, @y, t, h, @border_color, 1) # gauche
112
+ Gosu.draw_rect(@x + w - t, @y, t, h, @border_color, 1) # droite
113
+ end
114
+
115
+ def draw_items
116
+ text_y = @y + @padding
117
+ each_item do |label, selected|
118
+ text = selected ? "> #{label}" : " #{label}"
119
+ font.draw_text(text, @x + @padding, text_y, 2, 1, 1, selected ? @selected_color : @color)
120
+ text_y += @line_height
121
+ end
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,13 @@
1
+ module Bestguigui
2
+ # Sous-classe pour chaque écran (menu, jeu, game over...).
3
+ class State
4
+ attr_accessor :app
5
+
6
+ def enter; end
7
+ def exit; end
8
+ def update(dt); end
9
+ def draw; end
10
+ def button_down(id); end
11
+ def button_up(id); end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module Bestguigui
2
+ VERSION = "0.1.0"
3
+ end
data/lib/bestguigui.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "gosu"
2
+
3
+ require_relative "bestguigui/version"
4
+ require_relative "bestguigui/state"
5
+ require_relative "bestguigui/input"
6
+ require_relative "bestguigui/menu"
7
+ require_relative "bestguigui/entity"
8
+ require_relative "bestguigui/app"
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bestguigui
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Guillaume Quillet
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: gosu
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '1.4'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '1.4'
26
+ description: States, menus, input bindings and simple entities on top of Gosu, built
27
+ to move fast during game jams.
28
+ email:
29
+ - qgdevfr@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/bestguigui.rb
35
+ - lib/bestguigui/app.rb
36
+ - lib/bestguigui/entity.rb
37
+ - lib/bestguigui/input.rb
38
+ - lib/bestguigui/menu.rb
39
+ - lib/bestguigui/state.rb
40
+ - lib/bestguigui/version.rb
41
+ homepage: https://github.com/guillaumequillet/bestguigui
42
+ licenses:
43
+ - MIT
44
+ metadata:
45
+ source_code_uri: https://github.com/guillaumequillet/bestguigui
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '3.0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubygems_version: 4.0.16
61
+ specification_version: 4
62
+ summary: A small Gosu layer to build game jam games faster.
63
+ test_files: []