Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set up server in preparation for multiplayer games
- Loading branch information
1 parent
c68cbf4
commit 73a3075
Showing
11 changed files
with
327 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class Ball | ||
attr_accessor :x, :y, :vx, :vy | ||
def initialize(x, y) | ||
self.x = x | ||
self.y = y | ||
self.vx = 0 | ||
self.vy = 0 | ||
end | ||
def to_json(*a){ | ||
:x=>x, | ||
:y=>y, | ||
:vx=>vx, | ||
:vy=>vy | ||
}.to_json(*a) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Cue | ||
attr_accessor :x, :y | ||
def initialize | ||
self.x = 200 | ||
self.y = 200 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
class Game | ||
attr_accessor :cue, :players, :pits | ||
attr_accessor :players, :balls | ||
|
||
def self.width | ||
500 | ||
end | ||
def self.height | ||
500 | ||
end | ||
|
||
def initialize(player) | ||
self.balls = [] | ||
self.pits = [] | ||
self.players = [] | ||
self.players << player | ||
|
||
pits << Pit.new(75,75) | ||
pits << Pit.new(Game::width-75, 75) | ||
pits << Pit.new(75, Game::height-75) | ||
pits << Pit.new(Game::width-75, Game::height-75) | ||
|
||
self.cue = Cue.new | ||
10.times {generate_ball} | ||
end | ||
def add_player(player) | ||
raise unless players.length > 1 | ||
end | ||
|
||
def generate_ball | ||
ballX = 0 | ||
ballY = 0 | ||
overlap = false | ||
begin | ||
overlap = false | ||
ballX = 50+rand(400) | ||
ballY = 50+rand(400) | ||
|
||
balls.each do |ball| | ||
if (dist(ball.x, ball.y, ballX, ballY) < 40) | ||
overlap = true | ||
end | ||
end | ||
if (dist(cue.x, cue.y, ballX, ballY) < 40) | ||
overlap = true | ||
end | ||
pits.each do |pit| | ||
if (dist(pit.x, pit.y, ballX, ballY) < 40) | ||
overlap = true | ||
end | ||
end | ||
end while overlap==true | ||
|
||
balls << Ball.new(ballX, ballY) | ||
end | ||
|
||
def other_players(player) | ||
players.reject{|p| p==player} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Pit | ||
attr_accessor :x, :y | ||
def initialize(x,y) | ||
self.x = x | ||
self.y = y | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class Player | ||
attr_accessor :socket | ||
def initialize(socket) | ||
self.socket = socket | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require 'rubygems' | ||
require 'eventmachine' | ||
require 'em-websocket' | ||
require 'json' | ||
|
||
require 'pit' | ||
require 'cue' | ||
require 'ball' | ||
require 'game' | ||
require 'player' | ||
|
||
width = 500 | ||
height = 500 | ||
|
||
def dist(x1, y1, x2, y2) | ||
x_diff = (x1-x2) ** 2 | ||
y_diff = (y1-y2) ** 2 | ||
res = Math.sqrt( x_diff + y_diff ) | ||
res<0 ? -res : res | ||
end | ||
|
||
@games = [] | ||
@players = [] | ||
EventMachine.run do | ||
EventMachine::WebSocket.start(:host => '0.0.0.0', :port => 8080) do |socket| | ||
socket.onopen do | ||
begin | ||
puts 'user joined' | ||
# player = Player.new | ||
player = Player.new(socket) | ||
@players << player | ||
game = Game.new(player) | ||
@games << game | ||
socket.send({:balls=>game.balls}.to_json) | ||
rescue Exception => e | ||
puts e.inspect | ||
puts e.backtrace | ||
end | ||
end | ||
socket.onmessage do |mess| | ||
puts "move received" | ||
array = mess.split '/' | ||
# puts array | ||
player = @players.find{|p| p.socket = socket } | ||
game = @games.find{|g| g.players.include? player } | ||
# game.move(socket, array[0], array[1]) | ||
game.other_players(player).each do |s| | ||
s.send mess | ||
end | ||
end | ||
socket.onclose do | ||
# puts @games.inspect | ||
# player = @players.find{|p| p.socket = socket } | ||
# game = @games.find{|g| g.players.include player} | ||
# game.players.delete player | ||
# @players.delete player | ||
puts 'user left' | ||
end | ||
end | ||
|
||
`open /Users/skattyadz/code/couronne/game.html` | ||
end |