-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Ship Map): Creates the ECS components and systems for entities t…
…o traverse a ship map. Closes #265
- Loading branch information
1 parent
7fbe026
commit 43d0264
Showing
16 changed files
with
429 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import DeckPlugin, {DeckEdge} from "../../classes/Plugins/Ship/Deck"; | ||
import {ShipMapGraph} from "../../utils/shipMapPathfinder"; | ||
import {Component} from "server/src/components/utils"; | ||
|
||
const roundTo1000 = (num: number) => Math.round(num * 1000) / 1000; | ||
export class ShipMapComponent extends Component { | ||
static id = "shipMap" as const; | ||
static serialize(data: ShipMapComponent) { | ||
return { | ||
decks: data.decks.map(({backgroundUrl, name}) => ({ | ||
backgroundUrl, | ||
name, | ||
})), | ||
deckNodes: data.deckNodes.map( | ||
({flags, deckIndex, icon, id, isRoom, name, radius, x, y}) => { | ||
const output: Partial<DeckPlugin["nodes"][0]> & {deckIndex: number} = | ||
{ | ||
id, | ||
deckIndex, | ||
x: roundTo1000(x), | ||
y: roundTo1000(y), | ||
}; | ||
if (name) output.name = name; | ||
if (icon) output.icon = icon; | ||
if (isRoom) output.isRoom = isRoom; | ||
if (radius) output.radius = radius; | ||
if (flags?.length > 0) output.flags = flags; | ||
return output; | ||
} | ||
), | ||
deckEdges: data.deckEdges.map(({id, flags, from, to, isOpen, weight}) => { | ||
const output: Partial<DeckEdge> = { | ||
id, | ||
from, | ||
to, | ||
}; | ||
if (weight !== 1) output.weight = weight; | ||
if (!isOpen) output.isOpen = isOpen; | ||
if (flags?.length > 0) output.flags = flags; | ||
return output; | ||
}), | ||
}; | ||
} | ||
decks: Omit<DeckPlugin, "nodes">[] = []; | ||
deckNodes: (DeckPlugin["nodes"][0] & {deckIndex: number})[] = []; | ||
deckEdges: DeckEdge[] = []; | ||
graph: ShipMapGraph | null = null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import {MetersPerSecond} from "server/src/utils/unitTypes"; | ||
import {Component} from "../utils"; | ||
|
||
/** This class represents entities that can move around a ship */ | ||
export class PassengerMovementComponent extends Component { | ||
static id = "passengerMovement" as const; | ||
|
||
/** TODO June 16, 2022 - Some day it should be possible to connect from one ship to another and have entities move between them. */ | ||
nodePath: number[] = []; | ||
nextNodeIndex: number = 0; | ||
|
||
movementMaxVelocity: { | ||
x: MetersPerSecond; | ||
y: MetersPerSecond; | ||
z: MetersPerSecond; | ||
} = { | ||
x: 1.3, | ||
y: 1.3, | ||
z: 1.3 / 10, // The Z default is because decks are 10 meters high, so it should take 10x as long to move between decks. | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import {Entity, System} from "../utils/ecs"; | ||
|
||
export class PassengerDestinationSystem extends System { | ||
test(entity: Entity) { | ||
return !!entity.components.passengerMovement; | ||
} | ||
frequency = 10; | ||
update(entity: Entity, elapsed: number) { | ||
const elapsedRatio = elapsed / 1000; | ||
// console.log("Lets go!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import DeckPlugin, {DeckNode} from "../classes/Plugins/Ship/Deck"; | ||
import {Entity, System} from "../utils/ecs"; | ||
export class PassengerMovementSystem extends System { | ||
test(entity: Entity) { | ||
return !!( | ||
entity.components.passengerMovement && entity.components.position | ||
); | ||
} | ||
frequency = 20; | ||
update(entity: Entity, elapsed: number) { | ||
const elapsedRatio = elapsed / (1000 / this.frequency); | ||
const {position, passengerMovement} = entity.components; | ||
if (!position || !passengerMovement) return; | ||
const {x, y, z, parentId} = position; | ||
if (parentId === null) return; | ||
const ship = this.ecs.getEntityById(parentId); | ||
if (!ship) return; | ||
const nextNode = ship.components.shipMap?.deckNodes.find( | ||
node => | ||
node.id === passengerMovement.nodePath[passengerMovement.nextNodeIndex] | ||
); | ||
if (!nextNode) return; | ||
const distanceToNext = Math.hypot(x - nextNode?.x, y - nextNode?.y); | ||
// Increment to the next node | ||
if (distanceToNext <= 0.1 && z === nextNode.deckIndex) { | ||
passengerMovement.nextNodeIndex++; | ||
if ( | ||
passengerMovement.nextNodeIndex >= passengerMovement.nodePath.length | ||
) { | ||
// We've reached the end of the path, so we're done. | ||
entity.updateComponent("passengerMovement", { | ||
nodePath: [], | ||
nextNodeIndex: 0, | ||
}); | ||
passengerMovement.nodePath = []; | ||
passengerMovement.nextNodeIndex = 0; | ||
entity.updateComponent("position", { | ||
x: nextNode.x, | ||
y: nextNode.y, | ||
z: nextNode.deckIndex, | ||
}); | ||
} | ||
} else { | ||
// Move towards the next node | ||
const direction = Math.atan2(nextNode?.y - y, nextNode?.x - x); | ||
const velocity = Math.min( | ||
passengerMovement.movementMaxVelocity.x, | ||
distanceToNext | ||
); | ||
const zVelocity = Math.min( | ||
passengerMovement.movementMaxVelocity.z, | ||
nextNode.deckIndex - z | ||
); | ||
const newX = x + velocity * Math.cos(direction) * elapsedRatio; | ||
const newY = y + velocity * Math.sin(direction) * elapsedRatio; | ||
let newZ = z + zVelocity * elapsedRatio; | ||
if (Math.abs(newZ - nextNode.deckIndex) < 0.1) { | ||
// We've reached the next deck | ||
newZ = nextNode.deckIndex; | ||
} | ||
|
||
entity.updateComponent("position", {x: newX, y: newY, z: newZ}); | ||
} | ||
} | ||
} | ||
|
||
export function findClosestNode( | ||
nodes: DeckNode[], | ||
{x, y, z}: {x: number; y: number; z: number} | ||
) { | ||
const node = nodes.reduce( | ||
(acc: {distance3d: number; node: null | DeckNode}, node) => { | ||
const distance3d = Math.hypot(node.x - x, node.y - y, node.deckIndex - z); | ||
if (distance3d < acc.distance3d) return {distance3d, node}; | ||
return acc; | ||
}, | ||
{distance3d: Infinity, node: null} | ||
); | ||
return node.node; | ||
} |
Oops, something went wrong.