Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Players now respawn in their own spawn points

  • Loading branch information...
commit de7d0057e908b9119f8266d89357df9f5f79df5f 1 parent 01eda06
@MrJaba MrJaba authored
View
1  bomberman_server.rb
@@ -8,6 +8,7 @@
#Application Specifics
require 'controllers/root_controller'
require 'controllers/game_controller'
+require 'models/player'
require 'config/app'
Cramp::Controller::Websocket.backend = :thin
View
2  config.ru
@@ -17,6 +17,8 @@ require 'uuid'
require 'json'
#Application Specifics
+require 'models/player'
+require 'models/game'
require 'controllers/root_controller'
require 'controllers/game_controller'
require 'config/app'
View
42 controllers/game_controller.rb
@@ -5,10 +5,11 @@ class GameController < Cramp::Controller::Websocket
on_data :receive_message
class << self
attr_accessor :player_states
+ attr_accessor :games
attr_accessor :bomb_positions
end
TIMEOUT = 20
- COLOURS = %w{ blue brown red yellow }
+ @games = [Game.new]
@player_states = {}
@bomb_positions = {}
@@ -21,7 +22,7 @@ def receive_message(data)
end
def push_states
- data = {:type => 'update_positions', :positions => GameController.player_states}.to_json
+ data = {:type => 'update_positions', :positions => player_states}.to_json
render data
end
@@ -34,14 +35,15 @@ def push_bombs
def receive_register(message, null_uuid=nil)
player_uuid = UUID.new.generate
- player_colour = COLOURS[GameController.player_states.size] rescue COLOURS.first
- GameController.player_states[player_uuid] = {:x => 0, :y => 0, :score => 0, :last_message_time => Time.now, :player_colour => player_colour}
- render ({:type => 'register', :uuid => player_uuid, :colour => player_colour}).to_json
+ game = GameController.games.last
+ player = game.create_player
+ GameController.player_states[player_uuid] = player
+ render ({:type => 'register', :uuid => player_uuid, :colour => player.colour}).to_json
end
def receive_player_move(message, uuid)
if( uuid && GameController.player_states[uuid] )
- GameController.player_states[uuid].merge!({:x => message['data']['x'], :y => message['data']['y']})
+ GameController.player_states[uuid].update({:x => message['data']['x'], :y => message['data']['y']})
end
end
@@ -54,21 +56,16 @@ def receive_send_bomb_detonate(message, uuid)
GameController.bomb_positions.delete(uuid) unless uuid.nil?
end
- def receive_send_kill_player(message, uuid)
- GameController.player_states[uuid][:score] += 1
- restart_player(message['data']['killed'])
+ def receive_send_kill_player(message, uuid)
+ GameController.player_states[uuid].score += 1 if GameController.player_states[uuid]
+ player_killed = message['data']['killed']
+ GameController.player_states[player_killed].respawn(:death) if GameController.player_states[player_killed]
end
def receive_send_reset_state(message, uuid)
- GameController.player_states[uuid].delete(:state)
+ GameController.player_states[uuid].clearState
end
-
- def restart_player(uuid)
- GameController.player_states[uuid][:x] = 0
- GameController.player_states[uuid][:y] = 0
- GameController.player_states[uuid][:state] = 'restart'
- end
-
+
def cleanup
states = GameController.player_states.dup
states.each_pair do |uuid, state|
@@ -77,12 +74,19 @@ def cleanup
end
def timed_out?(player_state)
- (Time.now - player_state[:last_message_time]) > TIMEOUT
+ (Time.now - player_state.last_message_time) > TIMEOUT
end
def update_last_message_time(uuid)
if( uuid && GameController.player_states[uuid] )
- GameController.player_states[uuid][:last_message_time] = Time.now
+ GameController.player_states[uuid].last_message_time = Time.now
+ end
+ end
+
+ def player_states
+ GameController.player_states.inject({}) do |sum, uuid_player|
+ uuid, player = uuid_player
+ sum.merge( {uuid => player.to_param } )
end
end
View
26 models/game.rb
@@ -0,0 +1,26 @@
+class Game
+ COLOURS = %w{ blue brown red yellow }
+ attr_accessor :players
+
+ def initialize
+ self.players = []
+ end
+
+ def create_player
+ (players << Player.new(player_colour, spawn_point)).last
+ end
+
+ def player_colour
+ COLOURS[self.players.size] rescue COLOURS.first
+ end
+
+ def spawn_point
+ case players.size
+ when 0 then [0,0]
+ when 1 then [6,0]
+ when 2 then [0,6]
+ when 3 then [6,6]
+ end
+ end
+
+end
View
30 models/player.rb
@@ -0,0 +1,30 @@
+class Player
+ attr_accessor :x, :y ,:score, :last_message_time, :colour, :spawn, :state
+
+ def initialize(colour, spawn)
+ self.spawn = spawn
+ self.score = 0
+ self.last_message_time = Time.now
+ self.colour = colour
+ respawn
+ end
+
+ def update(attributes)
+ self.x = attributes[:x]
+ self.y = attributes[:y]
+ end
+
+ def respawn(cause=:start)
+ self.x = self.spawn.first
+ self.y = self.spawn.last
+ self.state = 'restart' if cause == :death
+ end
+
+ def clearState
+ self.state = nil
+ end
+
+ def to_param
+ {:x => self.x, :y => self.y, :score => self.score, :colour => self.colour, :state => self.state}
+ end
+end
View
6 public/javascripts/game.js
@@ -79,11 +79,9 @@ MrJaba.Bomberman = function(){
function drawBombsAndExplosions(row){
$.each(MrJaba.Bomberman.bombs, function(uuid, bomb){
- console.log("bomb:"+bomb.getTileY() +" "+ row+" "+ bomb.getTileX()+" "+bomb.getTileY());
if(bomb.getTileY() === row){ bomb.draw(); }
});
$.each(MrJaba.Bomberman.explosions, function(uuid, explosion){
- console.log("explosion:"+explosion.getTileY() +" "+ row +" "+explosion.getTileX() +" "+explosion.getTileY());
if(explosion.getTileY() === row){ explosion.draw(); }
});
}
@@ -101,7 +99,7 @@ MrJaba.Bomberman = function(){
}
function restartMe(position){
- MrJaba.Bomberman.me.setX(parseInt(position.x)); MrJaba.Bomberman.me.setY(parseInt(position.y))
+ MrJaba.Bomberman.me.setTileX(parseInt(position.x)); MrJaba.Bomberman.me.setTileY(parseInt(position.y))
MrJaba.Bomberman.GameClient.trigger('send_reset_state', "restart");
}
@@ -176,7 +174,7 @@ MrJaba.Bomberman = function(){
updateOpponentPositions: function(positions){
$("#scores").html("");
$.each(positions, function(uuid, position){
- if( isOpponent(uuid) ){ MrJaba.Bomberman.opponents[uuid] = {x:parseInt(position.x), y:parseInt(position.y), colour:position.player_colour} }
+ if( isOpponent(uuid) ){ MrJaba.Bomberman.opponents[uuid] = {x:parseInt(position.x), y:parseInt(position.y), colour:position.colour} }
if( iAmRestarting(uuid, position) ){ restartMe(position); }
var li_class = ( uuid === MrJaba.Bomberman.uuid ) ? "me" : "opponent"
$("#scores").prepend("<li class='"+li_class+"'><span class='score'>"+position.score+"</span><span class='uuid'>"+uuid+"</span></li>");
View
8 public/javascripts/sprite.js
@@ -36,6 +36,10 @@ Sprite.prototype = {
this.x = parseInt(x);
},
+ setTileX:function(tileX){
+ this.x = MrJaba.Bomberman.Images.tileWidth() * parseInt(tileX);
+ },
+
getFrameWidth:function(){
return (this.frameWidth || this.imgWidth);
},
@@ -54,6 +58,10 @@ Sprite.prototype = {
this.y = parseInt(y);
},
+ setTileY:function(tileY){
+ this.y = MrJaba.Bomberman.Images.visibleTileHeight() * parseInt(tileY);
+ },
+
getFrameHeight:function(){
return (this.frameHeight || this.imgHeight);
},
Please sign in to comment.
Something went wrong with that request. Please try again.