Skip to content

Commit

Permalink
Added readonly to unit values and found a couple bugs where the posit…
Browse files Browse the repository at this point in the history
…ion was overwritten
  • Loading branch information
bananu7 committed Oct 8, 2022
1 parent ff4e351 commit 94d2894
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
18 changes: 14 additions & 4 deletions packages/server/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,16 @@ function directionTo(a: Position, b: Position) {
return Math.atan2(b.y-a.y, b.x-a.x);
}

function vecSet(a: Position, b: Position) {
a.x = b.x;
a.y = b.y;
}

function vecAdd(a: Position, b: Position) {
a.x += b.x;
a.y += b.y;
}

function updateUnit(dt: Milliseconds, g: Game, unit: Unit, presence: PresenceMap) {
const stopMoving = () => {
unit.pathToNext = undefined;
Expand Down Expand Up @@ -256,7 +266,7 @@ function updateUnit(dt: Milliseconds, g: Game, unit: Unit, presence: PresenceMap
// can reach next path setp
if (dst < distanceLeft) {
// set the unit to the reached path step
unit.position = nextPathStep;
vecSet(unit.position, nextPathStep);
// subtract from distance "budget"
distanceLeft -= dst;
// pop the current path step off
Expand All @@ -282,9 +292,9 @@ function updateUnit(dt: Milliseconds, g: Game, unit: Unit, presence: PresenceMap
const desiredVelocity = {x: dx * distancePerTick, y: dy * distancePerTick };

// TODO - slow starts and braking
unit.velocity = checkMovePossibility(unit, unit.position, desiredVelocity, g.board.map, presence);
vecSet(unit.velocity, checkMovePossibility(unit, unit.position, desiredVelocity, g.board.map, presence));

unit.position = sum(unit.position, unit.velocity);
vecAdd(unit.position, unit.velocity);
return false;
}
}
Expand All @@ -295,7 +305,7 @@ function updateUnit(dt: Milliseconds, g: Game, unit: Unit, presence: PresenceMap
const findUnitPosition = (targetId: UnitId) => {
const target = g.units.find(u => u.id === targetId); // TODO Map
if (target)
return target.position;
return { x: target.position.x, y: target.position.y };
else
return;
}
Expand Down
14 changes: 7 additions & 7 deletions packages/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export type UnitState = {
id: number,
kind: string,
status: 'Moving'|'Attacking'|'Harvesting'|'Producing'|'Idle',
position: Position,
readonly position: Position,
velocity: Position, // TODO - Position to Vec2
direction: number,
owner: number,
Expand Down Expand Up @@ -159,15 +159,15 @@ export type PlayerIndex = number
export type UserId = string

export type Unit = {
id: number,
readonly id: number,
actionQueue: Action[],
kind: string, // TODO should this be in a component
owner: PlayerIndex,
position: Position,
readonly kind: string, // TODO should this be in a component
readonly owner: PlayerIndex,
readonly position: Position,
direction: number,
velocity: Position,
readonly velocity: Position,

components: Component[],
readonly components: Component[],

pathToNext?: TilePos[],
}
Expand Down
6 changes: 5 additions & 1 deletion packages/server/units.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ export function createStartingUnits(): Unit[] {
startingUnits.push(createUnit(lastUnitId++, 0, 'ResourceNode', {x:6, y:10}));
startingUnits.push(createUnit(lastUnitId++, 0, 'ResourceNode', {x:6, y:14}));

startingUnits.push(createUnit(lastUnitId++, 1, 'Harvester', {x:31, y:25}));
startingUnits.push(createUnit(lastUnitId++, 1, 'Base', {x:30, y:10}));
startingUnits.push(createUnit(lastUnitId++, 1, 'Harvester', {x:29, y:25}));
startingUnits.push(createUnit(lastUnitId++, 1, 'Harvester', {x:31, y:25}));
startingUnits.push(createUnit(lastUnitId++, 1, 'Harvester', {x:33, y:25}));

// TODO proper starting location placement/orientation
// bottom right
Expand All @@ -79,7 +81,9 @@ export function createStartingUnits(): Unit[] {
startingUnits.push(createUnit(lastUnitId++, 0, 'ResourceNode', {x:90, y:80}));

startingUnits.push(createUnit(lastUnitId++, 2, 'Base', {x:80, y:85}));
startingUnits.push(createUnit(lastUnitId++, 2, 'Harvester', {x:62, y:90}));
startingUnits.push(createUnit(lastUnitId++, 2, 'Harvester', {x:64, y:90}));
startingUnits.push(createUnit(lastUnitId++, 2, 'Harvester', {x:66, y:90}));

// left expo
startingUnits.push(createUnit(lastUnitId++, 0, 'ResourceNode', {x:6, y:50}));
Expand Down

0 comments on commit 94d2894

Please sign in to comment.