-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
164 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { jest } from "@jest/globals"; | ||
import { PhysicsEntity } from "../../src/entities/phys/physicsEntity"; | ||
import { GameWorld, RapierPhysicsObject } from "../../src/world"; | ||
import { Collider, Cuboid } from "@dimforge/rapier2d-compat"; | ||
import { Point, Sprite } from "pixi.js"; | ||
|
||
export class MockPhysicsEntity extends PhysicsEntity { | ||
|
||
public mockSprite: Sprite; | ||
|
||
constructor(world: GameWorld, position?: Point) { | ||
const mockSprite = { | ||
position: position ?? new Point(Math.ceil(Math.random() * 100), Math.ceil(Math.random() * 100)) | ||
} as Sprite; | ||
super(mockSprite, jest.mocked<Partial<RapierPhysicsObject>>({ | ||
collider: { | ||
shape: new Cuboid(5,5) | ||
} as Partial<Collider> as Collider, | ||
}) as RapierPhysicsObject, world); | ||
this.mockSprite = mockSprite; | ||
} | ||
|
||
public toString() { | ||
return "MockPhysObject"; | ||
} | ||
} |
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,10 @@ | ||
import { jest } from "@jest/globals"; | ||
import { EventEmitter } from "events"; | ||
|
||
export class MockViewport extends EventEmitter { | ||
constructor() { | ||
super(); | ||
} | ||
|
||
public moveCenter = jest.fn(); | ||
} |
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,94 @@ | ||
import { describe, expect, jest, test } from "@jest/globals"; | ||
import { CameraLockPriority, ViewportCamera } from "../../src/camera"; | ||
import { Viewport } from "pixi-viewport"; | ||
import { GameWorld } from "../../src/world"; | ||
import { Ticker } from "pixi.js"; | ||
import { MockViewport } from "../test-utils/viewport-mock"; | ||
import { MockPhysicsEntity } from "../test-utils/physent-mock"; | ||
|
||
function createTestEnv() { | ||
const viewport = new MockViewport(); | ||
const gameWorld = jest.mocked<Partial<GameWorld>>({ | ||
entities: new Map(), | ||
}); | ||
const camera = new ViewportCamera(viewport as unknown as Viewport, gameWorld as GameWorld); | ||
return { viewport, camera, gameWorld }; | ||
} | ||
|
||
describe('ViewportCamera', () => { | ||
test('camera starts with nolock', () => { | ||
const { camera } = createTestEnv(); | ||
camera.update(new Ticker(), undefined); | ||
expect(camera.lockTarget).toBeNull(); | ||
}); | ||
test('camera ignores targets with nolock', () => { | ||
const { camera, gameWorld } = createTestEnv(); | ||
const ent = new MockPhysicsEntity(gameWorld as GameWorld); | ||
ent.cameraLockPriority = CameraLockPriority.NoLock; | ||
gameWorld.entities?.set('foobar', ent); | ||
camera.update(new Ticker(), undefined); | ||
expect(camera.lockTarget).toBeNull(); | ||
}); | ||
test('camera locks onto a single target', () => { | ||
const { viewport, camera, gameWorld } = createTestEnv(); | ||
const ent = new MockPhysicsEntity(gameWorld as GameWorld); | ||
ent.cameraLockPriority = CameraLockPriority.SuggestedLockLocal; | ||
gameWorld.entities?.set('foobar', ent); | ||
camera.update(new Ticker(), undefined); | ||
expect(camera.lockTarget).toBe(ent); | ||
expect(viewport.moveCenter).toHaveBeenCalledWith(ent.sprite.position.x, ent.sprite.position.y); | ||
}); | ||
test('camera does not move if non-local lock', () => { | ||
const { viewport, camera, gameWorld } = createTestEnv(); | ||
const ent = new MockPhysicsEntity(gameWorld as GameWorld); | ||
ent.cameraLockPriority = CameraLockPriority.SuggestedLockNonLocal; | ||
gameWorld.entities?.set('foobar', ent); | ||
camera.update(new Ticker(), undefined); | ||
expect(camera.lockTarget).toBe(ent); | ||
expect(viewport.moveCenter).not.toHaveBeenCalled(); | ||
}); | ||
test('camera ignores lock if user wants to move', () => { | ||
const { viewport, camera, gameWorld } = createTestEnv(); | ||
const ent = new MockPhysicsEntity(gameWorld as GameWorld); | ||
ent.cameraLockPriority = CameraLockPriority.SuggestedLockLocal; | ||
gameWorld.entities?.set('foobar', ent); | ||
camera.update(new Ticker(), undefined); | ||
expect(camera.lockTarget).toBe(ent); | ||
expect(viewport.moveCenter).toHaveBeenCalledWith(ent.sprite.position.x, ent.sprite.position.y); | ||
viewport.emit('moved', { type: "test" }); | ||
camera.update(new Ticker(), undefined); | ||
// Ensure not recalled. | ||
expect(viewport.moveCenter).toHaveBeenCalledTimes(1); | ||
}); | ||
test('camera moves to a higher priority target', () => { | ||
const { viewport, camera, gameWorld } = createTestEnv(); | ||
const entLower = new MockPhysicsEntity(gameWorld as GameWorld); | ||
const entHigher = new MockPhysicsEntity(gameWorld as GameWorld); | ||
entLower.cameraLockPriority = CameraLockPriority.SuggestedLockLocal; | ||
gameWorld.entities?.set('1', entLower); | ||
gameWorld.entities?.set('2', entHigher); | ||
camera.update(new Ticker(), undefined); | ||
expect(camera.lockTarget).toBe(entLower); | ||
expect(viewport.moveCenter).toHaveBeenCalledWith(entLower.sprite.position.x, entLower.sprite.position.y); | ||
entHigher.cameraLockPriority = CameraLockPriority.LockIfNotLocalPlayer; | ||
camera.update(new Ticker(), undefined); | ||
expect(camera.lockTarget).toBe(entHigher); | ||
expect(viewport.moveCenter).toHaveBeenCalledWith(entHigher.sprite.position.x, entHigher.sprite.position.y); | ||
}); | ||
test('camera moves to a lower priority target when the higher cancels', () => { | ||
const { viewport, camera, gameWorld } = createTestEnv(); | ||
const entLower = new MockPhysicsEntity(gameWorld as GameWorld); | ||
const entHigher = new MockPhysicsEntity(gameWorld as GameWorld); | ||
entLower.cameraLockPriority = CameraLockPriority.SuggestedLockLocal | ||
entHigher.cameraLockPriority = CameraLockPriority.LockIfNotLocalPlayer; | ||
gameWorld.entities?.set('1', entLower); | ||
gameWorld.entities?.set('2', entHigher); | ||
camera.update(new Ticker(), undefined); | ||
expect(camera.lockTarget).toBe(entHigher); | ||
expect(viewport.moveCenter).toHaveBeenCalledWith(entHigher.sprite.position.x, entHigher.sprite.position.y); | ||
entHigher.cameraLockPriority = CameraLockPriority.NoLock; | ||
camera.update(new Ticker(), undefined); | ||
expect(camera.lockTarget).toBe(entLower); | ||
expect(viewport.moveCenter).toHaveBeenCalledWith(entLower.sprite.position.x, entLower.sprite.position.y); | ||
}); | ||
}); |
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 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