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
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/petli/stages/base.rb', line 50 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
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/petli/stages/base.rb', line 34 def action_pages pages = [] current_page = [] fmt_actions = self.actions.filter{|a,b| a != :else}.keys.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
30 31 32 |
# File 'lib/petli/stages/base.rb', line 30 def actions {} end |
#draw ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/petli/stages/base.rb', line 70 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 23 24 25 26 27 28 |
# File 'lib/petli/stages/base.rb', line 16 def keypress(event) key = event.value.downcase exit if key == "q" || key == "\e" || key == "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 keyact = self.actions.keys.find{|k| k[0].to_s.downcase == key} if !keyact.nil? actions[keyact].call elsif !self.actions[:else].nil? actions[:else].call end end |
#left ⇒ Object
62 63 64 |
# File 'lib/petli/stages/base.rb', line 62 def left ((screen_width-GAME_WIDTH)/2).round end |
#top ⇒ Object
66 67 68 |
# File 'lib/petli/stages/base.rb', line 66 def top ((screen_height-GAME_HEIGHT)/2).round end |