Permalink
Browse files

now using UDP!

  • Loading branch information...
hcatlin committed Jun 3, 2009
1 parent b397943 commit d8ae3ba2475f9022b184f12ecf18ce78ba0ea8f0
View
@@ -1,4 +1,5 @@
.DS_Store
tmp/*
+server/config.yml
*.o
*.bundle
View
@@ -0,0 +1,7 @@
+---
+:user: client
+:pass: client
+:identity: 59d8021688887cd91f6e262b4d1ee361
+:vhost: /game
+:host: spacebrawl.hamptoncatlin.com
+:log_level: debug
View
@@ -1,10 +1,7 @@
require 'rubygems'
require 'gosu'
-require 'models/space_brawl_server'
require 'models/player'
require 'models/game_object'
-require 'open-uri'
-require 'json'
class GameWindow < Gosu::Window
def self.window_size
@@ -15,7 +12,6 @@ def initialize
width, height = GameWindow.window_size
super(width, height, false, 25)
self.caption = "Space Brawl!"
- @player = Player.create
GameObject.load_assets(self)
@@ -24,39 +20,39 @@ def initialize
def update
if button_down? Gosu::Button::KbLeft or button_down? Gosu::Button::GpLeft then
- @player.turn_left
+ ServerClient.action "turn_left"
end
if button_down? Gosu::Button::KbRight or button_down? Gosu::Button::GpRight then
- @player.turn_right
+ ServerClient.action "turn_right"
end
if button_down? Gosu::Button::KbUp or button_down? Gosu::Button::GpButton0 then
- @player.accelerate
+ ServerClient.action "accelerate"
end
if button_down? Gosu::Button::KbSpace
- @player.fire_missile
+ ServerClient.action "fire_missile"
end
- data = SpaceBrawlServer.universe
-
- objects = data["objects"].collect { |data| GameObject.new(data) }
-
- my_ship = (objects.select do |object|
- object.player_id.to_i == @player.id.to_i
- end).first
+ if $game_objects.any?
+ objects = $game_objects.values.collect { |data| GameObject.new(data) }
- my_ship.draw_health
-
- # only if my ship has shown up
- if my_ship
- offset_x, offset_y = my_ship.offsets
+ my_ship = objects.detect do |object|
+ object.parent_id == $identity && object.image == "ship"
+ end
+
+ offset_x, offset_y = 0, 0
+ # only if my ship has shown up
+ if my_ship
+ my_ship.draw_health
+ offset_x, offset_y = my_ship.offsets
+ end
+
+
objects.each do |object|
object.draw offset_x, offset_y
end
@background_image.draw(-1 * (offset_x / 5) - 300, -1 * (offset_y / 5) - 200, 0)
end
-
-
end
def draw
@@ -65,14 +61,8 @@ def draw
def button_down(id)
if id == Gosu::Button::KbEscape
+ EM.stop
close
end
end
-end
-
-#def get(path)
-# start = Time.now
-# result = JSON.load(open("http://209.20.91.156/#{path}"))
-# puts "Took " + (start - Time.now).to_s + " seconds"
-# result
-#end
+end
View
@@ -0,0 +1,19 @@
+require 'rubygems'
+require 'gosu'
+require 'game_window'
+require 'server_client'
+
+$game_objects = {}
+$identity = "me"
+
+Thread.abort_on_exception = true
+
+gui_window = Proc.new do
+ window = GameWindow.new
+ window.show
+end
+
+EventMachine::run do
+ EventMachine::open_datagram_socket('0.0.0.0', rand(1000) + 7000, ServerClient)
+ EventMachine::defer gui_window, Proc.new { exit }
+end
@@ -1,21 +1,22 @@
class GameObject
- attr :player_id
+ attr :parent_id
attr :x
attr :y
+ attr :image
def initialize(data)
- @x = data["x"]
- @y = data["y"]
- @angle = data["angle"].radians_to_gosu
- @player_id = data["parent_id"] || data["player_id"]
- @id = data["id"]
- @image = data["image"]
- @health = data["health"]
+ @x = data[:x] || data["x"]
+ @y = data[:y] || data["y"]
+ @angle = (data[:angle] || data["angle"]).radians_to_gosu
+ @parent_id = data[:parent_id] || data[:player_id] || data["parent_id"] || data["player_id"]
+ @id = data[:id] || data["id"]
+ @image = data[:image] || data["image"]
+ @health = data[:health] || data["health"]
end
def self.load_assets(window)
- @@assets = {"ship" => Gosu::Image.new(window, "images/ship.png", false),
- "missile" => Gosu::Image.new(window, "images/missile.png", false)}
+ @@assets = {:ship => Gosu::Image.new(window, "images/ship.png", false),
+ :missile => Gosu::Image.new(window, "images/missile.png", false)}
@@health_font = Gosu::Font.new(window, "Myriad Pro", 20)
end
@@ -28,7 +29,7 @@ def offsets
def draw(offset_x, offset_y)
x = @x - offset_x
y = @y - offset_y
- @@assets[@image].draw_rot(x, y, 1, @angle)
+ @@assets[@image.to_sym].draw_rot(x, y, 1, @angle)
end
def draw_health
View
@@ -3,14 +3,15 @@
class Player
attr :id, true
- def initialize(data)
+ def initialize(data = {})
@name = data["name"]
@id = data["id"]
@score = data["score"]
end
- def self.create
- Player.new(SpaceBrawlServer.new_player)
+ def self.create(identity)
+ # Implement
+ Player.new("id" => identity)
end
def method_missing(method, *args)
@@ -1,26 +0,0 @@
-require 'rubygems'
-require 'httparty'
-
-class SpaceBrawlServer
- include HTTParty
- base_uri '209.20.91.156'
- format :json
-
- def self.universe
- #start = Time.now
- result = get("/universe")
- #puts "took #{Time.now - start}"
- result
- end
-
- def self.action(player, action)
- start = Time.now
- result = get("/players/#{player.id}/action", :do => action)
- puts "took #{Time.now - start}"
- result
- end
-
- def self.new_player
- get("/players/new")
- end
-end
View
@@ -1,3 +0,0 @@
-require 'game_window'
-require 'eventmachine'
-require 'nanite'
View
@@ -0,0 +1,46 @@
+require 'rubygems'
+require 'eventmachine'
+require 'json'
+
+module ServerClient
+ @@action = nil
+ @@connection = nil
+
+ def post_init
+ puts "client connected!"
+ @@connection = self
+ ServerClient.start_update_cycle
+ ServerClient.action("accelerate")
+ end
+
+ def self.action(action)
+ @@connection.send_datagram("#{$identity} #{action}", "0.0.0.0", 7779)
+ end
+
+ def self.start_update_cycle
+ $game_objects.keys.each do |key|
+ $game_objects[key][:tired] = true
+ end
+ ServerClient.action("update")
+ end
+
+ def receive_data(data)
+ if data == "done"
+ # clean the data of things we didn't get updates for
+ $game_objects.keys.each do |key|
+ if $game_objects[key][:tired]
+ $game_objects.delete key
+ end
+ end
+
+ #start the cycle again
+ ServerClient.start_update_cycle
+ else
+
+ # We are handling a torrent of updates right here
+ update = JSON::load(data)
+ $game_objects.merge!(update)
+ p update.inspect
+ end
+ end
+end
Binary file not shown.
@@ -20,7 +20,7 @@
EM.run do
# start up a new mapper with a ping time of 15 seconds
- Nanite.start_mapper(:host => 'localhost', :user => 'mapper', :pass => 'testing', :vhost => '/nanite', :log_level => 'info')
+ Nanite.start_mapper(:host => 'spacebrawl.hamptoncatlin.com', :user => 'server', :pass => 'test', :vhost => '/nanite', :log_level => 'info')
# have this run after 16 seconds so we can be pretty sure that the mapper
# has already received pings from running nanites and registered them.
@@ -1,5 +1,6 @@
---
+:host: spacebrawl.hamptoncatlin.com
:user: nanite
:pass: testing
:vhost: /nanite
-:log_level: debug
+:log_level: debug
@@ -0,0 +1,6 @@
+
+:host: localhost
+:user: nanite
+:pass: testing
+:vhost: /nanite
+:log_level: debug
View
@@ -14,5 +14,3 @@ def self.offset_y(angle, distance)
Math.sin(angle) * distance
end
end
-
-$cache = Moneta::Memcache.new(:server => "localhost")
@@ -40,11 +40,10 @@ def initialize
end
def load_new_ships
- Player.all(:conditions => {:ship_id => nil}).each do |player|
- ship = Ship.new(player.id)
- add_game_object ship
- player.ship_id = ship.id
- player.save
+ $players.keys.each do |player_id|
+ if !(@objects.detect { |o| o.parent_id == player_id })
+ add_game_object(Ship.new(player_id))
+ end
end
end
@@ -72,12 +71,14 @@ def update
@space.step(@dt)
@objects.each do |object|
- result = object.perform_actions
- if result.is_a? GameObject
- add_game_object result
- elsif result == false
- puts "REMOVING"
- remove_game_object(object)
+ results = object.perform_actions
+ results.each do |result|
+ if result.is_a? GameObject
+ add_game_object result
+ elsif result == false
+ puts "REMOVING"
+ remove_game_object(object)
+ end
end
end
View
@@ -16,9 +16,9 @@ def initialize(ship = nil)
def perform_actions
if @kill_at < Time.now
- false
+ [false]
else
- true
+ [true]
end
end
View
@@ -13,19 +13,26 @@ def self.shape_array
end
def self.mass
- 500.0
+ 100.0
end
def self.inertia
- 500.0
+ 60.0
end
+ # TODO: perform actions as actor
def perform_actions
- command = $cache.delete("player_#{@parent_id}")
- if command
- puts command + " #{@parent_id}"
- self.send(command)
+ new_objects = []
+ while(command = $players[@parent_id][:actions].pop)
+ if command
+ puts command + " #{@parent_id}"
+ new_object = self.send(command)
+ if new_object
+ new_objects << new_object
+ end
+ end
end
+ new_objects
end
def warp(vect)
@@ -58,7 +65,7 @@ def hit!
# forward momentum by creating a vector in the direction of the facing
# and with a magnitude representing the force we want to apply
def accelerate
- @shape.body.apply_force((@shape.body.a.radians_to_vec2 * (5000.0)), CP::Vec2.new(0.0, 0.0))
+ @shape.body.apply_force((@shape.body.a.radians_to_vec2 * (20000.0)), CP::Vec2.new(0.0, 0.0))
end
def fire_missile
Oops, something went wrong.

0 comments on commit d8ae3ba

Please sign in to comment.