Class: Game
- Inherits:
-
Object
- Object
- Game
- Defined in:
- lib/game_2d/game.rb
Instance Attribute Summary collapse
-
#tick ⇒ Object
readonly
Returns the value of attribute tick.
Class Method Summary collapse
-
.fire_duplicate_id(old_entity, new_entity) ⇒ Object
This should never happen.
-
.fire_entity_not_found(entity) ⇒ Object
This should never happen.
Instance Method Summary collapse
- #[](id) ⇒ Object
- #_create_server_port(*args) ⇒ Object
-
#add_npcs(npcs_json) ⇒ Object
Answering request from client.
- #add_player(player_name) ⇒ Object
- #add_player_action(action) ⇒ Object
- #delete_entity(entity) ⇒ Object
- #each_player_conn ⇒ Object
- #get_all_npcs ⇒ Object
- #get_all_players ⇒ Object
-
#initialize(args) ⇒ Game
constructor
A new instance of Game.
- #player_connection(player) ⇒ Object
- #player_data(player_name) ⇒ Object
- #player_id_connection(player_id) ⇒ Object
- #process_player_actions ⇒ Object
- #run ⇒ Object
- #save ⇒ Object
-
#send_full_updates ⇒ Object
New players always get a full update (with some additional information) Everyone else gets full registry dump every N ticks, where N == @registry_broadcast_every.
- #send_updated_entities(*entities) ⇒ Object
- #store_player_data(player_name, data) ⇒ Object
- #update ⇒ Object
- #update_npcs(npcs_json) ⇒ Object
- #world_cell_height ⇒ Object
- #world_cell_width ⇒ Object
- #world_highest_id ⇒ Object
- #world_id ⇒ Object
- #world_name ⇒ Object
Constructor Details
#initialize(args) ⇒ Game
Returns a new instance of Game.
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 |
# File 'lib/game_2d/game.rb', line 29 def initialize(args) all_storage = Storage.in_home_dir(args[:storage] || DEFAULT_STORAGE) @player_storage = all_storage.dir('players')['players'] @levels_storage = all_storage.dir('levels') level_storage = @levels_storage[args[:level]] if level_storage.empty? @space = GameSpace.new(self).establish_world( args[:level], nil, # level ID args[:width] || WORLD_WIDTH, args[:height] || WORLD_HEIGHT) @space.storage = level_storage else @space = GameSpace.load(self, level_storage) end @tick = -1 @player_actions = Hash.new {|h,tick| h[tick] = Array.new} @self_check, @profile, @registry_broadcast_every = args.values_at( :self_check, :profile, :registry_broadcast_every) @registry_broadcast_every ||= DEFAULT_REGISTRY_BROADCAST_EVERY # This should never happen. It can only happen client-side because a # registry update may create an entity before we get around to it in, # say, add_npc def @space.fire_duplicate_id(old_entity, new_entity) raise "#{old_entity} and #{new_entity} have same ID!" end # This should never happen. It can only happen client-side because a # registry update may delete an entity before we get around to it in # purge_doomed_entities def @space.fire_entity_not_found(entity) raise "Object #{entity} not in registry" end @port = _create_server_port(self, args[:port] || DEFAULT_PORT, args[:max_clients] || MAX_CLIENTS) end |
Instance Attribute Details
#tick ⇒ Object (readonly)
Returns the value of attribute tick.
76 77 78 |
# File 'lib/game_2d/game.rb', line 76 def tick @tick end |
Class Method Details
.fire_duplicate_id(old_entity, new_entity) ⇒ Object
This should never happen. It can only happen client-side because a registry update may create an entity before we get around to it in, say, add_npc
56 57 58 |
# File 'lib/game_2d/game.rb', line 56 def @space.fire_duplicate_id(old_entity, new_entity) raise "#{old_entity} and #{new_entity} have same ID!" end |
.fire_entity_not_found(entity) ⇒ Object
This should never happen. It can only happen client-side because a registry update may delete an entity before we get around to it in purge_doomed_entities
63 64 65 |
# File 'lib/game_2d/game.rb', line 63 def @space.fire_entity_not_found(entity) raise "Object #{entity} not in registry" end |
Instance Method Details
#[](id) ⇒ Object
147 148 149 |
# File 'lib/game_2d/game.rb', line 147 def [](id) @space[id] end |
#_create_server_port(*args) ⇒ Object
72 73 74 |
# File 'lib/game_2d/game.rb', line 72 def _create_server_port(*args) ServerPort.new *args end |
#add_npcs(npcs_json) ⇒ Object
Answering request from client
127 128 129 |
# File 'lib/game_2d/game.rb', line 127 def add_npcs(npcs_json) npcs_json.each {|json| @space << Serializable.from_json(json) } end |
#add_player(player_name) ⇒ Object
97 98 99 100 101 102 103 104 105 |
# File 'lib/game_2d/game.rb', line 97 def add_player(player_name) player = Player.new(player_name) player.x = (@space.width - Entity::WIDTH) / 2 player.y = (@space.height - Entity::HEIGHT) / 2 other_players = @space.players.dup @space << player other_players.each {|p| player_connection(p).add_player(player, @tick) } player end |
#add_player_action(action) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/game_2d/game.rb', line 159 def add_player_action(action) at_tick, player_id = action[:at_tick], action[:player_id] unless at_tick $stderr.puts "Received update from #{player_id} without at_tick!" at_tick = @tick + 1 end if at_tick <= @tick $stderr.puts "Received update from #{player_id} #{@tick + 1 - at_tick} ticks late" at_tick = @tick + 1 end @player_actions[at_tick] << action end |
#delete_entity(entity) ⇒ Object
119 120 121 122 123 124 |
# File 'lib/game_2d/game.rb', line 119 def delete_entity(entity) puts "Deleting #{entity}" @space.doom entity @space.purge_doomed_entities each_player_conn {|pc| pc.delete_entity entity, @tick } end |
#each_player_conn ⇒ Object
115 116 117 |
# File 'lib/game_2d/game.rb', line 115 def each_player_conn get_all_players.each {|p| yield player_connection(p)} end |
#get_all_npcs ⇒ Object
155 156 157 |
# File 'lib/game_2d/game.rb', line 155 def get_all_npcs @space.npcs end |
#get_all_players ⇒ Object
151 152 153 |
# File 'lib/game_2d/game.rb', line 151 def get_all_players @space.players end |
#player_connection(player) ⇒ Object
111 112 113 |
# File 'lib/game_2d/game.rb', line 111 def player_connection(player) player_id_connection(player.registry_id) end |
#player_data(player_name) ⇒ Object
88 89 90 |
# File 'lib/game_2d/game.rb', line 88 def player_data(player_name) @player_storage[player_name] end |
#player_id_connection(player_id) ⇒ Object
107 108 109 |
# File 'lib/game_2d/game.rb', line 107 def player_id_connection(player_id) @port.player_connection(player_id) end |
#process_player_actions ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/game_2d/game.rb', line 172 def process_player_actions if actions = @player_actions.delete(@tick) actions.each do |action| player_id = action.delete :player_id player = @space[player_id] unless player $stderr.puts "No such player #{player_id} -- dropping move" next end if (move = action[:move]) player.add_move move elsif (npcs = action[:add_npcs]) add_npcs npcs elsif (entities = action[:update_entities]) update_npcs entities elsif (entity_id = action[:snap_to_grid]) @space.snap_to_grid entity_id.to_sym else $stderr.puts "IGNORING BAD DATA from #{player}: #{action.inspect}" end end end end |
#run ⇒ Object
258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/game_2d/game.rb', line 258 def run run_start = Time.now.to_r loop do TICKS_PER_SECOND.times do |n| update # This results in something approaching TICKS_PER_SECOND @port.update_until(run_start + Rational(@tick, TICKS_PER_SECOND)) $stderr.puts "Updates per second: #{@tick / (Time.now.to_r - run_start)}" if @profile end # times end # infinite loop end |
#save ⇒ Object
84 85 86 |
# File 'lib/game_2d/game.rb', line 84 def save @space.save end |
#send_full_updates ⇒ Object
New players always get a full update (with some additional information) Everyone else gets full registry dump every N ticks, where N == @registry_broadcast_every
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/game_2d/game.rb', line 228 def send_full_updates # Set containing brand-new players' IDs new_players = @port.new_players each_player_conn do |pc| if new_players.include? pc.player_id response = { :you_are => pc.player_id, :world => { :world_name => world_name, :world_id => world_id, :highest_id => world_highest_id, :cell_width => world_cell_width, :cell_height => world_cell_height, }, :add_players => get_all_players, :add_npcs => get_all_npcs, :at_tick => tick, } pc.send_record response, true # answer login reliably elsif @registry_broadcast_every > 0 && (@tick % @registry_broadcast_every == 0) pc.send_record( { :registry => @space.all_registered, :highest_id => @space.highest_id, :at_tick => @tick, }, false, 1 ) end end end |
#send_updated_entities(*entities) ⇒ Object
143 144 145 |
# File 'lib/game_2d/game.rb', line 143 def send_updated_entities(*entities) each_player_conn {|pc| pc.update_entities entities, @tick } end |
#store_player_data(player_name, data) ⇒ Object
92 93 94 95 |
# File 'lib/game_2d/game.rb', line 92 def store_player_data(player_name, data) @player_storage[player_name] = data @player_storage.save end |
#update ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/game_2d/game.rb', line 196 def update @tick += 1 # This will: # 1) Queue up player actions for existing players # (create_npc included) # 2) Add new players in response to login messages # 3) Remove players in response to disconnections @port.update # This will execute player moves, and create NPCs process_player_actions # Objects that exist by now will be updated # Objects created during this update won't be updated # themselves this tick @space.update # Do this at the end, so the update contains all the # latest and greatest news send_full_updates if @self_check @space.check_for_grid_corruption @space.check_for_registry_leaks end end |
#update_npcs(npcs_json) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/game_2d/game.rb', line 131 def update_npcs(npcs_json) npcs_json.each do |json| id = json[:registry_id] if entity = @space[id] entity.update_from_json json entity.grab! else $stderr.puts "Can't update #{id}, doesn't exist" end end end |
#world_cell_height ⇒ Object
82 |
# File 'lib/game_2d/game.rb', line 82 def world_cell_height; @space.cell_height; end |
#world_cell_width ⇒ Object
81 |
# File 'lib/game_2d/game.rb', line 81 def world_cell_width; @space.cell_width; end |
#world_highest_id ⇒ Object
80 |
# File 'lib/game_2d/game.rb', line 80 def world_highest_id; @space.highest_id; end |
#world_id ⇒ Object
79 |
# File 'lib/game_2d/game.rb', line 79 def world_id; @space.world_id; end |
#world_name ⇒ Object
78 |
# File 'lib/game_2d/game.rb', line 78 def world_name; @space.world_name; end |