Skip to content

Commit 9368a57

Browse files
committed
net: Support object movement
1 parent b0d836d commit 9368a57

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

mpserv.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def serve(self):
201201
self.pos["y"] = msg["y"]
202202
self.moved()
203203

204-
elif t == "objSetOpen":
204+
elif t in ("objSetOpen", "objMove"):
205205
self.relay(msg)
206206

207207
elif t == "close":

src/net.ts

+20-5
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ module Netcode {
7676
send("ident", { name });
7777
}
7878

79-
function findObjectByPID(pid: number): Obj|null {
80-
return _.find(gMap.getObjects(), (obj: Obj) => obj.pid === pid) || null;
79+
function findObjectByUID(uid: number): Obj|null {
80+
return _.find(gMap.getObjects(), (obj: Obj) => obj.uid === uid) || null;
8181
}
8282

8383
function setupCommonEvents(): void {
@@ -93,13 +93,13 @@ module Netcode {
9393

9494
// Object open/close
9595
on("objSetOpen", (msg: any) => {
96-
const obj = findObjectByPID(msg.pid);
97-
console.assert(obj !== null, "objSetOpen: No such object");
96+
const obj = findObjectByUID(msg.uid);
97+
console.assert(obj !== null, "net.objSetOpen: No such object");
9898
setObjectOpen(obj, msg.open, false, false);
9999
});
100100

101101
Events.on("objSetOpen", (msg: any) => {
102-
send("objSetOpen", { pid: msg.obj.pid, open: msg.open });
102+
send("objSetOpen", { uid: msg.obj.uid, open: msg.open });
103103
});
104104
}
105105

@@ -155,6 +155,10 @@ module Netcode {
155155

156156
send("changeElevation", { elevation: currentElevation, position: player.position, orientation: player.orientation });
157157
});
158+
159+
Events.on("objMove", (e: any) => {
160+
send("objMove", { uid: e.obj.uid, position: e.position });
161+
});
158162
}
159163

160164
export function join(): void {
@@ -174,6 +178,17 @@ module Netcode {
174178
gMap.objects[currentElevation].push(netPlayer);
175179
}
176180
});
181+
182+
on("objMove", (e: any) => {
183+
const obj = findObjectByUID(e.uid);
184+
console.assert(obj !== null, "net.objMove: No such object");
185+
186+
console.log("Move: uid %o, obj %o, pos %o", e.uid, obj, e.position);
187+
188+
// Doesn't matter if we signal events or not, we're on the guest -- we won't be sending them (for now).
189+
// If this changes, we shouldn't signal events here.
190+
obj.move(e.position);
191+
});
177192
}
178193

179194
export function changeMap(): void {

src/object.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -574,9 +574,12 @@ class Obj {
574574

575575
// Moves the object; returns `true` if successfully moved,
576576
// or `false` if interrupted (such as by an exit grid).
577-
move(position: Point, curIdx?: number): boolean {
577+
move(position: Point, curIdx?: number, signalEvents: boolean=true): boolean {
578578
this.position = position
579579

580+
if(signalEvents)
581+
Events.emit("objMove", { obj: this, position })
582+
580583
// rebuild the lightmap
581584
if(Config.engine.doFloorLighting)
582585
Lightmap.rebuildLight()

0 commit comments

Comments
 (0)