Class: BloodChalice::Game

Inherits:
Object
  • Object
show all
Includes:
HighLine::SystemExtensions
Defined in:
lib/bloodchalice/game.rb

Constant Summary collapse

END_OF_THE_WORLD =
400

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Game

Returns a new instance of Game.



9
10
11
12
13
14
15
16
17
# File 'lib/bloodchalice/game.rb', line 9

def initialize(options = {})
  @non_playable_characters = []
  @map = Map.new('map1')
  @number_of_players = options[:number_of_players]
  @players = generate_players(map)
  generate_npc(map)
  @turn = 1 
  @event_deck = EventDeck.new(self)     
end

Instance Attribute Details

#chaliceObject

Returns the value of attribute chalice.



5
6
7
# File 'lib/bloodchalice/game.rb', line 5

def chalice
  @chalice
end

#event_deckObject

Returns the value of attribute event_deck.



5
6
7
# File 'lib/bloodchalice/game.rb', line 5

def event_deck
  @event_deck
end

#mapObject

Returns the value of attribute map.



5
6
7
# File 'lib/bloodchalice/game.rb', line 5

def map
  @map
end

#non_playable_charactersObject

Returns the value of attribute non_playable_characters.



5
6
7
# File 'lib/bloodchalice/game.rb', line 5

def non_playable_characters
  @non_playable_characters
end

#number_of_playersObject

Returns the value of attribute number_of_players.



5
6
7
# File 'lib/bloodchalice/game.rb', line 5

def number_of_players
  @number_of_players
end

#playersObject

Returns the value of attribute players.



5
6
7
# File 'lib/bloodchalice/game.rb', line 5

def players
  @players
end

#turnObject

Returns the value of attribute turn.



5
6
7
# File 'lib/bloodchalice/game.rb', line 5

def turn
  @turn
end

Instance Method Details

#active_player?(tile) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/bloodchalice/game.rb', line 86

def active_player?(tile)
  tile.value.to_i <= @number_of_players
end

#all_players_dead?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/bloodchalice/game.rb', line 65

def all_players_dead?
  @players.all? {|p| p.dead?}
end

#ask_next_action(player) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bloodchalice/game.rb', line 37

def ask_next_action(player)
  say "A   Move to the West"
  say "D   Move to the East"
  say "W   Move to the North"
  say "S   Move to the South"
  say "Q   Quit"

  print 'Your order: '
  action = get_character.chr.upcase

  case action.strip
  when 'W'
    player.move(:north)
  when 'D'
    player.move(:east)
  when 'S'
    player.move(:south)
  when 'A'
    player.move(:west)
  when 'Q'
    exit()
  end
end

#end_of_game?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/bloodchalice/game.rb', line 61

def end_of_game?()
  (@turn == END_OF_THE_WORLD) or all_players_dead? or @chalice.blood >= 50 
end

#generate_npc(map) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/bloodchalice/game.rb', line 94

def generate_npc(map)
  map.map.each_with_index do |line, y|
    line.each_with_index do |tile, x|
      if tile.peasant?              
        peasant = Peasant.new(map: map, position: [y, x], game: self)
        @non_playable_characters << peasant
        map.set_tile [y, x], peasant
      elsif tile.zombie?        
        zombie = Zombie.new(map: map, position: [y, x], game: self)
        @non_playable_characters << zombie
        map.set_tile [y, x], zombie
      elsif tile.knight?        
        knight = Knight.new(map: map, position: [y, x], game: self)
        @non_playable_characters << knight
        map.set_tile [y, x], knight
      elsif tile.chalice?
        @chalice = Chalice.new(map: map, position: [y, x], game: self)
        map.set_tile [y, x], @chalice
      end
    end
  end
end

#generate_players(map) ⇒ Object



69
70
71
# File 'lib/bloodchalice/game.rb', line 69

def generate_players(number_of_players)
  1.upto(number_of_players) { |i| @players << Player.new(number: i, map: @map) }
end

#next_turnObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/bloodchalice/game.rb', line 19

def next_turn()
  event_deck.take_a_card
  players.each do |player|
    player.reset_moves
    while player.moves?
      show_interface(player)
      ask_next_action(player)
    end
    show_interface(player)
    ask "Next Player: hit enter"
  end                 
  
  @non_playable_characters.each &:reset_moves
  @non_playable_characters.each &:think      
  
  @turn += 1
end

#remove_npc(npc) ⇒ Object



90
91
92
# File 'lib/bloodchalice/game.rb', line 90

def remove_npc(npc)
  @non_playable_characters.delete(npc)
end

#show_interface(player) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/bloodchalice/game.rb', line 73

def show_interface(player)
  system('clear')
  say "It is the turn of Player #{player.number} and there are #{END_OF_THE_WORLD - @turn} turns until dawn."
  say "The Blood Chalice has #{@chalice.blood} of 50 blood units"
  show_map
  say "You have #{player.moves}/#{Player::SPEED} moves left, #{player.blood} blood stored and #{player.life}/#{Player::MAX_LIFE} HP"
  say 'What are your orders, my Liege?'
end

#show_mapObject



82
83
84
# File 'lib/bloodchalice/game.rb', line 82

def show_map()
  say @map.to_s
end