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
15
16
17
# File 'lib/petli/stages/dice.rb', line 12

def actions
  {
    higher: -> {pick("h")},
    lower:  -> {pick("l")},
  }
end

#drawObject



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

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



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

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

#leaveObject



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

def leave
  @pet.reset
end

#pick(dir) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/petli/stages/dice.rb', line 30

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

#rollObject



27
28
# File 'lib/petli/stages/dice.rb', line 27

def roll
end