mirror of
https://github.com/cyberarm/i-mic-fps.git
synced 2025-12-15 07:32:35 +00:00
Compare commits
1 Commits
f10b98b0f0
...
networking
| Author | SHA1 | Date | |
|---|---|---|---|
| c18309614f |
1
Gemfile
1
Gemfile
@@ -2,6 +2,7 @@ source "https://rubygems.org"
|
||||
gem "opengl-bindings", require: "opengl"
|
||||
gem "cyberarm_engine", git: "https://github.com/cyberarm/cyberarm_engine"
|
||||
gem "nokogiri", ">= 1.11.0.rc1"
|
||||
gem "async-websocket"
|
||||
|
||||
group(:packaging) do
|
||||
gem "ocra"
|
||||
|
||||
16
i-mic-fps-server.rb
Normal file
16
i-mic-fps-server.rb
Normal file
@@ -0,0 +1,16 @@
|
||||
require "async/websocket"
|
||||
|
||||
require_relative "lib/networking/director"
|
||||
require_relative "lib/networking/packet_handler"
|
||||
require_relative "lib/networking/server"
|
||||
require_relative "lib/networking/client"
|
||||
|
||||
require_relative "lib/networking/backends/memory_server"
|
||||
require_relative "lib/networking/backends/memory_connection"
|
||||
|
||||
director = IMICFPS::Networking::Director.new(mode: :server, hostname: "0.0.0.0", port: 56789, interface: IMICFPS::Networking::MemoryServer)
|
||||
director.define_singleton_method(:tick) do |dt|
|
||||
puts "Ticked: #{dt}"
|
||||
end
|
||||
|
||||
director.run.join
|
||||
@@ -106,6 +106,15 @@ require_relative "lib/scenes/turn_table"
|
||||
require_relative "lib/crosshair"
|
||||
require_relative "lib/demo"
|
||||
|
||||
require_relative "lib/networking/director"
|
||||
require_relative "lib/networking/packet_handler"
|
||||
require_relative "lib/networking/client"
|
||||
require_relative "lib/networking/server"
|
||||
require_relative "lib/networking/connection"
|
||||
|
||||
require_relative "lib/networking/backends/memory_server"
|
||||
require_relative "lib/networking/backends/memory_connection"
|
||||
|
||||
require_relative "lib/window"
|
||||
|
||||
require_relative "lib/tools/asset_viewer"
|
||||
|
||||
@@ -32,9 +32,6 @@ class IMICFPS
|
||||
end
|
||||
|
||||
add_entity(Player.new(spawnpoint: @map_parser.spawnpoints.sample, manifest: Manifest.new(package: "base", name: "character")))
|
||||
|
||||
# add_light(Light.new(id: available_light, position: Vector.new(30, 10.0, 30)))
|
||||
# add_light(Light.new(id: available_light, position: Vector.new(0, 100, 0), diffuse: Color.new(1.0, 0.5, 0.1)))
|
||||
end
|
||||
|
||||
def data
|
||||
@@ -59,7 +56,6 @@ class IMICFPS
|
||||
@collision_manager.update
|
||||
|
||||
@entities.each(&:update)
|
||||
# @lights.each(&:update)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
6
lib/networking/backends/memory_connection.rb
Normal file
6
lib/networking/backends/memory_connection.rb
Normal file
@@ -0,0 +1,6 @@
|
||||
class IMICFPS
|
||||
module Networking
|
||||
class MemoryConnection < Connection
|
||||
end
|
||||
end
|
||||
end
|
||||
6
lib/networking/backends/memory_server.rb
Normal file
6
lib/networking/backends/memory_server.rb
Normal file
@@ -0,0 +1,6 @@
|
||||
class IMICFPS
|
||||
module Networking
|
||||
class MemoryServer < Server
|
||||
end
|
||||
end
|
||||
end
|
||||
18
lib/networking/client.rb
Normal file
18
lib/networking/client.rb
Normal file
@@ -0,0 +1,18 @@
|
||||
class IMICFPS
|
||||
module Networking
|
||||
class Client
|
||||
def initialize(socket:)
|
||||
@socket = socket
|
||||
end
|
||||
|
||||
def read
|
||||
end
|
||||
|
||||
def write
|
||||
end
|
||||
|
||||
def close
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
14
lib/networking/connection.rb
Normal file
14
lib/networking/connection.rb
Normal file
@@ -0,0 +1,14 @@
|
||||
class IMICFPS
|
||||
module Networking
|
||||
class Connection
|
||||
def initialize(hostname:, port:)
|
||||
end
|
||||
|
||||
def connect
|
||||
end
|
||||
|
||||
def close
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
61
lib/networking/director.rb
Normal file
61
lib/networking/director.rb
Normal file
@@ -0,0 +1,61 @@
|
||||
class IMICFPS
|
||||
module Networking
|
||||
class Director
|
||||
attr_reader :mode, :hostname, :port, :tick_rate, :storage
|
||||
def initialize(mode:, hostname:, port:, interface:, state: nil, tick_rate: 2)
|
||||
@mode = mode
|
||||
@hostname = hostname
|
||||
@port = port
|
||||
@state = state
|
||||
@tick_rate = (1000.0 / tick_rate) / 1000.0
|
||||
|
||||
case @mode
|
||||
when :server
|
||||
@server = interface.new(hostname: @hostname, port: @port)
|
||||
when :connection
|
||||
@connection = interface.new(hostname: @hostname, port: @port)
|
||||
when :memory
|
||||
@server = interface[:server].new(hostname: @hostname, port: @port)
|
||||
@connection = interface[:connection].new(hostname: @hostname, port: @port)
|
||||
else
|
||||
raise ArgumentError, "Expected mode to be :server, :connection, or :memory, not #{mode.inspect}"
|
||||
end
|
||||
|
||||
@last_tick_time = milliseconds
|
||||
@directing = true
|
||||
@storage = {}
|
||||
end
|
||||
|
||||
def run
|
||||
Thread.start do |thread|
|
||||
while(@directing)
|
||||
dt = milliseconds - @last_tick_time
|
||||
|
||||
tick(dt)
|
||||
|
||||
@server.update if @server
|
||||
@connection.update if @connection
|
||||
|
||||
@last_tick_time = milliseconds
|
||||
sleep(@tick_rate)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def tick(dt)
|
||||
end
|
||||
|
||||
def shutdown
|
||||
@directing = false
|
||||
|
||||
@clients.each(&:close)
|
||||
@server.update if @server
|
||||
@connection.update if @connection
|
||||
end
|
||||
|
||||
def milliseconds
|
||||
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
11
lib/networking/events.rb
Normal file
11
lib/networking/events.rb
Normal file
@@ -0,0 +1,11 @@
|
||||
class IMICFPS
|
||||
module Networking
|
||||
module Events
|
||||
def on_connect(client)
|
||||
end
|
||||
|
||||
def on_disconnect(client)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
18
lib/networking/packet.rb
Normal file
18
lib/networking/packet.rb
Normal file
@@ -0,0 +1,18 @@
|
||||
class IMICFPS
|
||||
module Networking
|
||||
class Packet
|
||||
def initialize(type:, payload:)
|
||||
end
|
||||
|
||||
def self.encode(packet)
|
||||
"#{packet.type}|#{packet.payload}"
|
||||
end
|
||||
|
||||
def self.decode(string)
|
||||
split = string.split("|")
|
||||
|
||||
Packet.new(split.first, split.last)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
6
lib/networking/packet_handler.rb
Normal file
6
lib/networking/packet_handler.rb
Normal file
@@ -0,0 +1,6 @@
|
||||
class IMICFPS
|
||||
module Networking
|
||||
class PacketHandler
|
||||
end
|
||||
end
|
||||
end
|
||||
26
lib/networking/server.rb
Normal file
26
lib/networking/server.rb
Normal file
@@ -0,0 +1,26 @@
|
||||
class IMICFPS
|
||||
module Networking
|
||||
MAX_CLIENTS = 32
|
||||
|
||||
class Server
|
||||
attr_reader :hostname, :port, :max_clients, :clients
|
||||
def initialize(hostname:, port:, max_clients: MAX_CLIENTS)
|
||||
@hostname = hostname
|
||||
@port = port
|
||||
@max_clients = max_clients
|
||||
|
||||
@clients = []
|
||||
@socket = nil
|
||||
end
|
||||
|
||||
def bind
|
||||
end
|
||||
|
||||
def broadcast(packet)
|
||||
end
|
||||
|
||||
def close
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -9,6 +9,7 @@ class IMICFPS
|
||||
@player = @map.find_entity_by(name: "character")
|
||||
@camera = Camera.new(position: @player.position.clone)
|
||||
@camera.attach_to(@player)
|
||||
@director = Networking::Director.new(mode: :memory, hostname: "i-mic.rubyclan.org", port: 56789, interface: { server: Networking::MemoryServer, connection: Networking::MemoryConnection }, state: self)
|
||||
|
||||
@crosshair = Crosshair.new
|
||||
|
||||
@@ -39,6 +40,7 @@ class IMICFPS
|
||||
control_player
|
||||
|
||||
@camera.update
|
||||
@director.tick(window.dt)
|
||||
|
||||
if window.config.get(:debug_options, :stats)
|
||||
@text.text = update_text
|
||||
|
||||
Reference in New Issue
Block a user