Class: Petli::Stages::Base
- Inherits:
-
Tatty::Stage
- Object
- Tatty::Stage
- Petli::Stages::Base
- Defined in:
- lib/petli/stages/base.rb
Constant Summary collapse
- GAME_WIDTH =
28
- GAME_HEIGHT =
13
Instance Attribute Summary
Attributes inherited from Tatty::Stage
Instance Method Summary collapse
- #action_bar ⇒ Object
- #action_pages ⇒ Object
- #actions ⇒ Object
- #draw ⇒ Object
-
#initialize(pet:) ⇒ Base
constructor
A new instance of Base.
- #keypress(event) ⇒ Object
- #left ⇒ Object
- #top ⇒ Object
Methods inherited from Tatty::Stage
#goto, #leave, #move_to, #render, #render_at, #render_box, #screen_height, #screen_size, #screen_width, #step
Constructor Details
Instance Method Details
#action_bar ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/petli/stages/base.rb', line 44 def return "" if @pet.dead? || @pet.busy? pages = action_pages p = Pastel.new page, page_len = pages[@page] = p.bold(@page > 0 ? "◀" : "◁") += page.map {|a| "#{p.bold(a[0..2])}#{a[3..]}"}.join(" ") += (' ' * (GAME_WIDTH-page_len-4)) += p.bold(pages.count > 1 && @page < pages.count - 1 ? "▶" : "▷") end |
#action_pages ⇒ Object
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 |
#actions ⇒ Object
24 25 26 |
# File 'lib/petli/stages/base.rb', line 24 def actions %w() end |
#draw ⇒ Object
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.) 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 |
#left ⇒ Object
56 57 58 |
# File 'lib/petli/stages/base.rb', line 56 def left ((screen_width-GAME_WIDTH)/2).round end |
#top ⇒ Object
60 61 62 |
# File 'lib/petli/stages/base.rb', line 60 def top ((screen_height-GAME_HEIGHT)/2).round end |