Skip to content

Commit

Permalink
Shift-clicking seems to work fine. Closes #15
Browse files Browse the repository at this point in the history
  • Loading branch information
bananu7 committed Oct 8, 2022
1 parent 602d128 commit b65fc3d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 27 deletions.
21 changes: 10 additions & 11 deletions packages/client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function App() {

const [selectedUnits, setSelectedUnits] = useState(new Set<UnitId>());

const mapClick = useCallback((p: Position, button: number) => {
const mapClick = useCallback((p: Position, button: number, shift: boolean) => {
if (selectedUnits.size === 0)
return;

Expand All @@ -78,17 +78,17 @@ function App() {
if (!selectedAction) {
break;
} else if (selectedAction.action === 'Move') {
multiplayer.moveCommand(Array.from(selectedUnits), p);
multiplayer.moveCommand(Array.from(selectedUnits), p, shift);
} else if (selectedAction.action === 'Attack') {
multiplayer.attackMoveCommand(Array.from(selectedUnits), p);
multiplayer.attackMoveCommand(Array.from(selectedUnits), p, shift);
} else if (selectedAction.action === 'Build') {
// Only send one harvester to build
// TODO send the closest one
multiplayer.buildCommand([selectedUnits.keys().next().value], selectedAction.building, p);
multiplayer.buildCommand([selectedUnits.keys().next().value], selectedAction.building, p, shift);
}
break;
case 2:
multiplayer.moveCommand(Array.from(selectedUnits), p);
multiplayer.moveCommand(Array.from(selectedUnits), p, shift);
break;
}

Expand Down Expand Up @@ -137,29 +137,28 @@ function App() {
}

if (selectedAction.action === 'Move') {
multiplayer.followCommand(Array.from(selectedUnits), targetId);
multiplayer.followCommand(Array.from(selectedUnits), targetId, shift);
} else if (selectedAction.action === 'Attack') {
multiplayer.attackCommand(Array.from(selectedUnits), targetId);
multiplayer.attackCommand(Array.from(selectedUnits), targetId, shift);
}
break;
case 2:
// TODO properly understand alliances
if (target.owner === 0) { // neutral
// TODO actually check if can harvest and is resource
multiplayer.harvestCommand(Array.from(selectedUnits), targetId);
multiplayer.harvestCommand(Array.from(selectedUnits), targetId, shift);
}
else if (target.owner === multiplayer.getPlayerIndex()) {
multiplayer.followCommand(Array.from(selectedUnits), targetId);
multiplayer.followCommand(Array.from(selectedUnits), targetId, shift);
}
else if (target.owner !== multiplayer.getPlayerIndex()) {
multiplayer.attackCommand(Array.from(selectedUnits), targetId);
multiplayer.attackCommand(Array.from(selectedUnits), targetId, shift);
}
break;
}
}, [lastUpdatePacket, selectedAction, selectedUnits]);

const boardSelectUnits = (newUnits: Set<UnitId>, shift: boolean) => {
console.log("boardSelectUnits", newUnits)
setSelectedAction(undefined);
if (shift) {
setSelectedUnits(units => new Set([...units, ...newUnits]));
Expand Down
24 changes: 12 additions & 12 deletions packages/client/src/Multiplayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,14 @@ export class Multiplayer {
this.channel.emit('chat message', 'msg')
}

moveCommand(unitIds: UnitId[], target: Position) {
moveCommand(unitIds: UnitId[], target: Position, shift: boolean) {
const cmd : CommandPacket = {
action: {
typ: 'Move',
target
},
unitIds,
shift: false,
shift,
};
this.channel.emit('command', cmd)
}
Expand All @@ -218,38 +218,38 @@ export class Multiplayer {
this.channel.emit('command', cmd)
}

followCommand(unitIds: UnitId[], target: UnitId) {
followCommand(unitIds: UnitId[], target: UnitId, shift: boolean) {
const cmd : CommandPacket = {
action: {
typ: 'Follow',
target
},
unitIds,
shift: false,
shift,
};
this.channel.emit('command', cmd);
}

attackCommand(unitIds: UnitId[], target: UnitId) {
attackCommand(unitIds: UnitId[], target: UnitId, shift: boolean) {
const cmd : CommandPacket = {
action: {
typ: 'Attack',
target
},
unitIds,
shift: false,
shift,
};
this.channel.emit('command', cmd);
}

attackMoveCommand(unitIds: UnitId[], target: Position) {
attackMoveCommand(unitIds: UnitId[], target: Position, shift: boolean) {
const cmd : CommandPacket = {
action: {
typ: 'AttackMove',
target
},
unitIds,
shift: false,
shift,
};
this.channel.emit('command', cmd);
}
Expand All @@ -266,27 +266,27 @@ export class Multiplayer {
this.channel.emit('command', cmd);
}

buildCommand(unitIds: UnitId[], building: string, position: Position) {
buildCommand(unitIds: UnitId[], building: string, position: Position, shift: boolean) {
const cmd : CommandPacket = {
action: {
typ: 'Build',
building,
position
},
unitIds,
shift: false,
shift,
};
this.channel.emit('command', cmd);
}

harvestCommand(unitIds: UnitId[], target: UnitId) {
harvestCommand(unitIds: UnitId[], target: UnitId, shift: boolean) {
const cmd : CommandPacket = {
action: {
typ: 'Harvest',
target,
},
unitIds,
shift: false,
shift,
};
this.channel.emit('command', cmd);
}
Expand Down
3 changes: 1 addition & 2 deletions packages/client/src/gfx/Board3D.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export interface Props {
selectedAction: SelectedAction | undefined;

select: (ids: Set<UnitId>, shift: boolean) => void;
mapClick: (p: Position, button: number) => void;
mapClick: (p: Position, button: number, shift: boolean) => void;
unitClick: (u: UnitId, button: number, shift: boolean) => void;
}

Expand Down Expand Up @@ -78,7 +78,6 @@ export function Board3D(props: Props) {
.filter(u => u.owner === props.playerIndex)
.map(u => u.id);

console.log("select in box");
props.select(new Set(selection), shift);
};

Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/gfx/Map3D.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as THREE from 'three';

import { Board, Unit, GameMap, UnitId, Position, UnitState } from 'server/types'

type Click = (p: Position, button: number) => void;
type Click = (p: Position, button: number, shift: boolean) => void;
type RawClick = (e: ThreeEvent<MouseEvent>) => void;
export type Box = { x1: number, y1: number, x2: number, y2: number };

Expand All @@ -26,7 +26,7 @@ export function Map3D(props: Map3DProps) {
const rawClick = (e: ThreeEvent<MouseEvent>) => {
e.stopPropagation();
// turn the 3D position into the 2D map position
props.click({x: e.point.x, y: e.point.z}, e.nativeEvent.button);
props.click({x: e.point.x, y: e.point.z}, e.nativeEvent.button, e.nativeEvent.shiftKey);
};

// selection box
Expand Down

0 comments on commit b65fc3d

Please sign in to comment.