Class: Petli::Stages::Dice

Inherits:
Base show all
Defined in:
lib/petli/stages/dice.rb

Constant Summary collapse

Symbols =
%w(⚀ ⚁ ⚂ ⚃ ⚄ ⚅)

Constants inherited from Base

Base::GAME_HEIGHT, Base::GAME_WIDTH

Instance Attribute Summary

Attributes inherited from Tatty::Stage

#framerate

Instance Method Summary collapse

Methods inherited from Base

#action_bar, #action_pages, #keypress, #left, #top

Methods inherited from Tatty::Stage

#goto, #keypress, #move_to, #render, #render_at, #render_box, #screen_height, #screen_size, #screen_width, #step

Constructor Details

#initialize(pet:) ⇒ Dice

Returns a new instance of Dice.



6
7
8
9
10
# File 'lib/petli/stages/dice.rb', line 6

def initialize(pet:)
  super(pet: pet)
  @value = rand(1..6)
  @countdown = -1
end

Instance Method Details

#actionsObject



12
13
14
# File 'lib/petli/stages/dice.rb', line 12

def actions
  %w(higher lower)
end

#drawObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/petli/stages/dice.rb', line 36

def draw
  super
  if @countdown == -1
    render_at(left+4, top+6, @value.to_s)
    render_at(left+23, top+6, Symbols[(0..5).to_a.sample])
  elsif @countdown == 0
    @won ? @pet.win : @pet.lose
    goto(Main, pet: @pet)
  else
    render_at(left+4, top+5, "▲") if @pickedhigher
    render_at(left+4, top+6, @value.to_s)
    render_at(left+23, top+6, Symbols[@pick-1])
    render_at(left+4, top+7, "▼") unless @pickedhigher
    @countdown -= 1
  end
end

#enterObject



16
17
18
# File 'lib/petli/stages/dice.rb', line 16

def enter
  @pet.play(game: :guess)
end

#leaveObject



20
21
22
# File 'lib/petli/stages/dice.rb', line 20

def leave
  @pet.reset
end

#onkey(event) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/petli/stages/dice.rb', line 27

def onkey(event)
  return if event.value != "h" and event.value != "l"
  @pickedhigher = event.value == "h"
  @pick = (1..6).to_a.sample
  @won = (event.value == "h" && @pick > @value) || (event.value == "l" && @pick < @value)
  @won ? @pet.celebrate : @pet.embarass
  @countdown = 10
end

#rollObject



24
25
# File 'lib/petli/stages/dice.rb', line 24

def roll
end