module Patterns::GameManager
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
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
Returns the current world state
Examples
=> GameManager.get_world
# File app/models/singleton/game_manager.rb, line 74 def get_world @game_map end
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
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
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
Prints the game world
Examples
=> GameManager.print_world
# File app/models/singleton/game_manager.rb, line 42 def print_world output = '' @game_map.each_index do |row| @game_map[0].each_index do |column| output << @game_map[row][column] end output << "\n" end puts "\nCurrent Game State: \n#{output}" end