Skip to content

Commit b2c1b43

Browse files
committed
Splitting out construction code and fixing lint errors
1 parent 0526e70 commit b2c1b43

File tree

3 files changed

+100
-114
lines changed

3 files changed

+100
-114
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Position } from '@engine/world/position';
2+
3+
4+
export const MAP_SIZE = 13;
5+
6+
7+
export type RoomType = 'empty' | 'empty_grass' | 'garden_1' | 'garden_2' | 'parlor';
8+
9+
10+
export type RoomTemplateMap = {
11+
[key in RoomType]: Position;
12+
};
13+
14+
export const roomTemplates: RoomTemplateMap = {
15+
empty: new Position(1856, 5056),
16+
empty_grass: new Position(1864, 5056),
17+
garden_1: new Position(1856, 5064),
18+
garden_2: new Position(1872, 5064),
19+
parlor: new Position(1856, 5112),
20+
};
21+
22+
23+
export const instance1 = new Position(6400, 6400);
24+
export const instance1PohSpawn = new Position(6400 + 36, 6400 + 36);
25+
export const instance1Max = new Position(6400 + 64, 6400 + 64);
26+
export const instance2 = new Position(6400, 6464);
27+
export const instance2PohSpawn = new Position(6400 + 36, 6464 + 36);
28+
export const instance2Max = new Position(6400 + 64, 6464 + 64);
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { MAP_SIZE, roomTemplates, RoomType } from '@plugins/skills/construction/con-constants';
2+
import { Position } from '@engine/world/position';
3+
4+
5+
export class House {
6+
7+
public readonly rooms: Room[][][];
8+
9+
public constructor() {
10+
this.rooms = new Array(4);
11+
for(let level = 0; level < 4; level++) {
12+
this.rooms[level] = new Array(MAP_SIZE);
13+
for(let x = 0; x < MAP_SIZE; x++) {
14+
this.rooms[level][x] = new Array(MAP_SIZE).fill(null);
15+
}
16+
}
17+
}
18+
19+
public getRoomData(): number[][][] {
20+
const roomData = new Array(4);
21+
for(let level = 0; level < 4; level++) {
22+
roomData[level] = new Array(MAP_SIZE);
23+
for(let x = 0; x < MAP_SIZE; x++) {
24+
roomData[level][x] = new Array(MAP_SIZE);
25+
for(let y = 0; y < MAP_SIZE; y++) {
26+
roomData[level][x][y] = this.rooms[level][x][y]?.roomData || null;
27+
}
28+
}
29+
}
30+
31+
return roomData;
32+
}
33+
34+
public getRoom(position: Position): Room {
35+
return this.rooms[position.level][position.x][position.y];
36+
}
37+
38+
}
39+
40+
41+
export class Room {
42+
43+
public readonly type: RoomType;
44+
45+
public orientation: number;
46+
47+
public constructor(type: RoomType, orientation: number = 0) {
48+
this.type = type;
49+
this.orientation = orientation;
50+
}
51+
52+
public get roomData(): number {
53+
const { x, y, level } = roomTemplates[this.type];
54+
return x / 8 << 14 | y / 8 << 3 | level % 4 << 24 | this.orientation % 4 << 1;
55+
}
56+
57+
}
Lines changed: 15 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,22 @@
11
import { Position } from '@engine/world/position';
22
import { Player } from '@engine/world/actor/player/player';
33
import { PlayerCommandAction } from '@engine/world/action/player-command.action';
4-
import { PlayerInitAction, playerInitActionHandler } from '@engine/world/action/player-init.action';
5-
import { World } from '@engine/world';
6-
import { world } from '@engine/game-server';
7-
import { schedule } from '@engine/world/task';
4+
import { PlayerInitAction } from '@engine/world/action/player-init.action';
85
import { ConstructedMap } from '@engine/world/map/region';
96

7+
import { instance1, instance1Max, instance1PohSpawn, instance2, instance2Max, MAP_SIZE } from './con-constants';
8+
import { House, Room } from './con-house';
109

11-
const MAX_HOUSE_SIZE = 13;
12-
13-
14-
type RoomType = 'empty' | 'empty_grass' | 'garden_1' | 'garden_2' | 'parlor';
15-
16-
17-
type RoomTemplateMap = {
18-
[key in RoomType]: Position;
19-
}
20-
21-
22-
const roomTemplates: RoomTemplateMap = {
23-
empty: new Position(1856, 5056),
24-
empty_grass: new Position(1864, 5056),
25-
garden_1: new Position(1856, 5064),
26-
garden_2: new Position(1872, 5064),
27-
parlor: new Position(1856, 5112),
28-
};
29-
30-
31-
class Room {
32-
33-
public readonly type: RoomType;
34-
35-
public orientation: number;
36-
37-
public constructor(type: RoomType, orientation: number = 0) {
38-
this.type = type;
39-
this.orientation = orientation;
40-
}
41-
42-
public get roomData(): number {
43-
const { x, y, level } = roomTemplates[this.type];
44-
return x / 8 << 14 | y / 8 << 3 | level % 4 << 24 | this.orientation % 4 << 1;
45-
}
46-
47-
}
48-
49-
50-
class House {
51-
52-
public readonly rooms: Room[][][];
53-
54-
public constructor() {
55-
this.rooms = new Array(4);
56-
for(let level = 0; level < 4; level++) {
57-
this.rooms[level] = new Array(MAX_HOUSE_SIZE);
58-
for(let x = 0; x < MAX_HOUSE_SIZE; x++) {
59-
this.rooms[level][x] = new Array(MAX_HOUSE_SIZE).fill(null);
60-
}
61-
}
62-
}
63-
64-
public getRoomData(): number[][][] {
65-
const roomData = new Array(4);
66-
for(let level = 0; level < 4; level++) {
67-
roomData[level] = new Array(MAX_HOUSE_SIZE);
68-
for(let x = 0; x < MAX_HOUSE_SIZE; x++) {
69-
roomData[level][x] = new Array(MAX_HOUSE_SIZE);
70-
for(let y = 0; y < MAX_HOUSE_SIZE; y++) {
71-
roomData[level][x][y] = this.rooms[level][x][y]?.roomData || null;
72-
}
73-
}
74-
}
75-
76-
return roomData;
77-
}
78-
79-
public getRoom(position: Position): Room {
80-
return this.rooms[position.level][position.x][position.y];
81-
}
82-
83-
}
84-
85-
86-
const instance1 = new Position(6400, 6400);
87-
const instance1PohSpawn = new Position(6400 + 36, 6400 + 36);
88-
const instance1Max = new Position(6400 + 64, 6400 + 64);
89-
90-
const instance2 = new Position(6400, 6464);
91-
const instance2PohSpawn = new Position(6400 + 36, 6464 + 36);
92-
const instance2Max = new Position(6400 + 64, 6464 + 64);
93-
94-
95-
96-
const openHouse = async (player: Player): Promise<void> => {
97-
player.sendMessage(player.position.key);
98-
player.sendMessage(`${player.position.chunkLocalX},${player.position.chunkLocalY}`);
9910

11+
const openHouse = (player: Player): void => {
10012
const house = new House();
10113

10214
const gardenPortal = new Room('garden_1');
10315
const firstParlor = new Room('parlor');
10416
const emptySpace = new Room('empty_grass');
10517

106-
for(let x = 0; x < MAX_HOUSE_SIZE; x++) {
107-
for(let y = 0; y < MAX_HOUSE_SIZE; y++) {
18+
for(let x = 0; x < MAP_SIZE; x++) {
19+
for(let y = 0; y < MAP_SIZE; y++) {
10820
if(x === 6 && y === 6) {
10921
house.rooms[0][x][y] = gardenPortal;
11022
} else if((x === 7 && y === 6) || (x === 6 && y === 7) || (x === 5 && y === 6)) {
@@ -131,40 +43,29 @@ const openHouse = async (player: Player): Promise<void> => {
13143
tileData: house.getRoomData()
13244
} as ConstructedMap;
13345

134-
player.sendMessage(player.position.key);
135-
player.sendMessage(`${player.position.chunkLocalX},${player.position.chunkLocalY}`);
136-
13746
player.sendMessage(`Welcome home.`);
13847
};
13948

14049

141-
const playerInitHomeCheck = async (player: Player): Promise<void> =>
142-
await openHouse(player);
50+
const playerInitHomeCheck = (player: Player): void => {
51+
if(player.position.within(instance1, instance1Max, false) ||
52+
player.position.within(instance2, instance2Max, false)) {
53+
openHouse(player);
54+
}
55+
};
14356

14457

14558
export default {
14659
pluginId: 'rs:construction',
14760
hooks: [
14861
{
14962
type: 'player_command',
150-
commands: [ 'con' ],
151-
handler: ({ player }: PlayerCommandAction): void => {
152-
openHouse(player);
153-
}
154-
},
155-
{
156-
type: 'player_command',
157-
commands: [ 'local' ],
158-
handler: ({ player }: PlayerCommandAction): void => {
159-
player.sendMessage(player.position.key);
160-
player.sendMessage(`${player.position.chunkLocalX},${player.position.chunkLocalY}`);
161-
}
63+
commands: [ 'con', 'poh', 'house' ],
64+
handler: ({ player }: PlayerCommandAction): void => openHouse(player)
16265
},
16366
{
16467
type: 'player_init',
165-
handler: ({ player }: PlayerInitAction): void => {
166-
playerInitHomeCheck(player);
167-
}
68+
handler: ({ player }: PlayerInitAction): void => playerInitHomeCheck(player)
16869
}
16970
]
17071
};

0 commit comments

Comments
 (0)