Class: Petli::Stages::Base

Inherits:
Tatty::Stage show all
Defined in:
lib/petli/stages/base.rb

Direct Known Subclasses

Dice, Feed, Guess, Main, Play

Constant Summary collapse

GAME_WIDTH =
28
GAME_HEIGHT =
13

Instance Attribute Summary

Attributes inherited from Tatty::Stage

#framerate

Instance Method Summary collapse

Methods inherited from Tatty::Stage

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

Constructor Details

#initialize(pet:) ⇒ Base

Returns a new instance of Base.



9
10
11
12
13
14
# File 'lib/petli/stages/base.rb', line 9

def initialize(pet:)
  super()
  @pet = pet
  @poop = Tatty::Anim.from_atlas(Petli.data_path('poop.txtanim'))
  @page = 0
end

Instance Method Details

#action_barObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/petli/stages/base.rb', line 44

def action_bar
  return "" if @pet.dead? || @pet.busy?
  pages = action_pages
  p = Pastel.new
  page, page_len = pages[@page]
  bar = p.bold(@page > 0 ? "" : "")
  bar += page.map {|a| "#{p.bold(a[0..2])}#{a[3..]}"}.join(" ")
  bar += (' ' * (GAME_WIDTH-page_len-4))
  bar += p.bold(pages.count > 1 && @page < pages.count - 1 ? "" : "")
  bar
end

#action_pagesObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/petli/stages/base.rb', line 28

def action_pages
  pages = []
  current_page = []
  fmt_actions = self.actions.map{|a| "[#{a[0].capitalize}]#{a[1..]}"}
  fmt_actions.each do |action|
    len = (current_page + [action]).join(" ").length
    if len >= GAME_WIDTH-2
      pages << [current_page, current_page.join(" ").length]
      current_page = [action]
    else
      current_page << action
    end
  end
  pages + [[current_page, current_page.join(" ").length]]
end

#actionsObject



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

def actions
  %w()
end

#drawObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/petli/stages/base.rb', line 64

def draw
  p = Pastel.new
  render_box(
    title: {
      top_left: p.bright_white.bold(" Petli "),
      bottom_right: p.green(" #{@pet.lifetime} days "),
    },
    width: GAME_WIDTH,
    height: GAME_HEIGHT,
    left: left,
    top: top,
  )

  poop = @poop.next
  @pet.poops.each_with_index do |_, i|
    x, y = Pet::POOP_LOCATIONS[i]
    render_at(left+1+x, top+1+y, poop)
  end

  render_at(left+9, top+4, @pet.display)
  sick = @pet.sick
  if sick > 0 && !@pet.dead?
    render_at(left+11-sick, top+4, "[#{'!'*sick}SICK#{'!'*sick}]")
  end

  render_at(left+1, top+1, "#{p.red("")*@pet.health}#{""*(10-@pet.health)}  #{@pet.sick > 0 ? p.red("") : ""}#{@pet.lights_out ? "" : p.yellow("")}  #{""*(10-@pet.happiness)}#{p.green("")*@pet.happiness}")
  render_at(left+1, top+GAME_HEIGHT-2, self.action_bar)
  render_at(left+GAME_WIDTH-2, top, p.bright_white.bold("[x]"))
end

#keypress(event) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/petli/stages/base.rb', line 16

def keypress(event)
  exit if event.value == "q" || event.value == "\e" || event.value == "x"
  return if @pet.busy? || @pet.dead?
  @page -= 1 if event.key.name == :left && @page > 0
  @page += 1 if event.key.name == :right && @page < action_pages.count - 1
  onkey(event)
end

#leftObject



56
57
58
# File 'lib/petli/stages/base.rb', line 56

def left
  ((screen_width-GAME_WIDTH)/2).round
end

#topObject



60
61
62
# File 'lib/petli/stages/base.rb', line 60

def top
  ((screen_height-GAME_HEIGHT)/2).round
end