Class: Bestguigui::Entity
- Inherits:
-
Object
- Object
- Bestguigui::Entity
- Defined in:
- lib/bestguigui/entity.rb
Overview
Rectangle positionné (x, y, width, height) avec détection de collision AABB. Sous-classe pour tes joueurs/ennemis/objets, avec ton propre update/draw.
class Player < Bestguigui::Entity
def initialize
super(x: 400, y: 280, width: 40, height: 40)
end
def update(dt)
self.x += 200 * dt if Gosu.(Gosu::KB_RIGHT)
end
def draw
Gosu.draw_rect(x, y, width, height, Gosu::Color::YELLOW, 1)
end
end
player.collides?(coin) # => true/false
player.collides_circle?(snowball) # => true/false, cercle inscrit dans le rectangle
Instance Attribute Summary collapse
-
#height ⇒ Object
Returns the value of attribute height.
-
#width ⇒ Object
Returns the value of attribute width.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Instance Method Summary collapse
- #bottom ⇒ Object
- #center_x ⇒ Object
- #center_y ⇒ Object
-
#collides?(other) ⇒ Boolean
Chevauchement de rectangles (AABB), le classique en 2D pour du jam.
-
#collides_circle?(other) ⇒ Boolean
Chevauchement de cercles (distance entre centres <= somme des rayons) — plus naturel que l'AABB pour des balles, projectiles, portées d'attaque.
- #draw ⇒ Object
-
#initialize(x: 0, y: 0, width: 0, height: 0) ⇒ Entity
constructor
A new instance of Entity.
- #left ⇒ Object
-
#radius ⇒ Object
Rayon du cercle inscrit dans le rectangle (moitié du plus grand côté) — utilisé par collides_circle?, pratique pour des projectiles/balles/ zones de portée sans avoir à gérer une forme séparée.
- #right ⇒ Object
- #top ⇒ Object
- #update(dt) ⇒ Object
Constructor Details
#initialize(x: 0, y: 0, width: 0, height: 0) ⇒ Entity
Returns a new instance of Entity.
25 26 27 28 29 30 |
# File 'lib/bestguigui/entity.rb', line 25 def initialize(x: 0, y: 0, width: 0, height: 0) @x = x @y = y @width = width @height = height end |
Instance Attribute Details
#height ⇒ Object
Returns the value of attribute height.
23 24 25 |
# File 'lib/bestguigui/entity.rb', line 23 def height @height end |
#width ⇒ Object
Returns the value of attribute width.
23 24 25 |
# File 'lib/bestguigui/entity.rb', line 23 def width @width end |
#x ⇒ Object
Returns the value of attribute x.
23 24 25 |
# File 'lib/bestguigui/entity.rb', line 23 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
23 24 25 |
# File 'lib/bestguigui/entity.rb', line 23 def y @y end |
Instance Method Details
#bottom ⇒ Object
47 48 49 |
# File 'lib/bestguigui/entity.rb', line 47 def bottom y + height end |
#center_x ⇒ Object
51 52 53 |
# File 'lib/bestguigui/entity.rb', line 51 def center_x x + width / 2.0 end |
#center_y ⇒ Object
55 56 57 |
# File 'lib/bestguigui/entity.rb', line 55 def center_y y + height / 2.0 end |
#collides?(other) ⇒ Boolean
Chevauchement de rectangles (AABB), le classique en 2D pour du jam.
67 68 69 |
# File 'lib/bestguigui/entity.rb', line 67 def collides?(other) left < other.right && right > other.left && top < other.bottom && bottom > other.top end |
#collides_circle?(other) ⇒ Boolean
Chevauchement de cercles (distance entre centres <= somme des rayons) — plus naturel que l'AABB pour des balles, projectiles, portées d'attaque.
73 74 75 |
# File 'lib/bestguigui/entity.rb', line 73 def collides_circle?(other) Gosu.distance(center_x, center_y, other.center_x, other.center_y) <= radius + other.radius end |
#draw ⇒ Object
33 |
# File 'lib/bestguigui/entity.rb', line 33 def draw; end |
#left ⇒ Object
35 36 37 |
# File 'lib/bestguigui/entity.rb', line 35 def left x end |
#radius ⇒ Object
Rayon du cercle inscrit dans le rectangle (moitié du plus grand côté) — utilisé par collides_circle?, pratique pour des projectiles/balles/ zones de portée sans avoir à gérer une forme séparée.
62 63 64 |
# File 'lib/bestguigui/entity.rb', line 62 def radius [width, height].max / 2.0 end |
#right ⇒ Object
39 40 41 |
# File 'lib/bestguigui/entity.rb', line 39 def right x + width end |
#top ⇒ Object
43 44 45 |
# File 'lib/bestguigui/entity.rb', line 43 def top y end |
#update(dt) ⇒ Object
32 |
# File 'lib/bestguigui/entity.rb', line 32 def update(dt); end |