Migrating position notification into the User class
This commit is contained in:
parent
892d1555b8
commit
23cea1c835
7 changed files with 59 additions and 52 deletions
|
@ -9,14 +9,6 @@ import {Zone} from "_Model/Zone";
|
|||
import {Movable} from "_Model/Movable";
|
||||
import {PositionInterface} from "_Model/PositionInterface";
|
||||
|
||||
function move(user: User, x: number, y: number, positionNotifier: PositionNotifier): void {
|
||||
positionNotifier.updatePosition(user, {
|
||||
x,
|
||||
y
|
||||
}, user.position);
|
||||
user.position.x = x;
|
||||
user.position.y = y;
|
||||
}
|
||||
|
||||
describe("PositionNotifier", () => {
|
||||
it("should receive notifications when player moves", () => {
|
||||
|
@ -37,14 +29,14 @@ describe("PositionNotifier", () => {
|
|||
y: 500,
|
||||
moving: false,
|
||||
direction: 'down'
|
||||
}, false);
|
||||
}, false, positionNotifier);
|
||||
|
||||
const user2 = new User(2, {
|
||||
x: -9999,
|
||||
y: -9999,
|
||||
moving: false,
|
||||
direction: 'down'
|
||||
}, false);
|
||||
}, false, positionNotifier);
|
||||
|
||||
positionNotifier.setViewport(user1, {
|
||||
left: 200,
|
||||
|
@ -53,21 +45,21 @@ describe("PositionNotifier", () => {
|
|||
bottom: 500
|
||||
});
|
||||
|
||||
move(user2, 500, 500, positionNotifier);
|
||||
user2.setPosition({x: 500, y: 500, direction: 'down', moving: false});
|
||||
|
||||
expect(enterTriggered).toBe(true);
|
||||
expect(moveTriggered).toBe(false);
|
||||
enterTriggered = false;
|
||||
|
||||
// Move inside the zone
|
||||
move(user2, 501, 500, positionNotifier);
|
||||
user2.setPosition({x:501, y:500, direction: 'down', moving: false});
|
||||
|
||||
expect(enterTriggered).toBe(false);
|
||||
expect(moveTriggered).toBe(true);
|
||||
moveTriggered = false;
|
||||
|
||||
// Move out of the zone in a zone that we don't track
|
||||
move(user2, 901, 500, positionNotifier);
|
||||
user2.setPosition({x: 901, y: 500, direction: 'down', moving: false});
|
||||
|
||||
expect(enterTriggered).toBe(false);
|
||||
expect(moveTriggered).toBe(false);
|
||||
|
@ -75,14 +67,14 @@ describe("PositionNotifier", () => {
|
|||
leaveTriggered = false;
|
||||
|
||||
// Move back in
|
||||
move(user2, 500, 500, positionNotifier);
|
||||
user2.setPosition({x: 500, y: 500, direction: 'down', moving: false});
|
||||
expect(enterTriggered).toBe(true);
|
||||
expect(moveTriggered).toBe(false);
|
||||
expect(leaveTriggered).toBe(false);
|
||||
enterTriggered = false;
|
||||
|
||||
// Move out of the zone in a zone that we do track
|
||||
move(user2, 200, 500, positionNotifier);
|
||||
user2.setPosition({x: 200, y: 500, direction: 'down', moving: false});
|
||||
expect(enterTriggered).toBe(false);
|
||||
expect(moveTriggered).toBe(true);
|
||||
expect(leaveTriggered).toBe(false);
|
||||
|
@ -115,14 +107,14 @@ describe("PositionNotifier", () => {
|
|||
y: 500,
|
||||
moving: false,
|
||||
direction: 'down'
|
||||
}, false);
|
||||
}, false, positionNotifier);
|
||||
|
||||
const user2 = new User(2, {
|
||||
x: -9999,
|
||||
y: -9999,
|
||||
x: 0,
|
||||
y: 0,
|
||||
moving: false,
|
||||
direction: 'down'
|
||||
}, false);
|
||||
}, false, positionNotifier);
|
||||
|
||||
let newUsers = positionNotifier.setViewport(user1, {
|
||||
left: 200,
|
||||
|
@ -131,14 +123,16 @@ describe("PositionNotifier", () => {
|
|||
bottom: 500
|
||||
});
|
||||
|
||||
expect(newUsers.length).toBe(0);
|
||||
|
||||
move(user2, 500, 500, positionNotifier);
|
||||
|
||||
expect(newUsers.length).toBe(2);
|
||||
expect(enterTriggered).toBe(true);
|
||||
expect(moveTriggered).toBe(false);
|
||||
enterTriggered = false;
|
||||
|
||||
user2.setPosition({x: 500, y: 500, direction: 'down', moving: false});
|
||||
|
||||
expect(enterTriggered).toBe(false);
|
||||
expect(moveTriggered).toBe(true);
|
||||
moveTriggered = false;
|
||||
|
||||
// Move the viewport but the user stays inside.
|
||||
positionNotifier.setViewport(user1, {
|
||||
left: 201,
|
||||
|
@ -176,6 +170,6 @@ describe("PositionNotifier", () => {
|
|||
expect(moveTriggered).toBe(false);
|
||||
expect(leaveTriggered).toBe(false);
|
||||
enterTriggered = false;
|
||||
expect(newUsers.length).toBe(1);
|
||||
expect(newUsers.length).toBe(2);
|
||||
});
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue