module Patterns::GameManager

GameManager -> Object

A different type of singleton that acts as a global constant object. Methods are added to the GameManager object. Note that this example is not thread safe! If you wish to access this class using threads you'll need to use a Mutex or Spinlock class << GameManager is an example of dynamically adding attributes and functionality to an instance of a class. This is the purpose of the Decorator Pattern, which exists inherently in Ruby in many forms.

Constants

FLOOR

The ASCII character to represent a floor

HEIGHT

Height of the game world

PLAYER

The ASCII character to represent a player

WALL

The ASCII character to represent a wall

WIDTH

Width of the game world

Public Instance Methods

get_player_position() click to toggle source

Returns the player's position

Examples

=> GameManager.get_player_position
# File app/models/singleton/game_manager.rb, line 99
def get_player_position
  @player.position
end
get_world() click to toggle source

Returns the current world state

Examples

=> GameManager.get_world
# File app/models/singleton/game_manager.rb, line 74
def get_world
  @game_map
end
get_world_size() click to toggle source

Returns the world size as a Vector2D Object

Examples

=> GameManager.get_world_size
# File app/models/singleton/game_manager.rb, line 108
def get_world_size
  Vector2D.new(WIDTH, HEIGHT)
end
get_world_string() click to toggle source

Returns the current world formatted for printing

Examples

=> GameManager.get_world_string
# File app/models/singleton/game_manager.rb, line 83
def get_world_string
  output = ''
  @game_map.each_index do |row|
    @game_map[0].each_index do |column|
      output << @game_map[row][column]
    end
    output << "\n"
  end
  output
end
init_world() click to toggle source

Initializes the game world, builds a game map, and sets the player's location

Examples

=> GameManager.init_world
# File app/models/singleton/game_manager.rb, line 30
def init_world
  @game_map = Array.new(HEIGHT){Array.new(WIDTH){0}}
  @player = Player.new(0, 0)
  generate_map
  place_player
end
log_world() click to toggle source

Logs the current world state

Examples

=> GameManager.log_world
# File app/models/singleton/game_manager.rb, line 58
def log_world
  output = ''
  @game_map.each_index do |row|
    @game_map[0].each_index do |column|
      output << @game_map[row][column]
    end
    output << "\n"
  end
  Logger.instance.info "\nCurrent Game State: \n#{output}"
end
print_world() click to toggle source

Prints the game world

Examples

=> GameManager.print_world