Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added in kills and score keeping

  • Loading branch information...
commit d8539f847ca752d9c3dc04b5d0257630d4dcecc5 1 parent fc6dcd3
@MrJaba authored
View
25 controllers/game_controller.rb
@@ -1,13 +1,13 @@
require 'ruby-debug'
class GameController < Cramp::Controller::Websocket
- periodic_timer :push_positions, :every => 0.1
+ periodic_timer :push_states, :every => 0.1
periodic_timer :push_bombs, :every => 0.1
on_data :receive_message
class << self
- attr_accessor :player_positions
+ attr_accessor :player_states
attr_accessor :bomb_positions
end
- @player_positions = {}
+ @player_states = {}
@bomb_positions = {}
def receive_message(data)
@@ -17,8 +17,8 @@ def receive_message(data)
self.send("receive_#{type}", message, uuid)
end
- def push_positions
- data = {:type => 'update_positions', :positions => GameController.player_positions}.to_json
+ def push_states
+ data = {:type => 'update_positions', :positions => GameController.player_states}.to_json
render data
end
@@ -31,12 +31,12 @@ def push_bombs
def receive_register(message, uuid=nil)
player_uuid = UUID.new.generate
- GameController.player_positions[player_uuid] = {:x => 0, :y => 0}
+ GameController.player_states[player_uuid] = {:x => 0, :y => 0, :state => 'restart', :score => 0}
render ({:type => 'uuid', :uuid => player_uuid, :class_id => self.object_id}).to_json
end
def receive_player_move(message, uuid)
- GameController.player_positions[uuid] = {:x => message['data']['x'], :y => message['data']['y']} unless uuid.nil?
+ GameController.player_states[uuid].merge!({:x => message['data']['x'], :y => message['data']['y']}) unless uuid.nil?
end
def receive_send_bomb_drop(message, uuid)
@@ -48,4 +48,15 @@ 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'])
+ end
+
+ def restart_player(uuid)
+ GameController.player_states[uuid][:x] = 0
+ GameController.player_states[uuid][:y] = 0
+ GameController.player_states[uuid][:state] = 'restart'
+ end
+
end
View
8 public/index.html
@@ -14,7 +14,6 @@
</head>
<body>
- <div id="log" style="color:black;"></div>
<table id="map" style="display:none">
<tr><td>StoneBlock</td><td>StoneBlock</td><td>StoneBlock</td><td>StoneBlock</td><td>StoneBlock</td><td>StoneBlock</td><td>StoneBlock</td></tr>
<tr><td>StoneBlock</td><td walk='false'>WallBlockTall</td><td>StoneBlock</td><td walk='false'>WallBlockTall</td><td>StoneBlock</td><td walk='false'>WallBlockTall</td><td>StoneBlock</td></tr>
@@ -24,7 +23,14 @@
<tr><td>StoneBlock</td><td walk='false'>WallBlockTall</td><td>StoneBlock</td><td walk='false'>WallBlockTall</td><td>StoneBlock</td><td walk='false'>WallBlockTall</td><td>StoneBlock</td></tr>
<tr><td>StoneBlock</td><td>StoneBlock</td><td>StoneBlock</td><td>StoneBlock</td><td>StoneBlock</td><td>StoneBlock</td><td>StoneBlock</td></tr>
</table>
+
<canvas id="canvas"></canvas>
+ <div id="score-holder" style="width:200px;float:right;">
+ <h3 style="border-bottom:1px solid black">Scores</h3>
+ <ul id="scores">
+ </ul>
+ </div>
+
</body>
</html>
View
6 public/javascripts/game.js
@@ -84,7 +84,8 @@ MrJaba.Bomberman = function(){
}
function killPlayer(uuid){
- delete MrJaba.Bomberman.opponents[uuid];
+ delete MrJaba.Bomberman.opponents[uuid];
+ MrJaba.Bomberman.GameClient.trigger('send_kill_player', uuid);
}
function initCharacter(id){
@@ -143,8 +144,11 @@ 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)} }
+ var style = ( uuid === MrJaba.Bomberman.uuid ) ? "background-color:red;" : ""
+ $("#scores").prepend("<ul style='"+style+"'>"+uuid+':'+position.score+"</ul>");
})
},
View
5 public/javascripts/game_client.js
@@ -38,6 +38,10 @@ var GameClient = function(){
return uuid;
}
+ var notifyPlayerKill = function(uuid){
+ return {killed:uuid};
+ }
+
var handleEvent = function(eventName, message){
var handler = callbacks[eventName];
if(typeof handler === undefined) return;
@@ -60,6 +64,7 @@ var GameClient = function(){
this.bind('update_bombs', updateBombPositions);
this.bind('send_bomb_drop', notifyBombDrop);
this.bind('send_bomb_detonate', notifyBombDetonate);
+ this.bind('send_kill_player', notifyPlayerKill);
}
$(document).bind( 'initDone', function(){ MrJaba.Bomberman.GameClient = new GameClient() } );
Please sign in to comment.
Something went wrong with that request. Please try again.