class Figure

Trida Figure reprezentuje postavy ve hre.

Attributes

direction[RW]

Smer pohybu

left[R]

Pozice na ose X

speed[R]

Rychlost pohybu

top[R]

Pozice na ose Y

Public Class Methods

new(left, top, speed, r) click to toggle source

Vytvori novou postavu umestenou na mape v poloze left a top, pohybujici se rychlosti speed a vychozim smerem pohybu r.

  • Args :

    • left -> poloha na ose X

    • top -> poloha na ose Y

    • speed -> poloha na ose rychlost pohybu

    • r -> vychozi smer pohybu

# File lib/figure.rb, line 14
def initialize(left, top, speed, r)
  @directions = [:left, :right, :up, :down]
  @left = left
  @top = top
  @future_left = @left
  @future_top = @top
  @speed = speed
  @direction = @directions[r]
end

Public Instance Methods

can_move(area) click to toggle source

Overi, jestli se postava puze dale pohybovat soucasnym smerem v oblasti area

  • Args :

    • area -> mapa hry

  • Returns :

    • true -> postava se muze dal pohybovat stejnym smrem

    • false -> postava musi zmeni smer pohybu

# File lib/figure.rb, line 43
def can_move(area)
  future_move
  area.colidate(@future_left + CROFTWIDTH / 2, @future_top + CROFTWIDTH / 2)
end
move() click to toggle source

Posune hrace soucasnym smerem

# File lib/figure.rb, line 50
def move
  real_move
  modulo
  future_make
end

Private Instance Methods

future_make() click to toggle source
# File lib/figure.rb, line 68
def future_make
  @future_left = @left
  @future_top = @top
end
future_move() click to toggle source
# File lib/figure.rb, line 73
def future_move
  future_make
  if @direction == :left
    @future_left -= speed
  elsif @direction == :right
    @future_left += speed
  elsif @direction == :up
    @future_top -= speed
  else
    @future_top += speed
  end
end
modulo() click to toggle source
# File lib/figure.rb, line 27
def modulo
  if @left > WIDTH
    @left = 0
  elsif @left < 0
    @left = WIDTH
  end
end
real_move() click to toggle source
# File lib/figure.rb, line 56
def real_move
  if @direction == :left
    @left -= speed
  elsif @direction == :right
    @left += speed
  elsif @direction == :up
    @top -= speed
  else
    @top += speed
  end
end