Compare commits
8 Commits
ef941cd344
...
main
Author | SHA1 | Date | |
---|---|---|---|
![]() |
cf607fa060 | ||
![]() |
ef87716a8c | ||
8459e7df21 | |||
9785da2e0d | |||
e67060aaf7 | |||
a84cb21d8b | |||
aa1be3c082 | |||
![]() |
62a1d22528 |
@@ -6,9 +6,10 @@ using UnityEngine.Networking;
|
||||
|
||||
public class CSNC : MonoBehaviour
|
||||
{
|
||||
public float tickInterval = 5f; // x times per sec
|
||||
public string ip;
|
||||
public GameObjectManager manager;
|
||||
|
||||
private float lastPing = 0; //last time connected to server
|
||||
public class EZtransform //ez consistent serialization of transforms
|
||||
{
|
||||
|
||||
@@ -79,8 +80,12 @@ public class CSNC : MonoBehaviour
|
||||
|
||||
void FixedUpdate()
|
||||
{
|
||||
IEnumerator req = request();
|
||||
StartCoroutine(req);
|
||||
if (Time.timeSinceLevelLoad - (1000f / tickInterval) > lastPing)
|
||||
{
|
||||
lastPing = Time.timeSinceLevelLoad;
|
||||
IEnumerator req = request();
|
||||
StartCoroutine(req);
|
||||
}
|
||||
}
|
||||
IEnumerator request()
|
||||
{
|
||||
|
@@ -1,5 +1,5 @@
|
||||
# Unity Setup
|
||||
First, set the ip of the csnc compatible server.
|
||||
First, set the ip of the csnc compatible server. \
|
||||
(it needs to be formatted as such http://192.51.100.123:1234/ or http://mydomain.tld:1234/)
|
||||

|
||||
|
||||
|
11
README.md
11
README.md
@@ -1,7 +1,16 @@
|
||||
# CSNC
|
||||
|
||||
Client-Sync-Net-Code for unity game objects
|
||||
Client-Sync-Net-Code for unity (but potentially not just unity) game objects
|
||||
|
||||
A set of very basic drop-in scripts for client-server games that allows 'p2p' style synchronization
|
||||
|
||||
great for synchronizing simple visual things that are mostly controlled by clients such as particles or thrown objects
|
||||
|
||||
## Unity Setup
|
||||
Please refer to : \
|
||||
https://gitea.apexfight.net/apex/CSNC/src/branch/main/Client/Unity#unity-setup
|
||||
|
||||
## NodeJS Setup
|
||||
Email me at vlrtch3571@gmail.com if you want me to make a tutorial or need help
|
||||
|
||||
this project is about 90% complete other than the godot and go implementations which will be done if i ever decide to make anything with either one
|
@@ -1,9 +1,13 @@
|
||||
const timeoutTickRate = 1; //timeout check every x seconds
|
||||
const timeoutLength = 5000; //timeout length in ms
|
||||
var gameObjectStore = {}; //object which stores CSNCgameobjects for each client
|
||||
|
||||
//below is an example of the structure of gameobjectstore
|
||||
/**
|
||||
* {
|
||||
* "2c0a48e9-40c6-4139-9697-992397773b12": [
|
||||
* "2c0a48e9-40c6-4139-9697-992397773b12":
|
||||
* time: Date.now(),
|
||||
* gameObjects: [
|
||||
* {
|
||||
"type": "razorblade",
|
||||
"transform": {
|
||||
@@ -42,6 +46,21 @@ var gameObjectStore = {}; //object which stores CSNCgameobjects for each client
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {string} uuid
|
||||
*/
|
||||
function removePlayer(uuid) {
|
||||
delete gameObjectStore[uuid];
|
||||
}
|
||||
|
||||
function checkTimeout() {
|
||||
for (let key in gameObjectStore) {
|
||||
if (Date.now() - gameObjectStore[key].time > timeoutLength) {
|
||||
removePlayer(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {http.ServerResponse<http.IncomingMessage>} res
|
||||
* @param {string} req
|
||||
@@ -53,9 +72,7 @@ async function handleResponse(res, req) {
|
||||
var request = JSON.parse(req);
|
||||
let gameObjects = request.gameObjects;
|
||||
|
||||
gameObjectStore[request.uuid] = gameObjects; //store gameobjects to send to other players
|
||||
|
||||
|
||||
gameObjectStore[request.uuid] = {gameObjects: gameObjects, time: Date.now()}; //store gameobjects to send to other players
|
||||
|
||||
res.writeHead(200);
|
||||
res.write(JSON.stringify(responseObjectHelper(request.uuid)));
|
||||
@@ -68,6 +85,7 @@ async function handleResponse(res, req) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//preps and returns data to send back to client
|
||||
function responseObjectHelper(uuid) {
|
||||
var responseObject = {
|
||||
@@ -76,9 +94,11 @@ function responseObjectHelper(uuid) {
|
||||
|
||||
for (let key in gameObjectStore) {
|
||||
if (key == uuid) continue; //dont send back players own data
|
||||
responseObject.gameObjects = responseObject.gameObjects.concat(gameObjectStore[key]);
|
||||
responseObject.gameObjects = responseObject.gameObjects.concat(gameObjectStore[key].gameObjects);
|
||||
}
|
||||
|
||||
return responseObject;
|
||||
}
|
||||
|
||||
setInterval(checkTimeout, timeoutTickRate * 1000);
|
||||
exports.handleResponse = handleResponse;
|
Reference in New Issue
Block a user