initial attempt w sock.lua library

This commit is contained in:
Tangent 2019-05-16 16:55:01 -07:00
commit f1f7e2060c
5 changed files with 83 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
!src/lume.lua
!src/bitser.lua
!src/sock.lua
*.lua

5
src/Ship.moon Normal file
View File

@ -0,0 +1,5 @@
class Ship
new: (@x=0, @y=0, @vx=0, @vy=0) =>
update: (dt) =>
@x += @vx * dt
@y += @vy * dt

42
src/client.moon Normal file
View File

@ -0,0 +1,42 @@
w, h = love.graphics.getDimensions!
sock = require "sock"
Ship = require "Ship"
-- TODO sync moving ships
ships = {}
ships[1] = Ship!
client = sock.newClient("localhost", 22122)
client\on "u", (contacts) ->
ships = contacts -- very naive
tmp_flag = false
request_frequency = 1/10
time = 0
love.update = (dt) ->
client\update!
unless tmp_flag
if client\getState! == "connected"
tmp_flag = true
client\send "n", ships[1]
if love.keyboard.isDown "w"
ships[1].vy -= 100 * dt
if love.keyboard.isDown "a"
ships[1].vx -= 100 * dt
if love.keyboard.isDown "s"
ships[1].vy += 100 * dt
if love.keyboard.isDown "d"
ships[1].vx += 100 * dt
time += dt
if time >= request_frequency
time -= request_frequency
client\send "u"
love.draw = ->
love.graphics.translate w / 2, h / 2

4
src/main.moon Normal file
View File

@ -0,0 +1,4 @@
if arg[2] == "server"
require "server"
else
require "client"

28
src/server.moon Normal file
View File

@ -0,0 +1,28 @@
sock = require "sock"
Ship = require "Ship"
ships = {}
server = sock.newServer "*", 22122
server\on "connect", (data, client) ->
print data, client
server\on "u", (data, client) ->
print data, client
client\send "u", ships
-- TODO filter to in-range, return only needed data (how will bitser parse classes?)
server\on "n", (data, client) ->
table.insert ships, data
time, status_frequency = 0, 5
love.update = (dt) ->
server\update!
for ship in *ships
ship\update dt
time += dt
if time >= status_frequency
time -= status_frequency
print server\getMaxChannels!, server\getMaxPeers!, server\getMessageTimeout!