Skip to content

Commit

Permalink
feat: Add shields system.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderson1993 committed Sep 6, 2024
1 parent d862595 commit 32ad64d
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 49 deletions.
8 changes: 8 additions & 0 deletions client/app/cards/Targeting/data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,14 @@ export const targeting = t.router({
});
}),

hull: t.procedure
.filter((publish: { shipId: number }, { ctx }) => {
if (publish && publish.shipId !== ctx.ship?.id) return false;
return true;
})
.request(({ ctx }) => {
return ctx.ship?.components.hull?.hull || 0;
}),
shields: t.router({
get: t.procedure
.filter((publish: { shipId: number }, { ctx }) => {
Expand Down
28 changes: 19 additions & 9 deletions client/app/cards/Targeting/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ export function Targeting({ cardLoaded }: CardProps) {
const [targetedContact] = q.targeting.targetedContact.useNetRequest();
const clickRef = React.useRef(false);
q.targeting.stream.useDataStream();

const [hull] = q.targeting.hull.useNetRequest();
return (
<CircleGridStoreProvider zoomMax={25000}>
<div className="grid grid-cols-4 h-full place-content-center gap-4">
<div className="flex flex-col justify-between">
<Shields cardLoaded={cardLoaded} />
<div>Hull: {hull}</div>
</div>
<div className="col-span-2 w-full aspect-square self-center">
<React.Suspense fallback={null}>
Expand Down Expand Up @@ -115,32 +116,37 @@ const shieldStyle = (
shields.forEach((s) => {
const integrity = s.strength / s.maxStrength;
const color = shieldColor(integrity);
if (s.direction === "starboard") {
if (
(s.direction === "starboard" && !extra) ||
(s.direction === "fore" && extra)
) {
output.push(`20px 0px 20px -15px ${color}`);
output.push(`inset -20px 0px 20px -15px ${color}`);
}
if (s.direction === "port") {
if (
(s.direction === "port" && !extra) ||
(s.direction === "aft" && extra)
) {
output.push(`-20px 0px 20px -15px ${color}`);
output.push(`inset 20px 0px 20px -15px ${color}`);
}
if (s.direction === "aft" && !extra) {
if (s.direction === "fore" && !extra) {
output.push(`0px -20px 20px -15px ${color}`);
output.push(`inset 0px 20px 20px -15px ${color}`);
}
if (s.direction === "fore" && !extra) {
if (s.direction === "aft" && !extra) {
output.push(`0px 20px 20px -15px ${color}`);
output.push(`inset 0px -20px 20px -15px ${color}`);
}
if (s.direction === "dorsal" && extra) {
if (s.direction === "ventral" && extra) {
output.push(`0px 20px 20px -15px ${color}`);
output.push(`inset 0px -20px 20px -15px ${color}`);
}
if (s.direction === "ventral" && extra) {
if (s.direction === "dorsal" && extra) {
output.push(`0px -20px 20px -15px ${color}`);
output.push(`inset 0px 20px 20px -15px ${color}`);
}
});
console.log(output);
return output.join(",");
};

Expand All @@ -153,7 +159,11 @@ function Shields({ cardLoaded }: { cardLoaded: boolean }) {

const { interpolate } = useLiveQuery();
useAnimationFrame(() => {
const shieldItems = [];
const shieldItems: {
strength: number;
maxStrength: number;
direction: "fore" | "aft" | "starboard" | "port" | "dorsal" | "ventral";
}[] = [];
for (const shield of shields) {
const strength = interpolate(shield.id)?.x || 0;
shieldItems.push({ ...shield, strength });
Expand Down
15 changes: 14 additions & 1 deletion client/app/cores/StarmapCore/data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -519,14 +519,27 @@ export const starmapCore = t.router({
.send(({ ctx, input }) => {
const torpedoEntity = new Entity();

const target = ctx.flight?.ecs.getEntityById(input.objectId);
if (!target) return;
const targetPosition = target.components.position
? new Vector3(
target.components.position.x,
target.components.position.y,
target.components.position.z,
)
: new Vector3();

torpedoEntity.addComponent("position", {
x: input.position.x,
y: input.position.y,
z: input.position.z,
parentId: input.position.parentId,
type: "solar",
});
const directionVector = new Vector3(0, 0, 1)
// Point the torpedo at the target
const directionVector = targetPosition
.clone()
.sub(new Vector3(input.position.x, input.position.y, input.position.z))
.normalize()
.multiplyScalar(50);

Expand Down
21 changes: 11 additions & 10 deletions server/src/classes/Plugins/ShipSystems/Shields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,17 @@ export function getWhichShield(
size: { x: number; y: number; z: number },
): ShieldDirections {
// Scale direction based on the size of the ship
const x = Math.abs(direction.x / size.x);
const y = Math.abs(direction.y / size.y);
const z = Math.abs(direction.z / size.z);
const max = Math.max(x, y, z);
if (max === x && max > 0) return "starboard";
if (max === x && max < 0) return "port";
if (max === z && max > 0) return "fore";
if (max === z && max < 0) return "aft";
if (max === y && max > 0) return "ventral";
if (max === y && max < 0) return "dorsal";
const x = direction.x / size.x;
const y = direction.y / size.y;
const z = direction.z / size.z;
const max = Math.max(Math.abs(x), Math.abs(y), Math.abs(z));
if (max === Math.abs(x) && x > 0) return "port";
if (max === Math.abs(x) && x < 0) return "starboard";
if (max === Math.abs(z) && z > 0) return "fore";
if (max === Math.abs(z) && z < 0) return "aft";
if (max === Math.abs(y) && y > 0) return "ventral";
if (max === Math.abs(y) && y < 0) return "dorsal";
console.log("No shield direction found");
// Default to fore in the very unlikely event that the direction is 0,0,0
return "fore";
}
3 changes: 2 additions & 1 deletion server/src/spawners/ship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ export async function spawnShip(
...params.assets,
},
});
entity.addComponent("hull", { hull: 1 });
// TODO September 3, 2024 - Make this configurable on the ship template
entity.addComponent("hull", { hull: 10 });
if (params.position) {
entity.addComponent("position", params.position);
}
Expand Down
18 changes: 12 additions & 6 deletions server/src/systems/PhysicsMovementSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,12 +365,19 @@ export class PhysicsMovementSystem extends System {
const entity2 = this.ecs.getEntityById(entityId2);

// This is the vector from entity1 to entity2,
const maxDirection = event.maxForceDirection();
const direction = new Vector3(
maxDirection.x,
maxDirection.y,
maxDirection.z,
);
body1?.translation().x,
body1?.translation().y,
body1?.translation().z,
)
.sub(
new Vector3(
body2?.translation().x,
body2?.translation().y,
body2?.translation().z,
),
)
.normalize();

handleCollisionDamage(
entity1,
Expand All @@ -395,7 +402,6 @@ export class PhysicsMovementSystem extends System {
otherEntity &&
!torpedoEntity.components.isDestroyed
) {
console.log("handling torpedo damage");
handleTorpedoDamage(torpedoEntity, otherEntity, direction);
}
}
Expand Down
60 changes: 38 additions & 22 deletions server/src/utils/collisionDamage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,43 @@ export function applyDamage(
// The vector from the ship to the impact point.
direction: Vector3,
) {
// TODO May 11, 2024: Apply damage to the shields first
const size = entity.components.size || { length: 1, width: 1, height: 1 };
const remainingDamage = applyShieldDamage(
entity,
damageInGigajoules,
direction,
);

// Apply damage to the hull
if (remainingDamage > 0 && entity.components.hull) {
entity.updateComponent("hull", {
hull: entity.components.hull.hull - remainingDamage,
});
pubsub.publish.targeting.hull({ shipId: entity.id });

if (entity.components.hull.hull <= 0) {
const mass = entity.components.mass?.mass || 1;
const explosion =
mass > 1_000_000_000
? "large"
: mass > 100_000_000
? "medium"
: "small";

entity.addComponent("isDestroyed", {
timeToDestroy: 5000,
explosion,
});
}
}
}

function applyShieldDamage(
entity: Entity,
damageInGigajoules: number,
// The vector from the ship to the impact point.
direction: Vector3,
) {
const size = /*entity.components.size ||*/ { length: 1, width: 1, height: 1 };
const shieldDirection = getWhichShield(direction, {
x: size.width,
y: size.height,
Expand Down Expand Up @@ -130,25 +165,6 @@ export function applyDamage(
} else {
remainingDamage = damageInGigajoules;
}
// Apply damage to the hull
if (remainingDamage > 0 && entity.components.hull) {
entity.updateComponent("hull", {
hull: entity.components.hull.hull - remainingDamage,
});

if (entity.components.hull.hull <= 0) {
const mass = entity.components.mass?.mass || 1;
const explosion =
mass > 1_000_000_000
? "large"
: mass > 100_000_000
? "medium"
: "small";

entity.addComponent("isDestroyed", {
timeToDestroy: 5000,
explosion,
});
}
}
return remainingDamage;
}

0 comments on commit 32ad64d

Please sign in to comment.