Class: ScubyWars::Client
- Inherits:
-
Object
- Object
- ScubyWars::Client
- Includes:
- HTTParty
- Defined in:
- lib/scuby_wars/client.rb
Instance Attribute Summary collapse
-
#server ⇒ Object
Returns the value of attribute server.
-
#world ⇒ Object
Returns the value of attribute world.
Class Method Summary collapse
-
.join(server, nick) ⇒ Object
Example: player = ScubyWars::Client.join(“localhost:3000”, “player-name”) player.stop! player.turn_left! player.run! player.fire!.
Instance Method Summary collapse
- #cease_fire ⇒ Object
- #fire ⇒ Object
- #fireing? ⇒ Boolean
-
#initialize(server, nick) ⇒ Client
constructor
A new instance of Client.
- #join ⇒ Object
- #run ⇒ Object
- #running? ⇒ Boolean
- #stop ⇒ Object
- #stop_fireing ⇒ Object
- #stop_turning ⇒ Object
- #turn_left ⇒ Object
- #turn_right ⇒ Object
- #turning? ⇒ Boolean
- #turning_left? ⇒ Boolean
- #turning_right? ⇒ Boolean
- #update_player ⇒ Object
Constructor Details
#initialize(server, nick) ⇒ Client
Returns a new instance of Client.
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/scuby_wars/client.rb', line 26 def initialize(server, nick) @player = {:nick => nick, :actions => {:thrust => false, :turnLeft => false, :turnRight => false, :fire => false}} @server = server begin self.class.post("http://#{@server}/players/#{@player[:nick]}") rescue => e puts "Could not connect to game-server" raise e end end |
Instance Attribute Details
#server ⇒ Object
Returns the value of attribute server.
5 6 7 |
# File 'lib/scuby_wars/client.rb', line 5 def server @server end |
#world ⇒ Object
Returns the value of attribute world.
5 6 7 |
# File 'lib/scuby_wars/client.rb', line 5 def world @world end |
Class Method Details
.join(server, nick) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/scuby_wars/client.rb', line 14 def self.join(server, nick) game = new(server, nick) if block_given? Yajl::HttpStream.get(URI.parse("http://#{server}/world")) do |world| game.world = world game.instance_eval(yield) game.update_player end end game end |
Instance Method Details
#cease_fire ⇒ Object
92 93 94 |
# File 'lib/scuby_wars/client.rb', line 92 def cease_fire @player[:actions][:fire] = false end |
#fire ⇒ Object
84 85 86 |
# File 'lib/scuby_wars/client.rb', line 84 def fire @player[:actions][:fire] = true end |
#fireing? ⇒ Boolean
88 89 90 |
# File 'lib/scuby_wars/client.rb', line 88 def fireing? @player[:actions][:fire] end |
#join ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/scuby_wars/client.rb', line 37 def join Yajl::HttpStream.get(URI.parse("http://#{server}/world")) do |world| self.world = world think update_player end end |
#run ⇒ Object
45 46 47 |
# File 'lib/scuby_wars/client.rb', line 45 def run @player[:actions][:thrust] = true end |
#running? ⇒ Boolean
49 50 51 |
# File 'lib/scuby_wars/client.rb', line 49 def running? @player[:actions][:thrust] end |
#stop ⇒ Object
53 54 55 |
# File 'lib/scuby_wars/client.rb', line 53 def stop @player[:actions][:thrust] = false end |
#stop_fireing ⇒ Object
96 97 98 |
# File 'lib/scuby_wars/client.rb', line 96 def stop_fireing cease_fire end |
#stop_turning ⇒ Object
79 80 81 82 |
# File 'lib/scuby_wars/client.rb', line 79 def stop_turning @player[:actions][:turnLeft] = false @player[:actions][:turnRight] = false end |
#turn_left ⇒ Object
57 58 59 60 |
# File 'lib/scuby_wars/client.rb', line 57 def turn_left @player[:actions][:turnLeft] = true @player[:actions][:turnRight] = false end |
#turn_right ⇒ Object
66 67 68 69 |
# File 'lib/scuby_wars/client.rb', line 66 def turn_right @player[:actions][:turnLeft] = false @player[:actions][:turnRight] = true end |
#turning? ⇒ Boolean
75 76 77 |
# File 'lib/scuby_wars/client.rb', line 75 def turning? turning_left? || turning_right? end |
#turning_left? ⇒ Boolean
62 63 64 |
# File 'lib/scuby_wars/client.rb', line 62 def turning_left? @player[:actions][:turnLeft] end |
#turning_right? ⇒ Boolean
71 72 73 |
# File 'lib/scuby_wars/client.rb', line 71 def turning_right? @player[:actions][:turnRight] end |
#update_player ⇒ Object
107 108 109 110 |
# File 'lib/scuby_wars/client.rb', line 107 def update_player puts "Sending #{@player.inspect} to http://#{@server}/players/#{@player[:nick]}" self.class.put("http://#{@server}/players/#{@player[:nick]}", :body => Yajl::Encoder.encode(@player)) end |