движение за курсором мышки
This commit is contained in:
+28
-36
@@ -25,17 +25,7 @@ const LogEntity = observer(function LogEntity({ log }: { log: LogDrop }) {
|
||||
);
|
||||
});
|
||||
|
||||
const TreeEntity = observer(function TreeEntity({
|
||||
tree,
|
||||
store,
|
||||
onPointerDown,
|
||||
onPointerUp,
|
||||
}: {
|
||||
tree: TreeState;
|
||||
store: GameStore;
|
||||
onPointerDown: (e: PointerEvent<HTMLDivElement>, id: string) => void;
|
||||
onPointerUp: (e: PointerEvent<HTMLDivElement>) => void;
|
||||
}) {
|
||||
const TreeEntity = observer(function TreeEntity({ tree, store }: { tree: TreeState; store: GameStore }) {
|
||||
const isChopping = tree.id === store.heldTreeId && store.isChopping;
|
||||
|
||||
return (
|
||||
@@ -44,9 +34,7 @@ const TreeEntity = observer(function TreeEntity({
|
||||
<div
|
||||
className={`entity tree ${isChopping ? 'tree--chopping' : ''}`}
|
||||
style={{ left: tree.x, top: tree.y, zIndex: Math.floor(tree.y) }}
|
||||
onPointerDown={(e) => onPointerDown(e, tree.id)}
|
||||
onPointerUp={onPointerUp}
|
||||
onPointerCancel={onPointerUp}
|
||||
data-tree-id={tree.id}
|
||||
>
|
||||
<PixelSprite pixels={TREE_SPRITE} size={64} />
|
||||
</div>
|
||||
@@ -102,35 +90,45 @@ function GameMap({ store }: Props) {
|
||||
};
|
||||
}, [store]);
|
||||
|
||||
function handleTreePointerDown(e: PointerEvent<HTMLDivElement>, id: string) {
|
||||
if (e.button !== 0)
|
||||
return;
|
||||
// Stop this from also bubbling to the map's own pointer handlers below (which would
|
||||
// otherwise interpret the same press as "walk to this empty ground point").
|
||||
e.stopPropagation();
|
||||
e.currentTarget.setPointerCapture(e.pointerId);
|
||||
store.holdTree(id);
|
||||
/** Finds the tree (if any) the pointer is currently over, by DOM hit-testing at its client
|
||||
* coordinates. */
|
||||
function treeIdAtPoint(clientX: number, clientY: number): string | null {
|
||||
const hit = document.elementFromPoint(clientX, clientY)?.closest('[data-tree-id]');
|
||||
return hit?.getAttribute('data-tree-id') ?? null;
|
||||
}
|
||||
|
||||
function handleTreePointerUp(e: PointerEvent<HTMLDivElement>) {
|
||||
e.stopPropagation();
|
||||
/** The character's action is purely a function of the current pointer state: if the primary
|
||||
* button isn't held, nothing happens; otherwise it heads for whatever's under the cursor right
|
||||
* now — a tree to chop, or open ground to walk to. Shared by pointerdown/move/up so panning the
|
||||
* cursor across a tree mid-drag starts/stops chopping it, and releasing the button stops it. */
|
||||
function followPointer(e: PointerEvent<HTMLDivElement>) {
|
||||
if ((e.buttons & 1) === 0) {
|
||||
store.release();
|
||||
return;
|
||||
}
|
||||
|
||||
function handleMapPointerDown(e: PointerEvent<HTMLDivElement>) {
|
||||
if (e.button !== 0)
|
||||
const treeId = treeIdAtPoint(e.clientX, e.clientY);
|
||||
if (treeId) {
|
||||
store.holdTree(treeId);
|
||||
return;
|
||||
}
|
||||
|
||||
const rect = e.currentTarget.getBoundingClientRect();
|
||||
e.currentTarget.setPointerCapture(e.pointerId);
|
||||
store.moveTo(e.clientX - rect.left, e.clientY - rect.top);
|
||||
}
|
||||
|
||||
function handlePointerDown(e: PointerEvent<HTMLDivElement>) {
|
||||
e.currentTarget.setPointerCapture(e.pointerId);
|
||||
followPointer(e);
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
className="game-map"
|
||||
ref={containerRef}
|
||||
onPointerDown={handleMapPointerDown}
|
||||
onPointerUp={store.release}
|
||||
onPointerDown={handlePointerDown}
|
||||
onPointerMove={followPointer}
|
||||
onPointerUp={followPointer}
|
||||
onPointerCancel={store.release}
|
||||
>
|
||||
{ready && (
|
||||
@@ -140,13 +138,7 @@ function GameMap({ store }: Props) {
|
||||
))}
|
||||
|
||||
{store.trees.map((tree) => (
|
||||
<TreeEntity
|
||||
key={tree.id}
|
||||
tree={tree}
|
||||
store={store}
|
||||
onPointerDown={handleTreePointerDown}
|
||||
onPointerUp={handleTreePointerUp}
|
||||
/>
|
||||
<TreeEntity key={tree.id} tree={tree} store={store} />
|
||||
))}
|
||||
|
||||
<CharacterEntity store={store} />
|
||||
|
||||
Reference in New Issue
Block a user