Class: Shattered::Input::Controls

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

Overview

Handles parsing and mapping of controls

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Controls

Constructor



11
12
13
# File 'lib/shattered_model/controls.rb', line 11

def initialize(file)
  @file = file
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



8
9
10
# File 'lib/shattered_model/controls.rb', line 8

def actions
  @actions
end

#fileObject

Returns the value of attribute file.



8
9
10
# File 'lib/shattered_model/controls.rb', line 8

def file
  @file
end

Instance Method Details

#active?Boolean

returns true if the controls are on, false otherwise

Returns:

  • (Boolean)


50
51
52
# File 'lib/shattered_model/controls.rb', line 50

def active?
  @active ||= false
end

#offObject

turns the controls off



61
62
63
# File 'lib/shattered_model/controls.rb', line 61

def off
  @active = false
end

#onObject

turns the controls on



55
56
57
58
# File 'lib/shattered_model/controls.rb', line 55

def on
  parse if @actions.nil?
  @active = true
end

#parseObject

parses the file and sets the action map



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/shattered_model/controls.rb', line 26

def parse
  fullpath = File.join(path, @file)
  fullpath+= ".controls" unless (File.exists? fullpath) || file.ends_with(".controls")        
  file = File.open(fullpath, "r")
  
  @actions = {}
  file.readlines.each_with_index do |line, line_number|
    line = (line.split("#"))[0] #comments
    next if line.strip.length == 0 #blank lines
    
    action_details = line.split("=>").collect {|x| x.strip}
    if(action_details.length == 2)
      action, condition = action_details
    elsif(action_details.length > 2)
      raise ControlsParseError.new("Error loading '#{line}' in #{fullpath}:#{line_number+1}.  Too many =>'s.")
    elsif(action_details.length < 2)
      raise ControlsParseError.new("Problem parsing '#{line}' in #{fullpath}:#{line_number+1}.  Badly formed keymap.") 
    end
    @actions[condition] = action
  end
  file.close
end

#pathObject

getter for path, which is static



21
22
23
# File 'lib/shattered_model/controls.rb', line 21

def path
  @@path ||= File.join(SHATTERED_ROOT,"config")
end

#path=(newpath) ⇒ Object

setter for path, which is static



16
17
18
# File 'lib/shattered_model/controls.rb', line 16

def path=(newpath)
  @@path = newpath
end