Class: Bestguigui::Entity

Inherits:
Object
  • Object
show all
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.button_down?(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

Instance Method Summary collapse

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

#heightObject

Returns the value of attribute height.



23
24
25
# File 'lib/bestguigui/entity.rb', line 23

def height
  @height
end

#widthObject

Returns the value of attribute width.



23
24
25
# File 'lib/bestguigui/entity.rb', line 23

def width
  @width
end

#xObject

Returns the value of attribute x.



23
24
25
# File 'lib/bestguigui/entity.rb', line 23

def x
  @x
end

#yObject

Returns the value of attribute y.



23
24
25
# File 'lib/bestguigui/entity.rb', line 23

def y
  @y
end

Instance Method Details

#bottomObject



47
48
49
# File 'lib/bestguigui/entity.rb', line 47

def bottom
  y + height
end

#center_xObject



51
52
53
# File 'lib/bestguigui/entity.rb', line 51

def center_x
  x + width / 2.0
end

#center_yObject



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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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

#drawObject



33
# File 'lib/bestguigui/entity.rb', line 33

def draw; end

#leftObject



35
36
37
# File 'lib/bestguigui/entity.rb', line 35

def left
  x
end

#radiusObject

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

#rightObject



39
40
41
# File 'lib/bestguigui/entity.rb', line 39

def right
  x + width
end

#topObject



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