Skip to content

Commit

Permalink
feat: Add the wander ship behavior.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderson1993 committed Mar 21, 2024
1 parent 86d4f9f commit 25fc634
Show file tree
Hide file tree
Showing 15 changed files with 330 additions and 110 deletions.
9 changes: 9 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"editor.defaultFormatter": "biomejs.biome",
"[typescript]": {
"editor.defaultFormatter": "biomejs.biome"
},
"[typescriptreact]": {
"editor.defaultFormatter": "biomejs.biome"
}
}
2 changes: 1 addition & 1 deletion client/app/cards/Viewscreen/WarpStars.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useFrame } from "@react-three/fiber";
import { useLiveQuery } from "@thorium/live-query/client";
import Star from "@client/components/Starmap/Star/StarMesh";
import { getSphericalPositionWithBias } from "@client/utils/getSphericalPositionWithBias";
import { randomPointInSphere } from "@client/utils/randomPointInSphere";
import { randomPointInSphere } from "@thorium/randomPoint/randomPointInSphere";
import { memo, useMemo } from "react";
import {
Color,
Expand Down
2 changes: 2 additions & 0 deletions client/app/components/ui/icons/name.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@
| "reactor"
| "repeat-2"
| "rocket"
| "route"
| "shield-half"
| "siren"
| "sparkles"
| "star"
| "sword"
| "text-cursor-input"
| "x"
| "zap";
Expand Down
2 changes: 1 addition & 1 deletion client/app/components/ui/icons/sprite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
60 changes: 59 additions & 1 deletion client/app/cores/StarmapCore/data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ import {
getObjectSystem,
} from "@server/utils/position";

const behavior = z.enum(["hold", "patrol", "attack", "defend", "avoid"]);
const behavior = z.enum([
"hold",
"patrol",
"wander",
"attack",
"defend",
"avoid",
]);

export const starmapCore = t.router({
systems: t.procedure.request(({ ctx }) => {
Expand Down Expand Up @@ -250,6 +257,20 @@ export const starmapCore = t.router({
desiredCoordinates: ship.position,
desiredSolarSystemId: ship.systemId,
});
entity?.updateComponent("shipBehavior", {
destination: {
parentId: ship.systemId,
x: ship.position.x,
y: ship.position.y,
z: ship.position.z,
},
target: {
parentId: ship.systemId,
x: ship.position.x,
y: ship.position.y,
z: ship.position.z,
},
});
if (typeof entity?.components.position?.parentId !== "undefined") {
systemIds.add(entity.components.position.parentId);
}
Expand Down Expand Up @@ -283,6 +304,20 @@ export const starmapCore = t.router({
desiredCoordinates: position,
desiredSolarSystemId: objectSystem.id,
});
entity?.updateComponent("shipBehavior", {
destination: {
parentId: objectSystem.id,
x: position.x,
y: position.y,
z: position.z,
},
target: {
parentId: objectSystem.id,
x: position.x,
y: position.y,
z: position.z,
},
});

if (typeof entity?.components.position?.parentId !== "undefined") {
systemIds.add(entity.components.position.parentId);
Expand Down Expand Up @@ -341,6 +376,29 @@ export const starmapCore = t.router({
objective: input.behavior,
});

if (input.behavior === "hold") {
const position = entity?.components.position;
if (position) {
entity.updateComponent("shipBehavior", {
destination: {
parentId: position.parentId || null,
x: position.x,
y: position.y,
z: position.z,
},
});
entity.updateComponent("autopilot", {
rotationAutopilot: true,
forwardAutopilot: true,
desiredCoordinates: {
x: position.x,
y: position.y,
z: position.z,
},
desiredSolarSystemId: position.parentId || null,
});
}
}
pubsub.publish.pilot.autopilot.get({ shipId });
pubsub.publish.ship.get({ shipId });
pubsub.publish.starmapCore.ship({ shipId });
Expand Down
31 changes: 16 additions & 15 deletions client/app/cores/StarmapCore/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,21 @@ function ShipControls() {
<Icon name="siren" />
</Button>
</Tooltip>
<Tooltip content="Wander">
<Button
onClick={() =>
q.starmapCore.setBehavior.netSend({
ships: selectedObjectIds,
behavior: "wander",
})
}
className={clsx("btn-sm btn-notice btn-outline", {
"btn-active": starmapShip.behavior.objective === "wander",
})}
>
<Icon name="route" />
</Button>
</Tooltip>
<Tooltip content="Hold Position">
<Button
onClick={() =>
Expand Down Expand Up @@ -122,21 +137,7 @@ function ShipControls() {
"btn-active": starmapShip.behavior.objective === "attack",
})}
>
<svg
xmlns="http://www.w3.org/2000/svg"
fillRule="evenodd"
strokeLinejoin="round"
strokeMiterlimit="2"
clipRule="evenodd"
viewBox="0 0 700 700"
className="w-4 h-4"
>
<title>attack</title>
<path
fill="currentColor"
d="M212 533L88 639l-25 48-56-9-7-55 49-24 112-119-74-78 26-27 101 74 30 31 70 105-28 26-74-78zM700 13l-70 155-368 320-11-15-30-32-13-10L540 77l160-64z"
/>
</svg>
<Icon name="sword" />
</Button>
</Tooltip>
<Tooltip content="Follow & Defend">
Expand Down
17 changes: 17 additions & 0 deletions client/app/icons/route.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions client/app/icons/sword.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions server/src/components/shipBehavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const shipBehavior = z
* - avoid: move away from a target
*/
objective: z
.enum(["hold", "patrol", "attack", "defend", "avoid"])
.enum(["hold", "patrol", "wander", "attack", "defend", "avoid"])
.default("hold"),
/**
* The target of the ship's objective
Expand Down Expand Up @@ -57,6 +57,6 @@ export const shipBehavior = z
* basically mid-orbit. When patrolling around a planet, set this
* to 5 times the planet's radius.
*/
patrolRadius: z.number().default(25_000),
patrolRadius: z.number().default(2_500),
})
.default({});
Loading

0 comments on commit 25fc634

Please sign in to comment.