Last active
September 23, 2022 15:26
-
-
Save at15four2020/6b405f224d91fa50975546031f764aec to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// BlockProgramming.gprog.js | |
if (typeof BlockProgramming == "undefined") { | |
BlockProgramming = (function() { | |
if (typeof parse_in_Users == 'undefined') { | |
load('https://gist.github.com/at15four2020/2d69302316f3705dec18e6a04c432896/raw/parse_in_Users.gprog.js') | |
} | |
if (typeof parse_in_UserUpdate == 'undefined') { | |
load('https://gist.github.com/at15four2020/2d69302316f3705dec18e6a04c432896/raw/parse_in_UserUpdate.gprog.js') | |
} | |
const watchingUsers = {} | |
const watchingObjects = {} | |
function handleUsers(message) { | |
const packet = message.getPacket() | |
parse_in_Users(packet) | |
} | |
function handleObjectUpdate(message) { | |
const packet = message.getPacket() | |
const objectUid = packet.readString() | |
if (watchingObjects[objectUid]) { | |
const handlers = watchingObjects[objectUid] | |
for (let i = 0; i < handlers.length; i++) { | |
if (typeof handlers[i] == 'function') | |
handlers[i]() | |
else | |
throw new Error("Unknown handler type: "+(typeof handlers[i])) | |
} | |
} | |
} | |
function handleUserUpdate(message) { | |
const packet = message.getPacket() | |
const users = parse_in_UserUpdate(packet).users | |
for (let i = 0; i < users.length; i++) { | |
const userIndex = users[i].index | |
const userId = parse_in_Users.users.indexToId[userIndex] | |
if (watchingUsers[userId]) { | |
const handlers = watchingUsers[userId] | |
for (let j = 0; j < handlers.length; j++) { | |
handlers[j](users[i].posX, users[i].posY) | |
} | |
} | |
} | |
} | |
function initIntercepting() { | |
interceptToClient("Users", handleUsers) | |
interceptToClient("ObjectDataUpdate", handleObjectUpdate) | |
interceptToClient("UserUpdate", handleUserUpdate) | |
} | |
function init() { | |
initIntercepting() | |
} | |
function exit() { | |
watchingUsers = {} | |
watchingObjects = {} | |
} | |
function when(watchable) { | |
const toDos = [] | |
function callback() { | |
for (let i = 0; i < toDos.length; i++) { | |
if (toDos[i].condition && !toDos[i].condition()) | |
continue | |
toDos[i].action() | |
} | |
} | |
function ifFunction(gettable) { | |
function ifDoFunction(action) { | |
toDos.push({ | |
condition: gettable.getter, | |
action: action, | |
}) | |
} | |
return { | |
do: ifDoFunction, | |
} | |
} | |
function doFunction(action) { | |
toDos.push({ action: action }) | |
} | |
watchable.watcher(callback) | |
return { | |
if: ifFunction, | |
do: doFunction, | |
} | |
} | |
function avatar(userId) { | |
function inPlace(expectedX, expectedY) { | |
let state = false | |
if (!watchingUsers[userId]) watchingUsers[userId] = [] | |
const watchers = [] | |
function handler(realX, realY) { | |
if (expectedX == realX && expectedY == realY) { | |
state = true | |
for (let i = 0; i < watchers.length; i++) { | |
watchers[i]() | |
} | |
} else { | |
state = false | |
} | |
} | |
watchingUsers[userId].push(handler) | |
function watcher(callback) { | |
watchers.push(callback) | |
} | |
function getter() { | |
return state | |
} | |
return { | |
watcher: watcher, | |
getter: getter, | |
} | |
} | |
return { | |
inPlace: inPlace, | |
} | |
} | |
function object(objectUid) { | |
function changeState() { | |
function watcher(callback) { | |
if (!watchingObjects[objectUid]) watchingObjects[objectUid] = [] | |
function handler() { | |
callback() | |
} | |
watchingObjects[objectUid].push(handler) | |
} | |
return { | |
watcher: watcher, | |
} | |
} | |
return { | |
changeState: changeState, | |
} | |
} | |
function moveTo(x, y) { | |
function action() { | |
sendToServer(HPacketToServer("MoveAvatar", [x, y])) | |
} | |
return action | |
} | |
return { | |
init: init, | |
exit: exit, | |
when: when, | |
object: object, | |
avatar: avatar, | |
ACTIONS: { | |
moveTo: moveTo, | |
} | |
} | |
})() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment