forked from Arkendorf/Kingsbowl-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Arkendorf#1 from Arkendorf/lua-enet
Lua enet has now replaced grease
- Loading branch information
Showing
6 changed files
with
91 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,29 @@ | ||
local grease = require("grease.init") | ||
local state = require "state" | ||
local gui = require "gui" | ||
local client = {} | ||
require "globals" | ||
client = {} | ||
|
||
client.init = function(t) | ||
state.networking = {} | ||
local networking = state.networking | ||
state.game = "client" | ||
client.grease = grease.udpClient() | ||
local success, err = client.grease:connect("127.0.0.1", 25565) | ||
print(success, err) | ||
if success then | ||
state.gui = gui.new(menus[3]) | ||
networking.host = enet.host_create() | ||
networking.server = networking.host:connect(ip.ip .. ':' .. ip.port) | ||
state.gui = gui.new(menus[3]) | ||
end | ||
|
||
client.update = function(dt) | ||
local event = state.networking.host:service(100) | ||
if event then | ||
if event.type == "connect" then | ||
print("Connected to", event.peer) | ||
end | ||
end | ||
end | ||
|
||
function client.update(dt) | ||
client.grease:update(dt) | ||
client.disconnect = function() | ||
state.networking.server:disconnect() | ||
state.networking.host:flush() | ||
end | ||
|
||
return client |
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,18 @@ | ||
local vector = require "vector" | ||
|
||
local getDistance = function(p1, p2) | ||
return vector.sum(p1, vector.scale(-1, p2)) | ||
end | ||
|
||
local circle_vs_circle = function(c1, c2) | ||
local r = c1.r+c2.r | ||
local dist_v = getDistance(c1.p, c2.p) | ||
local dist = math.sqrt(vector.mag_sq(dist_v)) | ||
local dist_norm = vector.norm(dist_v) | ||
local half_overlap = vector.scale((r - dist)/2, dist_norm) | ||
return {vector.sum(c1.p, half_overlap), vector.sum(c2.p, vector.scale(-1, half_overlap))} | ||
end | ||
|
||
return { | ||
circle_vs_circle = circle_vs_circle | ||
} |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
ip = {ip = "127.0.0.1", port = "25565"} | ||
keydowntable, keyuptable = unpack(require "keytable") | ||
require "enet" |
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 |
---|---|---|
@@ -1,15 +1,22 @@ | ||
local grease = require "grease.init" | ||
local gui = require "gui" | ||
local state = require "state" | ||
local server = {} | ||
require "globals" | ||
|
||
server.init = function() | ||
server.grease = grease.udpServer() | ||
server.grease:listen(25565) | ||
state.networking = {} | ||
local networking = state.networking | ||
state.game = "server" | ||
networking.host = enet.host_create(ip.ip .. ':' .. ip.port) | ||
state.gui = gui.new(menus[2]) | ||
end | ||
|
||
server.update = function(dt) | ||
server:update(dt) | ||
local event = state.networking.host:service(100) | ||
if event and event.type == "receive" then | ||
print("Got message: ", event.data, event.peer) | ||
event.peer:send(event.data) | ||
end | ||
end | ||
|
||
return server |
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
state = { | ||
local state = { | ||
game = nil, | ||
gui = nil | ||
gui = nil, | ||
networking = nil | ||
} | ||
|
||
return state |
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,40 @@ | ||
local sum = function(v1, v2) | ||
local result = {} | ||
for k,v1_k in pairs(v1) do | ||
result[k] = v1_k + v2[k] | ||
end | ||
return result | ||
end | ||
|
||
local scale = function(s, v) | ||
local result = {} | ||
for k,v_k in pairs(v) do | ||
result[k] = s*v_k | ||
end | ||
return result | ||
end | ||
|
||
local dot = function(v1, v2) | ||
local result = 0 | ||
for k,v1_k in pairs(v1) do | ||
result = result + v1_k * v2[k] | ||
end | ||
return result | ||
end | ||
|
||
local mag_sq = function(v) | ||
return dot(v, v) | ||
end | ||
|
||
local norm = function(v) | ||
local inv_mag = 1/math.sqrt(mag_sq(v)) | ||
return scale(inv_mag, v) | ||
end | ||
|
||
return { | ||
sum = sum, | ||
scale = scale, | ||
dot = dot, | ||
mag_sq = mag_sq, | ||
norm = norm | ||
} |