Class: Glitch::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/game.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGame

Returns a new instance of Game.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/game.rb', line 5

def initialize
  @player = Glitch::Player.new
  @bit_multiplier = 0
  @last_second = 0

  @types = {}
  [
    Glitch::Type.new('atom', initial_price: 10, multiplier: 1, count_available: 20, :description => 'a boring little atom, so lonely'),
    Glitch::Type.new('uber', initial_price: 100, multiplier: 10),
    Glitch::Type.new('matrix', initial_price: 150, multiplier: 11),
    Glitch::Type.new('hundo', initial_price: 99999, multiplier: 100),
    Glitch::Type.new('board', initial_price: 500, multiplier: 0, count_available: 4, price_calc: -> (type) { [type.initial_price * (type.count + 1), 2000].min }),
    Glitch::Type.new('clock', initial_price: 1337, multiplier: 0, count_available: 1, price_calc: -> (type) { type.initial_price }),
  ].each { |type| @types[type.shortcut] = type }

  @message_board_length = 30
  @messages = [
    'welcome to glitch',
    'you need bits',
    "you're not sure why",
    'you know you do',
    'survive...',
  ]
end

Class Method Details

.runObject



30
31
32
# File 'lib/game.rb', line 30

def self.run
  self.new.run
end

Instance Method Details

#draw_screenObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/game.rb', line 85

def draw_screen
  Curses.clear

  print_line_break
  print_glitch_string 'glitch!', 5
  print_line_break

  if @types['c'].count == 1
    print_glitch_string 'clock: ', 1, false
    print_glitch_string Time.now.to_s
  end

  @one_bit = true if !@one_bit && @player.bits > 0
  if @one_bit
    print_glitch_string 'bits: ', 1, false
    print_glitch_string @player.bits.to_s + (@bit_multiplier > 5 ? " (#{@bit_multiplier}/bps)" : '')
    print_line_break
  end

  print_glitch_string '-' * message_board_length
  if @messages
    @messages.each do |message|
      print_glitch_string message[0..message_board_length-4], 3, false
      if message.length > message_board_length-4
        Curses.addstr '...'
      end
      print_line_break
    end
  else
    print_glitch_string '...', 1, true, true
  end
  print_glitch_string '-' * message_board_length

  draw_types

  print_line_break
  Curses.addstr ':'
end

#runObject



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/game.rb', line 34

def run
  draw_screen

  Curses.curs_set 0
  Curses.timeout = 1000
  Curses.start_color

  loop do
    tick
  end
end

#tickObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/game.rb', line 46

def tick
  input = Curses.getch

  if input
    case
    when input == ' '
      @player.increment_bits
      add_message '1 bit'
    when available_types.map(&:shortcut).include?(input)
      type = @types[input]

      case
      when !type.available?
        add_message 'none left :('
      when @player.can_decrement_bits_by?(type.price)
        @player.decrement_bits type.price
        type.increment
        @bit_multiplier = @bit_multiplier + type.multiplier
        add_message "1 #{type.name}"
      else
        add_message "you've not enough bits for #{type.name}"
      end
    else
      add_message 'nope'
    end
  end

  # only once per second
  this_second = Time.now.to_i
  if this_second > @last_second
    @player.increment_bits 1 * @bit_multiplier
    @last_second = this_second
  end

  @messages = @messages.last(10)

  draw_screen
end