Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tag: initial_protot…
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 122 lines (94 sloc) 2.805 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 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 84 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
require_relative "../world"
require_relative "../game"

require_relative "juke_box"

module Blind
  module UI
    class GamePresenter
      def initialize(mine_count=40)
        @world = Blind::World.new(mine_count)
        @game = Blind::Game.new(@world)
        @sounds = {}
   
        setup_sounds
        setup_events
      end

      attr_reader :game_over_message

      def move(x,y)
        game.move(x,y)
      end

      def detect_danger_zone
        if in_danger_zone
          min = Blind::World::DANGER_ZONE_RANGE.min
          max = Blind::World::DANGER_ZONE_RANGE.max

          sounds[:siren].volume =
            ((world.distance(world.center_position) - min) / max.to_f) * 100
        else
          sounds[:siren].volume = 0
        end
      end

      def to_s
        "Player position #{world.current_position}\n"+
        "Region #{world.current_region}\n"+
        "Mines\n #{world.mine_positions.each_slice(5)
                         .map { |e| e.join(", ") }.join("\n")}\n"+
        "Exit\n #{world.exit_position}"
      end

      def player_position
        world.current_position
      end

      def finished?
        !!game_over_message
      end

      private

      def setup_sounds
        sounds[:phone] = JukeBox.phone(@game.world.exit_position)
        sounds[:siren] = JukeBox.siren
        sounds[:explosion] = JukeBox.explosion
        sounds[:celebration] = JukeBox.celebration
        sounds[:mines] = JukeBox.mines(@game.world.mine_positions)
      end

      def setup_events
        game.on_event(:enter_region, :danger_zone) do
          self.in_danger_zone = true
        end

        game.on_event(:leave_region, :danger_zone) do
          self.in_danger_zone = false
        end

        game.on_event(:enter_region, :deep_space) do
          lose_game("You drifted off into deep space! YOU LOSE!")
        end

        game.on_event(:mine_detonated) do
          lose_game("You got blasted by a mine! YOU LOSE!")
        end

        game.on_event(:exit_located) do
          win_game("You found the exit! YOU WIN!")
        end
      end

      def lose_game(message)
        silence_sounds

        sound = sounds[:explosion]
        sound.play

        self.game_over_message = message
      end

      def win_game(message)
        silence_sounds

        sound = sounds[:celebration]
        sound.play

        self.game_over_message = message
      end

      def silence_sounds
        sounds.each do |name, sound|
          case name
          when :mines
            sound.each { |s| s.stop }
          else
            sound.stop
          end
        end
      end

      private

      attr_accessor :in_danger_zone

      attr_reader :sounds, :world, :game
      attr_writer :game_over_message
    end
  end
end
Something went wrong with that request. Please try again.