BlueGit

timfa questward

Pickup ui options

timfa Tim Falken · pushed by timfa

Authored 2026-08-24 · 2 months ago

Hash
47f357e3b73ebae433d66a308347af97f0a022c1
Parent
85fcf3d8
2 files changed +136 −33
client/src/play/main.js +116 −31
@@ -26,6 +26,7 @@ let social;
26 26 let chat;
27 27 let inventory;
28 28 let contextMenu;
29 let pendingPickup = null;
29 30
30 31 function showError (message)
31 32 {
@@ -66,11 +67,54 @@ function uiBlocked ()
66 67 return !!(social?.isBlocking() || chat?.isBlocking() || inventory?.isBlocking() || contextMenu?.isOpen());
67 68 }
68 69
70 function clearPendingPickup ()
71 {
72 pendingPickup = null;
73 }
74
75 function requestPickup (ground)
76 {
77 if (!ground)
78 { return; }
79 pendingPickup = {
80 groundItemId: ground.groundItemId,
81 tileX: ground.tileX,
82 tileY: ground.tileY
83 };
84 const local = view.entities.get(view.localEntityId);
85 if (local && local.tileX === ground.tileX && local.tileY === ground.tileY)
86 {
87 flushPendingPickup();
88 return;
89 }
90 socket.send({ type: MessageType.moveTo, tileX: ground.tileX, tileY: ground.tileY });
91 }
92
93 function flushPendingPickup ()
94 {
95 if (!pendingPickup || !view)
96 { return; }
97 const item = view.groundItems.get(pendingPickup.groundItemId);
98 if (!item)
99 {
100 clearPendingPickup();
101 return;
102 }
103 const local = view.entities.get(view.localEntityId);
104 if (!local)
105 { return; }
106 if (local.tileX !== pendingPickup.tileX || local.tileY !== pendingPickup.tileY)
107 { return; }
108 socket.send({ type: MessageType.pickupItem, groundItemId: pendingPickup.groundItemId });
109 clearPendingPickup();
110 }
111
69 112 function handleLeftClick (mouseX, mouseY)
70 113 {
71 114 const entity = view.entityAtScreen(mouseX, mouseY);
72 115 if (entity && entity.entityId !== view.localEntityId && !entity.untargetable)
73 116 {
117 clearPendingPickup();
74 118 if (entity.characterId)
75 119 {
76 120 social.inspect(entity);
@@ -91,59 +135,90 @@ function handleLeftClick (mouseX, mouseY)
91 135 const tile = view.screenToTile(mouseX, mouseY);
92 136 const local = view.entities.get(view.localEntityId);
93 137 const ground = view.groundItemAtTile(tile.tileX, tile.tileY);
94 if (ground && local && local.tileX === ground.tileX && local.tileY === ground.tileY)
95 { socket.send({ type: MessageType.pickupItem, groundItemId: ground.groundItemId }); }
138 if (ground)
139 { requestPickup(ground); }
96 140 else if (isBankTile(view, tile.tileX, tile.tileY) && local && chebyshev(local.tileX, local.tileY, tile.tileX, tile.tileY) <= 1)
97 { socket.send({ type: MessageType.openBank }); }
141 {
142 clearPendingPickup();
143 socket.send({ type: MessageType.openBank });
144 }
98 145 else
99 { socket.send({ type: MessageType.moveTo, tileX: tile.tileX, tileY: tile.tileY }); }
146 {
147 clearPendingPickup();
148 socket.send({ type: MessageType.moveTo, tileX: tile.tileX, tileY: tile.tileY });
149 }
100 150 }
101 151
102 152 function handleRightClick (event, mouseX, mouseY)
103 153 {
104 154 event.preventDefault();
105 const entity = view.entityAtScreen(mouseX, mouseY);
106 155 const tile = view.screenToTile(mouseX, mouseY);
107 156 const items = [];
157 const walkTileX = tile.tileX;
158 const walkTileY = tile.tileY;
108 159
109 if (entity && entity.entityId !== view.localEntityId)
160 items.push({
161 label: "Walk here",
162 onSelect: () =>
163 {
164 clearPendingPickup();
165 socket.send({ type: MessageType.moveTo, tileX: walkTileX, tileY: walkTileY });
166 }
167 });
168
169 for (const entity of view.entitiesAtTile(tile.tileX, tile.tileY))
110 170 {
171 if (entity.entityId === view.localEntityId)
172 { continue; }
111 173 const targetName = entityLabel(entity);
112 items.push({
113 label: "Walk here",
114 onSelect: () => socket.send({ type: MessageType.moveTo, tileX: entity.tileX, tileY: entity.tileY })
115 });
116 174 items.push({
117 175 label: `Follow ${targetName}`,
118 onSelect: () => socket.send({ type: MessageType.follow, targetEntityId: entity.entityId })
119 });
120 if (!entity.untargetable)
121 {
122 if (entity.talkable)
176 onSelect: () =>
123 177 {
124 items.push({
125 label: "Talk",
126 onSelect: () => socket.send({ type: MessageType.interact, targetEntityId: entity.entityId })
127 });
128 }
129 if (entity.characterId)
130 {
131 items.push({
132 label: `${targetName} info`,
133 onSelect: () => social.inspect(entity)
134 });
178 clearPendingPickup();
179 socket.send({ type: MessageType.follow, targetEntityId: entity.entityId });
135 180 }
181 });
182 if (entity.untargetable)
183 { continue; }
184 if (entity.talkable)
185 {
186 items.push({
187 label: `Talk to ${targetName}`,
188 onSelect: () =>
189 {
190 clearPendingPickup();
191 socket.send({ type: MessageType.interact, targetEntityId: entity.entityId });
192 }
193 });
194 }
195 if (entity.characterId)
196 {
136 197 items.push({
137 label: `Attack ${targetName}`,
138 onSelect: () => socket.send({ type: MessageType.attack, targetEntityId: entity.entityId })
198 label: `${targetName} info`,
199 onSelect: () =>
200 {
201 clearPendingPickup();
202 social.inspect(entity);
203 }
139 204 });
140 205 }
206 items.push({
207 label: `Attack ${targetName}`,
208 onSelect: () =>
209 {
210 clearPendingPickup();
211 socket.send({ type: MessageType.attack, targetEntityId: entity.entityId });
212 }
213 });
141 214 }
142 else
215
216 for (const ground of view.groundItemsAtTile(tile.tileX, tile.tileY))
143 217 {
218 const name = groundLabel(ground);
144 219 items.push({
145 label: "Walk here",
146 onSelect: () => socket.send({ type: MessageType.moveTo, tileX: tile.tileX, tileY: tile.tileY })
220 label: `Pick up ${name}`,
221 onSelect: () => requestPickup(ground)
147 222 });
148 223 }
149 224
@@ -159,6 +234,14 @@ function entityLabel (entity)
159 234 return first || "target";
160 235 }
161 236
237 function groundLabel (ground)
238 {
239 const name = (ground?.displayName ?? "").trim() || "item";
240 if ((ground?.quantity ?? 1) > 1)
241 { return `${name} (${ground.quantity})`; }
242 return name;
243 }
244
162 245 function startPlay (payload)
163 246 {
164 247 playing = true;
@@ -192,6 +275,7 @@ function startPlay (payload)
192 275 }
193 276 if (playing)
194 277 {
278 flushPendingPickup();
195 279 social?.updateNametag(view, scene, view.localEntityId);
196 280 chat?.update(view, scene);
197 281 }
@@ -374,6 +458,7 @@ async function main ()
374 458 function returnToMenu (reason = "")
375 459 {
376 460 playing = false;
461 clearPendingPickup();
377 462 social?.reset();
378 463 chat?.reset();
379 464 inventory?.reset();
@@ -26,6 +26,7 @@ let social;
26let chat;
26let chat;
27let inventory;
27let inventory;
28let contextMenu;
28let contextMenu;
29let pendingPickup = null;
29
30
30function showError (message)
31function showError (message)
31{
32{
@@ -66,11 +67,54 @@ function uiBlocked ()
66 return !!(social?.isBlocking() || chat?.isBlocking() || inventory?.isBlocking() || contextMenu?.isOpen());
67 return !!(social?.isBlocking() || chat?.isBlocking() || inventory?.isBlocking() || contextMenu?.isOpen());
67}
68}
68
69
70function clearPendingPickup ()
71{
72 pendingPickup = null;
73}
74
75function requestPickup (ground)
76{
77 if (!ground)
78 { return; }
79 pendingPickup = {
80 groundItemId: ground.groundItemId,
81 tileX: ground.tileX,
82 tileY: ground.tileY
83 };
84 const local = view.entities.get(view.localEntityId);
85 if (local && local.tileX === ground.tileX && local.tileY === ground.tileY)
86 {
87 flushPendingPickup();
88 return;
89 }
90 socket.send({ type: MessageType.moveTo, tileX: ground.tileX, tileY: ground.tileY });
91}
92
93function flushPendingPickup ()
94{
95 if (!pendingPickup || !view)
96 { return; }
97 const item = view.groundItems.get(pendingPickup.groundItemId);
98 if (!item)
99 {
100 clearPendingPickup();
101 return;
102 }
103 const local = view.entities.get(view.localEntityId);
104 if (!local)
105 { return; }
106 if (local.tileX !== pendingPickup.tileX || local.tileY !== pendingPickup.tileY)
107 { return; }
108 socket.send({ type: MessageType.pickupItem, groundItemId: pendingPickup.groundItemId });
109 clearPendingPickup();
110}
111
69function handleLeftClick (mouseX, mouseY)
112function handleLeftClick (mouseX, mouseY)
70{
113{
71 const entity = view.entityAtScreen(mouseX, mouseY);
114 const entity = view.entityAtScreen(mouseX, mouseY);
72 if (entity && entity.entityId !== view.localEntityId && !entity.untargetable)
115 if (entity && entity.entityId !== view.localEntityId && !entity.untargetable)
73 {
116 {
117 clearPendingPickup();
74 if (entity.characterId)
118 if (entity.characterId)
75 {
119 {
76 social.inspect(entity);
120 social.inspect(entity);
@@ -91,59 +135,90 @@ function handleLeftClick (mouseX, mouseY)
91 const tile = view.screenToTile(mouseX, mouseY);
135 const tile = view.screenToTile(mouseX, mouseY);
92 const local = view.entities.get(view.localEntityId);
136 const local = view.entities.get(view.localEntityId);
93 const ground = view.groundItemAtTile(tile.tileX, tile.tileY);
137 const ground = view.groundItemAtTile(tile.tileX, tile.tileY);
94 if (ground && local && local.tileX === ground.tileX && local.tileY === ground.tileY)
138 if (ground)
95 { socket.send({ type: MessageType.pickupItem, groundItemId: ground.groundItemId }); }
139 { requestPickup(ground); }
96 else if (isBankTile(view, tile.tileX, tile.tileY) && local && chebyshev(local.tileX, local.tileY, tile.tileX, tile.tileY) <= 1)
140 else if (isBankTile(view, tile.tileX, tile.tileY) && local && chebyshev(local.tileX, local.tileY, tile.tileX, tile.tileY) <= 1)
97 { socket.send({ type: MessageType.openBank }); }
141 {
142 clearPendingPickup();
143 socket.send({ type: MessageType.openBank });
144 }
98 else
145 else
99 { socket.send({ type: MessageType.moveTo, tileX: tile.tileX, tileY: tile.tileY }); }
146 {
147 clearPendingPickup();
148 socket.send({ type: MessageType.moveTo, tileX: tile.tileX, tileY: tile.tileY });
149 }
100}
150}
101
151
102function handleRightClick (event, mouseX, mouseY)
152function handleRightClick (event, mouseX, mouseY)
103{
153{
104 event.preventDefault();
154 event.preventDefault();
105 const entity = view.entityAtScreen(mouseX, mouseY);
106 const tile = view.screenToTile(mouseX, mouseY);
155 const tile = view.screenToTile(mouseX, mouseY);
107 const items = [];
156 const items = [];
157 const walkTileX = tile.tileX;
158 const walkTileY = tile.tileY;
108
159
109 if (entity && entity.entityId !== view.localEntityId)
160 items.push({
161 label: "Walk here",
162 onSelect: () =>
163 {
164 clearPendingPickup();
165 socket.send({ type: MessageType.moveTo, tileX: walkTileX, tileY: walkTileY });
166 }
167 });
168
169 for (const entity of view.entitiesAtTile(tile.tileX, tile.tileY))
110 {
170 {
171 if (entity.entityId === view.localEntityId)
172 { continue; }
111 const targetName = entityLabel(entity);
173 const targetName = entityLabel(entity);
112 items.push({
113 label: "Walk here",
114 onSelect: () => socket.send({ type: MessageType.moveTo, tileX: entity.tileX, tileY: entity.tileY })
115 });
116 items.push({
174 items.push({
117 label: `Follow ${targetName}`,
175 label: `Follow ${targetName}`,
118 onSelect: () => socket.send({ type: MessageType.follow, targetEntityId: entity.entityId })
176 onSelect: () =>
119 });
120 if (!entity.untargetable)
121 {
122 if (entity.talkable)
123 {
177 {
124 items.push({
178 clearPendingPickup();
125 label: "Talk",
179 socket.send({ type: MessageType.follow, targetEntityId: entity.entityId });
126 onSelect: () => socket.send({ type: MessageType.interact, targetEntityId: entity.entityId })
127 });
128 }
129 if (entity.characterId)
130 {
131 items.push({
132 label: `${targetName} info`,
133 onSelect: () => social.inspect(entity)
134 });
135 }
180 }
181 });
182 if (entity.untargetable)
183 { continue; }
184 if (entity.talkable)
185 {
186 items.push({
187 label: `Talk to ${targetName}`,
188 onSelect: () =>
189 {
190 clearPendingPickup();
191 socket.send({ type: MessageType.interact, targetEntityId: entity.entityId });
192 }
193 });
194 }
195 if (entity.characterId)
196 {
136 items.push({
197 items.push({
137 label: `Attack ${targetName}`,
198 label: `${targetName} info`,
138 onSelect: () => socket.send({ type: MessageType.attack, targetEntityId: entity.entityId })
199 onSelect: () =>
200 {
201 clearPendingPickup();
202 social.inspect(entity);
203 }
139 });
204 });
140 }
205 }
206 items.push({
207 label: `Attack ${targetName}`,
208 onSelect: () =>
209 {
210 clearPendingPickup();
211 socket.send({ type: MessageType.attack, targetEntityId: entity.entityId });
212 }
213 });
141 }
214 }
142 else
215
216 for (const ground of view.groundItemsAtTile(tile.tileX, tile.tileY))
143 {
217 {
218 const name = groundLabel(ground);
144 items.push({
219 items.push({
145 label: "Walk here",
220 label: `Pick up ${name}`,
146 onSelect: () => socket.send({ type: MessageType.moveTo, tileX: tile.tileX, tileY: tile.tileY })
221 onSelect: () => requestPickup(ground)
147 });
222 });
148 }
223 }
149
224
@@ -159,6 +234,14 @@ function entityLabel (entity)
159 return first || "target";
234 return first || "target";
160}
235}
161
236
237function groundLabel (ground)
238{
239 const name = (ground?.displayName ?? "").trim() || "item";
240 if ((ground?.quantity ?? 1) > 1)
241 { return `${name} (${ground.quantity})`; }
242 return name;
243}
244
162function startPlay (payload)
245function startPlay (payload)
163{
246{
164 playing = true;
247 playing = true;
@@ -192,6 +275,7 @@ function startPlay (payload)
192 }
275 }
193 if (playing)
276 if (playing)
194 {
277 {
278 flushPendingPickup();
195 social?.updateNametag(view, scene, view.localEntityId);
279 social?.updateNametag(view, scene, view.localEntityId);
196 chat?.update(view, scene);
280 chat?.update(view, scene);
197 }
281 }
@@ -374,6 +458,7 @@ async function main ()
374function returnToMenu (reason = "")
458function returnToMenu (reason = "")
375{
459{
376 playing = false;
460 playing = false;
461 clearPendingPickup();
377 social?.reset();
462 social?.reset();
378 chat?.reset();
463 chat?.reset();
379 inventory?.reset();
464 inventory?.reset();
client/src/render/worldView.js +20 −2
@@ -232,12 +232,30 @@ export class WorldView
232 232
233 233 groundItemAtTile (tileX, tileY)
234 234 {
235 return this.groundItemsAtTile(tileX, tileY)[0] ?? null;
236 }
237
238 groundItemsAtTile (tileX, tileY)
239 {
240 const items = [];
235 241 for (const item of this.groundItems.values())
236 242 {
237 243 if (item.tileX === tileX && item.tileY === tileY)
238 { return item; }
244 { items.push(item); }
245 }
246 return items;
247 }
248
249 entitiesAtTile (tileX, tileY)
250 {
251 const matches = [];
252 for (const entity of this.entities.values())
253 {
254 if (entity.tileX === tileX && entity.tileY === tileY)
255 { matches.push(entity); }
239 256 }
240 return null;
257 matches.sort((a, b) => (b.object?.layer ?? 0) - (a.object?.layer ?? 0));
258 return matches;
241 259 }
242 260
243 261 setZoom (zoom, limits = { min: 1, max: 4 })
@@ -232,12 +232,30 @@ export class WorldView
232
232
233 groundItemAtTile (tileX, tileY)
233 groundItemAtTile (tileX, tileY)
234 {
234 {
235 return this.groundItemsAtTile(tileX, tileY)[0] ?? null;
236 }
237
238 groundItemsAtTile (tileX, tileY)
239 {
240 const items = [];
235 for (const item of this.groundItems.values())
241 for (const item of this.groundItems.values())
236 {
242 {
237 if (item.tileX === tileX && item.tileY === tileY)
243 if (item.tileX === tileX && item.tileY === tileY)
238 { return item; }
244 { items.push(item); }
245 }
246 return items;
247 }
248
249 entitiesAtTile (tileX, tileY)
250 {
251 const matches = [];
252 for (const entity of this.entities.values())
253 {
254 if (entity.tileX === tileX && entity.tileY === tileY)
255 { matches.push(entity); }
239 }
256 }
240 return null;
257 matches.sort((a, b) => (b.object?.layer ?? 0) - (a.object?.layer ?? 0));
258 return matches;
241 }
259 }
242
260
243 setZoom (zoom, limits = { min: 1, max: 4 })
261 setZoom (zoom, limits = { min: 1, max: 4 })