Skip to content

Commit

Permalink
fixed player restarts
Browse files Browse the repository at this point in the history
  • Loading branch information
MrJaba committed May 1, 2010
1 parent 5c7814d commit 9aaa2bd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
6 changes: 5 additions & 1 deletion controllers/game_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def push_bombs

def receive_register(message, uuid=nil)
player_uuid = UUID.new.generate
GameController.player_states[player_uuid] = {:x => 0, :y => 0, :state => 'restart', :score => 0}
GameController.player_states[player_uuid] = {:x => 0, :y => 0, :score => 0}
render ({:type => 'uuid', :uuid => player_uuid, :class_id => self.object_id}).to_json
end

Expand All @@ -53,6 +53,10 @@ def receive_send_kill_player(message, uuid)
restart_player(message['data']['killed'])
end

def receive_send_reset_state(message, uuid)
GameController.player_states[uuid].delete(:state)
end

def restart_player(uuid)
GameController.player_states[uuid][:x] = 0
GameController.player_states[uuid][:y] = 0
Expand Down
16 changes: 15 additions & 1 deletion public/javascripts/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@ MrJaba.Bomberman = function(){
MrJaba.Bomberman.GameClient.trigger('send_kill_player', uuid);
}

function isOpponent(uuid){
return uuid !== MrJaba.Bomberman.uuid;
}

function iAmRestarting(uuid, position){
return (uuid === MrJaba.Bomberman.uuid && position.state === "restart");
}

function restartMe(position){
MrJaba.Bomberman.me.setX(parseInt(position.x)); MrJaba.Bomberman.me.setY(parseInt(position.y))
MrJaba.Bomberman.GameClient.trigger('send_reset_state', "restart");
}

function initCharacter(id){
var me = new Sprite();
me.initialize(id, MrJaba.Bomberman.Images.getImage('CharacterBoy'), canvasNode());
Expand Down Expand Up @@ -149,7 +162,8 @@ MrJaba.Bomberman = function(){
updateOpponentPositions: function(positions){
$("#scores").html("");
$.each(positions, function(uuid, position){
if( uuid !== MrJaba.Bomberman.uuid ){ MrJaba.Bomberman.opponents[uuid] = {x: parseInt(position.x), y:parseInt(position.y)} }
if( isOpponent(uuid) ){ MrJaba.Bomberman.opponents[uuid] = {x:parseInt(position.x), y:parseInt(position.y)} }
if( iAmRestarting(uuid, position) ){ restartMe(position); }
var style = ( uuid === MrJaba.Bomberman.uuid ) ? "background-color:red;" : ""
$("#scores").prepend("<ul style='"+style+"'>"+uuid+':'+position.score+"</ul>");
})
Expand Down
5 changes: 5 additions & 0 deletions public/javascripts/game_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ var GameClient = function(){
return {killed:uuid};
}

var sendResetState = function(state){
return state;
}

var handleEvent = function(eventName, message){
var handler = callbacks[eventName];
if(typeof handler === undefined) return;
Expand All @@ -67,6 +71,7 @@ var GameClient = function(){
this.bind('send_bomb_drop', notifyBombDrop);
this.bind('send_bomb_detonate', notifyBombDetonate);
this.bind('send_kill_player', notifyPlayerKill);
this.bind('send_reset_state', sendResetState);
}

$(document).bind( 'initDone', function(){ MrJaba.Bomberman.GameClient = new GameClient() } );

0 comments on commit 9aaa2bd

Please sign in to comment.